Move toolbar and sharing service delegates from MainWindowController to separate objects.

This commit is contained in:
Brent Simmons 2018-02-17 20:33:30 -08:00
parent 054c7fb409
commit a633c49a6c
5 changed files with 77 additions and 53 deletions

View File

@ -100,6 +100,8 @@
849C646B1ED37A5D003D8FC0 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 849C64691ED37A5D003D8FC0 /* Main.storyboard */; };
849C64761ED37A5D003D8FC0 /* EvergreenTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 849C64751ED37A5D003D8FC0 /* EvergreenTests.swift */; };
849EE70F203919360082A1EA /* AppImages.swift in Sources */ = {isa = PBXBuildFile; fileRef = 849EE70E203919360082A1EA /* AppImages.swift */; };
849EE71F20391DF20082A1EA /* MainWindowToolbarDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 849EE71E20391DF20082A1EA /* MainWindowToolbarDelegate.swift */; };
849EE72120391F560082A1EA /* MainWindowSharingServicePickerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 849EE72020391F560082A1EA /* MainWindowSharingServicePickerDelegate.swift */; };
84A14FF320048CA70046AD9A /* SendToMicroBlogCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84A14FF220048CA70046AD9A /* SendToMicroBlogCommand.swift */; };
84A1500320048D660046AD9A /* SendToCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84A1500220048D660046AD9A /* SendToCommand.swift */; };
84A1500520048DDF0046AD9A /* SendToMarsEditCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84A1500420048DDF0046AD9A /* SendToMarsEditCommand.swift */; };
@ -623,6 +625,8 @@
849C64751ED37A5D003D8FC0 /* EvergreenTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EvergreenTests.swift; sourceTree = "<group>"; };
849C64771ED37A5D003D8FC0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
849EE70E203919360082A1EA /* AppImages.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = AppImages.swift; path = Evergreen/AppImages.swift; sourceTree = "<group>"; };
849EE71E20391DF20082A1EA /* MainWindowToolbarDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainWindowToolbarDelegate.swift; sourceTree = "<group>"; };
849EE72020391F560082A1EA /* MainWindowSharingServicePickerDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainWindowSharingServicePickerDelegate.swift; sourceTree = "<group>"; };
84A14FF220048CA70046AD9A /* SendToMicroBlogCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SendToMicroBlogCommand.swift; sourceTree = "<group>"; };
84A1500220048D660046AD9A /* SendToCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SendToCommand.swift; sourceTree = "<group>"; };
84A1500420048DDF0046AD9A /* SendToMarsEditCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SendToMarsEditCommand.swift; sourceTree = "<group>"; };
@ -813,6 +817,8 @@
849A97B01ED9FA69007D329B /* MainWindow.storyboard */,
842E45E21ED8C681000A8B52 /* KeyboardDelegateProtocol.swift */,
849A975D1ED9EB72007D329B /* MainWindowController.swift */,
849EE71E20391DF20082A1EA /* MainWindowToolbarDelegate.swift */,
849EE72020391F560082A1EA /* MainWindowSharingServicePickerDelegate.swift */,
842E45E41ED8C6B7000A8B52 /* MainWindowSplitView.swift */,
844B5B6B1FEA224B00C7C76A /* Keyboard */,
849A975F1ED9EB95007D329B /* Sidebar */,
@ -1951,6 +1957,7 @@
845A29241FC9255E007B49E3 /* SidebarCellAppearance.swift in Sources */,
845EE7B11FC2366500854A1F /* StarredFeedDelegate.swift in Sources */,
848F6AE51FC29CFB002D422E /* FaviconDownloader.swift in Sources */,
849EE72120391F560082A1EA /* MainWindowSharingServicePickerDelegate.swift in Sources */,
849A97981ED9EFAA007D329B /* Node-Extensions.swift in Sources */,
849EE70F203919360082A1EA /* AppImages.swift in Sources */,
849A97531ED9EAC0007D329B /* AddFeedController.swift in Sources */,
@ -1995,6 +2002,7 @@
D5F4EDB720074D6500B9E363 /* Feed+Scriptability.swift in Sources */,
84E850861FCB60CE0072EA88 /* AuthorAvatarDownloader.swift in Sources */,
8414AD251FCF5A1E00955102 /* TimelineHeaderView.swift in Sources */,
849EE71F20391DF20082A1EA /* MainWindowToolbarDelegate.swift in Sources */,
849A977A1ED9EC04007D329B /* TimelineTableCellView.swift in Sources */,
849A97761ED9EC04007D329B /* TimelineCellAppearance.swift in Sources */,
849A97A21ED9F180007D329B /* InitialFeedDownloader.swift in Sources */,

View File

