| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- //
- // AppDelegate.swift
- // smart_printer
- //
- import Cocoa
- @main
- class AppDelegate: NSObject, NSApplicationDelegate {
- private weak var mainWindowController: MainWindowController?
- func applicationWillFinishLaunching(_ notification: Notification) {
- AppSettings.applyAppearance()
- }
- func applicationDidFinishLaunching(_ notification: Notification) {
- resolveMainWindowController()
- configureMainWindow()
- configurePreferencesMenu()
- NSApp.activate(ignoringOtherApps: true)
- }
- @objc func showSettings(_ sender: Any?) {
- NSApp.activate(ignoringOtherApps: true)
- mainWindow?.makeKeyAndOrderFront(nil)
- NotificationCenter.default.post(name: .showSettings, object: nil)
- }
- private func configurePreferencesMenu() {
- guard let preferencesItem = NSApp.mainMenu?
- .item(withTitle: "smart_printer")?
- .submenu?
- .item(withTitle: "Preferences…") else { return }
- preferencesItem.target = self
- preferencesItem.action = #selector(showSettings(_:))
- }
- func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
- false
- }
- func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
- if !flag {
- resolveMainWindowController()
- mainWindowController?.showWindow(self)
- mainWindowController?.window?.makeKeyAndOrderFront(self)
- NSApp.activate(ignoringOtherApps: true)
- }
- return true
- }
- func applicationShouldRestoreApplicationState(_ app: NSApplication) -> Bool {
- false
- }
- private func configureMainWindow() {
- guard let window = mainWindow else {
- DispatchQueue.main.async { [weak self] in
- self?.configureMainWindow()
- }
- return
- }
- window.title = "Smart Printer"
- window.titlebarAppearsTransparent = true
- window.titleVisibility = .hidden
- window.styleMask.insert(.fullSizeContentView)
- window.isMovableByWindowBackground = true
- window.backgroundColor = AppTheme.background
- window.setContentSize(NSSize(width: AppTheme.windowWidth, height: AppTheme.windowHeight))
- window.minSize = NSSize(width: AppTheme.windowMinWidth, height: AppTheme.windowMinHeight)
- window.isRestorable = false
- centerWindowOnScreen(window)
- DispatchQueue.main.async {
- self.centerWindowOnScreen(window)
- window.makeKeyAndOrderFront(nil)
- }
- }
- private func centerWindowOnScreen(_ window: NSWindow) {
- let screen = NSScreen.main ?? window.screen ?? NSScreen.screens.first
- guard let visibleFrame = screen?.visibleFrame else {
- window.center()
- return
- }
- var frame = window.frame
- frame.size.width = min(frame.width, visibleFrame.width)
- frame.size.height = min(frame.height, visibleFrame.height)
- frame.origin.x = visibleFrame.midX - frame.width / 2
- frame.origin.y = visibleFrame.midY - frame.height / 2
- window.setFrame(frame, display: true)
- }
- private var mainWindow: NSWindow? {
- mainWindowController?.window
- ?? NSApplication.shared.windows.first(where: \.canBecomeMain)
- }
- private func resolveMainWindowController() {
- guard mainWindowController == nil else { return }
- mainWindowController = NSApplication.shared.windows
- .compactMap { $0.windowController as? MainWindowController }
- .first
- if mainWindowController == nil {
- DispatchQueue.main.async { [weak self] in
- self?.resolveMainWindowController()
- }
- }
- }
- func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
- true
- }
- }
|