From 39f76e5058b92f02bb9012387223006de2ca3855 Mon Sep 17 00:00:00 2001 From: Robert Bressi Date: Mon, 30 Sep 2019 22:25:43 -0700 Subject: [PATCH] Fixed contrast slider for brightness change (#132) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- MonitorControl/Display.swift | 54 ++++++++++++++++++--------- MonitorControl/UI/SliderHandler.swift | 10 +++++ 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/MonitorControl/Display.swift b/MonitorControl/Display.swift index 3d8d6d1..c8b8c08 100644 --- a/MonitorControl/Display.swift +++ b/MonitorControl/Display.swift @@ -159,23 +159,8 @@ class Display { func setBrightness(to osdValue: Int) { let ddcValue = UInt16(osdValue) - if self.prefs.bool(forKey: Utils.PrefKeys.lowerContrast.rawValue) { - if ddcValue == 0 { - DispatchQueue.global(qos: .userInitiated).async { - _ = self.ddc?.write(command: .contrast, value: ddcValue) - } - - if let slider = contrastSliderHandler?.slider { - slider.intValue = Int32(ddcValue) - } - } else if self.getValue(for: DDC.Command.brightness) == 0 { - let contrastValue = self.getValue(for: DDC.Command.contrast) - - DispatchQueue.global(qos: .userInitiated).async { - _ = self.ddc?.write(command: .contrast, value: UInt16(contrastValue)) - } - } - } + // Set the contrast value according to the brightness, if necessary + self.setContrastValueForBrightness(osdValue) DispatchQueue.global(qos: .userInitiated).async { guard self.ddc?.write(command: .brightness, value: ddcValue) == true else { @@ -192,6 +177,33 @@ class Display { self.saveValue(osdValue, for: .brightness) } + func setContrastValueForBrightness(_ brightness: Int) { + var contrastValue: Int? + + if brightness == 0 { + contrastValue = 0 + + // Save the current DDC value for contrast so it can be restored, even across app restarts + if self.getRestoreValue(for: .contrast) == 0 { + self.setRestoreValue(self.getValue(for: .contrast), for: .contrast) + } + } else if self.getValue(for: .brightness) == 0, brightness > 0 { + contrastValue = self.getRestoreValue(for: .contrast) + } + + // Only write the new contrast value if lowering contrast after brightness is enabled + if contrastValue != nil, self.prefs.bool(forKey: Utils.PrefKeys.lowerContrast.rawValue) { + DispatchQueue.global(qos: .userInitiated).async { + _ = self.ddc?.write(command: .contrast, value: UInt16(contrastValue!)) + self.saveValue(contrastValue!, for: .contrast) + } + + if let slider = contrastSliderHandler?.slider { + slider.intValue = Int32(contrastValue!) + } + } + } + func readDDCValues(for command: DDC.Command, tries: UInt, minReplyDelay delay: UInt64?) -> (current: UInt16, max: UInt16)? { var values: (UInt16, UInt16)? @@ -259,6 +271,14 @@ class Display { return max == 0 ? 100 : max } + func getRestoreValue(for command: DDC.Command) -> Int { + return self.prefs.integer(forKey: "restore-\(command.rawValue)-\(self.identifier)") + } + + func setRestoreValue(_ value: Int?, for command: DDC.Command) { + self.prefs.set(value, forKey: "restore-\(command.rawValue)-\(self.identifier)") + } + func setFriendlyName(_ value: String) { self.prefs.set(value, forKey: "friendlyName-\(self.identifier)") } diff --git a/MonitorControl/UI/SliderHandler.swift b/MonitorControl/UI/SliderHandler.swift index 7a2bd0b..d5a8c59 100644 --- a/MonitorControl/UI/SliderHandler.swift +++ b/MonitorControl/UI/SliderHandler.swift @@ -28,6 +28,16 @@ class SliderHandler { self.display.toggleMute(fromVolumeSlider: true) } + // If the command is to adjust brightness, also instruct the display to set the contrast value, if necessary + if self.cmd == .brightness { + self.display.setContrastValueForBrightness(value) + } + + // If the command is to adjust contrast, erase the previous value for the contrast to restore after brightness is increased + if self.cmd == .contrast { + self.display.setRestoreValue(nil, for: .contrast) + } + _ = self.display.ddc?.write(command: self.cmd, value: UInt16(value)) self.display.saveValue(value, for: self.cmd) }