Skip to content
This repository has been archived by the owner on Jul 5, 2019. It is now read-only.

Commit

Permalink
implement #3, add fallback for initial slider values
Browse files Browse the repository at this point in the history
- added a fallback when no initial value could be read from display. Instead we will read the last known value from prefs.
- implement better way to handle lowering contrast after brightness is 0, this way, contrast will slowly go down as the user keeps pressing brightness down until a default contrast value has been reached.
  • Loading branch information
JoniVR committed Feb 22, 2019
1 parent 22d22a4 commit 59620bb
Show file tree
Hide file tree
Showing 8 changed files with 276 additions and 265 deletions.
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ excluded:
- Pods
type_body_length: 500
file_length: 500
cyclomatic_complexity: 11
4 changes: 2 additions & 2 deletions MonitorControl.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@
files = (
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-MonitorControl/Pods-MonitorControl-frameworks.sh",
"${PODS_ROOT}/Target Support Files/Pods-MonitorControl/Pods-MonitorControl-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/AMCoreAudio/AMCoreAudio.framework",
"${BUILT_PRODUCTS_DIR}/MASPreferences/MASPreferences.framework",
"${BUILT_PRODUCTS_DIR}/MediaKeyTap/MediaKeyTap.framework",
Expand All @@ -372,7 +372,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-MonitorControl/Pods-MonitorControl-frameworks.sh\"\n";
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-MonitorControl/Pods-MonitorControl-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
C0EF20D28FC7408CBE89A686 /* [CP] Check Pods Manifest.lock */ = {
Expand Down
25 changes: 19 additions & 6 deletions MonitorControl/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,26 @@ extension AppDelegate: MediaKeyTapDelegate {
if (prefs.object(forKey: "\(display.identifier)-state") as? Bool) ?? true {
switch mediaKey {
case .brightnessUp:
let value = display.calcNewValue(for: BRIGHTNESS, withRel: +step)
display.setBrightness(to: value)
var brightnessValue = display.readValue(for: BRIGHTNESS)
var contrastValue = display.readValue(for: CONTRAST)
// increase brightness after contrast
if contrastValue < 70 && brightnessValue == 0 && prefs.bool(forKey: Utils.PrefKeys.lowerContrast.rawValue) {
contrastValue = display.calcNewValue(for: CONTRAST, withRel: +step)
display.setContrast(to: contrastValue)
} else {
brightnessValue = display.calcNewValue(for: BRIGHTNESS, withRel: +step)
}
display.setBrightness(to: brightnessValue)
case .brightnessDown:
let value = currentDisplay.calcNewValue(for: BRIGHTNESS, withRel: -step)
display.setBrightness(to: value)
var brightnessValue = display.readValue(for: BRIGHTNESS)
// lower contrast after brightness
if brightnessValue <= 0 && prefs.bool(forKey: Utils.PrefKeys.lowerContrast.rawValue) {
let contrastValue = display.calcNewValue(for: CONTRAST, withRel: -step)
display.setContrast(to: contrastValue)
} else {
brightnessValue = display.calcNewValue(for: BRIGHTNESS, withRel: -step)
}
display.setBrightness(to: brightnessValue)
case .mute:
display.mute()
case .volumeUp:
Expand All @@ -225,13 +240,11 @@ extension AppDelegate: MediaKeyTapDelegate {
case .volumeDown:
let value = display.calcNewValue(for: AUDIO_SPEAKER_VOLUME, withRel: -step)
display.setVolume(to: value)

default:
return
}
}
}

}

// MARK: - Prefs notification
Expand Down
2 changes: 1 addition & 1 deletion MonitorControl/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>1.3.1</string>
<key>CFBundleVersion</key>
<string>99</string>
<string>105</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
Expand Down
29 changes: 15 additions & 14 deletions MonitorControl/Objects/Display.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,6 @@ class Display {
}

func setBrightness(to value: Int) {
if prefs.bool(forKey: Utils.PrefKeys.lowerContrast.rawValue) {
if value == 0 {
Utils.sendCommand(CONTRAST, toMonitor: identifier, withValue: value)
if let slider = contrastSliderHandler?.slider {
slider.intValue = Int32(value)
}
} else if prefs.integer(forKey: "\(BRIGHTNESS)-\(identifier)") == 0 {
let contrastValue = prefs.integer(forKey: "\(CONTRAST)-\(identifier)")
Utils.sendCommand(CONTRAST, toMonitor: identifier, withValue: contrastValue)
}
}

Utils.sendCommand(BRIGHTNESS, toMonitor: identifier, withValue: value)
if let slider = brightnessSliderHandler?.slider {
slider.intValue = Int32(value)
Expand All @@ -78,15 +66,28 @@ class Display {
saveValue(value, for: BRIGHTNESS)
}

func calcNewValue(for command: Int32, withRel rel: Int) -> Int {
func setContrast(to value: Int) {
Utils.sendCommand(CONTRAST, toMonitor: identifier, withValue: value)
if let slider = contrastSliderHandler?.slider {
slider.intValue = Int32(value)
}
saveValue(value, for: CONTRAST)
}

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

func saveValue(_ value: Int, for command: Int32) {
prefs.set(value, forKey: "\(command)-\(identifier)")
}

func readValue(for command: Int32) -> Int {
return prefs.integer(forKey: "\(command)-\(identifier)")
}

private func showOsd(command: Int32, value: Int) {
if let manager = OSDManager.sharedManager() as? OSDManager {
var osdImage: Int64 = 1 // Brightness Image
Expand Down
1 change: 1 addition & 0 deletions MonitorControl/Util/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// Created by Joni Van Roost on 15/01/2019.
// Copyright © 2019 Mathew Kurian. All rights reserved.
// MIT Licensed.
//

import AppKit
Expand Down
Loading

0 comments on commit 59620bb

Please sign in to comment.