Skip to content

Commit 9239595

Browse files
committed
Merge branch 'refactoring'
2 parents 82a59a3 + f4163b3 commit 9239595

10 files changed

+94
-55
lines changed

devdocs-macos/AppDelegate.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import Cocoa
1+
import AppKit
2+
3+
public extension Notification.Name {
4+
static let MenuFindAction = Notification.Name(
5+
rawValue: "AppDelegateMenuFindActionNotification")
6+
}
27

38
@NSApplicationMain
49
class AppDelegate: NSObject, NSApplicationDelegate {
@@ -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/Documentation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Cocoa
1+
import Foundation
22

33
class Documentation: NSObject {
44
var url: URL!

devdocs-macos/DocumentationViewController.swift

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
import Cocoa
1+
import AppKit
22
import WebKit
33

4+
public extension Notification.Name {
5+
static let DocumentTitleDidChange = Notification.Name(
6+
rawValue: "DocumentationViewControllerDocumentTitleDidChangeNotification")
7+
static let DocumentURLDidChange = Notification.Name(
8+
rawValue: "DocumentationViewControllerDocumentURLDidChangeNotification")
9+
static let DocumentViewerStateDidChange = Notification.Name(
10+
rawValue: "DocumentationViewControllerViewerStateDidChangeNotification")
11+
}
12+
413
class DocumentationViewController:
514
NSViewController,
615
WKNavigationDelegate
716
{
817

9-
@objc enum ViewerState: Int {
18+
enum ViewerState {
1019
case blank
1120
case initializing
1221
case ready
@@ -15,9 +24,21 @@ class DocumentationViewController:
1524
private var webView: WKWebView!
1625
private var searchCVC: SearchControlViewController?
1726

18-
@objc dynamic var documentTitle: String?
19-
@objc dynamic var documentURL: URL?
20-
@objc dynamic var viewerState: ViewerState = .blank
27+
private(set) var documentTitle: String? {
28+
didSet {
29+
NotificationCenter.default.post(name: .DocumentTitleDidChange, object: self)
30+
}
31+
}
32+
var documentURL: URL? {
33+
didSet {
34+
NotificationCenter.default.post(name: .DocumentURLDidChange, object: self)
35+
}
36+
}
37+
private(set) var viewerState: ViewerState = .blank {
38+
didSet {
39+
NotificationCenter.default.post(name: .DocumentViewerStateDidChange, object: self)
40+
}
41+
}
2142

2243
override func viewDidLoad() {
2344
super.viewDidLoad()

devdocs-macos/DocumentationWindowController.swift

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Cocoa
1+
import AppKit
22
import WebKit
33

44
class DocumentationWindowController: NSWindowController {
@@ -22,58 +22,75 @@ class DocumentationWindowController: NSWindowController {
2222
}
2323

2424
override func windowDidLoad() {
25-
observeViewerState()
26-
observeDocumentTitle()
27-
observeDocumentURL()
2825
observeEffectiveAppearance()
29-
}
3026

31-
func activateFind() {
3227
guard let dvc = documentationViewController else { return }
33-
dvc.showSearchControl()
28+
29+
NotificationCenter.default.addObserver(self,
30+
selector: #selector(observeViewerState),
31+
name: .DocumentViewerStateDidChange,
32+
object: dvc)
33+
34+
NotificationCenter.default.addObserver(self,
35+
selector: #selector(observeDocumentTitle),
36+
name: .DocumentTitleDidChange,
37+
object: dvc)
38+
39+
NotificationCenter.default.addObserver(self,
40+
selector: #selector(observeDocumentURL),
41+
name: .DocumentURLDidChange,
42+
object: dvc)
43+
44+
NotificationCenter.default.addObserver(self,
45+
selector: #selector(observeMenuFindAction),
46+
name: .MenuFindAction,
47+
object: nil)
3448
}
3549

36-
private func observeViewerState() {
50+
// MARK:- NotificationCenter observers
51+
52+
@objc private func observeViewerState() {
3753
guard let dvc = documentationViewController else { return }
38-
observations.insert(
39-
dvc.observe(\DocumentationViewController.viewerState) { [weak self] (dvc, _) in
40-
if dvc.viewerState != .ready {
41-
return
42-
}
4354

44-
dvc.useNativeScrollbars(true)
55+
if dvc.viewerState != .ready {
56+
return
57+
}
4558

46-
guard let window = self?.window else { return }
47-
switch window.effectiveAppearance.name {
48-
case .aqua:
49-
dvc.useDarkMode(false)
50-
case .darkAqua:
51-
dvc.useDarkMode(true)
52-
default:
53-
break;
54-
}
55-
}
56-
)
59+
dvc.useNativeScrollbars(true)
60+
61+
guard let window = self.window else { return }
62+
switch window.effectiveAppearance.name {
63+
case .aqua:
64+
dvc.useDarkMode(false)
65+
case .darkAqua:
66+
dvc.useDarkMode(true)
67+
default:
68+
break;
69+
}
5770
}
5871

59-
private func observeDocumentTitle() {
72+
@objc private func observeDocumentTitle() {
6073
guard let dvc = documentationViewController else { return }
61-
observations.insert(
62-
dvc.observe(\DocumentationViewController.documentTitle) { [weak self] (dvc, _) in
63-
self?.window?.title = dvc.documentTitle ?? "DevDocs"
64-
}
65-
)
74+
self.window?.title = dvc.documentTitle ?? "DevDocs"
6675
}
6776

68-
private func observeDocumentURL() {
77+
@objc private func observeDocumentURL() {
6978
guard let dvc = documentationViewController else { return }
70-
observations.insert(
71-
dvc.observe(\DocumentationViewController.documentURL) { [weak self] (dvc, _) in
72-
self?.documentation.url = dvc.documentURL
73-
}
74-
)
79+
self.documentation.url = dvc.documentURL
80+
}
81+
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()
7590
}
7691

92+
// MARK:- KVO observers
93+
7794
private func observeEffectiveAppearance() {
7895
guard let window = self.window else { return }
7996
observations.insert(

devdocs-macos/DocumentationWindows.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Cocoa
1+
import AppKit
22

33
class DocumentationWindows: NSObject {
44
private var windowControllers: Set<DocumentationWindowController>

devdocs-macos/PreferencesWindowController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Cocoa
1+
import AppKit
22
import MASShortcut
33

44
class PreferencesWindowController: NSWindowController {

devdocs-macos/SearchControlViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Cocoa
1+
import AppKit
22

33
class SearchControlViewController: NSViewController {
44
weak var delegate: SearchControlDelegate?

devdocs-macos/Storage.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Cocoa
1+
import Foundation
22
import DefaultsKit
33

44
private extension DefaultsKey {

devdocs-macos/Summoner.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Cocoa
1+
import AppKit
22
import MASShortcut
33

44
class Summoner {

devdocs-macos/URLEventHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Cocoa
1+
import AppKit
22

33
class URLEventHandler {
44

0 commit comments

Comments
 (0)