touch-bar-simulator/Touch Bar Simulator/AppDelegate.swift
ThatsJustCheesy 2aab75f045 Add "Launch at Login" preference (#53)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
2019-09-13 15:55:39 +07:00

130 lines
4 KiB
Swift

import Cocoa
import Sparkle
import Defaults
import LaunchAtLogin
final class AppDelegate: NSObject, NSApplicationDelegate {
lazy var window = with(TouchBarWindow()) {
$0.alphaValue = CGFloat(Defaults[.windowTransparency])
$0.setUp()
}
lazy var statusItem = with(NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)) {
$0.menu = with(NSMenu()) {
$0.delegate = self
}
$0.button!.image = NSImage(named: "MenuBarIcon")
$0.button!.toolTip = "Right-click or option-click for menu"
}
func applicationWillFinishLaunching(_ notification: Notification) {
UserDefaults.standard.register(defaults: [
"NSApplicationCrashOnExceptions": true
])
}
func applicationDidFinishLaunching(_ notification: Notification) {
NSApp.servicesProvider = self
_ = SUUpdater()
_ = window
_ = statusItem
}
@objc
func captureScreenshot() {
let KEY_6: CGKeyCode = 0x58
pressKey(keyCode: KEY_6, flags: [.maskShift, .maskCommand])
}
@objc
func toggleView(_ pboard: NSPasteboard, userData: String, error: NSErrorPointer) {
toggleView()
}
func toggleView() {
window.setIsVisible(!window.isVisible)
}
}
extension AppDelegate: NSMenuDelegate {
private func update(menu: NSMenu) {
menu.removeAllItems()
guard statusItemShouldShowMenu() else {
return
}
menu.addItem(NSMenuItem(title: "Docking", action: nil, keyEquivalent: ""))
var statusMenuDockingItems: [NSMenuItem] = []
statusMenuDockingItems.append(NSMenuItem("Floating").bindChecked(to: .windowDocking, value: .floating))
statusMenuDockingItems.append(NSMenuItem("Docked to Top").bindChecked(to: .windowDocking, value: .dockedToTop))
statusMenuDockingItems.append(NSMenuItem("Docked to Bottom").bindChecked(to: .windowDocking, value: .dockedToBottom))
for item in statusMenuDockingItems {
item.indentationLevel = 1
}
menu.items.append(contentsOf: statusMenuDockingItems)
menu.addItem(NSMenuItem(title: "Transparency", action: nil, keyEquivalent: ""))
let transparencyItem = NSMenuItem("Transparency")
let transparencyView = NSView(frame: CGRect(origin: .zero, size: CGSize(width: 200, height: 20)))
let slider = MenubarSlider().alwaysRedisplayOnValueChanged().bindDoubleValue(to: .windowTransparency)
slider.frame = CGRect(x: 20, y: 4, width: 180, height: 11)
slider.minValue = 0.5
transparencyView.addSubview(slider)
slider.translatesAutoresizingMaskIntoConstraints = false
slider.leadingAnchor.constraint(equalTo: transparencyView.leadingAnchor, constant: 24).isActive = true
slider.trailingAnchor.constraint(equalTo: transparencyView.trailingAnchor, constant: -9).isActive = true
slider.centerYAnchor.constraint(equalTo: transparencyView.centerYAnchor).isActive = true
transparencyItem.view = transparencyView
menu.addItem(transparencyItem)
menu.addItem(NSMenuItem.separator())
menu.addItem(NSMenuItem("Capture Screenshot", keyEquivalent: "6", keyModifiers: [.shift, .command]) { _ in
self.captureScreenshot()
})
menu.addItem(NSMenuItem.separator())
menu.addItem(NSMenuItem("Show on All Desktops").bindState(to: .showOnAllDesktops))
menu.addItem(NSMenuItem("Hide and Show Automatically").bindState(to: .dockBehavior))
menu.addItem(NSMenuItem("Launch at Login", isChecked: LaunchAtLogin.isEnabled) { item in
item.isChecked.toggle()
LaunchAtLogin.isEnabled = item.isChecked
})
menu.addItem(NSMenuItem.separator())
menu.addItem(NSMenuItem("Quit Touch Bar Simulator", keyEquivalent: "q") { _ in
NSApp.terminate(nil)
})
}
private func statusItemShouldShowMenu() -> Bool {
!NSApp.isLeftMouseDown || NSApp.isOptionKeyDown
}
func menuNeedsUpdate(_ menu: NSMenu) {
update(menu: menu)
}
func menuWillOpen(_ menu: NSMenu) {
if !statusItemShouldShowMenu() {
statusItemButtonClicked()
}
}
private func statusItemButtonClicked() {
// When the user explicitly wants the Touch Bar to appear then `dockBahavior` should be disabled.
// This is also how the macOS Dock behaves.
Defaults[.dockBehavior] = false
toggleView()
if window.isVisible {
window.orderFront(nil)
}
}
}