Skip to content

Commit 4b7aeea

Browse files
Improved non-continous checks
Improved non-continous checks
2 parents 598a2d6 + abbd786 commit 4b7aeea

File tree

6 files changed

+86
-23
lines changed

6 files changed

+86
-23
lines changed

Release_Notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# 1.4.2 - Improved non-continous checks
22
* Show system HUDs after SlimHUD quit
3+
* Fix: HUDs didn't always appear when pressing keys
34

45
# 1.4.1 - Fixes
56
* Add option to make inside bar flat or round

SlimHUD.xcodeproj/project.pbxproj

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
FA95EE8A295758B1005B39CD /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA95EE89295758B1005B39CD /* Constants.swift */; };
7070
FAB5AA37295AF0FF00060CB6 /* SensorMethod.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAB5AA36295AF0FF00060CB6 /* SensorMethod.swift */; };
7171
FAB5AA39295B031900060CB6 /* UserDefaultsExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAB5AA38295B031900060CB6 /* UserDefaultsExtension.swift */; };
72+
FAD154E5296F8CFF00AC8BBB /* MediaKeyTap in Frameworks */ = {isa = PBXBuildFile; productRef = FAD154E4296F8CFF00AC8BBB /* MediaKeyTap */; };
7273
FAFC579E29675F3E00C32A80 /* AppleScriptRunner.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAFC579D29675F3E00C32A80 /* AppleScriptRunner.swift */; };
7374
FAFC57A029675F7700C32A80 /* AppleScriptError.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAFC579F29675F7700C32A80 /* AppleScriptError.swift */; };
7475
FAFC57A22967668500C32A80 /* XCUIApplicationExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAFC57A12967668500C32A80 /* XCUIApplicationExtension.swift */; };
@@ -169,6 +170,7 @@
169170
buildActionMask = 2147483647;
170171
files = (
171172
FA95EE242956FD3A005B39CD /* Sparkle in Frameworks */,
173+
FAD154E5296F8CFF00AC8BBB /* MediaKeyTap in Frameworks */,
172174
);
173175
runOnlyForDeploymentPostprocessing = 0;
174176
};
@@ -435,6 +437,7 @@
435437
name = SlimHUD;
436438
packageProductDependencies = (
437439
FA95EE232956FD3A005B39CD /* Sparkle */,
440+
FAD154E4296F8CFF00AC8BBB /* MediaKeyTap */,
438441
);
439442
productName = SlimHUD;
440443
productReference = FA95EDF12956FCBF005B39CD /* SlimHUD.app */;
@@ -510,6 +513,7 @@
510513
mainGroup = FA95EDE82956FCBF005B39CD;
511514
packageReferences = (
512515
FA95EE222956FD3A005B39CD /* XCRemoteSwiftPackageReference "Sparkle" */,
516+
FAD154E3296F8CFF00AC8BBB /* XCRemoteSwiftPackageReference "MediaKeyTap" */,
513517
);
514518
productRefGroup = FA95EDF22956FCBF005B39CD /* Products */;
515519
projectDirPath = "";
@@ -776,7 +780,6 @@
776780
GENERATE_INFOPLIST_FILE = YES;
777781
INFOPLIST_FILE = SlimHUD/Info.plist;
778782
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities";
779-
INFOPLIST_KEY_LSUIElement = YES;
780783
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2020 Alex Perathoner. All rights reserved.";
781784
INFOPLIST_KEY_NSMainStoryboardFile = MainMenu;
782785
INFOPLIST_KEY_NSPrincipalClass = SlimHUD.KeyPressObserver;
@@ -807,7 +810,6 @@
807810
GENERATE_INFOPLIST_FILE = YES;
808811
INFOPLIST_FILE = SlimHUD/Info.plist;
809812
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities";
810-
INFOPLIST_KEY_LSUIElement = YES;
811813
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2020 Alex Perathoner. All rights reserved.";
812814
INFOPLIST_KEY_NSMainStoryboardFile = MainMenu;
813815
INFOPLIST_KEY_NSPrincipalClass = SlimHUD.KeyPressObserver;
@@ -949,6 +951,14 @@
949951
minimumVersion = 2.0.0;
950952
};
951953
};
954+
FAD154E3296F8CFF00AC8BBB /* XCRemoteSwiftPackageReference "MediaKeyTap" */ = {
955+
isa = XCRemoteSwiftPackageReference;
956+
repositoryURL = "https://github.com/AlexPerathoner/MediaKeyTap";
957+
requirement = {
958+
kind = revision;
959+
revision = bd1402a89a0f0caa9b0a6cd0cc9b76fe55bf3f4e;
960+
};
961+
};
952962
/* End XCRemoteSwiftPackageReference section */
953963

