Skip to content

Commit a74fc42

Browse files
committed
Merge branch 'refactoring'
2 parents 0b4177d + fe70f00 commit a74fc42

17 files changed

+1963
-361
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/Carthage
2+
/node_modules

DevDocs.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
0680E1D12317576100CD5BB4 /* WeakWKScriptMessageHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0680E1D02317576100CD5BB4 /* WeakWKScriptMessageHandler.swift */; };
1011
06996D2D223DDBEB001507AD /* Summoner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06996D2C223DDBEB001507AD /* Summoner.swift */; };
1112
06996D36223DEDA3001507AD /* MASShortcut.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 06996D35223DEDA3001507AD /* MASShortcut.framework */; };
1213
06996D37223DEDA3001507AD /* MASShortcut.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 06996D35223DEDA3001507AD /* MASShortcut.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
@@ -59,6 +60,7 @@
5960
/* End PBXCopyFilesBuildPhase section */
6061

6162
/* Begin PBXFileReference section */
63+
0680E1D02317576100CD5BB4 /* WeakWKScriptMessageHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeakWKScriptMessageHandler.swift; sourceTree = "<group>"; };
6264
06996D2C223DDBEB001507AD /* Summoner.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Summoner.swift; sourceTree = "<group>"; };
6365
06996D35223DEDA3001507AD /* MASShortcut.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MASShortcut.framework; path = Carthage/Build/Mac/MASShortcut.framework; sourceTree = "<group>"; };
6466
06996D38223DEDC8001507AD /* MASShortcut.framework.dSYM */ = {isa = PBXFileReference; lastKnownFileType = wrapper.dsym; name = MASShortcut.framework.dSYM; path = Carthage/Build/Mac/MASShortcut.framework.dSYM; sourceTree = "<group>"; };
@@ -147,6 +149,7 @@
147149
06B5A73A21D36680007823B2 /* Storage.swift */,
148150
06996D2C223DDBEB001507AD /* Summoner.swift */,
149151
06CAA896223E6ACB00B296AC /* URLEventHandler.swift */,
152+
0680E1D02317576100CD5BB4 /* WeakWKScriptMessageHandler.swift */,
150153
06B5A71921D2013D007823B2 /* user-scripts */,
151154
);
152155
path = "devdocs-macos";
@@ -238,6 +241,7 @@
238241
06B5A73321D35552007823B2 /* Documentation.swift in Sources */,
239242
06EAE7FD21D0CE3E00F7C921 /* DocumentationWindowController.swift in Sources */,
240243
06EAE7ED21D0CB3D00F7C921 /* AppDelegate.swift in Sources */,
244+
0680E1D12317576100CD5BB4 /* WeakWKScriptMessageHandler.swift in Sources */,
241245
06B5A73B21D36680007823B2 /* Storage.swift in Sources */,
242246
06CAA897223E6ACB00B296AC /* URLEventHandler.swift in Sources */,
243247
);

devdocs-macos/AppDelegate.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {
1616
DocumentationWindows.shared.persist()
1717
}
1818

