Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Commit 12780b2

Browse files
authoredJan 26, 2021
fix blank title and escape key when editing (#28)
1 parent 679a81d commit 12780b2

File tree

6 files changed

+36
-8
lines changed

6 files changed

+36
-8
lines changed
 

‎DuckDuckGo/BrowserTab/ViewModel/WebViewStateObserver.swift

+14-2
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,27 @@ class WebViewStateObserver: NSObject {
8686
}
8787

8888
switch keyPath {
89-
case #keyPath(WKWebView.url): tabViewModel.tab.url = webView.url
89+
case #keyPath(WKWebView.url):
90+
tabViewModel.tab.url = webView.url
91+
updateTitle() // The title might not change if webView doesn't think anything is different so update title here as well
92+
9093
case #keyPath(WKWebView.canGoBack): tabViewModel.canGoBack = webView.canGoBack
9194
case #keyPath(WKWebView.canGoForward): tabViewModel.canGoForward = webView.canGoForward
9295
case #keyPath(WKWebView.isLoading): tabViewModel.isLoading = webView.isLoading
93-
case #keyPath(WKWebView.title): tabViewModel.tab.title = webView.title
96+
case #keyPath(WKWebView.title): updateTitle()
9497
default:
9598
os_log("%s: keyPath %s not handled", type: .error, className, keyPath)
9699
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
97100
}
98101
}
99102

103+
private func updateTitle() {
104+
if webView?.title?.trimmingWhitespaces().isEmpty ?? true {
105+
tabViewModel?.tab.title = webView?.url?.host?.drop(prefix: "www.")
106+
return
107+
}
108+
109+
tabViewModel?.tab.title = webView?.title
110+
}
111+
100112
}

‎DuckDuckGo/FindInPage/FindInPageViewController.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ class FindInPageViewController: NSViewController {
128128
extension FindInPageViewController {
129129

130130
@objc func textFieldFirstReponderNotification(_ notification: Notification) {
131-
// NSTextField passes its first responder status down to a child view of NSTextView class
132-
if let textView = notification.object as? NSTextView, textView.superview?.superview === textField {
131+
if view.window?.firstResponder == textField.currentEditor() {
133132
updateView(firstResponder: true)
134133
} else {
135134
updateView(firstResponder: false)

‎DuckDuckGo/Main/MainViewController.swift

+14
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,24 @@ extension MainViewController {
208208

209209
if Int(event.keyCode) == kVK_Escape {
210210
findInPageViewController?.findInPageDone(self)
211+
checkForEndAddressBarEditing()
211212
return true
212213
}
213214

214215
return false
215216
}
216217

218+
private func checkForEndAddressBarEditing() {
219+
let addressBarTextField = navigationBarViewController?.addressBarViewController?.addressBarTextField
220+
guard view.window?.firstResponder == addressBarTextField?.currentEditor() else { return }
221+
222+
// If the webview doesn't have content it doesn't handle becoming the first responder properly
223+
if tabCollectionViewModel.selectedTabViewModel?.tab.webView.url != nil {
224+
tabCollectionViewModel.selectedTabViewModel?.tab.webView.makeMeFirstResponder()
225+
} else {
226+
navigationBarContainerView.makeMeFirstResponder()
227+
}
228+
229+
}
230+
217231
}

‎DuckDuckGo/Main/View/MainWindow.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ class MainWindow: NSWindow {
3232

3333
override func makeFirstResponder(_ responder: NSResponder?) -> Bool {
3434
// The only reliable way to detect NSTextField is the first responder
35-
postFirstResponderNotification(with: responder)
35+
defer {
36+
// Send it after the first responder has been set on the super class so that window.firstResponder matches correctly
37+
postFirstResponderNotification(with: responder)
38+
}
39+
3640
return super.makeFirstResponder(responder)
3741
}
3842

‎DuckDuckGo/Menus/MainMenuActions.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extension MainViewController {
5454
os_log("MainViewController: Cannot reference address bar text field", type: .error)
5555
return
5656
}
57-
view.window?.makeFirstResponder(addressBarTextField)
57+
addressBarTextField.makeMeFirstResponder()
5858
}
5959

6060
@IBAction func closeTab(_ sender: Any?) {

‎DuckDuckGo/NavigationBar/View/AddressBarViewController.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,7 @@ class AddressBarViewController: NSViewController {
207207
extension AddressBarViewController {
208208

209209
@objc func textFieldFirstReponderNotification(_ notification: Notification) {
210-
// NSTextField passes its first responder status down to a child view of NSTextView class
211-
if let textView = notification.object as? NSTextView, textView.superview?.superview === addressBarTextField {
210+
if view.window?.firstResponder == addressBarTextField.currentEditor() {
212211
updateView(firstResponder: true)
213212
} else {
214213
if mode != .browsing {

0 commit comments

Comments
 (0)
This repository has been archived.