Skip to content

Commit

Permalink
Implement charts for all adaptive modes
Browse files Browse the repository at this point in the history
  • Loading branch information
alin23 committed Mar 16, 2019
1 parent 9a1aec8 commit beb9352
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 82 deletions.
10 changes: 6 additions & 4 deletions Lunar/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import Magnet
import ServiceManagement
import WAYWindow

let TEST_MODE = false

var lunarDisplayNames = [
"Moony",
"Celestial",
Expand Down Expand Up @@ -213,9 +215,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, CLLocationManagerDelegate, N
activity.tolerance = 10
brightnessAdapter.adaptBrightness()
activity.schedule { completion in
let displayIDs = brightnessAdapter.displays.values.map({ $0.objectID })
let displayIDs = brightnessAdapter.displays.values.map { $0.objectID }
do {
let displays = try displayIDs.map({ id in try datastore.context.existingObject(with: id) as! Display })
let displays = try displayIDs.map { id in try datastore.context.existingObject(with: id) as! Display }
brightnessAdapter.adaptBrightness(for: displays)
} catch {
log.error("Error on fetching Displays by IDs")
Expand All @@ -231,9 +233,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, CLLocationManagerDelegate, N
if let builtinBrightness = brightnessAdapter.getBuiltinDisplayBrightness(),
brightnessAdapter.lastBuiltinBrightness != builtinBrightness {
brightnessAdapter.lastBuiltinBrightness = builtinBrightness
let displayIDs = brightnessAdapter.displays.values.map({ $0.objectID })
let displayIDs = brightnessAdapter.displays.values.map { $0.objectID }
do {
let displays = try displayIDs.map({ id in try datastore.context.existingObject(with: id) as! Display })
let displays = try displayIDs.map { id in try datastore.context.existingObject(with: id) as! Display }
brightnessAdapter.adaptBrightness(for: displays, percent: builtinBrightness)
} catch {
log.error("Error on fetching Displays by IDs")
Expand Down
2 changes: 1 addition & 1 deletion Lunar/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMinY="YES" flexibleMaxY="YES"/>
<subviews>
<customView fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="ycx-jo-u8n" customClass="BrightnessContrastChartView" customModule="Lunar" customModuleProvider="target">
<rect key="frame" x="-40" y="-10" width="800" height="190"/>
<rect key="frame" x="-25" y="-10" width="800" height="190"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
</customView>
<imageView focusRingType="none" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="gjE-bb-i6q" customClass="DisplayView" customModule="Lunar" customModuleProvider="target">
Expand Down
83 changes: 40 additions & 43 deletions Lunar/BrightnessAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class BrightnessAdapter {
var firstDisplay: Display {
if !displays.isEmpty {
return displays.values.first(where: { d in d.active }) ?? displays.values.first!
} else if TEST_MODE {
return TEST_DISPLAY
} else {
return GENERIC_DISPLAY
}
Expand Down Expand Up @@ -115,11 +117,11 @@ class BrightnessAdapter {
private static func getDisplays() -> [CGDirectDisplayID: Display] {
var displays: [CGDirectDisplayID: Display]
let displayIDs = Set(DDC.findExternalDisplays())
var serialsAndNames = displayIDs.enumerated().map({ i, id in (DDC.getDisplaySerial(for: id).stripped, lunarDisplayNames[i % lunarDisplayNames.count]) })
var serials = serialsAndNames.map({ d in d.0 })
var serialsAndNames = displayIDs.enumerated().map { i, id in (DDC.getDisplaySerial(for: id).stripped, lunarDisplayNames[i % lunarDisplayNames.count]) }
var serials = serialsAndNames.map { d in d.0 }
if serials.count != Set(serials).count {
serials = zip(serials, displayIDs).map({ serial, id in "\(serial)-\(id)" })
serialsAndNames = zip(serialsAndNames, serials).map({ d, serial in (serial, d.1) })
serials = zip(serials, displayIDs).map { serial, id in "\(serial)-\(id)" }
serialsAndNames = zip(serialsAndNames, serials).map { d, serial in (serial, d.1) }
}
let displaySerialIDMapping = Dictionary(uniqueKeysWithValues: zip(serials, displayIDs))
let displaySerialNameMapping = Dictionary(uniqueKeysWithValues: serialsAndNames)
Expand Down Expand Up @@ -149,16 +151,16 @@ class BrightnessAdapter {
}

datastore.save()
BrightnessAdapter.logDisplays(displays.values.map({ d in d }))
BrightnessAdapter.logDisplays(displays.values.map { d in d })
return displays
} catch {
log.error("Error on fetching displays: \(error)")
displays = Dictionary(uniqueKeysWithValues: displayIDs.map { id in (id, Display(id: id, active: true)) })
displays.values.forEach({ $0.addObservers() })
displays.values.forEach { $0.addObservers() }
}

datastore.save()
BrightnessAdapter.logDisplays(displays.values.map({ d in d }))
BrightnessAdapter.logDisplays(displays.values.map { d in d })
return displays
}

Expand Down Expand Up @@ -215,7 +217,7 @@ class BrightnessAdapter {
}

func listenForRunningApps() {
let appNames = NSWorkspace.shared.runningApplications.map({ app in app.bundleIdentifier ?? "" })
let appNames = NSWorkspace.shared.runningApplications.map { app in app.bundleIdentifier ?? "" }
runningAppExceptions = (try? datastore.fetchAppExceptions(by: appNames)) ?? []
for app in runningAppExceptions {
app.addObservers()
Expand All @@ -224,8 +226,8 @@ class BrightnessAdapter {
adaptBrightness()

appObserver = NSWorkspace.shared.observe(\.runningApplications, options: [.old, .new], changeHandler: { _, change in
let oldAppNames = change.oldValue?.map({ app in app.bundleIdentifier ?? "" })
let newAppNames = change.newValue?.map({ app in app.bundleIdentifier ?? "" })
let oldAppNames = change.oldValue?.map { app in app.bundleIdentifier ?? "" }
let newAppNames = change.newValue?.map { app in app.bundleIdentifier ?? "" }
do {
if let names = newAppNames {
self.runningAppExceptions.append(contentsOf: try datastore.fetchAppExceptions(by: names))
Expand Down Expand Up @@ -323,79 +325,74 @@ class BrightnessAdapter {
)
}

func computeBrightnessFromPercent(percent: Double, for display: Display, appOffset: Int = 0) -> NSNumber {
return display.computeBrightness(from: percent, appOffset: appOffset)
func computeManualValueFromPercent(percent: Int8, key: String, minVal: Int? = nil, maxVal: Int? = nil) -> NSNumber {
let percent = Double(min(max(percent, 0), 100)) / 100.0
let minVal = minVal ?? datastore.defaults.integer(forKey: "\(key)LimitMin")
let maxVal = maxVal ?? datastore.defaults.integer(forKey: "\(key)LimitMax")
let value = Int(round(percent * Double(maxVal - minVal))) + minVal
return NSNumber(value: cap(value, minVal: minVal, maxVal: maxVal))
}

func setBrightnessPercent(value: Int8, for displays: [Display]? = nil) {
let percent = Double(min(max(value, 0), 100)) / 100.0
let minVal = datastore.defaults.brightnessLimitMin
let maxVal = datastore.defaults.brightnessLimitMax
let brightness = Int(round(percent * Double(maxVal - minVal))) + minVal
let nsBrightness = NSNumber(value: cap(brightness, minVal: minVal, maxVal: maxVal))

let brightness = computeManualValueFromPercent(percent: value, key: "brightness")
if let displays = displays {
displays.forEach({ display in display.brightness = nsBrightness })
displays.forEach { display in display.brightness = brightness }
} else {
self.displays.values.forEach({ display in display.brightness = nsBrightness })
self.displays.values.forEach { display in display.brightness = brightness }
}
}

func computeContrastFromPercent(percent: Double, for display: Display, appOffset: Int = 0) -> NSNumber {
return display.computeContrast(from: percent, appOffset: appOffset)
}

func setContrastPercent(value: Int8, for displays: [Display]? = nil) {
let percent = Double(min(max(value, 0), 100)) / 100.0
let minVal = datastore.defaults.contrastLimitMin
let maxVal = datastore.defaults.contrastLimitMax
let contrast = Int(round(percent * Double(maxVal - minVal))) + minVal
let nsContrast = NSNumber(value: cap(contrast, minVal: minVal, maxVal: maxVal))
let contrast = computeManualValueFromPercent(percent: value, key: "contrast")

if let displays = displays {
displays.forEach({ display in display.contrast = nsContrast })
displays.forEach { display in display.contrast = contrast }
} else {
self.displays.values.forEach({ display in display.contrast = nsContrast })
self.displays.values.forEach { display in display.contrast = contrast }
}
}

func setBrightness(brightness: NSNumber, for displays: [Display]? = nil) {
if let displays = displays {
displays.forEach({ display in display.brightness = brightness })
displays.forEach { display in display.brightness = brightness }
} else {
self.displays.values.forEach({ display in display.brightness = brightness })
self.displays.values.forEach { display in display.brightness = brightness }
}
}

func setContrast(contrast: NSNumber, for displays: [Display]? = nil) {
if let displays = displays {
displays.forEach({ display in display.contrast = contrast })
displays.forEach { display in display.contrast = contrast }
} else {
self.displays.values.forEach({ display in display.contrast = contrast })
self.displays.values.forEach { display in display.contrast = contrast }
}
}

func adjustBrightness(by offset: Int8, for displays: [Display]? = nil) {
if let displays = displays {
displays.forEach({ display in
displays.forEach { display in
let value = cap(display.brightness.int8Value + offset, minVal: 0, maxVal: 100)
display.brightness = NSNumber(value: value) })
display.brightness = NSNumber(value: value)
}
} else {
self.displays.values.forEach({ display in
self.displays.values.forEach { display in
let value = cap(display.brightness.int8Value + offset, minVal: 0, maxVal: 100)
display.brightness = NSNumber(value: value) })
display.brightness = NSNumber(value: value)
}
}
}

func adjustContrast(by offset: Int8, for displays: [Display]? = nil) {
if let displays = displays {
displays.forEach({ display in
displays.forEach { display in
let value = cap(display.contrast.int8Value + offset, minVal: 0, maxVal: 100)
display.contrast = NSNumber(value: value) })
display.contrast = NSNumber(value: value)
}
} else {
self.displays.values.forEach({ display in
self.displays.values.forEach { display in
let value = cap(display.contrast.int8Value + offset, minVal: 0, maxVal: 100)
display.contrast = NSNumber(value: value) })
display.contrast = NSNumber(value: value)
}
}
}
}
13 changes: 12 additions & 1 deletion Lunar/Controllers/DisplayViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ class DisplayViewController: NSViewController {
func updateDataset(minBrightness: UInt8? = nil, maxBrightness: UInt8? = nil, minContrast: UInt8? = nil, maxContrast: UInt8? = nil) {
var brightnessChartEntry = brightnessContrastChart.brightnessGraph.values
var contrastChartEntry = brightnessContrastChart.contrastGraph.values
let maxValues = brightnessContrastChart.maxValues

switch brightnessAdapter.mode {
case .location:
let maxValues = brightnessContrastChart.maxValuesLocation
for x in 0 ..< (maxValues - 1) {
let (brightness, contrast) = brightnessAdapter.getBrightnessContrast(
for: display,
Expand All @@ -68,6 +68,17 @@ class DisplayViewController: NSViewController {
}
brightnessChartEntry[maxValues - 1].y = brightnessChartEntry[0].y
contrastChartEntry[maxValues - 1].y = contrastChartEntry[0].y
case .sync:
let maxValues = brightnessContrastChart.maxValuesSync
let minBrightness = minBrightness != nil ? Double(minBrightness!) : nil
let maxBrightness = maxBrightness != nil ? Double(maxBrightness!) : nil
let minContrast = minContrast != nil ? Double(minContrast!) : nil
let maxContrast = maxContrast != nil ? Double(maxContrast!) : nil
for x in 0 ..< (maxValues - 1) {
let percent = Double(x)
brightnessChartEntry[x].y = display.computeBrightness(from: percent, minVal: minBrightness, maxVal: maxBrightness).doubleValue
contrastChartEntry[x].y = display.computeContrast(from: percent, minVal: minContrast, maxVal: maxContrast).doubleValue
}
default:
break
}
Expand Down
7 changes: 6 additions & 1 deletion Lunar/Controllers/PageController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class PageController: NSPageController, NSPageControllerDelegate {
})
arrangedObjects.append(contentsOf: displays)
} else {
if TEST_MODE {
arrangedObjects.append(TEST_DISPLAY)
}
arrangedObjects.append(GENERIC_DISPLAY)
}

Expand Down Expand Up @@ -171,7 +174,9 @@ class PageController: NSPageController, NSPageControllerDelegate {

if let controller = viewControllers[identifier] as? DisplayViewController,
let displayId = CGDirectDisplayID(identifier) {
if displayId != GENERIC_DISPLAY.id {
if displayId == TEST_DISPLAY.id {
controller.display = TEST_DISPLAY
} else if displayId != GENERIC_DISPLAY.id {
controller.display = brightnessAdapter.displays[displayId]
} else {
controller.display = GENERIC_DISPLAY
Expand Down
26 changes: 18 additions & 8 deletions Lunar/Controllers/SettingsPageController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class SettingsPageController: NSViewController {
noonDuration: Int? = nil,
brightnessOffset: Int? = nil,
contrastOffset: Int? = nil,
brightnessLimitMin _: Int? = nil,
contrastLimitMin _: Int? = nil,
brightnessLimitMax _: Int? = nil,
contrastLimitMax _: Int? = nil,
brightnessLimitMin: Int? = nil,
contrastLimitMin: Int? = nil,
brightnessLimitMax: Int? = nil,
contrastLimitMax: Int? = nil,
appBrightnessOffset: Int = 0,
appContrastOffset: Int = 0,
withAnimation: Bool = false
Expand All @@ -34,10 +34,9 @@ class SettingsPageController: NSViewController {
var brightnessChartEntry = brightnessContrastChart.brightnessGraph.values
var contrastChartEntry = brightnessContrastChart.contrastGraph.values

let maxValues = brightnessContrastChart.maxValues

switch brightnessAdapter.mode {
case .location:
let maxValues = brightnessContrastChart.maxValuesLocation
for x in 0 ..< (maxValues - 1) {
let (brightness, contrast) = brightnessAdapter.getBrightnessContrast(
for: display,
Expand All @@ -54,8 +53,19 @@ class SettingsPageController: NSViewController {
}
brightnessChartEntry[maxValues - 1].y = brightnessChartEntry[0].y
contrastChartEntry[maxValues - 1].y = contrastChartEntry[0].y
default:
break
case .sync:
let maxValues = brightnessContrastChart.maxValuesSync
for x in 0 ..< (maxValues - 1) {
let percent = Double(x)
brightnessChartEntry[x].y = display.computeBrightness(from: percent, offset: brightnessOffset, appOffset: appBrightnessOffset).doubleValue
contrastChartEntry[x].y = display.computeContrast(from: percent, offset: contrastOffset, appOffset: appContrastOffset).doubleValue
}
case .manual:
let maxValues = brightnessContrastChart.maxValuesSync
for x in 0 ..< (maxValues - 1) {
brightnessChartEntry[x].y = brightnessAdapter.computeManualValueFromPercent(percent: Int8(x), key: "brightness", minVal: brightnessLimitMin, maxVal: brightnessLimitMax).doubleValue
contrastChartEntry[x].y = brightnessAdapter.computeManualValueFromPercent(percent: Int8(x), key: "contrast", minVal: contrastLimitMin, maxVal: contrastLimitMax).doubleValue
}
}

if withAnimation {
Expand Down
Loading

0 comments on commit beb9352

Please sign in to comment.