暫無描述

AppDelegate.swift 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // AppDelegate.swift
  3. // smart_printer
  4. //
  5. import Cocoa
  6. @main
  7. class AppDelegate: NSObject, NSApplicationDelegate {
  8. private weak var mainWindowController: MainWindowController?
  9. func applicationWillFinishLaunching(_ notification: Notification) {
  10. AppSettings.applyAppearance()
  11. }
  12. func applicationDidFinishLaunching(_ notification: Notification) {
  13. resolveMainWindowController()
  14. configureMainWindow()
  15. configurePreferencesMenu()
  16. NSApp.activate(ignoringOtherApps: true)
  17. }
  18. @objc func showSettings(_ sender: Any?) {
  19. NSApp.activate(ignoringOtherApps: true)
  20. mainWindow?.makeKeyAndOrderFront(nil)
  21. NotificationCenter.default.post(name: .showSettings, object: nil)
  22. }
  23. private func configurePreferencesMenu() {
  24. guard let preferencesItem = NSApp.mainMenu?
  25. .item(withTitle: "smart_printer")?
  26. .submenu?
  27. .item(withTitle: "Preferences…") else { return }
  28. preferencesItem.target = self
  29. preferencesItem.action = #selector(showSettings(_:))
  30. }
  31. func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
  32. false
  33. }
  34. func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
  35. if !flag {
  36. resolveMainWindowController()
  37. mainWindowController?.showWindow(self)
  38. mainWindowController?.window?.makeKeyAndOrderFront(self)
  39. NSApp.activate(ignoringOtherApps: true)
  40. }
  41. return true
  42. }
  43. func applicationShouldRestoreApplicationState(_ app: NSApplication) -> Bool {
  44. false
  45. }
  46. private func configureMainWindow() {
  47. guard let window = mainWindow else {
  48. DispatchQueue.main.async { [weak self] in
  49. self?.configureMainWindow()
  50. }
  51. return
  52. }
  53. window.title = "Smart Printer"
  54. window.titlebarAppearsTransparent = true
  55. window.titleVisibility = .hidden
  56. window.styleMask.insert(.fullSizeContentView)
  57. window.isMovableByWindowBackground = true
  58. window.backgroundColor = AppTheme.background
  59. window.setContentSize(NSSize(width: AppTheme.windowWidth, height: AppTheme.windowHeight))
  60. window.minSize = NSSize(width: AppTheme.windowMinWidth, height: AppTheme.windowMinHeight)
  61. window.isRestorable = false
  62. centerWindowOnScreen(window)
  63. DispatchQueue.main.async {
  64. self.centerWindowOnScreen(window)
  65. window.makeKeyAndOrderFront(nil)
  66. }
  67. }
  68. private func centerWindowOnScreen(_ window: NSWindow) {
  69. let screen = NSScreen.main ?? window.screen ?? NSScreen.screens.first
  70. guard let visibleFrame = screen?.visibleFrame else {
  71. window.center()
  72. return
  73. }
  74. var frame = window.frame
  75. frame.size.width = min(frame.width, visibleFrame.width)
  76. frame.size.height = min(frame.height, visibleFrame.height)
  77. frame.origin.x = visibleFrame.midX - frame.width / 2
  78. frame.origin.y = visibleFrame.midY - frame.height / 2
  79. window.setFrame(frame, display: true)
  80. }
  81. private var mainWindow: NSWindow? {
  82. mainWindowController?.window
  83. ?? NSApplication.shared.windows.first(where: \.canBecomeMain)
  84. }
  85. private func resolveMainWindowController() {
  86. guard mainWindowController == nil else { return }
  87. mainWindowController = NSApplication.shared.windows
  88. .compactMap { $0.windowController as? MainWindowController }
  89. .first
  90. if mainWindowController == nil {
  91. DispatchQueue.main.async { [weak self] in
  92. self?.resolveMainWindowController()
  93. }
  94. }
  95. }
  96. func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
  97. true
  98. }
  99. }