Skip to content

Commit 8382a60

Browse files
authored
Merge pull request #1716 from pmarell/main
2 parents 71b8c5a + 51429f2 commit 8382a60

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

MonitorControl/Support/AppDelegate.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ import SimplyCoreAudio
1111
import Sparkle
1212

1313
class 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
}

MonitorControl/Support/MenuHandler.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class MenuHandler: NSMenu, NSMenuDelegate {
3333
if !dontClose {
3434
self.cancelTrackingWithoutAnimation()
3535
}
36-
app.statusItem.isVisible = prefs.integer(forKey: PrefKey.menuIcon.rawValue) == MenuIcon.show.rawValue ? true : false
36+
app.updateStatusItemVisibility(prefs.integer(forKey: PrefKey.menuIcon.rawValue) == MenuIcon.show.rawValue ? true : false)
3737
self.clearMenu()
3838
let currentDisplay = DisplayManager.shared.getCurrentDisplay()
3939
var displays: [Display] = []
@@ -185,7 +185,7 @@ class MenuHandler: NSMenu, NSMenuDelegate {
185185
self.addDisplayMenuBlock(addedSliderHandlers: addedSliderHandlers, blockName: display.readPrefAsString(key: .friendlyName) != "" ? display.readPrefAsString(key: .friendlyName) : display.name, monitorSubMenu: monitorSubMenu, numOfDisplays: numOfDisplays, asSubMenu: asSubMenu)
186186
}
187187
if addedSliderHandlers.count > 0, prefs.integer(forKey: PrefKey.menuIcon.rawValue) == MenuIcon.sliderOnly.rawValue {
188-
app.statusItem.isVisible = true
188+
app.updateStatusItemVisibility(true)
189189
}
190190
}
191191

MonitorControl/View Controllers/Preferences/MenuslidersPrefsViewController.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class MenuslidersPrefsViewController: NSViewController, SettingsPane {
8080
override func viewDidLoad() {
8181
super.viewDidLoad()
8282
self.populateSettings()
83+
prefs.addObserver(self, forKeyPath: PrefKey.menuIcon.rawValue, context: nil)
8384
}
8485

8586
func populateSettings() {
@@ -210,4 +211,12 @@ class MenuslidersPrefsViewController: NSViewController, SettingsPane {
210211
app.updateMenusAndKeys()
211212
self.updateGridLayout()
212213
}
214+
215+
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
216+
guard let object = object as? AnyObject else { return }
217+
if object === prefs, keyPath == PrefKey.menuIcon.rawValue {
218+
self.populateSettings()
219+
self.updateGridLayout()
220+
}
221+
}
213222
}

0 commit comments

Comments
 (0)