summaryrefslogtreecommitdiff
path: root/src/AppDelegate.swift
diff options
context:
space:
mode:
authorMike Vink <59492084+ivi-vink@users.noreply.github.com>2025-01-21 00:10:34 +0100
committerMike Vink <59492084+ivi-vink@users.noreply.github.com>2025-01-21 00:10:34 +0100
commit8cdb3d2150fb591894d85d5ca5243f9ef6156099 (patch)
treea8a2a325acafccfc54fdc9aeb16f32248f81dc52 /src/AppDelegate.swift
Squashed 'mut/dmenu-mac/' content from commit c86c277
git-subtree-dir: mut/dmenu-mac git-subtree-split: c86c2774b8c7ac19299dfdc93d2e66e1a0393326
Diffstat (limited to 'src/AppDelegate.swift')
-rw-r--r--src/AppDelegate.swift110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/AppDelegate.swift b/src/AppDelegate.swift
new file mode 100644
index 0000000..5c115b3
--- /dev/null
+++ b/src/AppDelegate.swift
@@ -0,0 +1,110 @@
+/*
+ * Copyright (c) 2016 Jose Pereira <onaips@gmail.com>.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+import Cocoa
+import Carbon
+import LaunchAtLogin
+import Preferences
+import KeyboardShortcuts
+
+extension Settings.PaneIdentifier {
+ static let general = Self("general")
+}
+
+// import legacy settings if they existed
+let kLegacyKc = "kDefaultsGlobalShortcutKeycode"
+let kLegacyMf = "kDefaultsGlobalShortcutModifiedFlags"
+
+extension KeyboardShortcuts.Name {
+ static let activateSearch = Self("activateSearchGlobalShortcut", default: .init(
+ (UserDefaults.standard.object(forKey: kLegacyKc) != nil) ?
+ KeyboardShortcuts.Key(rawValue: UserDefaults.standard.integer(forKey: kLegacyKc)):
+ .space,
+ modifiers: (UserDefaults.standard.object(forKey: kLegacyKc) != nil) ?
+ NSEvent.ModifierFlags(rawValue: UInt(UserDefaults.standard.integer(forKey: kLegacyMf))) :
+ [.command]))
+}
+
+@NSApplicationMain
+class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
+ @IBOutlet var controllerWindow: NSWindowController?
+
+ private var statusItem: NSStatusItem!
+ private var startAtLaunch: NSMenuItem!
+
+ func applicationDidFinishLaunching(_ aNotification: Notification) {
+ statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
+
+ if let button = statusItem.button {
+ button.title = "d"
+ }
+ setupMenus()
+ }
+
+ func applicationWillTerminate(_ aNotification: Notification) {
+ }
+
+ func setupMenus() {
+ let menu = NSMenu()
+
+ let open = NSMenuItem(title: "Open", action: #selector(resumeApp), keyEquivalent: "")
+ menu.addItem(open)
+
+ let settings = NSMenuItem(title: "Settings", action: #selector(openSettings), keyEquivalent: "")
+ menu.addItem(settings)
+
+ menu.addItem(NSMenuItem.separator())
+ startAtLaunch = NSMenuItem(title: "Launch at Login", action: #selector(toggleLaunchAtLogin), keyEquivalent: "")
+ startAtLaunch.state = LaunchAtLogin.isEnabled ? .on : .off
+ menu.addItem(startAtLaunch)
+
+ menu.addItem(NSMenuItem.separator())
+
+ menu.addItem(NSMenuItem(title: "Quit", action: #selector(NSApplication.terminate(_:)), keyEquivalent: ""))
+
+ statusItem.menu = menu
+ }
+
+ @objc func resumeApp() {
+ let storyboard = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: Bundle.main)
+ // swiftlint:disable force_cast
+ let mainPageController = storyboard.instantiateController(
+ withIdentifier: "SearchViewController") as! SearchViewController
+ // swiftlint:enable force_cast
+ mainPageController.resumeApp()
+ }
+
+ @objc func openSettings() {
+ settingsWindowController.show()
+ }
+
+ @objc func toggleLaunchAtLogin() {
+ let enabled = !LaunchAtLogin.isEnabled
+ LaunchAtLogin.isEnabled = enabled
+ startAtLaunch.state = enabled ? .on : .off
+ }
+
+ private lazy var settings: [SettingsPane] = [
+ GeneralSettingsViewController()
+ ]
+
+ private lazy var settingsWindowController = SettingsWindowController(
+ preferencePanes: settings,
+ style: .segmentedControl,
+ animated: true,
+ hidesToolbarForSingleItem: true
+ )
+}