@ -183,7 +183,7 @@
<toolbarItem reference="nv0-Ju-lP7"/>
</defaultToolbarItems>
<connections>
<outlet property="delegate" destination="B8D-0N-5wS" id="hrC-d9-Cge"/>
<outlet property="delegate" destination="V3e-nc-6hW" id="ncd-Wh-qZU"/>
</connections>
</toolbar>
<connections>
@ -191,10 +191,12 @@
</connections>
</window>
<connections>
<outlet property="toolbarDelegate" destination="V3e-nc-6hW" id="0rl-4U-DkU"/>
<segue destination="reS-fe-pD8" kind="relationship" relationship="window.shadowedContentViewController" id="WS2-WB-dc4"/>
</connections>
</windowController>
<customObject id="Oky-zY-oP4" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
<customObject id="V3e-nc-6hW" customClass="MainWindowToolbarDelegate" customModule="Evergreen" customModuleProvider="target"/>
</objects>
<point key="canvasLocation" x="-666" y="-124"/>
</scene>

View File

@ -13,10 +13,10 @@ import RSCore
private let kWindowFrameKey = "MainWindow"
class MainWindowController : NSWindowController, NSUserInterfaceValidations {
// MARK: NSWindowController
@IBOutlet var toolbarDelegate: MainWindowToolbarDelegate?
private let sharingServicePickerDelegate = MainWindowSharingServicePickerDelegate()
private let windowAutosaveName = NSWindow.FrameAutosaveName(rawValue: kWindowFrameKey)
private var unreadCount: Int = 0 {
@ -27,12 +27,13 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations {
}
}
static var didPositionWindowOnFirstRun = false
override func windowDidLoad() {
super.windowDidLoad()
if !AppDefaults.shared.showTitleOnMainWindow {
window?.titleVisibility = .hidden
}
@ -362,7 +363,7 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations {
let items = selectedArticles.map { ArticlePasteboardWriter(article: $0) }
let sharingServicePicker = NSSharingServicePicker(items: items)
sharingServicePicker.delegate = self
sharingServicePicker.delegate = sharingServicePickerDelegate
sharingServicePicker.show(relativeTo: view.bounds, of: view, preferredEdge: .minY)
}
@ -375,54 +376,6 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations {
}
}
// MARK: - NSSharingServicePickerDelegate
extension MainWindowController: NSSharingServicePickerDelegate {
func sharingServicePicker(_ sharingServicePicker: NSSharingServicePicker, sharingServicesForItems items: [Any], proposedSharingServices proposedServices: [NSSharingService]) -> [NSSharingService] {
let sendToServices = appDelegate.sendToCommands.compactMap { (sendToCommand) -> NSSharingService? in
guard let object = items.first else {
return nil
}
guard sendToCommand.canSendObject(object, selectedText: nil) else {
return nil
}
let image = sendToCommand.image ?? AppImages.genericFeedImage ?? NSImage()
return NSSharingService(title: sendToCommand.title, image: image, alternateImage: nil) {
sendToCommand.sendObject(object, selectedText: nil)
}
}
return proposedServices + sendToServices
}
}
// 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: - Scripting Access
/*

View File

@ -0,0 +1,31 @@
//
// MainWindowSharingServicePickerDelegate.swift
// Evergreen
//
// Created by Brent Simmons on 2/17/18.
// Copyright © 2018 Ranchero Software. All rights reserved.
//
import AppKit
@objc final class MainWindowSharingServicePickerDelegate: NSObject, NSSharingServicePickerDelegate {
func sharingServicePicker(_ sharingServicePicker: NSSharingServicePicker, sharingServicesForItems items: [Any], proposedSharingServices proposedServices: [NSSharingService]) -> [NSSharingService] {
let sendToServices = appDelegate.sendToCommands.compactMap { (sendToCommand) -> NSSharingService? in
guard let object = items.first else {
return nil
}
guard sendToCommand.canSendObject(object, selectedText: nil) else {
return nil
}
let image = sendToCommand.image ?? AppImages.genericFeedImage ?? NSImage()
return NSSharingService(title: sendToCommand.title, image: image, alternateImage: nil) {
sendToCommand.sendObject(object, selectedText: nil)
}
}
return proposedServices + sendToServices
}
}

View File

@ -0,0 +1,30 @@
//
// MainWindowToolbarDelegate.swift
// Evergreen
//
// Created by Brent Simmons on 2/17/18.
// Copyright © 2018 Ranchero Software. All rights reserved.
//
import AppKit
extension NSToolbarItem.Identifier {
static let Share = NSToolbarItem.Identifier("share")
}
@objc final class MainWindowToolbarDelegate: NSObject, 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)
}
}