91 lines
2.9 KiB
Swift
91 lines
2.9 KiB
Swift
import AppKit
|
|
import Combine
|
|
import SwiftUI
|
|
import MacToolsPluginKit
|
|
|
|
@MainActor
|
|
final class AppWindowRouter: NSObject, NSWindowDelegate {
|
|
private let pluginHost: PluginHost
|
|
private let appUpdater: AppUpdater
|
|
private let menuBarIconSettings: MenuBarIconSettings
|
|
private let menuBarIconGallery: MenuBarIconGalleryLibrary
|
|
private let launchAtLoginController: LaunchAtLoginController
|
|
private var settingsWindow: NSWindow?
|
|
private var runtimeLocaleCancellable: AnyCancellable?
|
|
|
|
static var settingsWindowTitle: String {
|
|
AppL10n.settings("settings.window.title", defaultValue: "设置")
|
|
}
|
|
|
|
init(
|
|
pluginHost: PluginHost,
|
|
appUpdater: AppUpdater,
|
|
menuBarIconSettings: MenuBarIconSettings,
|
|
menuBarIconGallery: MenuBarIconGalleryLibrary,
|
|
launchAtLoginController: LaunchAtLoginController
|
|
) {
|
|
self.pluginHost = pluginHost
|
|
self.appUpdater = appUpdater
|
|
self.menuBarIconSettings = menuBarIconSettings
|
|
self.menuBarIconGallery = menuBarIconGallery
|
|
self.launchAtLoginController = launchAtLoginController
|
|
super.init()
|
|
runtimeLocaleCancellable = PluginRuntimeLocalization.source.$revision
|
|
.dropFirst()
|
|
.sink { [weak self] _ in
|
|
Task { @MainActor [weak self] in
|
|
self?.settingsWindow?.title = Self.settingsWindowTitle
|
|
}
|
|
}
|
|
}
|
|
|
|
isolated deinit {
|
|
runtimeLocaleCancellable?.cancel()
|
|
}
|
|
|
|
func showSettings() {
|
|
let window = settingsWindow ?? makeSettingsWindow()
|
|
show(window)
|
|
settingsWindow = window
|
|
}
|
|
|
|
private func show(_ window: NSWindow) {
|
|
NSApplication.shared.activate(ignoringOtherApps: true)
|
|
window.makeKeyAndOrderFront(nil)
|
|
}
|
|
|
|
private func makeSettingsWindow() -> NSWindow {
|
|
let window = NSWindow(
|
|
contentRect: NSRect(x: 0, y: 0, width: 1040, height: 720),
|
|
styleMask: [.titled, .closable, .miniaturizable, .resizable],
|
|
backing: .buffered,
|
|
defer: false
|
|
)
|
|
window.title = Self.settingsWindowTitle
|
|
window.minSize = NSSize(width: 860, height: 560)
|
|
window.contentView = NSHostingView(
|
|
rootView: SettingsView(
|
|
pluginHost: pluginHost,
|
|
appUpdater: appUpdater,
|
|
menuBarIconSettings: menuBarIconSettings,
|
|
menuBarIconGallery: menuBarIconGallery,
|
|
launchAtLoginController: launchAtLoginController
|
|
)
|
|
)
|
|
window.delegate = self
|
|
window.isReleasedWhenClosed = false
|
|
window.center()
|
|
return window
|
|
}
|
|
|
|
func windowWillClose(_ notification: Notification) {
|
|
guard let window = notification.object as? NSWindow, window === settingsWindow else {
|
|
return
|
|
}
|
|
|
|
window.delegate = nil
|
|
window.contentView = nil
|
|
settingsWindow = nil
|
|
}
|
|
}
|