Merge pull request #3102 from robmathers/copy-url-menu-options

Add Copy Article URL & Copy External URL Menu Items. Fixes #1285.
This commit is contained in:
Maurice Parker 2021-09-12 21:17:50 -05:00 committed by GitHub
commit fa4b2531f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 92 additions and 0 deletions

View File

@ -157,6 +157,18 @@
<action selector="copy:" target="Ady-hI-5gd" id="G1f-GL-Joy"/>
</connections>
</menuItem>
<menuItem title="Copy Article URL" id="qNk-By-jKp">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="copyArticleURL:" target="Ady-hI-5gd" id="A5t-x7-Mmy"/>
</connections>
</menuItem>
<menuItem title="Copy External URL" id="fOF-99-6Iv">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="copyExternalURL:" target="Ady-hI-5gd" id="MkV-Do-Bc1"/>
</connections>
</menuItem>
<menuItem title="Paste" keyEquivalent="v" id="gVA-U4-sdL">
<connections>
<action selector="paste:" target="Ady-hI-5gd" id="UvS-8e-Qdg"/>

View File

@ -204,6 +204,14 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations {
public func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool {
if item.action == #selector(copyArticleURL(_:)) {
return canCopyArticleURL()
}
if item.action == #selector(copyExternalURL(_:)) {
return canCopyExternalURL()
}
if item.action == #selector(openArticleInBrowser(_:)) {
if let item = item as? NSMenuItem, item.keyEquivalentModifierMask.contains(.shift) {
item.title = Browser.titleForOpenInBrowserInverted
@ -302,6 +310,18 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations {
}
@IBAction func copyArticleURL(_ sender: Any?) {
if let link = currentLink {
URLPasteboardWriter.write(urlString: link, to: .general)
}
}
@IBAction func copyExternalURL(_ sender: Any?) {
if let link = oneSelectedArticle?.externalURL {
URLPasteboardWriter.write(urlString: link, to: .general)
}
}
@IBAction func openArticleInBrowser(_ sender: Any?) {
if let link = currentLink {
Browser.open(link, invertPreference: NSApp.currentEvent?.modifierFlags.contains(.shift) ?? false)
@ -1036,6 +1056,14 @@ private extension MainWindowController {
}
// MARK: - Command Validation
func canCopyArticleURL() -> Bool {
return currentLink != nil
}
func canCopyExternalURL() -> Bool {
return oneSelectedArticle?.externalURL != nil && oneSelectedArticle?.externalURL != currentLink
}
func canGoToNextUnread(wrappingToTop wrapping: Bool = false) -> Bool {

View File

@ -90,6 +90,13 @@ extension TimelineViewController {
}
Browser.open(urlString, inBackground: false)
}
@objc func copyURLFromContextualMenu(_ sender: Any?) {
guard let menuItem = sender as? NSMenuItem, let urlString = menuItem.representedObject as? String else {
return
}
URLPasteboardWriter.write(urlString: urlString, to: .general)
}
@objc func performShareServiceFromContextualMenu(_ sender: Any?) {
guard let menuItem = sender as? NSMenuItem, let sharingCommandInfo = menuItem.representedObject as? SharingCommandInfo else {
@ -168,6 +175,12 @@ private extension TimelineViewController {
if articles.count == 1, let link = articles.first!.preferredLink {
menu.addSeparatorIfNeeded()
menu.addItem(openInBrowserMenuItem(link))
menu.addSeparatorIfNeeded()
menu.addItem(copyArticleURLMenuItem(link))
if let externalLink = articles.first?.externalURL, externalLink != link {
menu.addItem(copyExternalURLMenuItem(externalLink))
}
}
if let sharingMenu = shareMenu(for: articles) {
@ -260,6 +273,15 @@ private extension TimelineViewController {
return menuItem(NSLocalizedString("Open in Browser", comment: "Command"), #selector(openInBrowserFromContextualMenu(_:)), urlString)
}
func copyArticleURLMenuItem(_ urlString: String) -> NSMenuItem {
return menuItem(NSLocalizedString("Copy Article URL", comment: "Command"), #selector(copyURLFromContextualMenu(_:)), urlString)
}
func copyExternalURLMenuItem(_ urlString: String) -> NSMenuItem {
return menuItem(NSLocalizedString("Copy External URL", comment: "Command"), #selector(copyURLFromContextualMenu(_:)), urlString)
}
func menuItem(_ title: String, _ action: Selector, _ representedObject: Any) -> NSMenuItem {

View File

@ -377,6 +377,17 @@ class MasterTimelineViewController: UITableViewController, UndoableCommandRunner
menuElements.append(UIMenu(title: "", options: .displayInline, children: secondaryActions))
}
var copyActions = [UIAction]()
if let action = self.copyArticleURLAction(article) {
copyActions.append(action)
}
if let action = self.copyExternalURLAction(article) {
copyActions.append(action)
}
if !copyActions.isEmpty {
menuElements.append(UIMenu(title: "", options: .displayInline, children: copyActions))
}
if let action = self.openInBrowserAction(article) {
menuElements.append(UIMenu(title: "", options: .displayInline, children: [action]))
}
@ -902,6 +913,25 @@ private extension MasterTimelineViewController {
}
return action
}
func copyArticleURLAction(_ article: Article) -> UIAction? {
guard let preferredLink = article.preferredLink, let url = URL(string: preferredLink) else { return nil }
let title = NSLocalizedString("Copy Article URL", comment: "Copy Article URL")
let action = UIAction(title: title, image: AppAssets.copyImage) { action in
UIPasteboard.general.url = url
}
return action
}
func copyExternalURLAction(_ article: Article) -> UIAction? {
guard let externalURL = article.externalURL, externalURL != article.preferredLink, let url = URL(string: externalURL) else { return nil }
let title = NSLocalizedString("Copy External URL", comment: "Copy External URL")
let action = UIAction(title: title, image: AppAssets.copyImage) { action in
UIPasteboard.general.url = url
}
return action
}
func openInBrowserAction(_ article: Article) -> UIAction? {
guard let _ = article.preferredURL else { return nil }