aboutsummaryrefslogtreecommitdiff
path: root/Hotline/Application-macOS.swift
blob: f6b5bc505b5a259f1c278dc82b90ae591a91dbe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import SwiftUI
import SwiftData
import UniformTypeIdentifiers

@Observable
final class AppLaunchState {
  static let shared = AppLaunchState()
  
  enum LaunchState {
    case loading
    case launched
    case terminated
  }
  
  var launchState = LaunchState.loading
}

class AppDelegate: NSObject, NSApplicationDelegate {
  func applicationDidFinishLaunching(_ notification: Notification) {
    AppLaunchState.shared.launchState = .launched
  }
  
  func applicationWillTerminate(_ notification: Notification) {
    AppLaunchState.shared.launchState = .terminated
  }
}

@main
struct Application: App {
  @Environment(\.scenePhase) private var scenePhase
  @Environment(\.openWindow) private var openWindow
  @Environment(\.openURL) private var openURL
  
  @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
  
  @State private var bookmarks = BookmarksOld()
  @State private var hotlinePanel: HotlinePanel? = nil

  @FocusedValue(\.activeHotlineModel) private var activeHotline: Hotline?
  @FocusedValue(\.activeServerState) private var activeServerState: ServerState?
    
  var body: some Scene {
    // MARK: Tracker Window
    Window("Servers", id: "servers") {
      TrackerView()
        .frame(minWidth: 250, minHeight: 250)
        .environment(bookmarks)
    }
    .modelContainer(for: Bookmark.self, isAutosaveEnabled: true, isUndoEnabled: true)
    .defaultSize(width: 700, height: 550)
    .defaultPosition(.center)
    .keyboardShortcut(.init("R"), modifiers: .command)
    .onChange(of: AppLaunchState.shared.launchState) {
      if AppLaunchState.shared.launchState == .launched {
        if Prefs.shared.showBannerToolbar {
          showBannerWindow()
        }
      }
    }
    
    // MARK: About Box
    Window("About", id: "about") {
      AboutView()
        .ignoresSafeArea()
        .background(Color.hotlineRed)
    }
    .windowResizability(.contentSize)
    .windowStyle(.hiddenTitleBar)
    .defaultPosition(.center)
    .commandsRemoved() // Remove About that was automatically added to Window menu.
    .commands {
      CommandGroup(replacing: CommandGroupPlacement.appInfo) {
        Button("About Hotline") {
          openWindow(id: "about")
        }
      }
    }
    
    // MARK: Server Window
    WindowGroup(id: "server", for: Server.self) { server in
      ServerView(server: server)
        .frame(minWidth: 430, minHeight: 300)
        .environment(bookmarks)
    } defaultValue: {
      Server(name: nil, description: nil, address: "")
    }
    .defaultSize(width: 750, height: 700)
    .defaultPosition(.center)
    .onChange(of: activeServerState) {
      ApplicationState.shared.activeServerState = activeServerState
    }
    .onChange(of: activeHotline) {
      ApplicationState.shared.activeHotline = activeHotline
    }
    .onChange(of: activeHotline?.serverTitle) {
      if let hotline = activeHotline {
        ApplicationState.shared.activeServerName = hotline.serverTitle
      }
    }
    .onChange(of: activeHotline?.bannerImage) {
      withAnimation {
        ApplicationState.shared.activeServerBanner = activeHotline?.bannerImage
      }
    }
    .onChange(of: activeHotline) {
      ApplicationState.shared.activeHotline = activeHotline
      if let hotline = activeHotline {
        ApplicationState.shared.activeServerName = hotline.serverTitle
      }
    }
    .commands {
      CommandGroup(replacing: .newItem) {
        Button("Connect to Server...") {
          openWindow(id: "server")
        }
        .keyboardShortcut(.init("K"), modifiers: .command)
      }
      CommandGroup(after: .singleWindowList) {
        Button("Toolbar") {
          toggleBannerWindow()
        }
        .keyboardShortcut(.init("\\"), modifiers: [.shift, .command])
      }
      CommandGroup(after: .help) {
        Divider()
        Button("Request Feature...") {
          if let url = URL(string: "https://github.com/mierau/hotline/issues/new?labels=enhancement") {
            openURL(url)
          }
        }
        Button("Report Bug...") {
          if let url = URL(string: "https://github.com/mierau/hotline/issues/new?labels=bug") {
            openURL(url)
          }
        }
        Divider()
        Button("Download Latest...") {
          if let url = URL(string: "https://github.com/mierau/hotline/releases/latest") {
            openURL(url)
          }
        }
      }
      CommandMenu("Server") {
        Button("Disconnect") {
          activeHotline?.disconnect()
        }
        .disabled(activeHotline?.status == .disconnected)
        Divider()
        Button("Broadcast Message...") {
          // TODO: Implement broadcast message when user is allowed.
        }
        .disabled(true)
        .keyboardShortcut(.init("B"), modifiers: .command)
        Divider()
        Button("Show Chat") {
          activeServerState?.selection = .chat
        }
        .disabled(activeHotline?.status != .loggedIn)
        .keyboardShortcut(.init("1"), modifiers: .command)
        Button("Show News") {
          activeServerState?.selection = .news
        }
        .disabled(activeHotline?.status != .loggedIn)
        .keyboardShortcut(.init("2"), modifiers: .command)
        Button("Show Message Board") {
          activeServerState?.selection = .board
        }
        .disabled(activeHotline?.status != .loggedIn)
        .keyboardShortcut(.init("3"), modifiers: .command)
        Button("Show Files") {
          activeServerState?.selection = .files
        }
        .disabled(activeHotline?.status != .loggedIn)
        .keyboardShortcut(.init("4"), modifiers: .command)
      }
    }
    
    // MARK: Settings Window
    Settings {
      SettingsView()
    }
    
    
    // MARK: News Editor Window
//    WindowGroup(id: "news-editor", for: NewsArticle.self) { $article in
//      NewsEditorView(article: $article)
//    }
//    .windowResizability(.contentSize)
//    .windowStyle(.titleBar)
//    .windowToolbarStyle(.unifiedCompact(showsTitle: true))
//    .defaultSize(width: 450, height: 550)
//    .defaultPosition(.center)
    
    // MARK: Image Preview Window
    WindowGroup(id: "preview-image", for: PreviewFileInfo.self) { $info in
      FilePreviewImageView(info: $info)
    }
    .windowResizability(.contentSize)
    .windowStyle(.titleBar)
    .windowToolbarStyle(.unifiedCompact(showsTitle: true))
    .defaultSize(width: 350, height: 150)
    .defaultPosition(.center)
    
    // MARK: Text Preview Window
    WindowGroup(id: "preview-text", for: PreviewFileInfo.self) { $info in
      FilePreviewTextView(info: $info)
    }
    .windowResizability(.automatic)
    .windowStyle(.titleBar)
    .windowToolbarStyle(.unifiedCompact(showsTitle: true))
    .defaultSize(width: 450, height: 550)
    .defaultPosition(.center)
  }
  
  func showBannerWindow() {
    if hotlinePanel == nil {
      hotlinePanel = HotlinePanel(HotlinePanelView())
    }
    
    if hotlinePanel?.isVisible == false {
      hotlinePanel?.orderFront(nil)
      Prefs.shared.showBannerToolbar = true
    }
  }
  
  func toggleBannerWindow() {
    if hotlinePanel == nil {
      hotlinePanel = HotlinePanel(HotlinePanelView())
    }
    
    if hotlinePanel?.isVisible == true {
      hotlinePanel?.orderOut(nil)
      Prefs.shared.showBannerToolbar = false
    }
    else {
      hotlinePanel?.orderFront(nil)
      Prefs.shared.showBannerToolbar = true
    }
  }
}