Skip to content

Commit 38b98a7

Browse files
authored
Merge pull request #35 from dteoh/window-subtitle
Split document title display
2 parents b58602a + 9be31ff commit 38b98a7

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

devdocs-macos/DocumentationViewController.swift

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import WebKit
44
public extension Notification.Name {
55
static let DocumentTitleDidChange = Notification.Name(
66
rawValue: "DocumentationViewControllerDocumentTitleDidChangeNotification")
7+
static let DocumentCategoryDidChange = Notification.Name(
8+
rawValue: "DocumentationViewControllerDocumentCategoryDidChangeNotification")
79
static let DocumentURLDidChange = Notification.Name(
810
rawValue: "DocumentationViewControllerDocumentURLDidChangeNotification")
911
static let DocumentViewerStateDidChange = Notification.Name(
@@ -26,6 +28,11 @@ class DocumentationViewController: NSViewController {
2628
NotificationCenter.default.post(name: .DocumentTitleDidChange, object: self)
2729
}
2830
}
31+
private(set) var documentCategory: String? {
32+
didSet {
33+
NotificationCenter.default.post(name: .DocumentCategoryDidChange, object: self)
34+
}
35+
}
2936
var documentURL: URL? {
3037
didSet {
3138
NotificationCenter.default.post(name: .DocumentURLDidChange, object: self)
@@ -149,15 +156,18 @@ class DocumentationViewController: NSViewController {
149156
}
150157

151158
private func handleTitleNotification(_ args: [AnyHashable: Any]) {
152-
guard let title = args["title"] as! String? else {
153-
return
154-
}
155-
let suffix = " — DevDocs"
156-
if title.hasSuffix(suffix) {
157-
self.documentTitle = title.replacingOccurrences(of: suffix, with: "")
158-
} else {
159-
self.documentTitle = title
159+
if let topic = args["topic"] as! String? {
160+
self.documentTitle = topic
161+
} else if let title = args["title"] as! String? {
162+
let suffix = " — DevDocs"
163+
if title.hasSuffix(suffix) {
164+
self.documentTitle = title.replacingOccurrences(of: suffix, with: "")
165+
} else {
166+
self.documentTitle = title
167+
}
160168
}
169+
170+
self.documentCategory = args["doc"] as! String?
161171
}
162172

163173
private func handleLocationNotification(_ args: [AnyHashable: Any]) {

devdocs-macos/DocumentationWindowController.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class DocumentationWindowController: NSWindowController {
3636
name: .DocumentTitleDidChange,
3737
object: dvc)
3838

39+
NotificationCenter.default.addObserver(self,
40+
selector: #selector(observeDocumentCategory),
41+
name: .DocumentCategoryDidChange,
42+
object: dvc)
43+
3944
NotificationCenter.default.addObserver(self,
4045
selector: #selector(observeDocumentURL),
4146
name: .DocumentURLDidChange,
@@ -65,6 +70,13 @@ class DocumentationWindowController: NSWindowController {
6570
self.window?.title = dvc.documentTitle ?? "DevDocs"
6671
}
6772

73+
@objc private func observeDocumentCategory() {
74+
guard let dvc = documentationViewController else { return }
75+
if #available(OSX 11.0, *) {
76+
self.window?.subtitle = dvc.documentCategory ?? ""
77+
}
78+
}
79+
6880
@objc private func observeDocumentURL() {
6981
guard let dvc = documentationViewController else { return }
7082
self.documentation.url = dvc.documentURL

devdocs-macos/user-scripts/page-observer.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
/* global $ */
22
(function () {
3+
const findTitleComponents = function () {
4+
const { entry } = window.app.document.content.view
5+
if (!entry) {
6+
return {}
7+
}
8+
return {
9+
doc: entry.doc.name,
10+
topic: entry.name
11+
}
12+
}
13+
314
const observer = new MutationObserver((mutations) => {
415
window.webkit.messageHandlers.vcBus.postMessage({
516
type: 'titleNotification',
617
args: {
7-
title: $('head title').innerText
18+
title: $('head title').innerText,
19+
...findTitleComponents()
820
}
921
})
1022
window.webkit.messageHandlers.vcBus.postMessage({

0 commit comments

Comments
 (0)