From e79cafc82e345240bde1c204470d53c2d7e30c2b Mon Sep 17 00:00:00 2001 From: Nate Weaver Date: Wed, 20 May 2020 19:41:23 -0500 Subject: [PATCH 1/6] Make shift key invert the "open in background" preference - Also add an appropriate menu item. --- Mac/Base.lproj/Main.storyboard | 6 ++++++ Mac/Browser.swift | 16 ++++++++++++++-- Mac/MainWindow/MainWindowController.swift | 4 ++++ .../Timeline/TimelineViewController.swift | 4 ++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Mac/Base.lproj/Main.storyboard b/Mac/Base.lproj/Main.storyboard index 64ecd1357..dcccfcf9c 100644 --- a/Mac/Base.lproj/Main.storyboard +++ b/Mac/Base.lproj/Main.storyboard @@ -496,6 +496,12 @@ + + + + + + diff --git a/Mac/Browser.swift b/Mac/Browser.swift index 335a1a560..e633589c6 100644 --- a/Mac/Browser.swift +++ b/Mac/Browser.swift @@ -6,14 +6,16 @@ // Copyright © 2016 Ranchero Software, LLC. All rights reserved. // -import Foundation +import AppKit import RSWeb struct Browser { static func open(_ urlString: String) { + let shouldInvertPreference = NSApp.currentEvent?.modifierFlags.contains(.shift) ?? false + // Opens according to prefs. - open(urlString, inBackground: AppDefaults.openInBrowserInBackground) + open(urlString, inBackground: shouldInvertPreference ? !AppDefaults.openInBrowserInBackground : AppDefaults.openInBrowserInBackground) } static func open(_ urlString: String, inBackground: Bool) { @@ -23,3 +25,13 @@ struct Browser { } } +extension Browser { + + static var titleForOpenInBrowserInverted: String { + let openInBackgroundPref = AppDefaults.openInBrowserInBackground + + return openInBackgroundPref ? + NSLocalizedString("Open in Browser in Foreground", comment: "Open in Browser in Foreground menu item title") : + NSLocalizedString("Open in Browser in Background", comment: "Open in Browser in Background menu item title") + } +} diff --git a/Mac/MainWindow/MainWindowController.swift b/Mac/MainWindow/MainWindowController.swift index 01e7335c2..9633142ef 100644 --- a/Mac/MainWindow/MainWindowController.swift +++ b/Mac/MainWindow/MainWindowController.swift @@ -179,6 +179,10 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations { public func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool { if item.action == #selector(openArticleInBrowser(_:)) { + if let item = item as? NSMenuItem, item.keyEquivalentModifierMask.contains(.shift) { + item.title = Browser.titleForOpenInBrowserInverted + } + return currentLink != nil } diff --git a/Mac/MainWindow/Timeline/TimelineViewController.swift b/Mac/MainWindow/Timeline/TimelineViewController.swift index ab1e9642d..00a2301ce 100644 --- a/Mac/MainWindow/Timeline/TimelineViewController.swift +++ b/Mac/MainWindow/Timeline/TimelineViewController.swift @@ -725,6 +725,10 @@ extension TimelineViewController: NSUserInterfaceValidations { func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool { if item.action == #selector(openArticleInBrowser(_:)) { + if let item = item as? NSMenuItem, item.keyEquivalentModifierMask.contains(.shift) { + item.title = Browser.titleForOpenInBrowserInverted + } + let currentLink = oneSelectedArticle?.preferredLink return currentLink != nil } From 24338b135e087f150d428d590bbe6744b45c7eb1 Mon Sep 17 00:00:00 2001 From: Nate Weaver Date: Wed, 20 May 2020 19:54:25 -0500 Subject: [PATCH 2/6] Don't do shift-key detection in Browser --- Mac/Browser.swift | 8 +++----- Mac/MainWindow/MainWindowController.swift | 6 ++++-- Mac/MainWindow/Timeline/TimelineViewController.swift | 4 +++- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Mac/Browser.swift b/Mac/Browser.swift index e633589c6..b36a27ea0 100644 --- a/Mac/Browser.swift +++ b/Mac/Browser.swift @@ -6,16 +6,14 @@ // Copyright © 2016 Ranchero Software, LLC. All rights reserved. // -import AppKit +import Foundation import RSWeb struct Browser { - static func open(_ urlString: String) { - let shouldInvertPreference = NSApp.currentEvent?.modifierFlags.contains(.shift) ?? false - + static func open(_ urlString: String, invertPreference invert: Bool = false) { // Opens according to prefs. - open(urlString, inBackground: shouldInvertPreference ? !AppDefaults.openInBrowserInBackground : AppDefaults.openInBrowserInBackground) + open(urlString, inBackground: invert ? !AppDefaults.openInBrowserInBackground : AppDefaults.openInBrowserInBackground) } static func open(_ urlString: String, inBackground: Bool) { diff --git a/Mac/MainWindow/MainWindowController.swift b/Mac/MainWindow/MainWindowController.swift index 9633142ef..60e25f3e6 100644 --- a/Mac/MainWindow/MainWindowController.swift +++ b/Mac/MainWindow/MainWindowController.swift @@ -182,7 +182,7 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations { if let item = item as? NSMenuItem, item.keyEquivalentModifierMask.contains(.shift) { item.title = Browser.titleForOpenInBrowserInverted } - + return currentLink != nil } @@ -264,8 +264,10 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations { } @IBAction func openArticleInBrowser(_ sender: Any?) { + let invert = NSApp.currentEvent?.modifierFlags.contains(.shift) ?? false + if let link = currentLink { - Browser.open(link) + Browser.open(link, invertPreference: invert) } } diff --git a/Mac/MainWindow/Timeline/TimelineViewController.swift b/Mac/MainWindow/Timeline/TimelineViewController.swift index 00a2301ce..3049653df 100644 --- a/Mac/MainWindow/Timeline/TimelineViewController.swift +++ b/Mac/MainWindow/Timeline/TimelineViewController.swift @@ -296,8 +296,10 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr // MARK: - Actions @objc func openArticleInBrowser(_ sender: Any?) { + let invert = NSApp.currentEvent?.modifierFlags.contains(.shift) ?? false + if let link = oneSelectedArticle?.preferredLink { - Browser.open(link) + Browser.open(link, invertPreference: invert) } } From 1e1fc06e7b913bcc90a0e7e20d24f9f90e966e8d Mon Sep 17 00:00:00 2001 From: Nate Weaver Date: Wed, 20 May 2020 19:59:05 -0500 Subject: [PATCH 3/6] Clean up shift-key checking and add it to more places --- Mac/MainWindow/Detail/DetailWebViewController.swift | 2 +- Mac/MainWindow/MainWindowController.swift | 4 +--- Mac/MainWindow/Sidebar/SidebarViewController.swift | 2 +- Mac/MainWindow/Timeline/TimelineViewController.swift | 4 +--- 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/Mac/MainWindow/Detail/DetailWebViewController.swift b/Mac/MainWindow/Detail/DetailWebViewController.swift index 8b4cdfc08..cadca1150 100644 --- a/Mac/MainWindow/Detail/DetailWebViewController.swift +++ b/Mac/MainWindow/Detail/DetailWebViewController.swift @@ -193,7 +193,7 @@ extension DetailWebViewController: WKNavigationDelegate { public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { if navigationAction.navigationType == .linkActivated { if let url = navigationAction.request.url { - Browser.open(url.absoluteString) + Browser.open(url.absoluteString, invertPreference: navigationAction.modifierFlags.contains(.shift)) } decisionHandler(.cancel) return diff --git a/Mac/MainWindow/MainWindowController.swift b/Mac/MainWindow/MainWindowController.swift index 60e25f3e6..4b5da5479 100644 --- a/Mac/MainWindow/MainWindowController.swift +++ b/Mac/MainWindow/MainWindowController.swift @@ -264,10 +264,8 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations { } @IBAction func openArticleInBrowser(_ sender: Any?) { - let invert = NSApp.currentEvent?.modifierFlags.contains(.shift) ?? false - if let link = currentLink { - Browser.open(link, invertPreference: invert) + Browser.open(link, invertPreference: NSApp.currentEvent?.modifierFlags.contains(.shift) ?? false) } } diff --git a/Mac/MainWindow/Sidebar/SidebarViewController.swift b/Mac/MainWindow/Sidebar/SidebarViewController.swift index b268be44f..7cb181d39 100644 --- a/Mac/MainWindow/Sidebar/SidebarViewController.swift +++ b/Mac/MainWindow/Sidebar/SidebarViewController.swift @@ -237,7 +237,7 @@ protocol SidebarDelegate: class { guard let feed = singleSelectedWebFeed, let homePageURL = feed.homePageURL else { return } - Browser.open(homePageURL) + Browser.open(homePageURL, invertPreference: NSApp.currentEvent?.modifierFlags.contains(.shift) ?? false) } @IBAction func gotoToday(_ sender: Any?) { diff --git a/Mac/MainWindow/Timeline/TimelineViewController.swift b/Mac/MainWindow/Timeline/TimelineViewController.swift index 3049653df..a283ebd41 100644 --- a/Mac/MainWindow/Timeline/TimelineViewController.swift +++ b/Mac/MainWindow/Timeline/TimelineViewController.swift @@ -296,10 +296,8 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr // MARK: - Actions @objc func openArticleInBrowser(_ sender: Any?) { - let invert = NSApp.currentEvent?.modifierFlags.contains(.shift) ?? false - if let link = oneSelectedArticle?.preferredLink { - Browser.open(link, invertPreference: invert) + Browser.open(link, invertPreference: NSApp.currentEvent?.modifierFlags.contains(.shift) ?? false) } } From 5cd8dc9d5d6ae4e5524f3572fd8399e09eb50365 Mon Sep 17 00:00:00 2001 From: Nate Weaver Date: Wed, 20 May 2020 20:08:13 -0500 Subject: [PATCH 4/6] Also let the Command key invert the background pref in web views --- Mac/MainWindow/Detail/DetailWebViewController.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Mac/MainWindow/Detail/DetailWebViewController.swift b/Mac/MainWindow/Detail/DetailWebViewController.swift index cadca1150..e77101f9d 100644 --- a/Mac/MainWindow/Detail/DetailWebViewController.swift +++ b/Mac/MainWindow/Detail/DetailWebViewController.swift @@ -193,7 +193,9 @@ extension DetailWebViewController: WKNavigationDelegate { public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { if navigationAction.navigationType == .linkActivated { if let url = navigationAction.request.url { - Browser.open(url.absoluteString, invertPreference: navigationAction.modifierFlags.contains(.shift)) + let flags = navigationAction.modifierFlags + let invert = flags.contains(.shift) || flags.contains(.command) + Browser.open(url.absoluteString, invertPreference: invert) } decisionHandler(.cancel) return From a38ab12fb3458998b078ddb8f279be4d9053646b Mon Sep 17 00:00:00 2001 From: Nate Weaver Date: Wed, 20 May 2020 20:14:29 -0500 Subject: [PATCH 5/6] Add a note about holding the Shift key --- Mac/Base.lproj/Preferences.storyboard | 53 ++++++++++++++++----------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/Mac/Base.lproj/Preferences.storyboard b/Mac/Base.lproj/Preferences.storyboard index 1e3394f8a..ffa4ba8d2 100644 --- a/Mac/Base.lproj/Preferences.storyboard +++ b/Mac/Base.lproj/Preferences.storyboard @@ -30,15 +30,15 @@ - - + + - + - + @@ -46,7 +46,7 @@ - + @@ -81,7 +81,7 @@ - + @@ -89,7 +89,7 @@ - + @@ -104,7 +104,7 @@ - + @@ -112,7 +112,7 @@ + + + + + + + + @@ -168,13 +176,15 @@ + - + + @@ -184,6 +194,7 @@ + @@ -205,7 +216,7 @@ - + @@ -385,16 +396,16 @@ - + - + - + - + @@ -501,7 +512,7 @@ - + @@ -556,16 +567,16 @@ - + - + - + - + @@ -668,7 +679,7 @@ - + From 82d673851d67f469f36423bd62e9b33b8d8367bd Mon Sep 17 00:00:00 2001 From: Nate Weaver Date: Wed, 20 May 2020 20:23:13 -0500 Subject: [PATCH 6/6] Tweak constraints, add period --- Mac/Base.lproj/Preferences.storyboard | 42 +++++++++++++-------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/Mac/Base.lproj/Preferences.storyboard b/Mac/Base.lproj/Preferences.storyboard index ffa4ba8d2..a1e976819 100644 --- a/Mac/Base.lproj/Preferences.storyboard +++ b/Mac/Base.lproj/Preferences.storyboard @@ -35,10 +35,10 @@ - + - + @@ -46,7 +46,7 @@ - + @@ -81,7 +81,7 @@ - + @@ -89,7 +89,7 @@ - + @@ -104,7 +104,7 @@ - + @@ -112,7 +112,7 @@ - - - + + + @@ -183,7 +183,7 @@ - + @@ -396,16 +396,16 @@ - + - + - + - + @@ -512,7 +512,7 @@ - + @@ -567,16 +567,16 @@ - + - + - + - + @@ -679,7 +679,7 @@ - +