summaryrefslogtreecommitdiff
path: root/mut/dmenu-mac/src/AppDelegate.swift
blob: 5c115b3bb6c4fc40dc6d6765d36f5cd82a9421c3 (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
/*
 * 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
    )
}