Skip to content

Commit f4163b3

Browse files
committed
Use NotificationCenter to trigger find action
1 parent 35818bd commit f4163b3

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

devdocs-macos/AppDelegate.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import AppKit
22

3+
public extension Notification.Name {
4+
static let MenuFindAction = Notification.Name(
5+
rawValue: "AppDelegateMenuFindActionNotification")
6+
}
7+
38
@NSApplicationMain
49
class AppDelegate: NSObject, NSApplicationDelegate {
510
func applicationWillFinishLaunching(_ notification: Notification) {
@@ -25,10 +30,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
2530
}
2631

2732
@IBAction func performFindAction(_ sender: Any) {
28-
guard let window = NSApp.mainWindow else { return }
29-
let dwc = window.windowController.map { wc in
30-
wc as! DocumentationWindowController
31-
}
32-
dwc?.activateFind()
33+
NotificationCenter.default.post(name: .MenuFindAction, object: nil)
3334
}
3435
}

devdocs-macos/DocumentationWindowController.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ class DocumentationWindowController: NSWindowController {
4040
selector: #selector(observeDocumentURL),
4141
name: .DocumentURLDidChange,
4242
object: dvc)
43-
}
4443

45-
func activateFind() {
46-
guard let dvc = documentationViewController else { return }
47-
dvc.showSearchControl()
44+
NotificationCenter.default.addObserver(self,
45+
selector: #selector(observeMenuFindAction),
46+
name: .MenuFindAction,
47+
object: nil)
4848
}
4949

50+
// MARK:- NotificationCenter observers
51+
5052
@objc private func observeViewerState() {
5153
guard let dvc = documentationViewController else { return }
5254

@@ -77,6 +79,18 @@ class DocumentationWindowController: NSWindowController {
7779
self.documentation.url = dvc.documentURL
7880
}
7981

82+
@objc private func observeMenuFindAction() {
83+
guard let window = self.window else { return }
84+
if !window.isKeyWindow {
85+
return
86+
}
87+
88+
guard let dvc = documentationViewController else { return }
89+
dvc.showSearchControl()
90+
}
91+
92+
// MARK:- KVO observers
93+
8094
private func observeEffectiveAppearance() {
8195
guard let window = self.window else { return }
8296
observations.insert(

0 commit comments

Comments
 (0)