Skip to content

Commit

Permalink
Improved non-continous checks
Browse files Browse the repository at this point in the history
Improved non-continous checks
  • Loading branch information
AlexPerathoner authored Jan 12, 2023
2 parents 598a2d6 + abbd786 commit 4b7aeea
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 23 deletions.
1 change: 1 addition & 0 deletions Release_Notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 1.4.2 - Improved non-continous checks
* Show system HUDs after SlimHUD quit
* Fix: HUDs didn't always appear when pressing keys

# 1.4.1 - Fixes
* Add option to make inside bar flat or round
Expand Down
19 changes: 17 additions & 2 deletions SlimHUD.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
FA95EE8A295758B1005B39CD /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA95EE89295758B1005B39CD /* Constants.swift */; };
FAB5AA37295AF0FF00060CB6 /* SensorMethod.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAB5AA36295AF0FF00060CB6 /* SensorMethod.swift */; };
FAB5AA39295B031900060CB6 /* UserDefaultsExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAB5AA38295B031900060CB6 /* UserDefaultsExtension.swift */; };
FAD154E5296F8CFF00AC8BBB /* MediaKeyTap in Frameworks */ = {isa = PBXBuildFile; productRef = FAD154E4296F8CFF00AC8BBB /* MediaKeyTap */; };
FAFC579E29675F3E00C32A80 /* AppleScriptRunner.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAFC579D29675F3E00C32A80 /* AppleScriptRunner.swift */; };
FAFC57A029675F7700C32A80 /* AppleScriptError.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAFC579F29675F7700C32A80 /* AppleScriptError.swift */; };
FAFC57A22967668500C32A80 /* XCUIApplicationExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAFC57A12967668500C32A80 /* XCUIApplicationExtension.swift */; };
Expand Down Expand Up @@ -169,6 +170,7 @@
buildActionMask = 2147483647;
files = (
FA95EE242956FD3A005B39CD /* Sparkle in Frameworks */,
FAD154E5296F8CFF00AC8BBB /* MediaKeyTap in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -435,6 +437,7 @@
name = SlimHUD;
packageProductDependencies = (
FA95EE232956FD3A005B39CD /* Sparkle */,
FAD154E4296F8CFF00AC8BBB /* MediaKeyTap */,
);
productName = SlimHUD;
productReference = FA95EDF12956FCBF005B39CD /* SlimHUD.app */;
Expand Down Expand Up @@ -510,6 +513,7 @@
mainGroup = FA95EDE82956FCBF005B39CD;
packageReferences = (
FA95EE222956FD3A005B39CD /* XCRemoteSwiftPackageReference "Sparkle" */,
FAD154E3296F8CFF00AC8BBB /* XCRemoteSwiftPackageReference "MediaKeyTap" */,
);
productRefGroup = FA95EDF22956FCBF005B39CD /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -776,7 +780,6 @@
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = SlimHUD/Info.plist;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities";
INFOPLIST_KEY_LSUIElement = YES;
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2020 Alex Perathoner. All rights reserved.";
INFOPLIST_KEY_NSMainStoryboardFile = MainMenu;
INFOPLIST_KEY_NSPrincipalClass = SlimHUD.KeyPressObserver;
Expand Down Expand Up @@ -807,7 +810,6 @@
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = SlimHUD/Info.plist;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities";
INFOPLIST_KEY_LSUIElement = YES;
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2020 Alex Perathoner. All rights reserved.";
INFOPLIST_KEY_NSMainStoryboardFile = MainMenu;
INFOPLIST_KEY_NSPrincipalClass = SlimHUD.KeyPressObserver;
Expand Down Expand Up @@ -949,6 +951,14 @@
minimumVersion = 2.0.0;
};
};
FAD154E3296F8CFF00AC8BBB /* XCRemoteSwiftPackageReference "MediaKeyTap" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/AlexPerathoner/MediaKeyTap";
requirement = {
kind = revision;
revision = bd1402a89a0f0caa9b0a6cd0cc9b76fe55bf3f4e;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
Expand All @@ -957,6 +967,11 @@
package = FA95EE222956FD3A005B39CD /* XCRemoteSwiftPackageReference "Sparkle" */;
productName = Sparkle;
};
FAD154E4296F8CFF00AC8BBB /* MediaKeyTap */ = {
isa = XCSwiftPackageProductDependency;
package = FAD154E3296F8CFF00AC8BBB /* XCRemoteSwiftPackageReference "MediaKeyTap" */;
productName = MediaKeyTap;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = FA95EDE92956FCBF005B39CD /* Project object */;
Expand Down
1 change: 0 additions & 1 deletion SlimHUD/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import Sparkle

@NSApplicationMain
class AppDelegate: NSWindowController, NSApplicationDelegate {

let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)

var settingsWindowController: SettingsWindowController?
Expand Down
36 changes: 20 additions & 16 deletions SlimHUD/Services/ChangesObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Cocoa
class ChangesObserver {
private var oldFullScreen: Bool
private var oldVolume: Float
private var oldMuted: Bool
private var oldBrightness: Float = 0
private var oldKeyboard: Float = 0

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

do {
oldBrightness = try DisplayManager.getDisplayBrightness()
Expand Down Expand Up @@ -62,26 +64,24 @@ class ChangesObserver {
}

private func createObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(showVolumeHUD), name: KeyPressObserver.volumeChanged, object: nil)
DistributedNotificationCenter.default.addObserver(self,
selector: #selector(showVolumeHUD),
name: NSNotification.Name(rawValue: "com.apple.sound.settingsChangedNotification"),
object: nil)

NotificationCenter.default.addObserver(self,
selector: #selector(showVolumeHUD),
name: KeyPressObserver.volumeChanged,
object: nil)
// observers for brightness
NotificationCenter.default.addObserver(self,
selector: #selector(showBrightnessHUD),
name: KeyPressObserver.brightnessChanged,
object: nil)

// observers for keyboard backlight
NotificationCenter.default.addObserver(self,
selector: #selector(showKeyboardHUD),
name: KeyPressObserver.keyboardIlluminationChanged, object: nil)
DistributedNotificationCenter.default.addObserver(self,
selector: #selector(showVolumeHUD),
name: NSNotification.Name(rawValue: "com.apple.sound.settingsChangedNotification"),
object: nil)
name: KeyPressObserver.keyboardIlluminationChanged,
object: nil)
}

