Popup a share menu on clicking the Share toolbar item. Fix #40.

This commit is contained in:
Brent Simmons 2017-12-26 12:50:34 -08:00
parent 0b2d7c6733
commit 9ec6188395
3 changed files with 81 additions and 9 deletions

View File

@ -588,7 +588,6 @@
849A977C1ED9EC42007D329B /* Detail */, 849A977C1ED9EC42007D329B /* Detail */,
849A97551ED9EAC3007D329B /* Add Feed */, 849A97551ED9EAC3007D329B /* Add Feed */,
849A97411ED9EAA9007D329B /* Add Folder */, 849A97411ED9EAA9007D329B /* Add Folder */,
84AFBB3A1FBE76D800BA41CF /* PanicButton */,
); );
name = MainWindow; name = MainWindow;
path = Evergreen/MainWindow; path = Evergreen/MainWindow;
@ -916,13 +915,6 @@
path = Evergreen/Dinosaurs; path = Evergreen/Dinosaurs;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
84AFBB3A1FBE76D800BA41CF /* PanicButton */ = {
isa = PBXGroup;
children = (
);
path = PanicButton;
sourceTree = "<group>";
};
84B06F971ED37DA000F0B54B /* Products */ = { 84B06F971ED37DA000F0B54B /* Products */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (

View File

@ -32,7 +32,7 @@
</searchFieldCell> </searchFieldCell>
</searchField> </searchField>
</toolbarItem> </toolbarItem>
<toolbarItem implicitItemIdentifier="C3050605-E4B0-40CB-BA19-F64DF68BCFFA" label="Share" paletteLabel="Share" toolTip="Share" image="NSShareTemplate" id="nv0-Ju-lP7" customClass="RSToolbarItem" customModule="RSCore"> <toolbarItem implicitItemIdentifier="C3050605-E4B0-40CB-BA19-F64DF68BCFFA" explicitItemIdentifier="share" label="Share" paletteLabel="Share" toolTip="Share" image="NSShareTemplate" id="nv0-Ju-lP7" customClass="RSToolbarItem" customModule="RSCore">
<size key="minSize" width="38" height="25"/> <size key="minSize" width="38" height="25"/>
<size key="maxSize" width="38" height="27"/> <size key="maxSize" width="38" height="27"/>
<button key="view" verticalHuggingPriority="750" id="ypo-pI-PTN"> <button key="view" verticalHuggingPriority="750" id="ypo-pI-PTN">
@ -43,6 +43,9 @@
<font key="font" metaFont="system"/> <font key="font" metaFont="system"/>
</buttonCell> </buttonCell>
</button> </button>
<connections>
<action selector="toolbarShowShareMenu:" target="Oky-zY-oP4" id="zUk-sS-frQ"/>
</connections>
</toolbarItem> </toolbarItem>
<toolbarItem implicitItemIdentifier="3552D790-D29C-4BF2-AF02-5BEFE49C7FB9" label="New Feed" paletteLabel="New Feed" toolTip="New Feed" image="NSAddTemplate" id="Skp-5r-70Q" customClass="RSToolbarItem" customModule="RSCore"> <toolbarItem implicitItemIdentifier="3552D790-D29C-4BF2-AF02-5BEFE49C7FB9" label="New Feed" paletteLabel="New Feed" toolTip="New Feed" image="NSAddTemplate" id="Skp-5r-70Q" customClass="RSToolbarItem" customModule="RSCore">
<size key="minSize" width="38" height="25"/> <size key="minSize" width="38" height="25"/>
@ -198,6 +201,9 @@
<toolbarItem reference="YMs-P5-Xhn"/> <toolbarItem reference="YMs-P5-Xhn"/>
<toolbarItem reference="1Ql-WJ-KYi"/> <toolbarItem reference="1Ql-WJ-KYi"/>
</defaultToolbarItems> </defaultToolbarItems>
<connections>
<outlet property="delegate" destination="B8D-0N-5wS" id="hrC-d9-Cge"/>
</connections>
</toolbar> </toolbar>
<connections> <connections>
<outlet property="delegate" destination="B8D-0N-5wS" id="JSn-lq-Uwe"/> <outlet property="delegate" destination="B8D-0N-5wS" id="JSn-lq-Uwe"/>

View File

@ -131,6 +131,10 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations {
return canMarkOlderArticlesAsRead() return canMarkOlderArticlesAsRead()
} }
if item.action == #selector(toolbarShowShareMenu(_:)) {
return canShowShareMenu()
}
return true return true
} }
@ -265,6 +269,57 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations {
sidebarViewController?.outlineView.selectNextRow(sender) 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 // 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 // MARK: - Navigation
func handleRightArrowFunctionKey(in view: NSView) { func handleRightArrowFunctionKey(in view: NSView) {