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

Fix keyboard hud not appearing #35

Merged
merged 8 commits into from
Dec 20, 2022
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
3 changes: 2 additions & 1 deletion Release_Notes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# 1.3.8
* Hiding MacOS' default HUD without modifying system configuration files.
* Fix bug: implemented new ways to retrieve brightness
* Fix bug: implemented new ways to retrieve brightness
* Fix bug: keyboard backlight hud working again
17 changes: 9 additions & 8 deletions SlimHUD.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
FA42646A2950C1CF0046CD2C /* Displayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA4264692950C1CF0046CD2C /* Displayer.swift */; };
FA48AE4B29521CC900A9E0E2 /* SensorError.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA48AE4A29521CC900A9E0E2 /* SensorError.swift */; };
FA51B9602469D4510095C8B7 /* AboutWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA51B95F2469D4510095C8B7 /* AboutWindowController.swift */; };
FA55B98C2423FE180071E6D9 /* NSImageExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA55B98B2423FE180071E6D9 /* NSImageExtension.swift */; };
FA55D1E8247D49DC005D3E00 /* LoginItemList.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA55D1E7247D49DC005D3E00 /* LoginItemList.swift */; };
FA5C77EC240B077E004E0937 /* BaseView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA5C77E9240B077E004E0937 /* BaseView.swift */; };
FA5C77ED240B077E004E0937 /* ProgressBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA5C77EA240B077E004E0937 /* ProgressBar.swift */; };
Expand All @@ -40,11 +39,12 @@
FAA2265D294E746D00E082CB /* OSDUIManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAA2265C294E746D00E082CB /* OSDUIManager.swift */; };
FAAFCD872458768E009E5E2E /* About.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FAAFCD862458768E009E5E2E /* About.storyboard */; };
FAAFCD8924588785009E5E2E /* AboutViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAAFCD8824588785009E5E2E /* AboutViewController.swift */; };
FABE1FDA23FEF17B009AD01F /* AppleScriptRunner.swift in Sources */ = {isa = PBXBuildFile; fileRef = FABE1FD923FEF17B009AD01F /* AppleScriptRunner.swift */; };
FABE1FDC23FEF17E009AD01F /* KeyPressObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = FABE1FDB23FEF17E009AD01F /* KeyPressObserver.swift */; };
FACB31042951ACAF00BFB792 /* SettingsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FACB31032951ACAF00BFB792 /* SettingsController.swift */; };
FACB310A2951ACE900BFB792 /* BarView.xib in Resources */ = {isa = PBXBuildFile; fileRef = FACB31082951ACE900BFB792 /* BarView.xib */; };
FACB310B2951ACE900BFB792 /* BarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FACB31092951ACE900BFB792 /* BarView.swift */; };
FACB31132951B00A00BFB792 /* AppleScriptRunner.swift in Sources */ = {isa = PBXBuildFile; fileRef = FACB31122951B00A00BFB792 /* AppleScriptRunner.swift */; };
FACB31152951B01400BFB792 /* NSImageExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = FACB31142951B01400BFB792 /* NSImageExtension.swift */; };
FACB31242951B96200BFB792 /* SlimHUDTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FACB31232951B96200BFB792 /* SlimHUDTests.swift */; };
FACB312C2951B9F000BFB792 /* PositionManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FACB312B2951B9F000BFB792 /* PositionManagerTests.swift */; };
FACD8CB02950971A00B1A786 /* UserDefaultsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = FACD8CAF2950971A00B1A786 /* UserDefaultsManager.swift */; };
Expand Down Expand Up @@ -80,7 +80,6 @@
FA4264692950C1CF0046CD2C /* Displayer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Displayer.swift; sourceTree = "<group>"; };
FA48AE4A29521CC900A9E0E2 /* SensorError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SensorError.swift; sourceTree = "<group>"; };
FA51B95F2469D4510095C8B7 /* AboutWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutWindowController.swift; sourceTree = "<group>"; };
FA55B98B2423FE180071E6D9 /* NSImageExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSImageExtension.swift; sourceTree = "<group>"; };
FA55D1E7247D49DC005D3E00 /* LoginItemList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = LoginItemList.swift; path = SlimHUD/Services/LoginItemList.swift; sourceTree = SOURCE_ROOT; };
FA5C77E9240B077E004E0937 /* BaseView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseView.swift; sourceTree = "<group>"; };
FA5C77EA240B077E004E0937 /* ProgressBar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProgressBar.swift; sourceTree = "<group>"; };
Expand All @@ -103,11 +102,12 @@
FAA2265C294E746D00E082CB /* OSDUIManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSDUIManager.swift; sourceTree = "<group>"; };
FAAFCD862458768E009E5E2E /* About.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = About.storyboard; sourceTree = "<group>"; };
FAAFCD8824588785009E5E2E /* AboutViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutViewController.swift; sourceTree = "<group>"; };
FABE1FD923FEF17B009AD01F /* AppleScriptRunner.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppleScriptRunner.swift; sourceTree = "<group>"; };
FABE1FDB23FEF17E009AD01F /* KeyPressObserver.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyPressObserver.swift; sourceTree = "<group>"; };
FACB31032951ACAF00BFB792 /* SettingsController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SettingsController.swift; sourceTree = "<group>"; };
FACB31082951ACE900BFB792 /* BarView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; name = BarView.xib; path = SlimHUD/Views/BarView/BarView.xib; sourceTree = SOURCE_ROOT; };
FACB31092951ACE900BFB792 /* BarView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = BarView.swift; path = SlimHUD/Views/BarView/BarView.swift; sourceTree = SOURCE_ROOT; };
FACB31122951B00A00BFB792 /* AppleScriptRunner.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppleScriptRunner.swift; sourceTree = "<group>"; };
FACB31142951B01400BFB792 /* NSImageExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSImageExtension.swift; sourceTree = "<group>"; };
FACB31212951B96200BFB792 /* SlimHUDTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SlimHUDTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
FACB31232951B96200BFB792 /* SlimHUDTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SlimHUDTests.swift; sourceTree = "<group>"; };
FACB312B2951B9F000BFB792 /* PositionManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PositionManagerTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -244,8 +244,8 @@
FACD8CB329509ED100B1A786 /* PositionManager.swift */,
FABE1FDB23FEF17E009AD01F /* KeyPressObserver.swift */,
FA4264662950BD850046CD2C /* ChangesObserver.swift */,
FABE1FD923FEF17B009AD01F /* AppleScriptRunner.swift */,
FAA1D1BA295073F4006F6936 /* DisplayManager.swift */,
FACB31122951B00A00BFB792 /* AppleScriptRunner.swift */,
FAA1D1C02950764D006F6936 /* KeyboardManager.swift */,
FAA1D1B52950724B006F6936 /* VolumeManager.swift */,
FA55D1E7247D49DC005D3E00 /* LoginItemList.swift */,
Expand All @@ -267,8 +267,8 @@
FAA1D19F29506E9F006F6936 /* Extensions */ = {
isa = PBXGroup;
children = (
FA55B98B2423FE180071E6D9 /* NSImageExtension.swift */,
FAA1D1AD29506F4F006F6936 /* NSSegmentedControlExtension.swift */,
FACB31142951B01400BFB792 /* NSImageExtension.swift */,
FAA1D1AA29506F25006F6936 /* NSViewExtension.swift */,
FAA1D1A729506F0E006F6936 /* BoolExtension.swift */,
FAA1D1A029506ED3006F6936 /* NSButtonExtension.swift */,
Expand Down Expand Up @@ -488,13 +488,12 @@
FA48AE4B29521CC900A9E0E2 /* SensorError.swift in Sources */,
FACD8CB02950971A00B1A786 /* UserDefaultsManager.swift in Sources */,
FA69AC8723F9E2E600AA73FD /* AppDelegate.swift in Sources */,
FABE1FDA23FEF17B009AD01F /* AppleScriptRunner.swift in Sources */,
FAA1D1C429507814006F6936 /* Position.swift in Sources */,
FAA1D1A829506F0E006F6936 /* BoolExtension.swift in Sources */,
FA5C77EC240B077E004E0937 /* BaseView.swift in Sources */,
FA12C21524EA93A500F92CA3 /* SettingsViewController+Position.swift in Sources */,
FA4264672950BD850046CD2C /* ChangesObserver.swift in Sources */,
FA55B98C2423FE180071E6D9 /* NSImageExtension.swift in Sources */,
FACB31152951B01400BFB792 /* NSImageExtension.swift in Sources */,
FA12C21324EA937800F92CA3 /* SettingsViewController+General.swift in Sources */,
FA12C21724EA93DF00F92CA3 /* SettingsViewController+Style.swift in Sources */,
FAF5F6532406D1480011B766 /* Hud.swift in Sources */,
Expand All @@ -512,6 +511,8 @@
FACD8CB429509ED100B1A786 /* PositionManager.swift in Sources */,
FAAFCD8924588785009E5E2E /* AboutViewController.swift in Sources */,
FA12C21124EA934800F92CA3 /* HudsControllerInterface.swift in Sources */,
FACB31132951B00A00BFB792 /* AppleScriptRunner.swift in Sources */,
FA78F53F242FA73000785678 /* SettingsController.swift in Sources */,
FAA1D1B62950724B006F6936 /* VolumeManager.swift in Sources */,
FAA1D1C729509317006F6936 /* EnabledBars.swift in Sources */,
);
Expand Down
2 changes: 1 addition & 1 deletion SlimHUD/Controllers/HudsControllerInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Cocoa

protocol HudsControllerInterface: class {
protocol HudsControllerInterface: AnyObject {
func updateShadows(enabled: Bool)
func updateIcons(isHidden: Bool)
func setupDefaultBarsColors()
Expand Down
32 changes: 23 additions & 9 deletions SlimHUD/Services/ChangesObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ class ChangesObserver {
private var oldFullScreen: Bool
private var oldVolume: Float
private var oldBrightness: Float
private var oldKeyboard: Float

var settingsManager: SettingsManager = SettingsManager.getInstance()
var positionManager: PositionManager
var displayer: Displayer
var volumeView: BarView
var brightnessView: BarView
var keyboardView: BarView
private var settingsManager: SettingsManager = SettingsManager.getInstance()
private var positionManager: PositionManager
private var displayer: Displayer
private var volumeView: BarView
private var brightnessView: BarView
private var keyboardView: BarView

init(positionManager: PositionManager, displayer: Displayer, volumeView: BarView, brightnessView: BarView, keyboardView: BarView) {
oldFullScreen = DisplayManager.isInFullscreenMode()
oldVolume = VolumeManager.getOutputVolume()
oldBrightness = DisplayManager.getDisplayBrightness()
oldKeyboard = KeyboardManager.getKeyboardBrightness()

self.positionManager = positionManager
self.displayer = displayer
Expand Down Expand Up @@ -88,17 +90,20 @@ class ChangesObserver {
if settingsManager.enabledBars.brightnessBar {
checkBrightnessChanges()
}
if settingsManager.enabledBars.keyboardBar {
checkKeyboardChanges()
}
if settingsManager.shouldContinuouslyCheck && settingsManager.enabledBars.volumeBar {
checkVolumeChanges()
}
}

func isAlmost(firstNumber: Float, secondNumber: Float) -> Bool { // used to partially prevent the bars to display when no user input happened
private func isAlmost(firstNumber: Float, secondNumber: Float) -> Bool { // used to partially prevent the bars to display when no user input happened
let marginValue = Float(settingsManager.marginValue) / 100.0
return (firstNumber + marginValue >= secondNumber && firstNumber - marginValue <= secondNumber)
}

func checkVolumeChanges() {
private func checkVolumeChanges() {
let newVolume = VolumeManager.getOutputVolume()
volumeView.bar!.progress = newVolume
if !isAlmost(firstNumber: oldVolume, secondNumber: newVolume) {
Expand All @@ -108,7 +113,7 @@ class ChangesObserver {
volumeView.bar!.progress = newVolume
}

func checkBrightnessChanges() {
private func checkBrightnessChanges() {
if NSScreen.screens.count == 0 {return}
let newBrightness = DisplayManager.getDisplayBrightness()
if !isAlmost(firstNumber: oldBrightness, secondNumber: newBrightness) {
Expand All @@ -117,4 +122,13 @@ class ChangesObserver {
}
brightnessView.bar?.progress = newBrightness
}

private func checkKeyboardChanges() {
let newKeyboard = KeyboardManager.getKeyboardBrightness()
if !isAlmost(firstNumber: oldKeyboard, secondNumber: newKeyboard) {
displayer.showKeyboardHUD()
oldKeyboard = newKeyboard
}
keyboardView.bar?.progress = KeyboardManager.getKeyboardBrightness()
}
}
6 changes: 3 additions & 3 deletions SlimHUD/Services/Displayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ class Displayer: HudsControllerInterface {

func showVolumeHUD() {
if !settingsManager.enabledBars.volumeBar {return}
let disabled = VolumeManager.isMuted()
let muted = VolumeManager.isMuted()
let volumeView = getBarView(hud: volumeHud)
setColor(for: volumeView.bar!, disabled)
setColor(for: volumeView.bar!, muted)
if !settingsManager.shouldContinuouslyCheck {
volumeView.bar!.progress = VolumeManager.getOutputVolume()
}

if disabled {
if muted {
volumeView.image!.image = NSImage(named: NSImage.NoVolumeImageFileName)
} else {
volumeView.image!.image = NSImage(named: NSImage.VolumeImageFileName)
Expand Down
15 changes: 12 additions & 3 deletions SlimHUD/Services/KeyboardManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import Foundation

class KeyboardManager {
private init() {}

private static let MaxKeyboardBrightness: Float = 342

private static var useM1KeyboardBrightnessMethod = false

Expand All @@ -18,26 +20,33 @@ class KeyboardManager {
return getM1KeyboardBrightness()
} else {
do {
return try getStandardKeyboardBrightness()
return try getKeyboardBrightnessProportioned(raw: getRawKeyboardBrightness())
} catch {
KeyboardManager.useM1KeyboardBrightnessMethod = true
return getM1KeyboardBrightness()
}
}
}

private static func getStandardKeyboardBrightness() throws -> Float {
// Raw value of sensor is non linear, correcting it
private static func getKeyboardBrightnessProportioned(raw: Float) -> Float {
if raw <= 0.07 { return 0 }
return (log10(raw+0.03)+1)
}

private static func getRawKeyboardBrightness() throws -> Float {
let service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleHIDKeyboardEventDriverV2"))
defer {
IOObjectRelease(service)
}

if let ser: CFTypeRef = IORegistryEntryCreateCFProperty(service, "KeyboardBacklightBrightness" as CFString, kCFAllocatorDefault, 0)?.takeUnretainedValue() {
let result = ser as! Float
return result / 342 // max value is 342, proportioning to %
return result / KeyboardManager.MaxKeyboardBrightness
}
throw SensorError.keyboardBrightnessFailure
}

private static func getM1KeyboardBrightness() -> Float {
let task = Process()
task.launchPath = "/usr/libexec/corebrightnessdiag"
Expand Down