JoniVR / MonitorControl

Control your external monitor brightness & volume on your Mac
MIT License
46 stars 1 forks source link

Better way of lowering contrast after brightness #3

Open MHX792 opened 5 years ago

MHX792 commented 5 years ago

First of all, thank you very much for fixing the infinite mouse sluggishness!

As my external monitor is still too high on the lowest brightness level, I activated the option to lower the contrast after brightness is set to 0. But the contrast drop was way too high and I missed an option to incremently lower it.

So I changed the func handle(mediaKey: MediaKey, event: KeyEvent?) method like so:

case .brightnessUp:
    if !prefs.bool(forKey: Utils.PrefKeys.lowerContrast.rawValue) {
        // default brightness increasing
        let brightnessValue = display.calcNewValue(for: BRIGHTNESS, withRel: +step)
        display.setBrightness(to: brightnessValue)
    } else {
        // first increase contrast value until default value, then start increasing brightness value
        let contrastValue = currentDisplay.calcNewValue(for: CONTRAST, withRel: +step)
        if contrastValue <= display.defaultContrastValue {
            display.setContrast(to: contrastValue)
            display.setBrightness(to: 0)
        } else {
            display.setContrast(to: display.defaultContrastValue)
            let brightnessValue = display.calcNewValue(for: BRIGHTNESS, withRel: +step)
            display.setBrightness(to: brightnessValue)
        }
    }

case .brightnessDown:
    if !prefs.bool(forKey: Utils.PrefKeys.lowerContrast.rawValue) {
        // default brightness descreasing
        let brightnessValue = currentDisplay.calcNewValue(for: BRIGHTNESS, withRel: -step)
        display.setBrightness(to: brightnessValue)
    } else {
        // first decrease brightness until 0, then start decreasing contrast value
        let brightnessValue = currentDisplay.calcNewValue(for: BRIGHTNESS, withRel: -step)
        if brightnessValue >= 0 {
            display.setBrightness(to: brightnessValue)
            display.setContrast(to: display.defaultContrastValue)
        } else {
            display.setBrightness(to: 0)
            let contrastValue = currentDisplay.calcNewValue(for: CONTRAST, withRel: -step)
            display.setContrast(to: contrastValue)
        }
    }

In Display.swift I added a hard-coded default value for the contrast and a method for decreasing the contrast:

let defaultContrastValue = 70

func setContrast(to value: Int) {
    let contrastValue = value < 0 ? 0 : value
    Utils.sendCommand(CONTRAST, toMonitor: identifier, withValue: contrastValue)
    if let slider = contrastSliderHandler?.slider {
        slider.intValue = Int32(contrastValue)
    }
    saveValue(contrastValue, for: CONTRAST)
}

func setBrightness(to value: Int) {
    let brightnessValue = value < 0 ? 0 : value
    Utils.sendCommand(BRIGHTNESS, toMonitor: identifier, withValue: brightnessValue)
    if let slider = brightnessSliderHandler?.slider {
        slider.intValue = Int32(brightnessValue)
    }
    showOsd(command: BRIGHTNESS, value: brightnessValue)
    saveValue(brightnessValue, for: BRIGHTNESS)
}

For this to work, the calcNewValue method needs to be able to return negative values:

func calcNewValue(for command: Int32, withRel rel: Int) -> Int {
    let currentValue = prefs.integer(forKey: "\(command)-\(identifier)")
    // return max(0, min(100, currentValue + rel))
    return min(100, currentValue + rel)
}

Works like a charm and my eyes are happy at night.

Things to improve:

JoniVR commented 5 years ago

hi @MHX792

I'll look into your code when I get some time, the problem is that I don't have 2 monitors (only 1 external ultrawide) so I'm unable to test that part properly.

  • Maybe add an OSD for contrast or combine both into one like this:
screenshot 2019-01-26 at 11 57 44

I'm afraid that wouldn't be possible, since it's using the default OSD from Apple, which would be pretty hard to modify.


I'm currently a bit busy so it might take some time for me to look into this, just so you know. Also, if this is an issue with your monitor and not the other ones I won't fix it (obviously) because it simply depends on how well your monitor follows a certain standard.

Best regards Joni

MHX792 commented 5 years ago

Also, if this is an issue with your monitor and not the other ones I won't fix it (obviously) because it simply depends on how well your monitor follows a certain standard.

There is no issue with the app itself or hardware wise with my monitor. I just missed an option to finetune / further decrease the contrast after the brightness has reached level 0. :) The contrast drop was just way too high in my opinion.

I'm afraid that wouldn't be possible, since it's using the default OSD from Apple, which would be pretty hard to modify.

I saw it as a "nice to have" feature anyway. An alternative would be to create a custom OSD, which probably is not worth the effort.

JoniVR commented 5 years ago

I tested it myself without your code (I never actually used the contrast stuff) and I see what you mean, if you decrease the brightness further after it is 0, it just jumps to the lowest, right?

JoniVR commented 5 years ago

Your code works but the handle(mediaKey: MediaKey, event: KeyEvent?) function becomes way to complex, so I'll look into refactoring it to make it a bit cleaner before I can add this feature.

JoniVR commented 5 years ago

Hi, just letting you know, I've been added as a contributor in the previous fork the0neyouseek/MonitorControl and will thus be focussing on improving that one.