diff --git a/Evergreen.xcodeproj/project.pbxproj b/Evergreen.xcodeproj/project.pbxproj index 3b0a91839..ffc80d5d3 100644 --- a/Evergreen.xcodeproj/project.pbxproj +++ b/Evergreen.xcodeproj/project.pbxproj @@ -588,7 +588,6 @@ 849A977C1ED9EC42007D329B /* Detail */, 849A97551ED9EAC3007D329B /* Add Feed */, 849A97411ED9EAA9007D329B /* Add Folder */, - 84AFBB3A1FBE76D800BA41CF /* PanicButton */, ); name = MainWindow; path = Evergreen/MainWindow; @@ -916,13 +915,6 @@ path = Evergreen/Dinosaurs; sourceTree = ""; }; - 84AFBB3A1FBE76D800BA41CF /* PanicButton */ = { - isa = PBXGroup; - children = ( - ); - path = PanicButton; - sourceTree = ""; - }; 84B06F971ED37DA000F0B54B /* Products */ = { isa = PBXGroup; children = ( diff --git a/Evergreen/Base.lproj/MainWindow.storyboard b/Evergreen/Base.lproj/MainWindow.storyboard index e22bbca18..9b23483ee 100644 --- a/Evergreen/Base.lproj/MainWindow.storyboard +++ b/Evergreen/Base.lproj/MainWindow.storyboard @@ -32,7 +32,7 @@ - + + + + @@ -198,6 +201,9 @@ + + + diff --git a/Evergreen/MainWindow/MainWindowController.swift b/Evergreen/MainWindow/MainWindowController.swift index 763487533..2587f5cdc 100644 --- a/Evergreen/MainWindow/MainWindowController.swift +++ b/Evergreen/MainWindow/MainWindowController.swift @@ -130,6 +130,10 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations { if item.action == #selector(markOlderArticlesAsRead(_:)) { return canMarkOlderArticlesAsRead() } + + if item.action == #selector(toolbarShowShareMenu(_:)) { + return canShowShareMenu() + } return true } @@ -265,6 +269,57 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations { sidebarViewController?.outlineView.selectNextRow(sender) } + + @IBAction func toolbarShowShareMenu(_ sender: Any?) { + + guard let selectedArticles = selectedArticles, !selectedArticles.isEmpty else { + assertionFailure("Expected toolbarShowShareMenu to be called only when there are selected articles.") + return + } + guard let shareToolbarItem = shareToolbarItem else { + assertionFailure("Expected toolbarShowShareMenu to be called only by the Share item in the toolbar.") + return + } + guard let view = shareToolbarItem.view else { + // TODO: handle menu form representation + return + } + + let items = selectedArticles.map { ArticlePasteboardWriter(article: $0) } + let sharingServicePicker = NSSharingServicePicker(items: items) + sharingServicePicker.show(relativeTo: view.bounds, of: view, preferredEdge: .minY) + } + + private func canShowShareMenu() -> Bool { + + guard let selectedArticles = selectedArticles else { + return false + } + return !selectedArticles.isEmpty + } +} + +// MARK: - NSToolbarDelegate + +extension NSToolbarItem.Identifier { + static let Share = NSToolbarItem.Identifier("share") +} + +extension MainWindowController: NSToolbarDelegate { + + func toolbarWillAddItem(_ notification: Notification) { + + // The share button should send its action on mouse down, not mouse up. + + guard let item = notification.userInfo?["item"] as? NSToolbarItem else { + return + } + guard item.itemIdentifier == .Share, let button = item.view as? NSButton else { + return + } + + button.sendAction(on: .leftMouseDown) + } } // MARK: - Private @@ -359,6 +414,25 @@ private extension MainWindowController { } } + // MARK: - Toolbar + + private var shareToolbarItem: NSToolbarItem? { + return existingToolbarItem(identifier: .Share) + } + + func existingToolbarItem(identifier: NSToolbarItem.Identifier) -> NSToolbarItem? { + + guard let toolbarItems = window?.toolbar?.items else { + return nil + } + for toolbarItem in toolbarItems { + if toolbarItem.itemIdentifier == identifier { + return toolbarItem + } + } + return nil + } + // MARK: - Navigation func handleRightArrowFunctionKey(in view: NSView) {