Skip to content

Commit 31c313d

Browse files
authored
Add open sound/brightness preferences from media keys (#168)
1 parent afa3140 commit 31c313d

File tree

6 files changed

+32
-6
lines changed

6 files changed

+32
-6
lines changed

.swiftformat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--indent 2
2-
--ranges no-space
2+
--nospaceoperators
33
--self insert
44
--exponentcase lowercase
55
--exclude Carthage

MonitorControl/AppDelegate.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
6767
func clearDisplays() {
6868
if self.statusMenu.items.count > 2 {
6969
var items: [NSMenuItem] = []
70-
for i in 0..<self.statusMenu.items.count - 2 {
70+
for i in 0 ..< self.statusMenu.items.count - 2 {
7171
items.append(self.statusMenu.items[i])
7272
}
7373

@@ -192,12 +192,35 @@ class AppDelegate: NSObject, NSApplicationDelegate {
192192
}
193193
return nil
194194
}
195+
196+
func handleOpenPrefPane(mediaKey: MediaKey, event: KeyEvent?, modifiers: NSEvent.ModifierFlags?) -> Bool {
197+
guard let modifiers = modifiers else { return false }
198+
if !(modifiers.contains(.option) && !modifiers.contains(.shift)) {
199+
return false
200+
}
201+
if event?.keyRepeat == true {
202+
return false
203+
}
204+
switch mediaKey {
205+
case .brightnessUp, .brightnessDown:
206+
NSWorkspace.shared.open(URL(fileURLWithPath: "/System/Library/PreferencePanes/Displays.prefPane"))
207+
case .mute, .volumeUp, .volumeDown:
208+
NSWorkspace.shared.open(URL(fileURLWithPath: "/System/Library/PreferencePanes/Sound.prefPane"))
209+
default:
210+
return false
211+
}
212+
return true
213+
}
195214
}
196215

197216
// MARK: - Media Key Tap delegate
198217

199218
extension AppDelegate: MediaKeyTapDelegate {
200219
func handle(mediaKey: MediaKey, event: KeyEvent?, modifiers: NSEvent.ModifierFlags?) {
220+
if self.handleOpenPrefPane(mediaKey: mediaKey, event: event, modifiers: modifiers) {
221+
return
222+
}
223+
201224
let isSmallIncrement = modifiers?.isSuperset(of: NSEvent.ModifierFlags([.shift, .option])) ?? false
202225

203226
// control internal display when holding ctrl modifier
@@ -222,7 +245,10 @@ extension AppDelegate: MediaKeyTapDelegate {
222245
}
223246
mediaKeyTimer.invalidate()
224247
}
248+
self.sendDisplayCommand(mediaKey: mediaKey, isRepeat: isRepeat, isSmallIncrement: isSmallIncrement)
249+
}
225250

251+
private func sendDisplayCommand(mediaKey: MediaKey, isRepeat: Bool, isSmallIncrement: Bool) {
226252
let displays = DisplayManager.shared.getAllDisplays()
227253
guard let currentDisplay = DisplayManager.shared.getCurrentDisplay() else { return }
228254

MonitorControl/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<key>CFBundleShortVersionString</key>
2020
<string>$(MARKETING_VERSION)</string>
2121
<key>CFBundleVersion</key>
22-
<string>640</string>
22+
<string>642</string>
2323
<key>LSApplicationCategoryType</key>
2424
<string>public.app-category.utilities</string>
2525
<key>LSMinimumSystemVersion</key>

MonitorControl/Model/ExternalDisplay.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class ExternalDisplay: Display {
4646
return
4747
}
4848

49-
for _ in 0..<20 {
49+
for _ in 0 ..< 20 {
5050
_ = self.ddc?.write(command: .osd, value: UInt16(1), errorRecoveryWaitTime: 2000)
5151
}
5252
}

MonitorControlHelper/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<key>CFBundleShortVersionString</key>
2020
<string>$(MARKETING_VERSION)</string>
2121
<key>CFBundleVersion</key>
22-
<string>640</string>
22+
<string>642</string>
2323
<key>LSApplicationCategoryType</key>
2424
<string>public.app-category.utilities</string>
2525
<key>LSBackgroundOnly</key>

MonitorControlHelper/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
1111
}
1212

1313
let pathComponents = bundlePath.pathComponents
14-
let path = NSString.path(withComponents: Array(pathComponents[0..<(pathComponents.count - 4)]))
14+
let path = NSString.path(withComponents: Array(pathComponents[0 ..< (pathComponents.count - 4)]))
1515

1616
NSWorkspace.shared.launchApplication(path)
1717
NSApp.terminate(nil)

0 commit comments

Comments
 (0)