Skip to content

Commit

Permalink
menu fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
neilsbak committed Oct 4, 2020
1 parent 77ebd17 commit fa7202d
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 51 deletions.
82 changes: 46 additions & 36 deletions MacPassnger/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) }()

Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}
}

Expand All @@ -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()
Expand All @@ -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 {
Expand Down
18 changes: 13 additions & 5 deletions MacPassnger/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="16097.3" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="17156" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="16097.3"/>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="17156"/>
</dependencies>
<scenes>
<!--Application-->
<scene sceneID="JPo-4y-FX3">
<objects>
<application id="hnw-xV-0zn" sceneMemberID="viewController">
<menu key="mainMenu" title="Main Menu" systemMenu="main" id="AYu-sK-qS6">
<menu key="mainMenu" title="Main Menu" systemMenu="main" showsStateColumn="NO" id="AYu-sK-qS6">
<items>
<menuItem title="Passnger" id="1Xt-HY-uBw">
<modifierMask key="keyEquivalentModifierMask"/>
Expand Down Expand Up @@ -69,6 +70,7 @@
<menu key="submenu" title="Edit" id="W48-6f-4Dl">
<items>
<menuItem title="Copy" keyEquivalent="c" id="x3v-GG-iWU">
<attributedString key="attributedTitle"/>
<connections>
<action selector="copy:" target="Ady-hI-5gd" id="G1f-GL-Joy"/>
</connections>
Expand Down Expand Up @@ -136,6 +138,12 @@ CA
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="eu3-7i-yIM"/>
<menuItem title="Passnger Viewer" keyEquivalent="0" id="5PD-ck-IZ0">
<connections>
<action selector="openWindow:" target="Ady-hI-5gd" id="Q81-oA-zqE"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="1eh-kt-seb"/>
<menuItem title="Bring All to Front" id="LE2-aR-0XJ">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
Expand Down Expand Up @@ -163,11 +171,11 @@ CA
<outlet property="delegate" destination="Voe-Tx-rLC" id="PrD-fu-P6m"/>
</connections>
</application>
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="MacPassnger" customModuleProvider="target"/>
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="Passnger" customModuleProvider="target"/>
<customObject id="YLy-65-1bz" customClass="NSFontManager"/>
<customObject id="Ady-hI-5gd" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="75" y="0.0"/>
<point key="canvasLocation" x="134" y="27"/>
</scene>
</scenes>
</document>
4 changes: 2 additions & 2 deletions MacPassnger/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>LSApplicationCategoryType</key>
Expand All @@ -31,7 +31,7 @@
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>NSSupportsAutomaticTermination</key>
<true/>
<false/>
<key>NSSupportsSuddenTermination</key>
<true/>
</dict>
Expand Down
12 changes: 8 additions & 4 deletions Passnger.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions Passnger/Base.lproj/LaunchScreen.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16097.3" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17156" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16087"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17125"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand All @@ -19,13 +20,13 @@
<rect key="frame" x="83" y="328" width="248" height="250.5"/>
</imageView>
</subviews>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
<color key="backgroundColor" red="0.18428918720000001" green="0.1843278706" blue="0.18428674340000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="qHV-Ia-4O9" firstAttribute="centerY" secondItem="6Tk-OE-BBY" secondAttribute="centerY" id="8j9-9P-0p4"/>
<constraint firstItem="qHV-Ia-4O9" firstAttribute="centerX" secondItem="6Tk-OE-BBY" secondAttribute="centerX" id="zVp-LO-rzi"/>
<constraint firstItem="qHV-Ia-4O9" firstAttribute="width" secondItem="Ze5-6b-2t3" secondAttribute="width" multiplier="0.6" id="zpa-01-OkQ"/>
</constraints>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
Expand Down
2 changes: 1 addition & 1 deletion Passnger/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>LSRequiresIPhoneOS</key>
Expand Down

0 comments on commit fa7202d

Please sign in to comment.