Add open sound/brightness preferences from media keys (#168)

This commit is contained in:
paphonb 2020-05-31 23:06:30 +07:00 committed by GitHub
parent afa31400d8
commit 31c313d17b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 6 deletions

View file

@ -1,5 +1,5 @@
--indent 2 --indent 2
--ranges no-space --nospaceoperators
--self insert --self insert
--exponentcase lowercase --exponentcase lowercase
--exclude Carthage --exclude Carthage

View file

@ -67,7 +67,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func clearDisplays() { func clearDisplays() {
if self.statusMenu.items.count > 2 { if self.statusMenu.items.count > 2 {
var items: [NSMenuItem] = [] var items: [NSMenuItem] = []
for i in 0..<self.statusMenu.items.count - 2 { for i in 0 ..< self.statusMenu.items.count - 2 {
items.append(self.statusMenu.items[i]) items.append(self.statusMenu.items[i])
} }
@ -192,12 +192,35 @@ class AppDelegate: NSObject, NSApplicationDelegate {
} }
return nil return nil
} }
func handleOpenPrefPane(mediaKey: MediaKey, event: KeyEvent?, modifiers: NSEvent.ModifierFlags?) -> Bool {
guard let modifiers = modifiers else { return false }
if !(modifiers.contains(.option) && !modifiers.contains(.shift)) {
return false
}
if event?.keyRepeat == true {
return false
}
switch mediaKey {
case .brightnessUp, .brightnessDown:
NSWorkspace.shared.open(URL(fileURLWithPath: "/System/Library/PreferencePanes/Displays.prefPane"))
case .mute, .volumeUp, .volumeDown:
NSWorkspace.shared.open(URL(fileURLWithPath: "/System/Library/PreferencePanes/Sound.prefPane"))
default:
return false
}
return true
}
} }
// MARK: - Media Key Tap delegate // MARK: - Media Key Tap delegate
extension AppDelegate: MediaKeyTapDelegate { extension AppDelegate: MediaKeyTapDelegate {
func handle(mediaKey: MediaKey, event: KeyEvent?, modifiers: NSEvent.ModifierFlags?) { func handle(mediaKey: MediaKey, event: KeyEvent?, modifiers: NSEvent.ModifierFlags?) {
if self.handleOpenPrefPane(mediaKey: mediaKey, event: event, modifiers: modifiers) {
return
}
let isSmallIncrement = modifiers?.isSuperset(of: NSEvent.ModifierFlags([.shift, .option])) ?? false let isSmallIncrement = modifiers?.isSuperset(of: NSEvent.ModifierFlags([.shift, .option])) ?? false
// control internal display when holding ctrl modifier // control internal display when holding ctrl modifier
@ -222,7 +245,10 @@ extension AppDelegate: MediaKeyTapDelegate {
} }
mediaKeyTimer.invalidate() mediaKeyTimer.invalidate()
} }
self.sendDisplayCommand(mediaKey: mediaKey, isRepeat: isRepeat, isSmallIncrement: isSmallIncrement)
}
private func sendDisplayCommand(mediaKey: MediaKey, isRepeat: Bool, isSmallIncrement: Bool) {
let displays = DisplayManager.shared.getAllDisplays() let displays = DisplayManager.shared.getAllDisplays()
guard let currentDisplay = DisplayManager.shared.getCurrentDisplay() else { return } guard let currentDisplay = DisplayManager.shared.getCurrentDisplay() else { return }

View file

@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key> <key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string> <string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>640</string> <string>642</string>
<key>LSApplicationCategoryType</key> <key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string> <string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key> <key>LSMinimumSystemVersion</key>

View file

@ -46,7 +46,7 @@ class ExternalDisplay: Display {
return return
} }
for _ in 0..<20 { for _ in 0 ..< 20 {
_ = self.ddc?.write(command: .osd, value: UInt16(1), errorRecoveryWaitTime: 2000) _ = self.ddc?.write(command: .osd, value: UInt16(1), errorRecoveryWaitTime: 2000)
} }
} }

View file

@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key> <key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string> <string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>640</string> <string>642</string>
<key>LSApplicationCategoryType</key> <key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string> <string>public.app-category.utilities</string>
<key>LSBackgroundOnly</key> <key>LSBackgroundOnly</key>

View file

@ -11,7 +11,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
} }
let pathComponents = bundlePath.pathComponents let pathComponents = bundlePath.pathComponents
let path = NSString.path(withComponents: Array(pathComponents[0..<(pathComponents.count - 4)])) let path = NSString.path(withComponents: Array(pathComponents[0 ..< (pathComponents.count - 4)]))
NSWorkspace.shared.launchApplication(path) NSWorkspace.shared.launchApplication(path)
NSApp.terminate(nil) NSApp.terminate(nil)