From 2fbc2fb628c20841cb5e77f71e761ad0f79c50e9 Mon Sep 17 00:00:00 2001 From: Nate Weaver Date: Fri, 7 Oct 2022 18:48:10 -0500 Subject: [PATCH] Support opening multiple articles in browser from main menu Move openArticleURLs() to MainWindowController. --- Mac/MainWindow/MainWindowController.swift | 32 +++++++++++++++++-- ...melineViewController+ContextualMenus.swift | 26 ++------------- .../Timeline/TimelineViewController.swift | 9 +++--- 3 files changed, 35 insertions(+), 32 deletions(-) diff --git a/Mac/MainWindow/MainWindowController.swift b/Mac/MainWindow/MainWindowController.swift index 7ca1a1166..7052e8efb 100644 --- a/Mac/MainWindow/MainWindowController.swift +++ b/Mac/MainWindow/MainWindowController.swift @@ -340,10 +340,36 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations { } } + func openArticleURLs(_ urlStrings: [String]) { + func doOpenURLs() { + for urlString in urlStrings { + Browser.open(urlString, inBackground: false) + } + } + + if urlStrings.count > 20 { + let alert = NSAlert() + let messageFormat = NSLocalizedString("Are you sure you want to open %ld articles in your browser?", comment: "") + alert.messageText = String.localizedStringWithFormat(messageFormat, urlStrings.count) + alert.addButton(withTitle: NSLocalizedString("Open", comment: "")) + alert.addButton(withTitle: NSLocalizedString("Cancel", comment: "")) + + guard let window = self.window else { return } + + alert.beginSheetModal(for: window) { response in + if response == .alertFirstButtonReturn { + doOpenURLs() + } + } + } else { + doOpenURLs() + } + } + @IBAction func openArticleInBrowser(_ sender: Any?) { - if let link = currentLink { - Browser.open(link, invertPreference: NSApp.currentEvent?.modifierFlags.contains(.shift) ?? false) - } + guard let selectedArticles else { return } + let urlStrings = selectedArticles.compactMap { $0.preferredLink } + openArticleURLs(urlStrings) } @IBAction func openInBrowser(_ sender: Any?) { diff --git a/Mac/MainWindow/Timeline/TimelineViewController+ContextualMenus.swift b/Mac/MainWindow/Timeline/TimelineViewController+ContextualMenus.swift index 04b5ea554..aca01d921 100644 --- a/Mac/MainWindow/Timeline/TimelineViewController+ContextualMenus.swift +++ b/Mac/MainWindow/Timeline/TimelineViewController+ContextualMenus.swift @@ -89,34 +89,12 @@ extension TimelineViewController { } @objc func openInBrowserFromContextualMenu(_ sender: Any?) { - guard let menuItem = sender as? NSMenuItem, let urlStrings = menuItem.representedObject as? [String] else { return } - func doOpenURLs() { - for urlString in urlStrings { - Browser.open(urlString, inBackground: false) - } - } - - if urlStrings.count > 20 { - let alert = NSAlert() - let messageFormat = NSLocalizedString("Are you sure you want to open %ld articles in your browser?", comment: "") - alert.messageText = String.localizedStringWithFormat(messageFormat, urlStrings.count) - alert.addButton(withTitle: NSLocalizedString("Open", comment: "")) - alert.addButton(withTitle: NSLocalizedString("Cancel", comment: "")) - - guard let window = self.view.window else { return } - - alert.beginSheetModal(for: window) { response in - if response == .alertFirstButtonReturn { - doOpenURLs() - } - } - } else { - doOpenURLs() - } + guard let windowController = self.view.window?.windowController as? MainWindowController else { return } + windowController.openArticleURLs(urlStrings) } @objc func copyURLFromContextualMenu(_ sender: Any?) { diff --git a/Mac/MainWindow/Timeline/TimelineViewController.swift b/Mac/MainWindow/Timeline/TimelineViewController.swift index 27b303339..5b024edbc 100644 --- a/Mac/MainWindow/Timeline/TimelineViewController.swift +++ b/Mac/MainWindow/Timeline/TimelineViewController.swift @@ -315,9 +315,9 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr // MARK: - Actions @objc func openArticleInBrowser(_ sender: Any?) { - if let link = oneSelectedArticle?.preferredLink { - Browser.open(link, invertPreference: NSApp.currentEvent?.modifierFlags.contains(.shift) ?? false) - } + guard let windowController = self.view.window?.windowController as? MainWindowController else { return } + let urlStrings = selectedArticles.compactMap { $0.preferredLink } + windowController.openArticleURLs(urlStrings) } @IBAction func toggleStatusOfSelectedArticles(_ sender: Any?) { @@ -779,8 +779,7 @@ extension TimelineViewController: NSUserInterfaceValidations { item.title = Browser.titleForOpenInBrowserInverted } - let currentLink = oneSelectedArticle?.preferredLink - return currentLink != nil + return selectedArticles.first { $0.preferredLink != nil } != nil } if item.action == #selector(copy(_:)) {