Skip to content

Commit 85f4293

Browse files
committed
🎉 v1.2
Many changes in commit… - Contrast slider is back ! - Added a Preferences Window - Function keys (should) work - If (like me) your screen backlight is still too bright even at 0, you can now set the contrast to 0 when the brightness hit 0 too - App can now be started at login (Needs to be tested) Signed-off-by: Guillaume Broder <[email protected]>
1 parent 087bb13 commit 85f4293

12 files changed

Lines changed: 186 additions & 146 deletions

File tree

MonitorControl.xcodeproj/project.pbxproj

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,13 @@
287287
TargetAttributes = {
288288
56754EAA1D9A4016007BCDC5 = {
289289
CreatedOnToolsVersion = 8.0;
290-
DevelopmentTeam = KGY56RWR9A;
290+
DevelopmentTeam = AZJ6KXSZ74;
291291
LastSwiftMigration = 0900;
292292
ProvisioningStyle = Automatic;
293293
};
294294
F06792E6200A73460066C438 = {
295295
CreatedOnToolsVersion = 9.2;
296-
DevelopmentTeam = KGY56RWR9A;
296+
DevelopmentTeam = AZJ6KXSZ74;
297297
ProvisioningStyle = Automatic;
298298
};
299299
};
@@ -363,13 +363,11 @@
363363
);
364364
inputPaths = (
365365
"${SRCROOT}/Pods/Target Support Files/Pods-MonitorControl/Pods-MonitorControl-frameworks.sh",
366-
"${BUILT_PRODUCTS_DIR}/HotKey/HotKey.framework",
367366
"${BUILT_PRODUCTS_DIR}/MASPreferences/MASPreferences.framework",
368367
"${BUILT_PRODUCTS_DIR}/MediaKeyTap/MediaKeyTap.framework",
369368
);
370369
name = "[CP] Embed Pods Frameworks";
371370
outputPaths = (
372-
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/HotKey.framework",
373371
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MASPreferences.framework",
374372
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MediaKeyTap.framework",
375373
);
@@ -614,10 +612,10 @@
614612
CODE_SIGN_IDENTITY = "Mac Developer";
615613
CODE_SIGN_STYLE = Automatic;
616614
COMBINE_HIDPI_IMAGES = YES;
617-
DEVELOPMENT_TEAM = KGY56RWR9A;
615+
DEVELOPMENT_TEAM = AZJ6KXSZ74;
618616
INFOPLIST_FILE = "$(SRCROOT)/MonitorControl/Info.plist";
619617
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
620-
PRODUCT_BUNDLE_IDENTIFIER = me.guillaumeb.MonitorControl;
618+
PRODUCT_BUNDLE_IDENTIFIER = me.guillaumeb.MonitorControlD;
621619
PRODUCT_NAME = "$(TARGET_NAME)";
622620
PROVISIONING_PROFILE_SPECIFIER = "";
623621
SWIFT_OBJC_BRIDGING_HEADER = "MonitorControl/Bridging-Header.h";
@@ -633,10 +631,10 @@
633631
CODE_SIGN_IDENTITY = "Mac Developer";
634632
CODE_SIGN_STYLE = Automatic;
635633
COMBINE_HIDPI_IMAGES = YES;
636-
DEVELOPMENT_TEAM = KGY56RWR9A;
634+
DEVELOPMENT_TEAM = AZJ6KXSZ74;
637635
INFOPLIST_FILE = "$(SRCROOT)/MonitorControl/Info.plist";
638636
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
639-
PRODUCT_BUNDLE_IDENTIFIER = me.guillaumeb.MonitorControl;
637+
PRODUCT_BUNDLE_IDENTIFIER = me.guillaumeb.MonitorControlD;
640638
PRODUCT_NAME = "$(TARGET_NAME)";
641639
PROVISIONING_PROFILE_SPECIFIER = "";
642640
SWIFT_OBJC_BRIDGING_HEADER = "MonitorControl/Bridging-Header.h";
@@ -655,7 +653,7 @@
655653
CODE_SIGN_IDENTITY = "Mac Developer";
656654
CODE_SIGN_STYLE = Automatic;
657655
COMBINE_HIDPI_IMAGES = YES;
658-
DEVELOPMENT_TEAM = KGY56RWR9A;
656+
DEVELOPMENT_TEAM = AZJ6KXSZ74;
659657
GCC_C_LANGUAGE_STANDARD = gnu11;
660658
INFOPLIST_FILE = MonitorControlHelper/Info.plist;
661659
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
@@ -678,7 +676,7 @@
678676
CODE_SIGN_IDENTITY = "Mac Developer";
679677
CODE_SIGN_STYLE = Automatic;
680678
COMBINE_HIDPI_IMAGES = YES;
681-
DEVELOPMENT_TEAM = KGY56RWR9A;
679+
DEVELOPMENT_TEAM = AZJ6KXSZ74;
682680
GCC_C_LANGUAGE_STANDARD = gnu11;
683681
INFOPLIST_FILE = MonitorControlHelper/Info.plist;
684682
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";