@objc func showVolumeHUD() {
Expand All @@ -101,14 +101,16 @@ class ChangesObserver {
positionManager.setupHUDsPosition(newFullScreen)
oldFullScreen = newFullScreen
}

if settingsManager.enabledBars.brightnessBar && !temporarelyDisabledBars.brightnessBar {
checkBrightnessChanges()
}
if settingsManager.enabledBars.keyboardBar && !temporarelyDisabledBars.keyboardBar {
checkKeyboardChanges()
if settingsManager.shouldContinuouslyCheck {
if settingsManager.enabledBars.brightnessBar && !temporarelyDisabledBars.brightnessBar {
checkBrightnessChanges()
}
if settingsManager.enabledBars.keyboardBar && !temporarelyDisabledBars.keyboardBar {
checkKeyboardChanges()
}
}
if settingsManager.shouldContinuouslyCheck && settingsManager.enabledBars.volumeBar {
// volume can't change on its own, so we always continuously check it
if settingsManager.enabledBars.volumeBar && settingsManager.enabledBars.volumeBar {
checkVolumeChanges()
}
}
Expand All @@ -120,10 +122,12 @@ class ChangesObserver {

private func checkVolumeChanges() {
let newVolume = VolumeManager.getOutputVolume()
let newMuted = VolumeManager.isMuted()
volumeView.bar!.progress = newVolume
if !isAlmost(firstNumber: oldVolume, secondNumber: newVolume) {
if !isAlmost(firstNumber: oldVolume, secondNumber: newVolume) || newMuted != oldMuted {
displayer.showVolumeHUD()
oldVolume = newVolume
oldMuted = newMuted
}
volumeView.bar!.progress = newVolume
}
Expand Down
28 changes: 25 additions & 3 deletions SlimHUD/Services/Displayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ class Displayer: HudsControllerInterface {
let muted = VolumeManager.isMuted()
let volumeView = getBarView(hud: volumeHud)
setColor(for: volumeView.bar!, muted)
if !settingsManager.shouldContinuouslyCheck {
volumeView.bar!.progress = VolumeManager.getOutputVolume()
}
volumeView.bar!.progress = VolumeManager.getOutputVolume()

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

func showBrightnessHUD() {
if !settingsManager.enabledBars.brightnessBar {return}
// if the function is being called because the key has been pressed, the display's brightness
// hasn't completely changed yet (or not at all). So for the next half a second, we continously check its value.
let timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { _ in
do {
self.getProgressBar(hud: self.brightnessHud).progress = try DisplayManager.getDisplayBrightness()
} catch {
NSLog("Failed to retrieve display brightness. See https://github.com/AlexPerathoner/SlimHUD/issues/60")
}
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
timer.invalidate()
}
brightnessHud.show()
volumeHud.hide(animated: false)
keyboardHud.hide(animated: false)
brightnessHud.dismiss(delay: 1.5)
}
func showKeyboardHUD() {
if !settingsManager.enabledBars.keyboardBar {return}
// if the function is being called because the key has been pressed, the keyboard's brightness
// hasn't completely changed yet (or not at all). So for the next half a second, we continously check its value.
let timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { _ in
do {
self.getProgressBar(hud: self.keyboardHud).progress = try KeyboardManager.getKeyboardBrightness()
} catch {
NSLog("Failed to retrieve display brightness. See https://github.com/AlexPerathoner/SlimHUD/issues/60")
}
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
timer.invalidate()
}
keyboardHud.show()
volumeHud.hide(animated: false)
brightnessHud.hide(animated: false)
Expand Down
24 changes: 23 additions & 1 deletion SlimHUD/Services/KeyPressObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
//

import Cocoa
import MediaKeyTap

class KeyPressObserver: NSApplication {

static let volumeChanged = Notification.Name("SlimHUD.volumeChanged")
static let brightnessChanged = Notification.Name("SlimHUD.brightnessChanged")
static let keyboardIlluminationChanged = Notification.Name("SlimHUD.keyboardIlluminationChanged")
Expand Down Expand Up @@ -45,4 +45,26 @@ class KeyPressObserver: NSApplication {
}
}
}

var mediaKeyTap: MediaKeyTap?

override func awakeFromNib() {
self.mediaKeyTap = MediaKeyTap(delegate: self, on: .keyDownAndUp)
self.mediaKeyTap?.start()
}
}

extension KeyPressObserver: MediaKeyTapDelegate {
func handle(mediaKey: MediaKey, event: KeyEvent?, modifiers: NSEvent.ModifierFlags?) {
switch mediaKey {
case .volumeUp, .volumeDown, .mute:
NotificationCenter.default.post(name: KeyPressObserver.volumeChanged, object: self)
case .brightnessDown, .brightnessUp:
NotificationCenter.default.post(name: KeyPressObserver.brightnessChanged, object: self)
case .keyboardBrightnessUp, .keyboardBrightnessDown, .keyboardBrightnessToggle:
NotificationCenter.default.post(name: KeyPressObserver.keyboardIlluminationChanged, object: self)
default:
return
}
}
}

0 comments on commit 4b7aeea

Please sign in to comment.