954964
/* Begin XCSwiftPackageProductDependency section */
@@ -957,6 +967,11 @@
957967
package = FA95EE222956FD3A005B39CD /* XCRemoteSwiftPackageReference "Sparkle" */;
958968
productName = Sparkle;
959969
};
970+
FAD154E4296F8CFF00AC8BBB /* MediaKeyTap */ = {
971+
isa = XCSwiftPackageProductDependency;
972+
package = FAD154E3296F8CFF00AC8BBB /* XCRemoteSwiftPackageReference "MediaKeyTap" */;
973+
productName = MediaKeyTap;
974+
};
960975
/* End XCSwiftPackageProductDependency section */
961976
};
962977
rootObject = FA95EDE92956FCBF005B39CD /* Project object */;

SlimHUD/AppDelegate.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import Sparkle
1313

1414
@NSApplicationMain
1515
class AppDelegate: NSWindowController, NSApplicationDelegate {
16-
1716
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
1817

1918
var settingsWindowController: SettingsWindowController?

SlimHUD/Services/ChangesObserver.swift

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Cocoa
1111
class ChangesObserver {
1212
private var oldFullScreen: Bool
1313
private var oldVolume: Float
14+
private var oldMuted: Bool
1415
private var oldBrightness: Float = 0
1516
private var oldKeyboard: Float = 0
1617

@@ -26,6 +27,7 @@ class ChangesObserver {
2627
init(positionManager: PositionManager, displayer: Displayer, volumeView: BarView, brightnessView: BarView, keyboardView: BarView) {
2728
oldFullScreen = DisplayManager.isInFullscreenMode()
2829
oldVolume = VolumeManager.getOutputVolume()
30+
oldMuted = VolumeManager.isMuted()
2931

3032
do {
3133
oldBrightness = try DisplayManager.getDisplayBrightness()
@@ -62,26 +64,24 @@ class ChangesObserver {
6264
}
6365

6466
private func createObservers() {
65-
NotificationCenter.default.addObserver(self, selector: #selector(showVolumeHUD), name: KeyPressObserver.volumeChanged, object: nil)
6667
DistributedNotificationCenter.default.addObserver(self,
6768
selector: #selector(showVolumeHUD),
6869
name: NSNotification.Name(rawValue: "com.apple.sound.settingsChangedNotification"),
6970
object: nil)
70-
71+
NotificationCenter.default.addObserver(self,
72+
selector: #selector(showVolumeHUD),
73+
name: KeyPressObserver.volumeChanged,
74+
object: nil)
7175
// observers for brightness
7276
NotificationCenter.default.addObserver(self,
7377
selector: #selector(showBrightnessHUD),
7478
name: KeyPressObserver.brightnessChanged,
7579
object: nil)
76-
7780
// observers for keyboard backlight
7881
NotificationCenter.default.addObserver(self,
7982
selector: #selector(showKeyboardHUD),
80-
name: KeyPressObserver.keyboardIlluminationChanged, object: nil)
81-
DistributedNotificationCenter.default.addObserver(self,
82-
selector: #selector(showVolumeHUD),
83-
name: NSNotification.Name(rawValue: "com.apple.sound.settingsChangedNotification"),
84-
object: nil)
83+
name: KeyPressObserver.keyboardIlluminationChanged,
84+
object: nil)
8585
}
8686

8787
@objc func showVolumeHUD() {
@@ -101,14 +101,16 @@ class ChangesObserver {
101101
positionManager.setupHUDsPosition(newFullScreen)
102102
oldFullScreen = newFullScreen
103103
}
104-
105-
if settingsManager.enabledBars.brightnessBar && !temporarelyDisabledBars.brightnessBar {
106-
checkBrightnessChanges()
107-
}
108-
if settingsManager.enabledBars.keyboardBar && !temporarelyDisabledBars.keyboardBar {
109-
checkKeyboardChanges()
104+
if settingsManager.shouldContinuouslyCheck {
105+
if settingsManager.enabledBars.brightnessBar && !temporarelyDisabledBars.brightnessBar {
106+
checkBrightnessChanges()
107+
}
108+
if settingsManager.enabledBars.keyboardBar && !temporarelyDisabledBars.keyboardBar {
109+
checkKeyboardChanges()
110+
}
110111
}
111-
if settingsManager.shouldContinuouslyCheck && settingsManager.enabledBars.volumeBar {
112+
// volume can't change on its own, so we always continuously check it
113+
if settingsManager.enabledBars.volumeBar && settingsManager.enabledBars.volumeBar {
112114
checkVolumeChanges()
113115
}
114116
}
@@ -120,10 +122,12 @@ class ChangesObserver {
120122

121123
private func checkVolumeChanges() {
122124
let newVolume = VolumeManager.getOutputVolume()
125+
let newMuted = VolumeManager.isMuted()
123126
volumeView.bar!.progress = newVolume
124-
if !isAlmost(firstNumber: oldVolume, secondNumber: newVolume) {
127+
if !isAlmost(firstNumber: oldVolume, secondNumber: newVolume) || newMuted != oldMuted {
125128
displayer.showVolumeHUD()
126129
oldVolume = newVolume
130+
oldMuted = newMuted
127131
}
128132
volumeView.bar!.progress = newVolume
129133
}

SlimHUD/Services/Displayer.swift

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ class Displayer: HudsControllerInterface {
5454
let muted = VolumeManager.isMuted()
5555
let volumeView = getBarView(hud: volumeHud)
5656
setColor(for: volumeView.bar!, muted)
57-
if !settingsManager.shouldContinuouslyCheck {
58-
volumeView.bar!.progress = VolumeManager.getOutputVolume()
59-
}
57+
volumeView.bar!.progress = VolumeManager.getOutputVolume()
6058

6159
if muted {
6260
volumeView.image!.image = NSImage(named: NSImage.NoVolumeImageFileName)
@@ -71,13 +69,37 @@ class Displayer: HudsControllerInterface {
7169

7270
func showBrightnessHUD() {
7371
if !settingsManager.enabledBars.brightnessBar {return}
72+
// if the function is being called because the key has been pressed, the display's brightness
73+
// hasn't completely changed yet (or not at all). So for the next half a second, we continously check its value.
74+
let timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { _ in
75+
do {
76+
self.getProgressBar(hud: self.brightnessHud).progress = try DisplayManager.getDisplayBrightness()
77+
} catch {
78+
NSLog("Failed to retrieve display brightness. See https://github.com/AlexPerathoner/SlimHUD/issues/60")
79+
}
80+
}
81+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
82+
timer.invalidate()
83+
}
7484
brightnessHud.show()
7585
volumeHud.hide(animated: false)
7686
keyboardHud.hide(animated: false)
7787
brightnessHud.dismiss(delay: 1.5)
7888
}
7989
func showKeyboardHUD() {
8090
if !settingsManager.enabledBars.keyboardBar {return}
91+
// if the function is being called because the key has been pressed, the keyboard's brightness
92+
// hasn't completely changed yet (or not at all). So for the next half a second, we continously check its value.
93+
let timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { _ in
94+
do {
95+
self.getProgressBar(hud: self.keyboardHud).progress = try KeyboardManager.getKeyboardBrightness()
96+
} catch {
97+
NSLog("Failed to retrieve display brightness. See https://github.com/AlexPerathoner/SlimHUD/issues/60")
98+
}
99+
}
100+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
101+
timer.invalidate()
102+
}
81103
keyboardHud.show()
82104
volumeHud.hide(animated: false)
83105
brightnessHud.hide(animated: false)

SlimHUD/Services/KeyPressObserver.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
//
77

88
import Cocoa
9+
import MediaKeyTap
910

1011
class KeyPressObserver: NSApplication {
11-
1212
static let volumeChanged = Notification.Name("SlimHUD.volumeChanged")
1313
static let brightnessChanged = Notification.Name("SlimHUD.brightnessChanged")
1414
static let keyboardIlluminationChanged = Notification.Name("SlimHUD.keyboardIlluminationChanged")
@@ -45,4 +45,26 @@ class KeyPressObserver: NSApplication {
4545
}
4646
}
4747
}
48+
49+
var mediaKeyTap: MediaKeyTap?
50+
51+
override func awakeFromNib() {
52+
self.mediaKeyTap = MediaKeyTap(delegate: self, on: .keyDownAndUp)
53+
self.mediaKeyTap?.start()
54+
}
55+
}
56+
57+
extension KeyPressObserver: MediaKeyTapDelegate {
58+
func handle(mediaKey: MediaKey, event: KeyEvent?, modifiers: NSEvent.ModifierFlags?) {
59+
switch mediaKey {
60+
case .volumeUp, .volumeDown, .mute:
61+
NotificationCenter.default.post(name: KeyPressObserver.volumeChanged, object: self)
62+
case .brightnessDown, .brightnessUp:
63+
NotificationCenter.default.post(name: KeyPressObserver.brightnessChanged, object: self)
64+
case .keyboardBrightnessUp, .keyboardBrightnessDown, .keyboardBrightnessToggle:
65+
NotificationCenter.default.post(name: KeyPressObserver.keyboardIlluminationChanged, object: self)
66+
default:
67+
return
68+
}
69+
}
4870
}

0 commit comments

Comments
 (0)