mirror of
https://github.com/neon443/ShhShell.git
synced 2026-03-11 13:26:16 +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 hosts: [Host] = []
|
||||||
@Published var themes: [Theme] = []
|
@Published var themes: [Theme] = []
|
||||||
|
@Published var selectedThemeIndex: Int = -1
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
loadHosts()
|
loadHosts()
|
||||||
@@ -48,6 +49,19 @@ class HostsManager: ObservableObject, @unchecked Sendable {
|
|||||||
task.resume()
|
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) {
|
func renameTheme(_ theme: Theme?, to newName: String) {
|
||||||
guard let theme else { return }
|
guard let theme else { return }
|
||||||
guard theme.name != newName else { return }
|
guard theme.name != newName else { return }
|
||||||
|
|||||||
@@ -185,7 +185,7 @@ struct ConnectionView: View {
|
|||||||
}
|
}
|
||||||
.onAppear {
|
.onAppear {
|
||||||
if shellView == nil {
|
if shellView == nil {
|
||||||
shellView = ShellView(handler: handler)
|
shellView = ShellView(handler: handler, hostsManager: hostsManager)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onAppear {
|
.onAppear {
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ struct HostsView: View {
|
|||||||
NavigationLink() {
|
NavigationLink() {
|
||||||
ForEach(hostsManager.hosts) { host in
|
ForEach(hostsManager.hosts) { host in
|
||||||
let miniHandler = SSHHandler(host: host)
|
let miniHandler = SSHHandler(host: host)
|
||||||
TerminalController(handler: miniHandler)
|
TerminalController(handler: miniHandler, hostsManager: hostsManager)
|
||||||
.onAppear { miniHandler.go() }
|
.onAppear { miniHandler.go() }
|
||||||
}
|
}
|
||||||
} label: {
|
} label: {
|
||||||
|
|||||||
@@ -12,10 +12,14 @@ import SwiftTerm
|
|||||||
@MainActor
|
@MainActor
|
||||||
final class SSHTerminalDelegate: TerminalView, Sendable, @preconcurrency TerminalViewDelegate {
|
final class SSHTerminalDelegate: TerminalView, Sendable, @preconcurrency TerminalViewDelegate {
|
||||||
var handler: SSHHandler?
|
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.init(frame: frame)
|
||||||
self.handler = handler
|
self.handler = handler
|
||||||
|
self.hostsManager = hostsManager
|
||||||
|
|
||||||
|
applyTheme(index: hostsManager.selectedThemeIndex)
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
Task {
|
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().installPalette(colors: theme.ansi)
|
||||||
getTerminal().foregroundColor = theme.foreground
|
getTerminal().foregroundColor = theme.foreground
|
||||||
getTerminal().backgroundColor = theme.background
|
getTerminal().backgroundColor = theme.background
|
||||||
|
|||||||
@@ -9,13 +9,14 @@ import SwiftUI
|
|||||||
|
|
||||||
struct ShellView: View {
|
struct ShellView: View {
|
||||||
@ObservedObject var handler: SSHHandler
|
@ObservedObject var handler: SSHHandler
|
||||||
|
@ObservedObject var hostsManager: HostsManager
|
||||||
|
|
||||||
@Environment(\.dismiss) var dismiss
|
@Environment(\.dismiss) var dismiss
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationStack {
|
NavigationStack {
|
||||||
ZStack {
|
ZStack {
|
||||||
TerminalController(handler: handler)
|
TerminalController(handler: handler, hostsManager: hostsManager)
|
||||||
.onAppear {
|
.onAppear {
|
||||||
TerminalController.TerminalViewContainer.shared?.restoreScrollback()
|
TerminalController.TerminalViewContainer.shared?.restoreScrollback()
|
||||||
}
|
}
|
||||||
@@ -61,5 +62,8 @@ struct ShellView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#Preview {
|
#Preview {
|
||||||
ShellView(handler: SSHHandler(host: Host.debug))
|
ShellView(
|
||||||
|
handler: SSHHandler(host: Host.debug),
|
||||||
|
hostsManager: HostsManager()
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import SwiftTerm
|
|||||||
|
|
||||||
struct TerminalController: UIViewRepresentable {
|
struct TerminalController: UIViewRepresentable {
|
||||||
@ObservedObject var handler: SSHHandler
|
@ObservedObject var handler: SSHHandler
|
||||||
|
@ObservedObject var hostsManager: HostsManager
|
||||||
|
|
||||||
final class TerminalViewContainer {
|
final class TerminalViewContainer {
|
||||||
@MainActor static var shared: SSHTerminalDelegate?
|
@MainActor static var shared: SSHTerminalDelegate?
|
||||||
@@ -22,7 +23,11 @@ struct TerminalController: UIViewRepresentable {
|
|||||||
return existing
|
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.translatesAutoresizingMaskIntoConstraints = false
|
||||||
tv.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
tv.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
||||||
|
|
||||||
|
|||||||
@@ -31,6 +31,10 @@ struct ThemeManagerView: View {
|
|||||||
LazyHGrid(rows: [grid, grid], alignment: .center, spacing: 8) {
|
LazyHGrid(rows: [grid, grid], alignment: .center, spacing: 8) {
|
||||||
ForEach(hostsManager.themes) { theme in
|
ForEach(hostsManager.themes) { theme in
|
||||||
ThemePreview(theme: theme)
|
ThemePreview(theme: theme)
|
||||||
|
.scaleEffect(hostsManager.isThemeSelected(theme) ? 1.2 : 1)
|
||||||
|
.onTapGesture {
|
||||||
|
hostsManager.selectTheme(theme)
|
||||||
|
}
|
||||||
.contextMenu {
|
.contextMenu {
|
||||||
Button() {
|
Button() {
|
||||||
themeToRename = theme
|
themeToRename = theme
|
||||||
|
|||||||
Reference in New Issue
Block a user