Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macOS: Fix missing icon in menu bar #1243

Merged
merged 6 commits into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Packages/App/Sources/AppUIMain/Views/AppMenu/macOS/AppMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public struct AppMenu: View {
public var body: some View {
versionItem
Divider()
showToggle
showButton
loginToggle
keepToggle
Divider()
Expand All @@ -73,9 +73,18 @@ private extension AppMenu {
Text(BundleConfiguration.mainVersionString)
}

var showToggle: some View {
var showButton: some View {
Button(Strings.Global.Actions.show) {
settings.isVisible = true
Task {
let url = Bundle.main.bundleURL
let config = NSWorkspace.OpenConfiguration()
config.createsNewApplicationInstance = false
do {
try await NSWorkspace.shared.openApplication(at: url, configuration: config)
} catch {
pp_log(.app, .error, "Unable to reopen app: \(error)")
}
}
}
}

Expand Down
19 changes: 11 additions & 8 deletions Packages/App/Sources/CommonUtils/Views/AppWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public final class AppWindow {

public var isVisible: Bool {
get {
NSApp.activationPolicy() == .regular && window.isVisible
NSApp.activationPolicy() == .regular && window?.isVisible == true
}
set {
NSApp.setActivationPolicy(newValue ? .regular : .accessory)
if newValue {
NSApp.activate(ignoringOtherApps: true)
window.makeKeyAndOrderFront(self)
window?.makeKeyAndOrderFront(self)
} else {
window.close()
window?.close()
}
}
}
Expand All @@ -51,11 +51,14 @@ public final class AppWindow {
}

private extension AppWindow {
var window: NSWindow {
guard let window = NSApp.windows.first else {
fatalError("No Mac window?")
}
return window
var window: NSWindow? {
NSApp.windows.first(where: \.isWindow)
}
}

private extension NSWindow {
var isWindow: Bool {
!className.contains("NSStatusBar")
}
}

Expand Down
19 changes: 3 additions & 16 deletions Packages/App/Sources/UILibrary/Business/MacSettingsModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,18 @@ import ServiceManagement
public final class MacSettingsModel: ObservableObject {
private let defaults: UserDefaults

private let appWindow: AppWindow

private let appService: SMAppService

public var isStartedFromLoginItem: Bool {
NSApp.isHidden
}

public var isVisible: Bool {
get {
appWindow.isVisible
}
set {
appWindow.isVisible = newValue
objectWillChange.send()
}
}

public var launchesOnLogin: Bool {
get {
appService.status == .enabled
}
set {
objectWillChange.send()
do {
if newValue {
try appService.register()
Expand All @@ -67,7 +56,6 @@ public final class MacSettingsModel: ObservableObject {
} catch {
pp_log(.app, .error, "Unable to (un)register login item: \(error)")
}
objectWillChange.send()
}
}

Expand All @@ -76,14 +64,13 @@ public final class MacSettingsModel: ObservableObject {
defaults.bool(forKey: UIPreference.keepsInMenu.key)
}
set {
defaults.set(newValue, forKey: UIPreference.keepsInMenu.key)
objectWillChange.send()
defaults.set(newValue, forKey: UIPreference.keepsInMenu.key)
}
}

public init(defaults: UserDefaults, appWindow: AppWindow, loginItemId: String) {
public init(defaults: UserDefaults, loginItemId: String) {
self.defaults = defaults
self.appWindow = appWindow
appService = SMAppService.loginItem(identifier: loginItemId)
}
}
Expand Down
1 change: 0 additions & 1 deletion Passepartout/App/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ final class AppDelegate: NSObject {
#if os(macOS)
let settings = MacSettingsModel(
defaults: .standard,
appWindow: .shared,
loginItemId: BundleConfiguration.mainString(for: .loginItemId)
)
#endif
Expand Down
Loading