19-
@IBAction func newTab(sender: Any) {
19+
@IBAction func newTab(_ sender: Any) {
2020
DocumentationWindows.shared.newWindow()
2121
}
2222

23-
@IBAction func showAppPreferences(sender: Any) {
23+
@IBAction func showAppPreferences(_ sender: Any) {
2424
PreferencesWindowController.shared.showWindow(self)
2525
}
2626

devdocs-macos/Base.lproj/MainMenu.xib

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<menuItem title="Preferences…" id="BOF-NM-1cW">
3030
<modifierMask key="keyEquivalentModifierMask"/>
3131
<connections>
32-
<action selector="showAppPreferencesWithSender:" target="Voe-Tx-rLC" id="LDP-dv-TTw"/>
32+
<action selector="showAppPreferences:" target="Voe-Tx-rLC" id="WEE-am-D7X"/>
3333
</connections>
3434
</menuItem>
3535
<menuItem isSeparatorItem="YES" id="wFC-TO-SCJ"/>
@@ -70,7 +70,7 @@
7070
<items>
7171
<menuItem title="New Tab" keyEquivalent="t" id="Was-JA-tGl">
7272
<connections>
73-
<action selector="newTabWithSender:" target="Voe-Tx-rLC" id="Swf-eT-1ST"/>
73+
<action selector="newTab:" target="Voe-Tx-rLC" id="aJ7-hj-He2"/>
7474
</connections>
7575
</menuItem>
7676
<menuItem isSeparatorItem="YES" id="m54-Is-iLE"/>

devdocs-macos/DocumentationViewController.swift

Lines changed: 68 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import WebKit
33

44
class DocumentationViewController:
55
NSViewController,
6-
WKNavigationDelegate,
7-
WKUIDelegate,
8-
WKScriptMessageHandler
6+
WKNavigationDelegate
97
{
108

119
@objc enum ViewerState: Int {
@@ -29,13 +27,20 @@ class DocumentationViewController:
2927
loadWebsite()
3028
}
3129

30+
override func viewDidDisappear() {
31+
let ucc = webView.configuration.userContentController
32+
ucc.removeScriptMessageHandler(forName: "vcBus")
33+
ucc.removeAllUserScripts()
34+
super.viewDidDisappear()
35+
}
36+
3237
private func setupWebView() {
3338
let config = WKWebViewConfiguration()
3439

3540
let userContentController = WKUserContentController()
3641
config.userContentController = userContentController;
3742

38-
userContentController.add(self, name: "vcBus");
43+
userContentController.add(WeakWKScriptMessageHandler.init(self), name: "vcBus");
3944

4045
if let integrationScript = readUserScript("integration") {
4146
let integration = WKUserScript(source: integrationScript, injectionTime: .atDocumentStart, forMainFrameOnly: true)
@@ -95,6 +100,8 @@ class DocumentationViewController:
95100
webView.load(request)
96101
}
97102

103+
// MARK:- Page search
104+
98105
func showSearchControl() {
99106
if viewerState != .ready {
100107
return
@@ -108,60 +115,6 @@ class DocumentationViewController:
108115
vc.dismissSearch(self)
109116
}
110117

111-
// MARK:- WKUIDelegate
112-
113-
func webView(_ webView: WKWebView,
114-
createWebViewWith configuration: WKWebViewConfiguration,
115-
for navigationAction: WKNavigationAction,
116-
windowFeatures: WKWindowFeatures) -> WKWebView? {
117-
guard let requestURL = navigationAction.request.url else {
118-
return nil
119-
}
120-
121-
if let host = requestURL.host, host == "devdocs.io" {
122-
DocumentationWindows.shared.newWindowFor(url: requestURL)
123-
return nil
124-
}
125-
126-
if let scheme = requestURL.scheme {
127-
switch scheme {
128-
case "http", "https", "mailto":
129-
NSWorkspace.shared.open(requestURL)
130-
default:
131-
break;
132-
}
133-
}
134-
135-
return nil
136-
}
137-
138-
// MARK:- WKScriptMessageHandler
139-
140-
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
141-
guard let msg = message.body as? [AnyHashable: Any] else {
142-
return
143-
}
144-
guard let type = msg["type"] as? String else {
145-
return
146-
}
147-
switch type {
148-
case "afterInit":
149-
handleAfterInit()
150-
case "titleNotification":
151-
guard let args = msg["args"] as? [AnyHashable: Any] else {
152-
return
153-
}
154-
handleTitleNotification(args)
155-
case "locationNotification":
156-
guard let args = msg["args"] as? [AnyHashable: Any] else {
157-
return
158-
}
159-
handleLocationNotification(args)
160-
default:
161-
return
162-
}
163-
}
164-
165118
// MARK:- JS integration
166119

167120
func useDarkMode(_ using: Bool) {
@@ -216,6 +169,63 @@ class DocumentationViewController:
216169
}
217170
}
218171

172+
// MARK:- WKUIDelegate
173+
extension DocumentationViewController: WKUIDelegate {
174+
func webView(_ webView: WKWebView,
175+
createWebViewWith configuration: WKWebViewConfiguration,
176+
for navigationAction: WKNavigationAction,
177+
windowFeatures: WKWindowFeatures) -> WKWebView? {
178+
guard let requestURL = navigationAction.request.url else {
179+
return nil
180+
}
181+
182+
if let host = requestURL.host, host == "devdocs.io" {
183+
DocumentationWindows.shared.newWindow(forURL: requestURL)
184+
return nil
185+
}
186+
187+
if let scheme = requestURL.scheme {
188+
switch scheme {
189+
case "http", "https", "mailto":
190+
NSWorkspace.shared.open(requestURL)
191+
default:
192+
break;
193+
}
194+
}
195+
196+
return nil
197+
}
198+
}
199+
200+
// MARK:- WKScriptMessageHandler
201+
extension DocumentationViewController: WKScriptMessageHandler {
202+
func userContentController(_ userContentController: WKUserContentController,
203+
didReceive message: WKScriptMessage) {
204+
guard let msg = message.body as? [AnyHashable: Any] else {
205+
return
206+
}
207+
guard let type = msg["type"] as? String else {
208+
return
209+
}
210+
switch type {
211+
case "afterInit":
212+
handleAfterInit()
213+
case "titleNotification":
214+
guard let args = msg["args"] as? [AnyHashable: Any] else {
215+
return
216+
}
217+
handleTitleNotification(args)
218+
case "locationNotification":
219+
guard let args = msg["args"] as? [AnyHashable: Any] else {
220+
return
221+
}
222+
handleLocationNotification(args)
223+
default:
224+
return
225+
}
226+
}
227+
}
228+
219229
// MARK:- SearchControlDelegate
220230
extension DocumentationViewController: SearchControlDelegate {
221231
func search(term: String) {

devdocs-macos/DocumentationWindowController.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,4 @@ class DocumentationWindowController: NSWindowController {
9292
}
9393
)
9494
}
95-
9695
}

devdocs-macos/DocumentationWindows.swift

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Cocoa
22

3-
class DocumentationWindows: NSObject, NSWindowDelegate {
3+
class DocumentationWindows: NSObject {
44
private var windowControllers: Set<DocumentationWindowController>
55

66
static let shared = DocumentationWindows()
@@ -10,11 +10,11 @@ class DocumentationWindows: NSObject, NSWindowDelegate {
1010
}
1111

1212
func newWindow() {
13-
newWindowFor(documentation: Documentation.init())
13+
newWindow(forDoc: Documentation.init())
1414
}
1515

16-
func newWindowFor(url: URL) {
17-
newWindowFor(documentation: Documentation.init(withURL: url))
16+
func newWindow(forURL url: URL) {
17+
newWindow(forDoc: Documentation.init(withURL: url))
1818
}
1919

2020
func newWindowIfNoWindow() {
@@ -23,15 +23,7 @@ class DocumentationWindows: NSObject, NSWindowDelegate {
2323
}
2424
}
2525

26-
func windowWillClose(_ notification: Notification) {
27-
guard let window = notification.object as! NSWindow? else { return }
28-
guard let dwc = window.windowController as! DocumentationWindowController? else {
29-
return
30-
}
31-
windowControllers.remove(dwc)
32-
}
33-
34-
private func newWindowFor(documentation: Documentation) {
26+
private func newWindow(forDoc documentation: Documentation) {
3527
let dwc = DocumentationWindowController.init(window: nil)
3628
dwc.documentation = documentation
3729
dwc.window?.delegate = self
@@ -60,11 +52,21 @@ class DocumentationWindows: NSObject, NSWindowDelegate {
6052
return false
6153
}
6254
}.forEach { url in
63-
newWindowFor(url: url)
55+
newWindow(forURL: url)
6456
}
6557
} else {
6658
newWindow()
6759
}
6860
}
61+
}
6962

63+
// MARK:- NSWindowDelegate
64+
extension DocumentationWindows: NSWindowDelegate {
65+
func windowWillClose(_ notification: Notification) {
66+
guard let window = notification.object as! NSWindow? else { return }
67+
guard let dwc = window.windowController as! DocumentationWindowController? else {
68+
return
69+
}
70+
windowControllers.remove(dwc)
71+
}
7072
}

devdocs-macos/PreferencesWindowController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class PreferencesWindowController: NSWindowController {
1919
}
2020
}
2121

22-
@IBAction func restoreDefaults(sender: Any) {
22+
@IBAction func restoreDefaults(_ sender: Any) {
2323
if let recorderView = masShortcutRecorderView {
2424
recorderView.shortcutValue = Summoner.defaultShortcut
2525
}

devdocs-macos/PreferencesWindowController.xib

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14460.31"/>
4+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14490.70"/>
55
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
66
</dependencies>
77
<objects>
@@ -17,7 +17,7 @@
1717
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES"/>
1818
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
1919
<rect key="contentRect" x="196" y="240" width="318" height="102"/>
20-
<rect key="screenRect" x="0.0" y="0.0" width="1440" height="877"/>
20+
<rect key="screenRect" x="0.0" y="0.0" width="1280" height="777"/>
2121
<view key="contentView" wantsLayer="YES" id="se5-gp-TjO">
2222
<rect key="frame" x="0.0" y="0.0" width="318" height="102"/>
2323
<autoresizingMask key="autoresizingMask"/>
@@ -34,7 +34,7 @@
3434
<font key="font" metaFont="system"/>
3535
</buttonCell>
3636
<connections>
37-
<action selector="restoreDefaultsWithSender:" target="-2" id="XBR-gg-hEJ"/>
37+
<action selector="restoreDefaults:" target="-2" id="bad-yA-Cme"/>
3838
</connections>
3939
</button>
4040
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="SLs-UG-n2b">

devdocs-macos/URLEventHandler.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ class URLEventHandler {
4141
}
4242

4343
NSApp.activate(ignoringOtherApps: true)
44-
DocumentationWindows.shared.newWindowFor(url: searchURL)
44+
DocumentationWindows.shared.newWindow(forURL: searchURL)
4545
case "newWindow":
4646
NSApp.activate(ignoringOtherApps: true)
4747
DocumentationWindows.shared.newWindow()
4848
case "devdocs.io":
4949
NSApp.activate(ignoringOtherApps: true)
50-
DocumentationWindows.shared.newWindowFor(url: url!)
50+
DocumentationWindows.shared.newWindow(forURL: url!)
5151
default:
5252
return
5353
}

0 commit comments

Comments
 (0)