/
AppDelegate.swift
89 lines (71 loc) 路 2.76 KB
/
AppDelegate.swift
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
//
// AppDelegate.swift
// siro
//
// Created by nainoya on 2019/11/27.
// Copyright 漏 2019 ainoya.tokyo. All rights reserved.
//
import Cocoa
import HotKey
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.squareLength)
static let popover = NSPopover()
let hotKey = HotKey(key: .m, modifiers: [.control, .shift])
static var slackService: SlackService? = SlackService.restore()
@IBOutlet weak var menu: NSMenu!
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
if let button = statusItem.button {
// button.title = "馃悾"
button.image = NSImage(named:NSImage.Name("StatusBarImage"))
button.action = #selector(togglePopover(_:))
button.sendAction(on: [.leftMouseUp, .rightMouseUp])
}
AppDelegate.popover.contentViewController = ViewController.freshController()
AppDelegate.popover.animates = false
hotKey.keyDownHandler = {
self.showPopover(sender: nil)
}
}
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
let _ = showSiroSettings(sender)
return true
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
@objc func togglePopover(_ sender: Any?) {
print("event \(NSApp.currentEvent!.type)")
switch NSApp.currentEvent!.type {
case .rightMouseUp:
statusItem.menu = menu
statusItem.button?.performClick(nil)
statusItem.menu = nil
default:
if AppDelegate.popover.isShown {
closePopover(sender: sender)
} else {
showPopover(sender: sender)
}
}
}
func showPopover(sender: Any?) {
if let button = statusItem.button {
AppDelegate.popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY )
}
}
func closePopover(sender: Any?) {
AppDelegate.popover.performClose(sender)
}
var windowController : NSWindowController!
@IBAction func quit(_ sender: Any) {
NSApplication.shared.terminate(sender)
}
@IBAction func showSiroSettings(_ sender: Any) {
let mainStoryBoard = NSStoryboard(name: "Main", bundle: nil)
windowController = mainStoryBoard.instantiateController(withIdentifier: "Settings") as? NSWindowController
// make initial settings before showing the window
windowController.showWindow(self)
}
}