@@ -32,7 +32,6 @@ class ExternalDisplay: Display {
3232 }
3333
3434 private var audioPlayer : AVAudioPlayer ?
35- private let osdChicletBoxes : Float = 16
3635
3736 override init ( _ identifier: CGDirectDisplayID , name: String , vendorNumber: UInt32 ? , modelNumber: UInt32 ? ) {
3837 super. init ( identifier, name: name, vendorNumber: vendorNumber, modelNumber: modelNumber)
@@ -90,7 +89,7 @@ class ExternalDisplay: Display {
9089
9190 if !fromVolumeSlider {
9291 self . hideDisplayOsd ( )
93- self . showOsd ( command: volumeOSDValue > 0 ? . audioSpeakerVolume : . audioMuteScreenBlank, value: volumeOSDValue)
92+ self . showOsd ( command: volumeOSDValue > 0 ? . audioSpeakerVolume : . audioMuteScreenBlank, value: volumeOSDValue, roundChiclet : true )
9493
9594 if volumeOSDValue > 0 {
9695 self . playVolumeChangedSound ( )
@@ -106,7 +105,6 @@ class ExternalDisplay: Display {
106105 var muteValue : Int ?
107106 let volumeOSDValue = self . calcNewValue ( for: . audioSpeakerVolume, isUp: isUp, isSmallIncrement: isSmallIncrement)
108107 let volumeDDCValue = UInt16 ( volumeOSDValue)
109-
110108 if self . isMuted ( ) , volumeOSDValue > 0 {
111109 muteValue = 2
112110 } else if !self . isMuted ( ) , volumeOSDValue == 0 {
@@ -132,7 +130,7 @@ class ExternalDisplay: Display {
132130 }
133131
134132 self . hideDisplayOsd ( )
135- self . showOsd ( command: . audioSpeakerVolume, value: volumeOSDValue)
133+ self . showOsd ( command: . audioSpeakerVolume, value: volumeOSDValue, roundChiclet : !isSmallIncrement )
136134
137135 if !isAlreadySet {
138136 self . saveValue ( volumeOSDValue, for: . audioSpeakerVolume)
@@ -163,7 +161,7 @@ class ExternalDisplay: Display {
163161 }
164162 }
165163
166- self . showOsd ( command: . brightness, value: osdValue)
164+ self . showOsd ( command: . brightness, value: osdValue, roundChiclet : !isSmallIncrement )
167165
168166 if !isAlreadySet {
169167 if let slider = self . brightnessSliderHandler? . slider {
@@ -221,25 +219,32 @@ class ExternalDisplay: Display {
221219 func calcNewValue( for command: DDC . Command , isUp: Bool , isSmallIncrement: Bool ) -> Int {
222220 let currentValue = self . getValue ( for: command)
223221 let nextValue : Int
222+ let maxValue = Float ( self . getMaxValue ( for: command) )
224223
225224 if isSmallIncrement {
226225 nextValue = currentValue + ( isUp ? 1 : - 1 )
227226 } else {
228- let filledChicletBoxes = self . osdChicletBoxes * ( Float ( currentValue) / Float( self . getMaxValue ( for: command) ) )
229-
230- var nextFilledChicletBoxes : Float
231- var filledChicletBoxesRel : Float = isUp ? 1 : - 1
232-
233- // This is a workaround to ensure that if the user has set the value using a small step (that is, the current chiclet box isn't completely filled,
234- // the next regular up or down step will only fill or empty that chiclet, and not the next one as well - it only really works because the max value is 100
235- if ( isUp && ceil ( filledChicletBoxes) - filledChicletBoxes > 0.15 ) || ( !isUp && filledChicletBoxes - floor( filledChicletBoxes) > 0.15 ) {
236- filledChicletBoxesRel = 0
227+ let osdChicletFromValue = OSDUtils . chiclet ( fromValue: Float ( currentValue) , maxValue: maxValue)
228+
229+ let distance = OSDUtils . getDistance ( fromNearestChiclet: osdChicletFromValue)
230+ // get the next rounded chiclet
231+ var nextFilledChiclet = isUp ? ceil ( osdChicletFromValue) : floor ( osdChicletFromValue)
232+
233+ // Depending on the direction, if the chiclet is above or below a certain threshold, we go to the next whole chiclet
234+ let distanceThreshold = Float ( 0.25 ) // 25% of the distance between the edges of an osd box
235+ if distance == 0 {
236+ nextFilledChiclet += isUp ? 1 : - 1
237+ } else if !isUp, distance < distanceThreshold {
238+ nextFilledChiclet -= 1
239+ } else if isUp, distance > ( 1 - distanceThreshold) {
240+ nextFilledChiclet += 1
237241 }
238242
239- nextFilledChicletBoxes = isUp ? ceil ( filledChicletBoxes + filledChicletBoxesRel) : floor ( filledChicletBoxes + filledChicletBoxesRel)
240- nextValue = Int ( Float ( self . getMaxValue ( for: command) ) * ( nextFilledChicletBoxes / self . osdChicletBoxes) )
243+ nextValue = Int ( round ( OSDUtils . value ( fromChiclet: nextFilledChiclet, maxValue: maxValue) ) )
244+
245+ os_log ( " next: .value %{public}@/%{public}@, .osd %{public}@/%{public}@ " , type: . debug, String ( nextValue) , String ( maxValue) , String ( nextFilledChiclet) , String ( OSDUtils . chicletCount) )
241246 }
242- return max ( 0 , min ( self . getMaxValue ( for: command) , Int ( nextValue) ) )
247+ return max ( 0 , min ( self . getMaxValue ( for: command) , nextValue) )
243248 }
244249
245250 func getValue( for command: DDC . Command ) -> Int {
@@ -308,11 +313,11 @@ class ExternalDisplay: Display {
308313 }
309314
310315 private func stepSize( for command: DDC . Command , isSmallIncrement: Bool ) -> Int {
311- return isSmallIncrement ? 1 : Int ( floor ( Float ( self . getMaxValue ( for: command) ) / self . osdChicletBoxes ) )
316+ return isSmallIncrement ? 1 : Int ( floor ( Float ( self . getMaxValue ( for: command) ) / OSDUtils . chicletCount ) )
312317 }
313318
314- override func showOsd( command: DDC . Command , value: Int , maxValue _: Int = 100 ) {
315- super. showOsd ( command: command, value: value, maxValue: self . getMaxValue ( for: command) )
319+ override func showOsd( command: DDC . Command , value: Int , maxValue _: Int = 100 , roundChiclet : Bool = false ) {
320+ super. showOsd ( command: command, value: value, maxValue: self . getMaxValue ( for: command) , roundChiclet : roundChiclet )
316321 }
317322
318323 private func supportsMuteCommand( ) -> Bool {
0 commit comments