From a633c49a6ca667731269aa957ce69673b0b8bce7 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 17 Feb 2018 20:33:30 -0800 Subject: [PATCH] Move toolbar and sharing service delegates from MainWindowController to separate objects. --- Evergreen.xcodeproj/project.pbxproj | 8 +++ Evergreen/Base.lproj/MainWindow.storyboard | 4 +- .../MainWindow/MainWindowController.swift | 57 ++----------------- ...inWindowSharingServicePickerDelegate.swift | 31 ++++++++++ .../MainWindowToolbarDelegate.swift | 30 ++++++++++ 5 files changed, 77 insertions(+), 53 deletions(-) create mode 100644 Evergreen/MainWindow/MainWindowSharingServicePickerDelegate.swift create mode 100644 Evergreen/MainWindow/MainWindowToolbarDelegate.swift diff --git a/Evergreen.xcodeproj/project.pbxproj b/Evergreen.xcodeproj/project.pbxproj index abab8eb9c..d9beffcd8 100644 --- a/Evergreen.xcodeproj/project.pbxproj +++ b/Evergreen.xcodeproj/project.pbxproj @@ -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 = ""; }; 849C64771ED37A5D003D8FC0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 849EE70E203919360082A1EA /* AppImages.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = AppImages.swift; path = Evergreen/AppImages.swift; sourceTree = ""; }; + 849EE71E20391DF20082A1EA /* MainWindowToolbarDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainWindowToolbarDelegate.swift; sourceTree = ""; }; + 849EE72020391F560082A1EA /* MainWindowSharingServicePickerDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainWindowSharingServicePickerDelegate.swift; sourceTree = ""; }; 84A14FF220048CA70046AD9A /* SendToMicroBlogCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SendToMicroBlogCommand.swift; sourceTree = ""; }; 84A1500220048D660046AD9A /* SendToCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SendToCommand.swift; sourceTree = ""; }; 84A1500420048DDF0046AD9A /* SendToMarsEditCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SendToMarsEditCommand.swift; sourceTree = ""; }; @@ -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 */, diff --git a/Evergreen/Base.lproj/MainWindow.storyboard b/Evergreen/Base.lproj/MainWindow.storyboard index 84a2f7180..3bffb2c55 100644 --- a/Evergreen/Base.lproj/MainWindow.storyboard +++ b/Evergreen/Base.lproj/MainWindow.storyboard @@ -183,7 +183,7 @@ - + @@ -191,10 +191,12 @@ + + diff --git a/Evergreen/MainWindow/MainWindowController.swift b/Evergreen/MainWindow/MainWindowController.swift index b047149b1..ff05208af 100644 --- a/Evergreen/MainWindow/MainWindowController.swift +++ b/Evergreen/MainWindow/MainWindowController.swift @@ -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 /* diff --git a/Evergreen/MainWindow/MainWindowSharingServicePickerDelegate.swift b/Evergreen/MainWindow/MainWindowSharingServicePickerDelegate.swift new file mode 100644 index 000000000..d598126a3 --- /dev/null +++ b/Evergreen/MainWindow/MainWindowSharingServicePickerDelegate.swift @@ -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 + } +} diff --git a/Evergreen/MainWindow/MainWindowToolbarDelegate.swift b/Evergreen/MainWindow/MainWindowToolbarDelegate.swift new file mode 100644 index 000000000..208969094 --- /dev/null +++ b/Evergreen/MainWindow/MainWindowToolbarDelegate.swift @@ -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) + } +}