added selecting and applying themes

added selectTheme to set the selectedtheme index
added isthemeselected to check if a theme is selected
applytheme takes an index now

ui needs work lol - cant select default, and selction doesnt save between sessions
This commit is contained in:
neon443
2025-06-28 16:06:11 +01:00
parent 0f3f936638
commit f19f32682f
7 changed files with 42 additions and 7 deletions

View File

@@ -14,6 +14,7 @@ class HostsManager: ObservableObject, @unchecked Sendable {
@Published var hosts: [Host] = []
@Published var themes: [Theme] = []
@Published var selectedThemeIndex: Int = -1
init() {
loadHosts()
@@ -48,6 +49,19 @@ class HostsManager: ObservableObject, @unchecked Sendable {
task.resume()
}
func selectTheme(_ selectedTheme: Theme) {
guard let index = themes.firstIndex(where: { $0 == selectedTheme }) else {
withAnimation { selectedThemeIndex = -1 }
return
}
withAnimation { selectedThemeIndex = index }
}
func isThemeSelected(_ themeInQuestion: Theme) -> Bool {
guard let index = themes.firstIndex(where: { $0 == themeInQuestion }) else { return false }
return index == selectedThemeIndex
}
func renameTheme(_ theme: Theme?, to newName: String) {
guard let theme else { return }
guard theme.name != newName else { return }

View File

@@ -185,7 +185,7 @@ struct ConnectionView: View {
}
.onAppear {
if shellView == nil {
shellView = ShellView(handler: handler)
shellView = ShellView(handler: handler, hostsManager: hostsManager)
}
}
.onAppear {

View File

@@ -21,7 +21,7 @@ struct HostsView: View {
NavigationLink() {
ForEach(hostsManager.hosts) { host in
let miniHandler = SSHHandler(host: host)
TerminalController(handler: miniHandler)
TerminalController(handler: miniHandler, hostsManager: hostsManager)
.onAppear { miniHandler.go() }
}
} label: {

View File

@@ -12,10 +12,14 @@ import SwiftTerm
@MainActor
final class SSHTerminalDelegate: TerminalView, Sendable, @preconcurrency TerminalViewDelegate {
var handler: SSHHandler?
var hostsManager: HostsManager?
public convenience init(frame: CGRect, handler: SSHHandler) {
public convenience init(frame: CGRect, handler: SSHHandler, hostsManager: HostsManager) {
self.init(frame: frame)
self.handler = handler
self.hostsManager = hostsManager
applyTheme(index: hostsManager.selectedThemeIndex)
DispatchQueue.main.async {
Task {
@@ -48,7 +52,11 @@ final class SSHTerminalDelegate: TerminalView, Sendable, @preconcurrency Termina
}
}
func applyTheme(_ theme: Theme) {
func applyTheme(index themeIndex: Int) {
guard themeIndex != -1 else { return }
guard let hostsManager = hostsManager else { return }
let theme = hostsManager.themes[themeIndex]
getTerminal().installPalette(colors: theme.ansi)
getTerminal().foregroundColor = theme.foreground
getTerminal().backgroundColor = theme.background

View File

@@ -9,13 +9,14 @@ import SwiftUI
struct ShellView: View {
@ObservedObject var handler: SSHHandler
@ObservedObject var hostsManager: HostsManager
@Environment(\.dismiss) var dismiss
var body: some View {
NavigationStack {
ZStack {
TerminalController(handler: handler)
TerminalController(handler: handler, hostsManager: hostsManager)
.onAppear {
TerminalController.TerminalViewContainer.shared?.restoreScrollback()
}
@@ -61,5 +62,8 @@ struct ShellView: View {
}
#Preview {
ShellView(handler: SSHHandler(host: Host.debug))
ShellView(
handler: SSHHandler(host: Host.debug),
hostsManager: HostsManager()
)
}

View File

@@ -12,6 +12,7 @@ import SwiftTerm
struct TerminalController: UIViewRepresentable {
@ObservedObject var handler: SSHHandler
@ObservedObject var hostsManager: HostsManager
final class TerminalViewContainer {
@MainActor static var shared: SSHTerminalDelegate?
@@ -22,7 +23,11 @@ struct TerminalController: UIViewRepresentable {
return existing
}
let tv = SSHTerminalDelegate(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: .zero), handler: handler)
let tv = SSHTerminalDelegate(
frame: CGRect(origin: CGPoint(x: 0, y: 0), size: .zero),
handler: handler,
hostsManager: hostsManager
)
tv.translatesAutoresizingMaskIntoConstraints = false
tv.autoresizingMask = [.flexibleWidth, .flexibleHeight]

View File

@@ -31,6 +31,10 @@ struct ThemeManagerView: View {
LazyHGrid(rows: [grid, grid], alignment: .center, spacing: 8) {
ForEach(hostsManager.themes) { theme in
ThemePreview(theme: theme)
.scaleEffect(hostsManager.isThemeSelected(theme) ? 1.2 : 1)
.onTapGesture {
hostsManager.selectTheme(theme)
}
.contextMenu {
Button() {
themeToRename = theme