added thememanager view and thempreviw

it uses a lazyhgrid
This commit is contained in:
neon443
2025-06-27 20:48:41 +01:00
parent c343066fb5
commit 3698232a51
5 changed files with 138 additions and 96 deletions

View File

@@ -68,7 +68,7 @@ struct HostsView: View {
Section() {
NavigationLink {
ThemesView(hostsManager: hostsManager)
ThemeManagerView(hostsManager: hostsManager)
} label: {
Label("Themes", systemImage: "swatchpalette")
}

View File

@@ -0,0 +1,79 @@
//
// ThemesView.swift
// ShhShell
//
// Created by neon443 on 27/06/2025.
//
import SwiftUI
struct ThemeManagerView: View {
@ObservedObject var hostsManager: HostsManager
@State var showAlert: Bool = false
@State var importURL: String = ""
@State var toImportName: String = ""
let grid = GridItem(
.adaptive(minimum: 80, maximum: 150),
spacing: 0,
alignment: .center
)
var body: some View {
GeometryReader { geo in
NavigationStack {
let columns = Int(geo.size.width/150)
ScrollView(.horizontal) {
LazyHGrid(rows: Array(repeating: grid, count: columns), alignment: .center, spacing: 0) {
ForEach(hostsManager.themes) { theme in
ThemePreview(theme: theme)
}
}
}
.scrollIndicators(.hidden)
.alert("Enter URL", isPresented: $showAlert) {
TextField("", text: $importURL, prompt: Text("URL"))
Button() {
hostsManager.downloadTheme(fromUrl: URL(string: importURL))
importURL = ""
} label: {
Label("Import", systemImage: "square.and.arrow.down")
}
}
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button() {
if let pasteboard = UIPasteboard().string {
hostsManager.importTheme(name: toImportName, data: pasteboard.data(using: .utf8))
}
} label: {
Label("Import", systemImage: "plus")
}
}
ToolbarItem() {
Button() {
UIApplication.shared.open(URL(string: "https://iterm2colorschemes.com")!)
} label: {
Label("Open themes site", systemImage: "safari")
}
}
ToolbarItem() {
Button() {
showAlert.toggle()
} label: {
Label("From URL", systemImage: "link")
}
}
}
}
}
}
}
#Preview {
ThemeManagerView(
hostsManager: HostsManager(),
importURL: "https://raw.githubusercontent.com/mbadolato/iTerm2-Color-Schemes/master/schemes/catppuccin-frappe.itermcolors"
)
}

View File

@@ -0,0 +1,50 @@
//
// ThemePreview.swift
// ShhShell
//
// Created by neon443 on 27/06/2025.
//
import SwiftUI
struct ThemePreview: View {
@State var theme: Theme
var body: some View {
ZStack(alignment: .center) {
RoundedRectangle(cornerRadius: 5)
.fill(theme.background.suiColor)
VStack(alignment: .leading) {
Text(theme.name)
.foregroundStyle(theme.foreground.suiColor)
.font(.headline)
HStack {
ForEach(0..<8, id: \.self) { index in
Rectangle()
.frame(width: 15, height: 15)
.foregroundStyle(theme.ansi[index].suiColor)
}
}
HStack {
ForEach(8..<16, id: \.self) { index in
Rectangle()
.frame(width: 15, height: 15)
.foregroundStyle(theme.ansi[index].suiColor)
}
}
}
.padding(5)
}
.fixedSize()
.frame(maxWidth: 150, maxHeight: 80)
}
}
#Preview {
let url = URL(string: "https://raw.githubusercontent.com/mbadolato/iTerm2-Color-Schemes/master/schemes/catppuccin-frappe.itermcolors")!
let data = try! Data(contentsOf: url)
ThemePreview(
theme: Theme.decodeTheme(name: "theme", data: data)!
)
}

View File

@@ -1,91 +0,0 @@
//
// ThemesView.swift
// ShhShell
//
// Created by neon443 on 27/06/2025.
//
import SwiftUI
struct ThemesView: View {
@ObservedObject var hostsManager: HostsManager
@State var showAlert: Bool = false
@State var importURL: String = ""
@State var toImportName: String = ""
var body: some View {
NavigationStack {
List {
ScrollView(.horizontal) {
HStack {
ForEach(hostsManager.themes) { theme in
ZStack(alignment: .center) {
RoundedRectangle(cornerRadius: 10)
.fill(theme.background.suiColor)
VStack(alignment: .leading) {
Text(theme.name)
.foregroundStyle(theme.foreground.suiColor)
HStack {
ForEach(0..<8, id: \.self) { index in
Rectangle()
.frame(width: 12, height: 12)
.foregroundStyle(theme.ansi[index].suiColor)
}
}
HStack {
ForEach(8..<16, id: \.self) { index in
Rectangle()
.frame(width: 12, height: 12)
.foregroundStyle(theme.ansi[index].suiColor)
}
}
}
}
.frame(width: 100, height: 100)
}
}
}
.scrollIndicators(.hidden)
}
.alert("Enter URL of your theme", isPresented: $showAlert) {
TextField("", text: $importURL, prompt: Text("from iterm2colorschemes.com"))
Button() {
hostsManager.downloadTheme(fromUrl: URL(string: importURL))
importURL = ""
} label: {
Label("Import", systemImage: "square.and.arrow.down")
}
}
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button() {
if let pasteboard = UIPasteboard().string {
hostsManager.importTheme(name: toImportName, data: pasteboard.data(using: .utf8))
}
} label: {
Label("Import", systemImage: "plus")
}
}
ToolbarItem() {
Button() {
UIApplication.shared.open(URL(string: "https://iterm2colorschemes.com")!)
} label: {
Label("Open themes site", systemImage: "safari")
}
}
ToolbarItem() {
Button() {
showAlert.toggle()
} label: {
Label("From URL", systemImage: "link")
}
}
}
}
}
}
#Preview {
ThemesView(hostsManager: HostsManager())
}