From d33b7d4eaf68fcdfc862c1deb79d3e9780a9237b Mon Sep 17 00:00:00 2001 From: neon443 <69979447+neon443@users.noreply.github.com> Date: Fri, 27 Jun 2025 18:01:27 +0100 Subject: [PATCH] added downloadtheme funciton to download and import a theme from an iterm2colorschemes.com link added import theme to import a theme updated loadthemes updated themesview to add import buttons and updated the view fix themesview making a new instance of hostsmanager added suiColor to get a swiftui color from a swiftterm color fix uicolor returning black all the time --- ShhShell/Host/HostsManager.swift | 28 +++- ShhShell/ShhShellApp.swift | 218 ------------------------- ShhShell/Themes/Theme.swift | 21 ++- ShhShell/Views/Hosts/HostsView.swift | 2 +- ShhShell/Views/Themes/ThemesView.swift | 79 ++++++++- 5 files changed, 115 insertions(+), 233 deletions(-) diff --git a/ShhShell/Host/HostsManager.swift b/ShhShell/Host/HostsManager.swift index 8078360..8b59c73 100644 --- a/ShhShell/Host/HostsManager.swift +++ b/ShhShell/Host/HostsManager.swift @@ -17,6 +17,7 @@ class HostsManager: ObservableObject, @unchecked Sendable { init() { loadSavedHosts() + loadThemes() } @@ -28,12 +29,31 @@ class HostsManager: ObservableObject, @unchecked Sendable { guard let decodedThemeNames = try? JSONDecoder().decode([String].self, from: dataThemeNames) else { return } for index in 0.. - - - - Ansi 0 Color - - Blue Component - 0.0 - Green Component - 0.0 - Red Component - 0.0 - - Ansi 1 Color - - Blue Component - 0.40000000000000002 - Green Component - 0.40000000000000002 - Red Component - 0.80000000000000004 - - Ansi 10 Color - - Blue Component - 0.40784313729999999 - Green Component - 0.74117647060000003 - Red Component - 0.70980392160000005 - - Ansi 11 Color - - Blue Component - 0.4549019608 - Green Component - 0.77647058820000003 - Red Component - 0.94117647059999998 - - Ansi 12 Color - - Blue Component - 0.74509803919999995 - Green Component - 0.63529411759999999 - Red Component - 0.50588235290000005 - - Ansi 13 Color - - Blue Component - 0.73333333329999995 - Green Component - 0.58039215690000001 - Red Component - 0.69803921570000005 - - Ansi 14 Color - - Blue Component - 0.71764705880000002 - Green Component - 0.74509803919999995 - Red Component - 0.54117647059999996 - - Ansi 15 Color - - Blue Component - 0.99999129772186279 - Green Component - 0.99997437000274658 - Red Component - 1 - - Ansi 2 Color - - Blue Component - 0.40784313725490196 - Green Component - 0.74117647058823533 - Red Component - 0.70980392156862748 - - Ansi 3 Color - - Blue Component - 0.45490196078431372 - Green Component - 0.77647058823529413 - Red Component - 0.94117647058823528 - - Ansi 4 Color - - Blue Component - 0.74509803921568629 - Green Component - 0.63529411764705879 - Red Component - 0.50588235294117645 - - Ansi 5 Color - - Blue Component - 0.73333333333333328 - Green Component - 0.58039215686274503 - Red Component - 0.69803921568627447 - - Ansi 6 Color - - Blue Component - 0.71764705882352942 - Green Component - 0.74509803921568629 - Red Component - 0.54117647058823526 - - Ansi 7 Color - - Blue Component - 0.99999129772186279 - Green Component - 0.99997437000274658 - Red Component - 1 - - Ansi 8 Color - - Blue Component - 0.0 - Green Component - 0.0 - Red Component - 0.0 - - Ansi 9 Color - - Blue Component - 0.40000000000000002 - Green Component - 0.40000000000000002 - Red Component - 0.80000000000000004 - - Background Color - - Blue Component - 0.12941177189350128 - Green Component - 0.12156862765550613 - Red Component - 0.11372549086809158 - - Bold Color - - Blue Component - 0.77647058820000003 - Green Component - 0.7843137255 - Red Component - 0.7725490196 - - Cursor Color - - Blue Component - 0.77647058820000003 - Green Component - 0.7843137255 - Red Component - 0.7725490196 - - Cursor Text Color - - Blue Component - 0.12941177189350128 - Green Component - 0.12156862765550613 - Red Component - 0.11372549086809158 - - Foreground Color - - Blue Component - 0.77647058823529413 - Green Component - 0.78431372549019607 - Red Component - 0.77254901960784317 - - Selected Text Color - - Blue Component - 0.77647058820000003 - Green Component - 0.7843137255 - Red Component - 0.7725490196 - - Selection Color - - Blue Component - 0.25490196078431371 - Green Component - 0.23137254901960785 - Red Component - 0.21568627450980393 - - - - """.data(using: .utf8) - print("theme \(Theme.fromiTermColors(name: "tomorrow night", data: data))") - } } } } diff --git a/ShhShell/Themes/Theme.swift b/ShhShell/Themes/Theme.swift index 5f648c4..ac1c580 100644 --- a/ShhShell/Themes/Theme.swift +++ b/ShhShell/Themes/Theme.swift @@ -49,13 +49,16 @@ struct Theme: Hashable, Equatable, Identifiable { ) } - static func fromiTermColors(name: String, data: Data?) -> Theme? { + static func decodeTheme(name: String, data: Data?) -> Theme? { guard let data else { return nil } - let decoder = PropertyListDecoder() - - guard let decoded = try? decoder.decode(ThemeCodable.self, from: data) else { return nil } + let plistDecoder = PropertyListDecoder() + let jsonDecoder = JSONDecoder() + guard let decoded = + (try? plistDecoder.decode(ThemeCodable.self, from: data)) ?? + (try? jsonDecoder.decode(ThemeCodable.self, from: data)) + else { return nil } let theme = Theme( name: name, ansi: decoded.ansi, @@ -161,10 +164,14 @@ extension SwiftTerm.Color { } extension SwiftTerm.Color { + var suiColor: SwiftUI.Color { + return Color(uiColor: self.uiColor) + } + var uiColor: UIColor { - let red = CGFloat(self.red/65535) - let green = CGFloat(self.green/65535) - let blue = CGFloat(self.blue/65535) + let red = CGFloat(self.red)/65535 + let green = CGFloat(self.green)/65535 + let blue = CGFloat(self.blue)/65535 return UIColor(red: red, green: green, blue: blue, alpha: 1) } } diff --git a/ShhShell/Views/Hosts/HostsView.swift b/ShhShell/Views/Hosts/HostsView.swift index a1b2df8..9c3069b 100644 --- a/ShhShell/Views/Hosts/HostsView.swift +++ b/ShhShell/Views/Hosts/HostsView.swift @@ -68,7 +68,7 @@ struct HostsView: View { Section() { NavigationLink { - ThemesView(hostsManager: HostsManager()) + ThemesView(hostsManager: hostsManager) } label: { Label("Themes", systemImage: "swatchpalette") } diff --git a/ShhShell/Views/Themes/ThemesView.swift b/ShhShell/Views/Themes/ThemesView.swift index ba2aa49..e87fe72 100644 --- a/ShhShell/Views/Themes/ThemesView.swift +++ b/ShhShell/Views/Themes/ThemesView.swift @@ -10,10 +10,83 @@ 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 { - ForEach(hostsManager.themes) { theme in - ZStack { - RoundedRectangle(cornerRadius: 10) + 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) + .foregroundStyle(theme.ansi[index].suiColor) + .onAppear { + print(index) + } + } + } + HStack { + ForEach(8..<16, id: \.self) { index in + Rectangle() + .frame(width: 12) + .foregroundStyle(theme.ansi[index].suiColor) + .onAppear { + print(index) + } + } + } + } + } + .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") + } + } } } }