@@ -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 }
0 commit comments