From fa7202d5453ea75c28c82b136bda3d573c6bda55 Mon Sep 17 00:00:00 2001 From: Neil Bakhle Date: Sun, 4 Oct 2020 17:09:49 -0400 Subject: [PATCH] menu fixes --- MacPassnger/AppDelegate.swift | 82 ++++++++++++--------- MacPassnger/Base.lproj/Main.storyboard | 18 +++-- MacPassnger/Info.plist | 4 +- Passnger.xcodeproj/project.pbxproj | 12 ++- Passnger/Base.lproj/LaunchScreen.storyboard | 7 +- Passnger/Info.plist | 2 +- 6 files changed, 74 insertions(+), 51 deletions(-) diff --git a/MacPassnger/AppDelegate.swift b/MacPassnger/AppDelegate.swift index a906bba..6ace144 100644 --- a/MacPassnger/AppDelegate.swift +++ b/MacPassnger/AppDelegate.swift @@ -13,7 +13,7 @@ import Combine @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate { - var window: NSWindow! + var window: NSWindow? private lazy var toolbarObservable = { ToolbarObservable(model: self.model) }() @@ -56,26 +56,24 @@ class AppDelegate: NSObject, NSApplicationDelegate { private var toolbarCancellable: AnyCancellable? - func applicationDidFinishLaunching(_ aNotification: Notification) { // Create the SwiftUI view that provides the window contents. - let toolbar = NSToolbar(identifier: "MainToolbar") - toolbar.displayMode = .iconOnly - toolbar.delegate = self - toolbarCancellable = toolbarObservable.$selectedPassword.sink(receiveValue: { passwordItem in self.deleteToolbarButton.isEnabled = (passwordItem != nil) self.copyButton.isEnabled = (passwordItem != nil) self.infoButton.isEnabled = (passwordItem != nil) - self.copyMenuItem?.isEnabled = (passwordItem != nil) - self.deleteMenuItem?.isEnabled = (passwordItem != nil) - self.infoMenuItem?.isEnabled = (passwordItem != nil) - }) + window = getWindow() + window?.makeKeyAndOrderFront(nil) + } - // Create the window and set the content view. - window = NSWindow( + func getWindow() -> NSWindow { + let toolbar = NSToolbar(identifier: "MainToolbar") + toolbar.displayMode = .iconOnly + toolbar.delegate = self + + let window = NSWindow( contentRect: NSRect(x: 0, y: 0, width: 480, height: 300), styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], backing: .buffered, defer: false) @@ -86,11 +84,25 @@ class AppDelegate: NSObject, NSApplicationDelegate { window.contentView = NSHostingView(rootView: ContentView(model: model, toolbar: toolbarObservable)) window.toolbar = toolbar window.delegate = self - window.makeKeyAndOrderFront(nil) + window.isReleasedWhenClosed = false + return window } - func applicationWillTerminate(_ aNotification: Notification) { - // Insert code here to tear down your application + func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool { + if flag { + return false + } + self.openWindow(nil) + return true + } + + @IBAction func openWindow(_ sender: Any?) { + if let window = window { + window.makeKeyAndOrderFront(nil) + } else { + window = getWindow() + window?.makeKeyAndOrderFront(nil) + } } } @@ -104,31 +116,13 @@ extension AppDelegate: NSWindowDelegate { } func windowShouldClose(_ sender: NSWindow) -> Bool { - NSApplication.shared.hide(self) - return false + self.window = nil + return true } - func applicationDidBecomeActive(_ notification: Notification) { - NSApplication.shared.unhide(self) - } } -extension AppDelegate { - - private func menuItem(widthTitle title: String) -> NSMenuItem? { - return NSApplication.shared.mainMenu?.item(withTitle: title) - } - - var copyMenuItem: NSMenuItem? { menuItem(widthTitle: "Copy") } - var pasteMenuItem: NSMenuItem? { menuItem(widthTitle: "Paste") } - var deleteMenuItem: NSMenuItem? { menuItem(widthTitle: "Delete") } - var infoMenuItem: NSMenuItem? { menuItem(widthTitle: "Info") } - - private func disableMenuItems() { - copyMenuItem?.isEnabled = false - deleteMenuItem?.isEnabled = false - infoMenuItem?.isEnabled = false - } +extension AppDelegate: NSUserInterfaceValidations { @IBAction func newDocument(_ sender: Any) { createPassword() @@ -150,6 +144,22 @@ extension AppDelegate { searchField.becomeFirstResponder() } + func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool { + if item.action == #selector(openWindow(_:)) { + return true + } + if item.action == #selector(copy(_:)) { + return copyButton.isEnabled + } + if item.action == #selector(delete(_:)) { + return deleteToolbarButton.isEnabled + } + if item.action == #selector(info(_:)) { + return infoButton.isEnabled + } + return NSApplication.shared.validateUserInterfaceItem(item) + } + } extension AppDelegate: NSSearchFieldDelegate { diff --git a/MacPassnger/Base.lproj/Main.storyboard b/MacPassnger/Base.lproj/Main.storyboard index d8823d3..2db80fa 100644 --- a/MacPassnger/Base.lproj/Main.storyboard +++ b/MacPassnger/Base.lproj/Main.storyboard @@ -1,14 +1,15 @@ - + - + + - + @@ -69,6 +70,7 @@ + @@ -136,6 +138,12 @@ CA + + + + + + @@ -163,11 +171,11 @@ CA - + - + diff --git a/MacPassnger/Info.plist b/MacPassnger/Info.plist index 894880b..40d6ee5 100644 --- a/MacPassnger/Info.plist +++ b/MacPassnger/Info.plist @@ -17,7 +17,7 @@ CFBundlePackageType $(PRODUCT_BUNDLE_PACKAGE_TYPE) CFBundleShortVersionString - 1.0 + $(MARKETING_VERSION) CFBundleVersion $(CURRENT_PROJECT_VERSION) LSApplicationCategoryType @@ -31,7 +31,7 @@ NSPrincipalClass NSApplication NSSupportsAutomaticTermination - + NSSupportsSuddenTermination diff --git a/Passnger.xcodeproj/project.pbxproj b/Passnger.xcodeproj/project.pbxproj index 38c669e..0090e2f 100644 --- a/Passnger.xcodeproj/project.pbxproj +++ b/Passnger.xcodeproj/project.pbxproj @@ -534,7 +534,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 5; + CURRENT_PROJECT_VERSION = 7; DEVELOPMENT_ASSET_PATHS = "\"MacPassnger/Preview Content\""; DEVELOPMENT_TEAM = 54ZXBTM75W; ENABLE_HARDENED_RUNTIME = YES; @@ -545,6 +545,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 10.15; + MARKETING_VERSION = 1.01; PRODUCT_BUNDLE_IDENTIFIER = com.nbakhle.passnger; PRODUCT_NAME = Passnger; SDKROOT = macosx; @@ -560,7 +561,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 5; + CURRENT_PROJECT_VERSION = 7; DEVELOPMENT_ASSET_PATHS = "\"MacPassnger/Preview Content\""; DEVELOPMENT_TEAM = 54ZXBTM75W; ENABLE_HARDENED_RUNTIME = YES; @@ -571,6 +572,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 10.15; + MARKETING_VERSION = 1.01; PRODUCT_BUNDLE_IDENTIFIER = com.nbakhle.passnger; PRODUCT_NAME = Passnger; SDKROOT = macosx; @@ -699,7 +701,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 4; + CURRENT_PROJECT_VERSION = 5; DEVELOPMENT_ASSET_PATHS = "\"Passnger/Preview Content\""; DEVELOPMENT_TEAM = 54ZXBTM75W; ENABLE_PREVIEWS = YES; @@ -709,6 +711,7 @@ "$(inherited)", "@executable_path/Frameworks", ); + MARKETING_VERSION = 1.01; PRODUCT_BUNDLE_IDENTIFIER = com.nbakhle.passnger; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 5.0; @@ -721,7 +724,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 4; + CURRENT_PROJECT_VERSION = 5; DEVELOPMENT_ASSET_PATHS = "\"Passnger/Preview Content\""; DEVELOPMENT_TEAM = 54ZXBTM75W; ENABLE_PREVIEWS = YES; @@ -731,6 +734,7 @@ "$(inherited)", "@executable_path/Frameworks", ); + MARKETING_VERSION = 1.01; PRODUCT_BUNDLE_IDENTIFIER = com.nbakhle.passnger; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 5.0; diff --git a/Passnger/Base.lproj/LaunchScreen.storyboard b/Passnger/Base.lproj/LaunchScreen.storyboard index 9a1de9b..db14e8b 100644 --- a/Passnger/Base.lproj/LaunchScreen.storyboard +++ b/Passnger/Base.lproj/LaunchScreen.storyboard @@ -1,8 +1,9 @@ - + - + + @@ -19,13 +20,13 @@ + - diff --git a/Passnger/Info.plist b/Passnger/Info.plist index 2e34cc0..7d508e5 100644 --- a/Passnger/Info.plist +++ b/Passnger/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType $(PRODUCT_BUNDLE_PACKAGE_TYPE) CFBundleShortVersionString - 1.0 + $(MARKETING_VERSION) CFBundleVersion $(CURRENT_PROJECT_VERSION) LSRequiresIPhoneOS