Skip to content

Commit 39f76e5

Browse files
robertbressiJoniVR
authored andcommitted
Fixed contrast slider for brightness change (MonitorControl#132)
- FIXED: Issue where changing brightness back from 0 wouldn’t restore previous contrast value - FIXED: Restoring saved contrast value across app restarts - FIXED: Issue where using the brightness slider wouldn’t adjust contrast - FIXED: Issue where setting brightness to 0 after it was already 0 wouldn’t restore the previous contrast setting
1 parent da91d76 commit 39f76e5

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

MonitorControl/Display.swift

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -159,23 +159,8 @@ class Display {
159159
func setBrightness(to osdValue: Int) {
160160
let ddcValue = UInt16(osdValue)
161161

162-
if self.prefs.bool(forKey: Utils.PrefKeys.lowerContrast.rawValue) {
163-
if ddcValue == 0 {
164-
DispatchQueue.global(qos: .userInitiated).async {
165-
_ = self.ddc?.write(command: .contrast, value: ddcValue)
166-
}
167-
168-
if let slider = contrastSliderHandler?.slider {
169-
slider.intValue = Int32(ddcValue)
170-
}
171-
} else if self.getValue(for: DDC.Command.brightness) == 0 {
172-
let contrastValue = self.getValue(for: DDC.Command.contrast)
173-
174-
DispatchQueue.global(qos: .userInitiated).async {
175-
_ = self.ddc?.write(command: .contrast, value: UInt16(contrastValue))
176-
}
177-
}
178-
}
162+
// Set the contrast value according to the brightness, if necessary
163+
self.setContrastValueForBrightness(osdValue)
179164

180165
DispatchQueue.global(qos: .userInitiated).async {
181166
guard self.ddc?.write(command: .brightness, value: ddcValue) == true else {
@@ -192,6 +177,33 @@ class Display {
192177
self.saveValue(osdValue, for: .brightness)
193178
}
194179

180+
func setContrastValueForBrightness(_ brightness: Int) {
181+
var contrastValue: Int?
182+
183+
if brightness == 0 {
184+
contrastValue = 0
185+
186+
// Save the current DDC value for contrast so it can be restored, even across app restarts
187+
if self.getRestoreValue(for: .contrast) == 0 {
188+
self.setRestoreValue(self.getValue(for: .contrast), for: .contrast)
189+
}
190+
} else if self.getValue(for: .brightness) == 0, brightness > 0 {
191+
contrastValue = self.getRestoreValue(for: .contrast)
192+
}
193+
194+
// Only write the new contrast value if lowering contrast after brightness is enabled
195+
if contrastValue != nil, self.prefs.bool(forKey: Utils.PrefKeys.lowerContrast.rawValue) {
196+
DispatchQueue.global(qos: .userInitiated).async {
197+
_ = self.ddc?.write(command: .contrast, value: UInt16(contrastValue!))
198+
self.saveValue(contrastValue!, for: .contrast)
199+
}
200+
201+
if let slider = contrastSliderHandler?.slider {
202+
slider.intValue = Int32(contrastValue!)
203+
}
204+
}
205+
}
206+
195207
func readDDCValues(for command: DDC.Command, tries: UInt, minReplyDelay delay: UInt64?) -> (current: UInt16, max: UInt16)? {
196208
var values: (UInt16, UInt16)?
197209

@@ -259,6 +271,14 @@ class Display {
259271
return max == 0 ? 100 : max
260272
}
261273

274+
func getRestoreValue(for command: DDC.Command) -> Int {
275+
return self.prefs.integer(forKey: "restore-\(command.rawValue)-\(self.identifier)")
276+
}
277+
278+
func setRestoreValue(_ value: Int?, for command: DDC.Command) {
279+
self.prefs.set(value, forKey: "restore-\(command.rawValue)-\(self.identifier)")
280+
}
281+
262282
func setFriendlyName(_ value: String) {
263283
self.prefs.set(value, forKey: "friendlyName-\(self.identifier)")
264284
}

MonitorControl/UI/SliderHandler.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ class SliderHandler {
2828
self.display.toggleMute(fromVolumeSlider: true)
2929
}
3030

31+
// If the command is to adjust brightness, also instruct the display to set the contrast value, if necessary
32+
if self.cmd == .brightness {
33+
self.display.setContrastValueForBrightness(value)
34+
}
35+
36+
// If the command is to adjust contrast, erase the previous value for the contrast to restore after brightness is increased
37+
if self.cmd == .contrast {
38+
self.display.setRestoreValue(nil, for: .contrast)
39+
}
40+
3141
_ = self.display.ddc?.write(command: self.cmd, value: UInt16(value))
3242
self.display.saveValue(value, for: self.cmd)
3343
}

0 commit comments

Comments
 (0)