diff --git a/Maccy/AppDelegate.swift b/Maccy/AppDelegate.swift index f1806e7a..69b68d16 100644 --- a/Maccy/AppDelegate.swift +++ b/Maccy/AppDelegate.swift @@ -92,7 +92,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { panel = FloatingPanel( contentRect: NSRect(origin: .zero, size: Defaults[.windowSize]), - title: Bundle.main.bundleIdentifier ?? "org.p0deje.Maccy", + identifier: Bundle.main.bundleIdentifier ?? "org.p0deje.Maccy", statusBarButton: statusItem.button ) { ContentView() diff --git a/Maccy/FloatingPanel.swift b/Maccy/FloatingPanel.swift index ae00c073..9bf6bfd6 100644 --- a/Maccy/FloatingPanel.swift +++ b/Maccy/FloatingPanel.swift @@ -14,7 +14,7 @@ class FloatingPanel: NSPanel, NSWindowDelegate { init( contentRect: NSRect, - title: String = "", + identifier: String = "", statusBarButton: NSStatusBarButton? = nil, view: () -> Content ) { @@ -26,7 +26,7 @@ class FloatingPanel: NSPanel, NSWindowDelegate { ) self.statusBarButton = statusBarButton - self.title = title + self.identifier = NSUserInterfaceItemIdentifier(identifier) Defaults[.windowSize] = contentRect.size delegate = self @@ -45,7 +45,6 @@ class FloatingPanel: NSPanel, NSWindowDelegate { standardWindowButton(.miniaturizeButton)?.isHidden = true standardWindowButton(.zoomButton)?.isHidden = true - contentView = NSHostingView( rootView: view() // The safe area is ignored because the title bar still interferes with the geometry diff --git a/Maccy/Views/ContentView.swift b/Maccy/Views/ContentView.swift index df100bd6..9734e1c5 100644 --- a/Maccy/Views/ContentView.swift +++ b/Maccy/Views/ContentView.swift @@ -46,12 +46,16 @@ struct ContentView: View { .environment(\.scenePhase, scenePhase) // FloatingPanel is not a scene, so let's implement custom scenePhase.. .onReceive(NotificationCenter.default.publisher(for: NSWindow.didBecomeKeyNotification)) { - if ($0.object as? NSWindow)?.title == Bundle.main.bundleIdentifier { + if let window = $0.object as? NSWindow, + let bundleIdentifier = Bundle.main.bundleIdentifier, + window.identifier == NSUserInterfaceItemIdentifier(bundleIdentifier) { scenePhase = .active } } .onReceive(NotificationCenter.default.publisher(for: NSWindow.didResignKeyNotification)) { - if ($0.object as? NSWindow)?.title == Bundle.main.bundleIdentifier { + if let window = $0.object as? NSWindow, + let bundleIdentifier = Bundle.main.bundleIdentifier, + window.identifier == NSUserInterfaceItemIdentifier(bundleIdentifier) { scenePhase = .background } } diff --git a/MaccyTests/MenuFoooterTests.swift b/MaccyTests/MenuFoooterTests.swift deleted file mode 100644 index fe86813c..00000000 --- a/MaccyTests/MenuFoooterTests.swift +++ /dev/null @@ -1,103 +0,0 @@ -import XCTest -import Defaults -@testable import Maccy - -// swiftlint:disable force_cast -class MenuFooterTests: XCTestCase { - let savedShowFooter = Defaults[.showFooter] - - let expected: KeyValuePairs = [ - "separator": [ - "isAlternate": false, - "keyEquivalent": "", - "keyEquivalentModifierMask": NSEvent.ModifierFlags([]), - "tag": 100, - "title": "", - "tooltip": "" - ], - "clear": [ - "isAlternate": false, - "keyEquivalent": "⌫", - "keyEquivalentModifierMask": NSEvent.ModifierFlags([.command, .option]), - "tag": 101, - "title": "Clear", - "tooltip": "Clear unpinned items.\nSelect with ⇧ to clear all." - ], - "clear_all": [ - "isAlternate": true, - "keyEquivalent": "⌫", - "keyEquivalentModifierMask": NSEvent.ModifierFlags([.command, .option, .shift]), - "tag": 106, - "title": "Clear all", - "tooltip": "Clear all items." - ], - "preferences": [ - "isAlternate": false, - "keyEquivalent": ",", - "keyEquivalentModifierMask": NSEvent.ModifierFlags([.command]), - "tag": 107, - "title": "Preferences…", - "tooltip": "" - ], - "about": [ - "isAlternate": false, - "keyEquivalent": "", - "keyEquivalentModifierMask": NSEvent.ModifierFlags([]), - "tag": 103, - "title": "About", - "tooltip": "Read more about application." - ], - "quit": [ - "isAlternate": false, - "keyEquivalent": "q", - "keyEquivalentModifierMask": NSEvent.ModifierFlags([.command]), - "tag": 104, - "title": "Quit", - "tooltip": "Quit application." - ] - ] - - var actual: [NSMenuItem] { MenuFooter.allCases.map({ $0.menuItem }) } - - override func setUp() { - super.setUp() - Defaults[.showFooter] = true - } - - override func tearDown() { - super.tearDown() - Defaults[.showFooter] = savedShowFooter - } - - func testIsAlternate() { - XCTAssertEqual(actual.map({ $0.isAlternate }), expected.map({ $1["isAlternate"] as! Bool })) - } - - func testKeyEquivalent() { - XCTAssertEqual(actual.map({ $0.keyEquivalent }), expected.map({ $1["keyEquivalent"] as! String })) - } - - func testKeyEquivalentModifierMask() { - XCTAssertEqual(actual.map({ $0.keyEquivalentModifierMask }), - expected.map({ $1["keyEquivalentModifierMask"] as! NSEvent.ModifierFlags })) - } - - func testTag() { - XCTAssertEqual(actual.map({ $0.tag }), expected.map({ $1["tag"] as! Int })) - } - - func testTitle() { - XCTAssertEqual(actual.map({ $0.title }), expected.map({ $1["title"] as! String })) - } - - func testTooltip() { - XCTAssertEqual(actual.map({ $0.toolTip }), expected.map({ $1["tooltip"] as! String })) - } - - func testHiddenFooter() { - Defaults[.showFooter] = false - XCTAssertEqual(Set(actual.map({ $0.isAlternate })), [false]) - XCTAssertEqual(Set(actual.map({ $0.isHidden })), [true]) - } -} -// swiftlint:enable force_cast