@@ -11,11 +11,17 @@ import SimplyCoreAudio
1111import Sparkle
1212
1313class AppDelegate : NSObject , NSApplicationDelegate {
14- let statusItem = NSStatusBar . system. statusItem ( withLength: NSStatusItem . variableLength)
14+ let statusItem : NSStatusItem = {
15+ let item = NSStatusBar . system. statusItem ( withLength: NSStatusItem . variableLength)
16+ item. behavior = . removalAllowed
17+ return item
18+ } ( )
1519 var mediaKeyTap = MediaKeyTapManager ( )
1620 var keyboardShortcuts = KeyboardShortcutsManager ( )
1721 let coreAudio = SimplyCoreAudio ( )
1822 var accessibilityObserver : NSObjectProtocol !
23+ var statusItemObserver : NSObjectProtocol !
24+ var statusItemVisibilityChangedByUser = true
1925 var reconfigureID : Int = 0 // dispatched reconfigure command ID
2026 var sleepID : Int = 0 // sleep event ID
2127 var safeMode = false
@@ -83,7 +89,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
8389 func applicationWillTerminate( _: Notification ) {
8490 os_log ( " Goodbye! " , type: . info)
8591 DisplayManager . shared. resetSwBrightnessForAllDisplays ( noPrefSave: true )
86- self . statusItem . isVisible = true
92+ self . updateStatusItemVisibility ( true )
8793 }
8894
8995 private func setPrefsBuildNumber( ) {
@@ -169,6 +175,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
169175 NSWorkspace . shared. notificationCenter. addObserver ( self , selector: #selector( self . sleepNotification) , name: NSWorkspace . willSleepNotification, object: nil )
170176 NSWorkspace . shared. notificationCenter. addObserver ( self , selector: #selector( self . wakeNotification) , name: NSWorkspace . didWakeNotification, object: nil )
171177 _ = DistributedNotificationCenter . default ( ) . addObserver ( forName: NSNotification . Name ( rawValue: NSNotification . Name. accessibilityApi. rawValue) , object: nil , queue: nil ) { _ in DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.1 ) { self . updateMediaKeyTap ( ) } } // listen for accessibility status changes
178+ self . statusItemObserver = statusItem. observe ( \. isVisible, options: [ . old, . new] ) { _, _ in self . statusItemVisibilityChanged ( ) }
172179 }
173180
174181 @objc private func sleepNotification( ) {
@@ -270,7 +277,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
270277 if let bundleID = Bundle . main. bundleIdentifier {
271278 prefs. removePersistentDomain ( forName: bundleID)
272279 }
273- app. statusItem . isVisible = true
280+ app. updateStatusItemVisibility ( true )
274281 self . setDefaultPrefs ( )
275282 self . checkPermissions ( )
276283 self . updateMediaKeyTap ( )
@@ -352,4 +359,16 @@ class AppDelegate: NSObject, NSApplicationDelegate {
352359 onboardingVc? . window? . center ( )
353360 NSApp . activate ( ignoringOtherApps: true )
354361 }
362+
363+ private func statusItemVisibilityChanged( ) {
364+ if !self . statusItem. isVisible, self . statusItemVisibilityChangedByUser {
365+ prefs. set ( MenuIcon . hide. rawValue, forKey: PrefKey . menuIcon. rawValue)
366+ }
367+ }
368+
369+ func updateStatusItemVisibility( _ visible: Bool ) {
370+ statusItemVisibilityChangedByUser = false
371+ statusItem. isVisible = visible
372+ statusItemVisibilityChangedByUser = true
373+ }
355374}
0 commit comments