MonitorControl/AppDelegate.swift

Lines changed: 83 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,19 @@ class AppDelegate: NSObject, NSApplicationDelegate, MediaKeyTapDelegate {
3131
var mediaKeyTap: MediaKeyTap?
3232
var prefsController: NSWindowController?
3333

34+
var keysListenedFor: [MediaKey] = [.brightnessUp, .brightnessDown, .mute, .volumeUp, .volumeDown]
35+
3436
func applicationDidFinishLaunching(_ aNotification: Notification) {
3537
app = self
36-
mediaKeyTap = MediaKeyTap.init(delegate: self, forKeys: [.brightnessUp, .brightnessDown, .mute, .volumeUp, .volumeDown], observeBuiltIn: false)
38+
39+
let listenFor = prefs.integer(forKey: Utils.PrefKeys.listenFor.rawValue)
40+
if listenFor == Utils.ListenForKeys.brightnessOnlyKeys.rawValue {
41+
keysListenedFor.removeSubrange(2...4)
42+
} else if listenFor == Utils.ListenForKeys.volumeOnlyKeys.rawValue {
43+
keysListenedFor.removeSubrange(0...1)
44+
}
45+
46+
mediaKeyTap = MediaKeyTap.init(delegate: self, forKeys: keysListenedFor, observeBuiltIn: false)
3747
let storyboard: NSStoryboard = NSStoryboard.init(name: NSStoryboard.Name(rawValue: "Main"), bundle: Bundle.main)
3848
let views = [
3949
storyboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "MainPrefsVC")),
@@ -42,6 +52,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, MediaKeyTapDelegate {
4252
]
4353
prefsController = MASPreferencesWindowController(viewControllers: views, title: NSLocalizedString("Preferences", comment: "Shown in Preferences window"))
4454

55+
NotificationCenter.default.addObserver(self, selector: #selector(handleListenForChanged), name: NSNotification.Name.init(Utils.PrefKeys.listenFor.rawValue), object: nil)
56+
NotificationCenter.default.addObserver(self, selector: #selector(handleShowContrastChanged), name: NSNotification.Name.init(Utils.PrefKeys.showContrast.rawValue), object: nil)
57+
4558
statusItem.image = NSImage.init(named: NSImage.Name(rawValue: "status"))
4659
statusItem.menu = statusMenu
4760

@@ -65,6 +78,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, MediaKeyTapDelegate {
6578
@IBAction func prefsClicked(_ sender: AnyObject) {
6679
if let prefsController = prefsController {
6780
prefsController.showWindow(sender)
81+
NSApp.activate(ignoringOtherApps: true)
6882
prefsController.window?.makeKeyAndOrderFront(sender)
6983
}
7084
}
@@ -76,15 +90,24 @@ class AppDelegate: NSObject, NSApplicationDelegate, MediaKeyTapDelegate {
7690
prefs.set(true, forKey: Utils.PrefKeys.appAlreadyLaunched.rawValue)
7791

7892
prefs.set(false, forKey: Utils.PrefKeys.startAtLogin.rawValue)
79-
prefs.set(true, forKey: Utils.PrefKeys.startWhenExternal.rawValue)
93+
94+
prefs.set(false, forKey: Utils.PrefKeys.showContrast.rawValue)
95+
prefs.set(false, forKey: Utils.PrefKeys.lowerContrast.rawValue)
8096
}
8197
}
8298

8399
// MARK: - Menu
84100

85101
func clearDisplays() {
86-
for monitor in monitorItems {
87-
statusMenu.removeItem(monitor)
102+
if statusMenu.items.count > 2 {
103+
var items: [NSMenuItem] = []
104+
for i in 0..<statusMenu.items.count - 2 {
105+
items.append(statusMenu.items[i])
106+
}
107+
108+
for item in items {
109+
statusMenu.removeItem(item)
110+
}
88111
}
89112

90113
monitorItems = []
@@ -93,7 +116,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, MediaKeyTapDelegate {
93116

94117
func updateDisplays() {
95118
clearDisplays()
96-
sleep(1)
97119

98120
var filteredScreens = NSScreen.screens.filter { screen -> Bool in
99121
if let id = screen.deviceDescription[NSDeviceDescriptionKey.init("NSScreenNumber")] as? CGDirectDisplayID {
@@ -155,6 +177,14 @@ class AppDelegate: NSObject, NSApplicationDelegate, MediaKeyTapDelegate {
155177
forDisplay: display,
156178
command: BRIGHTNESS,
157179
title: NSLocalizedString("Brightness", comment: "Shown in menu"))
180+
if prefs.bool(forKey: Utils.PrefKeys.showContrast.rawValue) {
181+
let contrastSliderHandler = Utils.addSliderMenuItem(toMenu: monitorSubMenu,
182+
forDisplay: display,
183+
command: CONTRAST,
184+
title: NSLocalizedString("Contrast", comment: "Shown in menu"))
185+
display.contrastSliderHandler = contrastSliderHandler
186+
}
187+
158188
display.volumeSliderHandler = volumeSliderHandler
159189
display.brightnessSliderHandler = brightnessSliderHandler
160190
displays.append(display)
@@ -173,33 +203,57 @@ class AppDelegate: NSObject, NSApplicationDelegate, MediaKeyTapDelegate {
173203

174204
// MARK: - Media Key Tap delegate
175205

176-
func handle(mediaKey: MediaKey, event: KeyEvent) {
206+
func handle(mediaKey: MediaKey, event: KeyEvent?) {
177207
guard let currentDisplay = Utils.getCurrentDisplay(from: displays) else { return }
178-
var rel = 0
179-
180-
switch mediaKey {
181-
case .brightnessUp:
182-
rel = +self.step
183-
let value = currentDisplay.calcNewValue(for: BRIGHTNESS, withRel: rel)
184-
currentDisplay.setBrightness(to: value)
185-
case .brightnessDown:
186-
rel = -self.step
187-
let value = currentDisplay.calcNewValue(for: BRIGHTNESS, withRel: rel)
188-
currentDisplay.setBrightness(to: value)
189-
case .mute:
190-
currentDisplay.mute()
191-
case .volumeUp:
192-
rel = +self.step
193-
let value = currentDisplay.calcNewValue(for: AUDIO_SPEAKER_VOLUME, withRel: rel)
194-
currentDisplay.setVolume(to: value)
195-
case .volumeDown:
196-
rel = -self.step
197-
let value = currentDisplay.calcNewValue(for: AUDIO_SPEAKER_VOLUME, withRel: rel)
198-
currentDisplay.setVolume(to: value)
199-
default:
200-
return
208+
let allDisplays = prefs.bool(forKey: Utils.PrefKeys.allScreens.rawValue) ? displays : [currentDisplay]
209+
for display in allDisplays {
210+
var rel = 0
211+
if prefs.bool(forKey: "\(display.identifier)-state") {
212+
switch mediaKey {
213+
case .brightnessUp:
214+
rel = +self.step
215+
let value = display.calcNewValue(for: BRIGHTNESS, withRel: rel)
216+
display.setBrightness(to: value)
217+
case .brightnessDown:
218+
rel = -self.step
219+
let value = currentDisplay.calcNewValue(for: BRIGHTNESS, withRel: rel)
220+
display.setBrightness(to: value)
221+
case .mute:
222+
display.mute()
223+
case .volumeUp:
224+
rel = +self.step
225+
let value = display.calcNewValue(for: AUDIO_SPEAKER_VOLUME, withRel: rel)
226+
display.setVolume(to: value)
227+
case .volumeDown:
228+
rel = -self.step
229+
let value = display.calcNewValue(for: AUDIO_SPEAKER_VOLUME, withRel: rel)
230+
display.setVolume(to: value)
231+
default:
232+
return
233+
}
234+
}
235+
}
236+
237+
}
238+
239+
// MARK: - Prefs notification
240+
241+
@objc func handleListenForChanged() {
242+
let listenFor = prefs.integer(forKey: Utils.PrefKeys.listenFor.rawValue)
243+
keysListenedFor = [.brightnessUp, .brightnessDown, .mute, .volumeUp, .volumeDown]
244+
if listenFor == Utils.ListenForKeys.brightnessOnlyKeys.rawValue {
245+
keysListenedFor.removeSubrange(2...4)
246+
} else if listenFor == Utils.ListenForKeys.volumeOnlyKeys.rawValue {
247+
keysListenedFor.removeSubrange(0...1)
201248
}
202249

250+
mediaKeyTap?.stop()
251+
mediaKeyTap = MediaKeyTap.init(delegate: self, forKeys: keysListenedFor, observeBuiltIn: false)
252+
mediaKeyTap?.start()
253+
}
254+
255+
@objc func handleShowContrastChanged() {
256+
self.updateDisplays()
203257
}
204258

205259
}

0 commit comments

Comments
 (0)