-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAppDelegate.swift
More file actions
159 lines (141 loc) · 5.93 KB
/
AppDelegate.swift
File metadata and controls
159 lines (141 loc) · 5.93 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import AppKit
import InterposeKit
import SwiftUI
@main
class AppDelegate: NSObject, NSApplicationDelegate {
// ============================================================================ //
// MARK: Window & Content View
// ============================================================================ //
private lazy var window: NSWindow = {
let window = NSWindow(
contentRect: .zero,
styleMask: [.titled, .closable, .miniaturizable],
backing: .buffered,
defer: false
)
let hostingController = NSHostingController(rootView: self.contentView)
window.contentViewController = hostingController
return window
}()
private lazy var contentView: ContentView = {
ContentView(
onHookToggled: { [weak self] example, isEnabled in
guard let self else { return }
let hook = self.hook(for: example)
do {
if isEnabled {
try hook.apply()
} else {
try hook.revert()
}
} catch {
fatalError("\(error)")
}
},
onWindowTitleChanged: { [weak self] title in
self?.window.title = title
}
)
}()
// ============================================================================ //
// MARK: Hooks
// ============================================================================ //
private func hook(
for example: HookExample
) -> Hook {
if let hook = self._hooks[example] { return hook }
let hook = self._makeHook(for: example)
self._hooks[example] = hook
return hook
}
private func _makeHook(
for example: HookExample
) -> Hook {
do {
switch example {
case .NSWindow_setTitle:
return try Interpose.prepareHook(
on: self.window,
for: #selector(setter: NSWindow.title),
methodSignature: (@convention(c) (NSWindow, Selector, String) -> Void).self,
hookSignature: (@convention(block) (NSWindow, String) -> Void).self
) { hook in
return { `self`, title in
hook.original(self, hook.selector, "## \(title.uppercased()) ##")
}
}
case .NSWindow_miniaturize:
return try Interpose.prepareHook(
on: self.window,
for: #selector(NSWindow.miniaturize(_:)),
methodSignature: (@convention(c) (NSWindow, Selector, Any?) -> Void).self,
hookSignature: (@convention(block) (NSWindow, Any?) -> Void).self
) { hook in
return { `self`, sender in
let alert = NSAlert()
alert.messageText = "Miniaturization Intercepted"
alert.informativeText = "This window refused to minimize because a hook was applied to\n -[NSWindow miniaturize:]."
alert.runModal()
}
}
case .NSApplication_sendEvent:
return try Interpose.prepareHook(
on: NSApplication.shared,
for: #selector(NSApplication.sendEvent(_:)),
methodSignature: (@convention(c) (NSApplication, Selector, NSEvent) -> Void).self,
hookSignature: (@convention(block) (NSApplication, NSEvent) -> Void).self
) { hook in
return { `self`, event in
print("NSApplication.sendEvent(_:) \(event)")
hook.original(self, hook.selector, event)
}
}
case .NSMenuItem_title:
return try Interpose.prepareHook(
on: NSMenuItem.self,
for: #selector(getter: NSMenuItem.title),
methodSignature: (@convention(c) (NSMenuItem, Selector) -> String).self,
hookSignature: (@convention(block) (NSMenuItem) -> String).self
) { hook in
return { `self` in
let title = hook.original(`self`, hook.selector)
return "## \(title) ##"
}
}
case .NSColor_labelColor:
return try Interpose.prepareHook(
on: NSColor.self,
for: #selector(getter: NSColor.labelColor),
methodKind: .class,
methodSignature: (@convention(c) (NSColor.Type, Selector) -> NSColor).self,
hookSignature: (@convention(block) (NSColor.Type) -> NSColor).self
) { hook in
return { `self` in
return self.systemPink
}
}
}
} catch {
fatalError("\(error)")
}
}
private var _hooks = [HookExample: Hook]()
// ============================================================================ //
// MARK: NSApplicationDelegate
// ============================================================================ //
func applicationWillFinishLaunching(_ notification: Notification) {
Interpose.isLoggingEnabled = true
}
func applicationDidFinishLaunching(_ notification: Notification) {
self.window.contentView?.layoutSubtreeIfNeeded()
Task { @MainActor in
self.window.center()
self.window.makeKeyAndOrderFront(nil)
}
}
func applicationShouldTerminateAfterLastWindowClosed(
_ sender: NSApplication
) -> Bool {
return true
}
}