mirror of
https://github.com/neon443/ShhShell.git
synced 2026-03-11 05:19:13 +00:00
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:
@@ -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 }
|
||||
|
||||
@@ -185,7 +185,7 @@ struct ConnectionView: View {
|
||||
}
|
||||
.onAppear {
|
||||
if shellView == nil {
|
||||
shellView = ShellView(handler: handler)
|
||||
shellView = ShellView(handler: handler, hostsManager: hostsManager)
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
|
||||
@@ -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: {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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]
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user