Skip to content

Commit 7033aed

Browse files
committed
Added example hook for -[NSWindow miniaturize:]
1 parent e296bf7 commit 7033aed

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Example/InterposeKitExample/Sources/AppDelegate.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ class AppDelegate: NSObject, NSApplicationDelegate {
7575
hook.original(self, hook.selector, "## \(title.uppercased()) ##")
7676
}
7777
}
78+
case .NSWindow_miniaturize:
79+
return try Interpose.prepareHook(
80+
on: self.window,
81+
for: #selector(NSWindow.miniaturize(_:)),
82+
methodSignature: (@convention(c) (NSWindow, Selector, Any?) -> Void).self,
83+
hookSignature: (@convention(block) (NSWindow, Any?) -> Void).self
84+
) { hook in
85+
return { `self`, sender in
86+
let alert = NSAlert()
87+
alert.messageText = "Miniaturization Intercepted"
88+
alert.informativeText = "This window refused to minimize because a hook was applied to\n -[NSWindow miniaturize:]."
89+
alert.runModal()
90+
}
91+
}
7892
case .NSApplication_sendEvent:
7993
return try Interpose.prepareHook(
8094
on: NSApplication.shared,

Example/InterposeKitExample/Sources/HookExample.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
enum HookExample: CaseIterable {
22
case NSWindow_setTitle
3+
case NSWindow_miniaturize
34
case NSApplication_sendEvent
45
case NSMenuItem_title
56
case NSColor_labelColor
@@ -10,6 +11,8 @@ extension HookExample {
1011
switch self {
1112
case .NSWindow_setTitle:
1213
return "-[NSWindow setTitle:]"
14+
case .NSWindow_miniaturize:
15+
return "-[NSWindow miniaturize:]"
1316
case .NSApplication_sendEvent:
1417
return "-[NSApplication sendEvent:]"
1518
case .NSMenuItem_title:
@@ -26,6 +29,11 @@ extension HookExample {
2629
An object hook on the main NSWindow that uppercases the title and wraps it with \
2730
decorative markers whenever it’s set. This can be tested using the text field below.
2831
"""
32+
case .NSWindow_miniaturize:
33+
return """
34+
An object hook on the main NSWindow that intercepts miniaturization and shows \
35+
an alert instead of minimizing the window.
36+
"""
2937
case .NSApplication_sendEvent:
3038
return """
3139
A class hook on NSApplication that logs all events passed through sendEvent(_:).

0 commit comments

Comments
 (0)