diff options
| author | Dustin Mierau <dustin@mierau.me> | 2025-10-28 10:06:33 -0700 |
|---|---|---|
| committer | Dustin Mierau <dustin@mierau.me> | 2025-10-28 10:06:33 -0700 |
| commit | c5165d348c1efbcc553b3c31dbeaa23353914fb6 (patch) | |
| tree | 6ba4121a34c095d1180d3025f59e5a300b533937 /Hotline | |
| parent | d23d0ad9405c1622569415e6ce16d3768af2936a (diff) | |
Some state cleanup and banner color caching in ServerState.
Diffstat (limited to 'Hotline')
| -rw-r--r-- | Hotline/MacApp.swift | 16 | ||||
| -rw-r--r-- | Hotline/State/AppState.swift | 13 | ||||
| -rw-r--r-- | Hotline/Utility/ColorArt.swift | 11 | ||||
| -rw-r--r-- | Hotline/macOS/HotlinePanelView.swift | 13 | ||||
| -rw-r--r-- | Hotline/macOS/ServerView.swift | 22 |
5 files changed, 36 insertions, 39 deletions
diff --git a/Hotline/MacApp.swift b/Hotline/MacApp.swift index dd7e190..89db36e 100644 --- a/Hotline/MacApp.swift +++ b/Hotline/MacApp.swift @@ -192,26 +192,12 @@ struct Application: App { .defaultSize(width: 690, height: 760) .defaultPosition(.center) .onChange(of: activeServerState) { - AppState.shared.activeServerState = activeServerState - } - .onChange(of: activeHotline) { - AppState.shared.activeHotline = activeHotline - } - .onChange(of: activeHotline?.serverTitle) { - if let hotline = activeHotline { - AppState.shared.activeServerName = hotline.serverTitle - } - } - .onChange(of: activeHotline?.bannerImage) { withAnimation { - AppState.shared.activeServerBanner = activeHotline?.bannerImage + AppState.shared.activeServerState = activeServerState } } .onChange(of: activeHotline) { AppState.shared.activeHotline = activeHotline - if let hotline = activeHotline { - AppState.shared.activeServerName = hotline.serverTitle - } } .commands { CommandGroup(replacing: .newItem) { diff --git a/Hotline/State/AppState.swift b/Hotline/State/AppState.swift index 11bae74..3917ad0 100644 --- a/Hotline/State/AppState.swift +++ b/Hotline/State/AppState.swift @@ -7,18 +7,13 @@ extension EnvironmentValues { @Observable final class AppState { static let shared = AppState() - + private init() { - + } - + var activeHotline: Hotline? = nil var activeServerState: ServerState? = nil - - // Frontmost server window information - var activeServerID: UUID? = nil - var activeServerBanner: NSImage? = nil - var activeServerName: String? = nil - + var cloudKitReady: Bool = false } diff --git a/Hotline/Utility/ColorArt.swift b/Hotline/Utility/ColorArt.swift index abbe04b..1f99f0f 100644 --- a/Hotline/Utility/ColorArt.swift +++ b/Hotline/Utility/ColorArt.swift @@ -32,13 +32,20 @@ import SwiftUI fileprivate let kColorThresholdMinimumPercentage: CGFloat = 0.001 -struct ColorArt { +struct ColorArt: Equatable { let backgroundColor: NSColor let primaryColor: NSColor let secondaryColor: NSColor let detailColor: NSColor let scaledImage: NSImage - + + static func == (lhs: ColorArt, rhs: ColorArt) -> Bool { + return lhs.backgroundColor == rhs.backgroundColor && + lhs.primaryColor == rhs.primaryColor && + lhs.secondaryColor == rhs.secondaryColor && + lhs.detailColor == rhs.detailColor + } + init?(image: NSImage, scaledSize: NSSize = .zero) { let finalImage = Self.scaleImage(image, size: scaledSize) self.scaledImage = finalImage diff --git a/Hotline/macOS/HotlinePanelView.swift b/Hotline/macOS/HotlinePanelView.swift index 4ae4924..ee4d198 100644 --- a/Hotline/macOS/HotlinePanelView.swift +++ b/Hotline/macOS/HotlinePanelView.swift @@ -4,16 +4,9 @@ struct HotlinePanelView: View { @Environment(\.openWindow) var openWindow @Environment(\.colorScheme) var colorScheme - private var colorArt: ColorArt? { - if let banner = AppState.shared.activeServerBanner { - return ColorArt(image: banner, scaledSize: NSSize(width: 100, height: 100)) - } - return nil - } - var body: some View { VStack(spacing: 0) { - Image(nsImage: AppState.shared.activeServerBanner ?? NSImage(named: "Default Banner")!) + Image(nsImage: AppState.shared.activeServerState?.serverBanner ?? NSImage(named: "Default Banner")!) .interpolation(.high) .resizable() .scaledToFill() @@ -123,8 +116,8 @@ struct HotlinePanelView: View { .padding(.top, 12) .padding(.bottom, 12) .padding([.leading, .trailing], 12) - .background(colorArt.map { Color(nsColor: $0.backgroundColor) } ?? Color(nsColor: .controlBackgroundColor)) - .foregroundStyle(colorArt.map { Color(nsColor: $0.primaryColor) } ?? Color.primary) + .background(AppState.shared.activeServerState?.bannerColors.map { Color(nsColor: $0.backgroundColor) } ?? Color(nsColor: .controlBackgroundColor)) + .foregroundStyle(AppState.shared.activeServerState?.bannerColors.map { Color(nsColor: $0.primaryColor) } ?? Color.primary) // .background(Color.red.opacity(0.5).blendMode(.multiply)) // GroupBox { diff --git a/Hotline/macOS/ServerView.swift b/Hotline/macOS/ServerView.swift index 77b6dc8..6d5743c 100644 --- a/Hotline/macOS/ServerView.swift +++ b/Hotline/macOS/ServerView.swift @@ -6,11 +6,14 @@ import AppKit class ServerState: Equatable { var id: UUID = UUID() var selection: ServerNavigationType - + var serverName: String? = nil + var serverBanner: NSImage? = nil + var bannerColors: ColorArt? = nil + init(selection: ServerNavigationType) { self.selection = selection } - + static func == (lhs: ServerState, rhs: ServerState) -> Bool { return lhs.id == rhs.id } @@ -104,7 +107,7 @@ extension FocusedValues { get { self[ActiveHotlineModelFocusedValueKey.self] } set { self[ActiveHotlineModelFocusedValueKey.self] = newValue } } - + var activeServerState: ServerState? { get { self[ActiveServerStateFocusedValueKey.self] } set { self[ActiveServerStateFocusedValueKey.self] = newValue } @@ -238,6 +241,19 @@ struct ServerView: View { .onDisappear { model.disconnect() } + .onChange(of: model.serverTitle) { oldTitle, newTitle in + state.serverName = newTitle + } + .onChange(of: model.bannerImage) { oldBanner, newBanner in + withAnimation { + state.serverBanner = newBanner + if let banner = newBanner { + state.bannerColors = ColorArt(image: banner, scaledSize: NSSize(width: 100, height: 100)) + } else { + state.bannerColors = nil + } + } + } .alert(model.errorMessage ?? "Server Error", isPresented: $model.errorDisplayed) { Button("OK") {} } |