forked from quoid/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppDelegate.swift
More file actions
97 lines (85 loc) · 3.66 KB
/
Copy pathAppDelegate.swift
File metadata and controls
97 lines (85 loc) · 3.66 KB
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
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var window: NSWindow!
private var windowForego = false
private var windowLoaded = false
private let logger = USLogger(#fileID)
@IBOutlet weak var enbaleNativeLogger: NSMenuItem!
@objc func handleGetURL(event: NSAppleEventDescriptor, replyEvent: NSAppleEventDescriptor) {
// issue: https://developer.apple.com/forums/thread/697217
// sendExtensionMessage(name: "URL_SCHEME_STARTED")
// if open panel is already open, stop processing the URL scheme
if NSApplication.shared.keyWindow?.accessibilityIdentifier() == "open-panel" { return }
// handle URL scheme
if let urlString = event.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))?.stringValue,
let url = URL(string: urlString) {
logger?.info("\(#function, privacy: .public) - \(urlString, privacy: .public)")
if url.host == "changesavelocation" {
// avoid opening the panel repeatedly and playing unnecessary warning sounds
if NSApplication.shared.keyWindow?.identifier?.rawValue == "changeSaveLocation" { return }
if windowLoaded {
let viewController = window.contentViewController as? ViewController
viewController?.changeSaveLocation()
} else {
windowForego = true
schemeChangeSaveLocation()
}
}
}
}
// https://developer.apple.com/documentation/appkit/nsapplicationdelegate/1428623-applicationwillfinishlaunching/
func applicationWillFinishLaunching(_ notification: Notification) {
// https://developer.apple.com/documentation/foundation/nsappleeventmanager/1416131-seteventhandler
NSAppleEventManager.shared().setEventHandler(
self,
andSelector: #selector(handleGetURL(event:replyEvent:)),
forEventClass: AEEventClass(kInternetEventClass),
andEventID: AEEventID(kAEGetURL)
)
}
// https://developer.apple.com/documentation/appkit/nsapplicationdelegate/1428385-applicationdidfinishlaunching
func applicationDidFinishLaunching(_ notification: Notification) {
// Initialize menu items
enbaleNativeLogger.state = Preferences.enableLogger ? .on : .off
// Whether to initialize the main window
logger?.debug("\(#function, privacy: .public) - windowForego: \(self.windowForego, privacy: .public)")
if windowForego { return }
// https://developer.apple.com/documentation/appkit/nswindow/
window = NSWindow()
window.styleMask = [.titled, .closable, .miniaturizable]
// window.styleMask = [.titled, .closable, .resizable, .miniaturizable] // DEBUG
window.titlebarAppearsTransparent = true
// Initialize webview
window.contentViewController = ViewController()
// https://developer.apple.com/documentation/uikit/appearance_customization/supporting_dark_mode_in_your_interface#2993897
window.backgroundColor = NSColor(named: NSColor.Name("USBackgroundColor"))
// window.backgroundColor = .clear // DEBUG
window.center()
window.setIsVisible(true)
window.makeMain()
window.makeKeyAndOrderFront(nil)
// window.level = NSWindow.Level.floating // DEBUG
windowLoaded = true
}
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
return true
}
func applicationWillTerminate(_ notification: Notification) {
// Insert code here to tear down your application
USLogStoreToFile()
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
@IBAction func enableLogger(_ sender: NSMenuItem) {
let enable = sender.state == .on ? false : true
sender.state = enable ? .on : .off
USLoggerSwitch(enable)
}
@IBAction func applicationHelp(_ sender: NSMenuItem) {
if let url = URL(string: "https://github.com/quoid/userscripts") {
NSWorkspace.shared.open(url)
}
}
}