Skip to content

Commit c2b29d7

Browse files
committed
wip
1 parent 565b104 commit c2b29d7

File tree

8 files changed

+197
-85
lines changed

8 files changed

+197
-85
lines changed

Cargo.lock

Lines changed: 1 addition & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ human-repr = "1.1.0"
3232
image = "0.24.7"
3333
itertools = "0.11.0"
3434
log = "0.4.14"
35-
maplit = "1.0.2"
3635
png = "0.17.10"
3736
pollster = "0.3.0"
3837
serde = { version = "1.0.110", features = ["derive"] }

src/apply.rs

Lines changed: 58 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::calculator::calculate_brightness;
2-
use crate::config::{BrightnessValues, Location, MonitorOverride};
2+
use crate::config::{BrightnessValues, Location, MonitorOverride, MonitorProperty};
33
use brightness::blocking::{Brightness, BrightnessDevice};
4-
use maplit::btreemap;
4+
use itertools::Itertools;
55
use serde::Serialize;
6-
use std::collections::BTreeMap;
6+
use std::collections::HashMap;
77
use std::time::{SystemTime, UNIX_EPOCH};
88
use sunrise_sunset_calculator::SunriseSunsetParameters;
99
use wildmatch::WildMatch;
@@ -34,12 +34,22 @@ impl From<sunrise_sunset_calculator::SunriseSunsetResult> for SunriseSunsetResul
3434

3535
#[derive(Debug, Serialize)]
3636
pub struct MonitorResult {
37-
pub device_name: String,
38-
pub properties: BTreeMap<&'static str, String>,
37+
pub properties: MonitorProperties,
3938
pub brightness: Option<BrightnessDetails>,
4039
pub error: Option<String>,
4140
}
4241

42+
#[derive(Debug, Serialize)]
43+
pub struct MonitorProperties {
44+
pub device_name: String,
45+
#[cfg(windows)]
46+
pub device_description: String,
47+
#[cfg(windows)]
48+
pub device_key: String,
49+
#[cfg(windows)]
50+
pub device_path: String,
51+
}
52+
4353
#[derive(Debug, Serialize)]
4454
pub struct BrightnessDetails {
4555
pub expiry_time: Option<i64>,
@@ -50,15 +60,15 @@ pub struct BrightnessDetails {
5060

5161
pub struct MonitorOverrideCompiled {
5262
pub pattern: WildMatch,
53-
pub key: String,
63+
pub key: MonitorProperty,
5464
pub brightness: Option<BrightnessValues>,
5565
}
5666

5767
impl From<&MonitorOverride> for MonitorOverrideCompiled {
5868
fn from(value: &MonitorOverride) -> Self {
5969
Self {
6070
pattern: WildMatch::new(&value.pattern),
61-
key: value.key.clone(),
71+
key: value.key,
6272
brightness: value.brightness.clone(),
6373
}
6474
}
@@ -67,10 +77,11 @@ impl From<&MonitorOverride> for MonitorOverrideCompiled {
6777
/// Find the first override that matches this monitor's properties
6878
fn match_monitor<'o>(
6979
overrides: &'o [MonitorOverrideCompiled],
70-
monitor: &BTreeMap<&'static str, String>,
80+
monitor: &MonitorProperties,
7181
) -> Option<&'o MonitorOverrideCompiled> {
82+
let map = monitor.to_map();
7283
for o in overrides {
73-
if let Some(value) = monitor.get(o.key.as_str()) {
84+
if let Some(value) = map.get(&o.key) {
7485
if o.pattern.matches(value) {
7586
return Some(o);
7687
}
@@ -108,8 +119,7 @@ pub fn apply_brightness(
108119
let monitor_results = monitors
109120
.into_iter()
110121
.map(|m| {
111-
let device_name = m.device_name().unwrap_or_default();
112-
let properties = get_properties(&m).unwrap_or_default();
122+
let properties = MonitorProperties::from_device(&m);
113123
let monitor_values = match match_monitor(&overrides, &properties) {
114124
None => Some(BrightnessValues {
115125
brightness_day,
@@ -132,25 +142,28 @@ pub fn apply_brightness(
132142
);
133143
log::debug!(
134144
"Computed brightness for '{}' = {:?} (day={}) (night={})",
135-
device_name,
145+
properties.device_name,
136146
brightness,
137147
brightness_day,
138148
brightness_night
139149
);
140150

141151
let error = m.set(brightness.brightness).err();
142152
if let Some(err) = error.as_ref() {
143-
log::error!("Failed to set brightness for '{}': {:?}", device_name, err);
153+
log::error!(
154+
"Failed to set brightness for '{}': {:?}",
155+
properties.device_name,
156+
err
157+
);
144158
} else {
145159
log::info!(
146160
"Successfully set brightness for '{}' to {}%",
147-
device_name,
161+
properties.device_name,
148162
brightness.brightness
149163
);
150164
}
151165

152166
MonitorResult {
153-
device_name,
154167
properties,
155168
brightness: Some(BrightnessDetails {
156169
expiry_time: brightness.expiry_time,
@@ -161,15 +174,18 @@ pub fn apply_brightness(
161174
error: error.map(|e| e.to_string()),
162175
}
163176
} else {
164-
log::info!("Skipping '{}' due to monitor override", device_name,);
177+
log::info!(
178+
"Skipping '{}' due to monitor override",
179+
properties.device_name
180+
);
165181
MonitorResult {
166-
device_name,
167182
properties,
168183
brightness: None,
169184
error: None,
170185
}
171186
}
172187
})
188+
.sorted_by_key(|m| m.properties.device_name.clone())
173189
.collect::<Vec<_>>();
174190

175191
ApplyResults {
@@ -179,24 +195,30 @@ pub fn apply_brightness(
179195
}
180196
}
181197

182-
#[cfg(windows)]
183-
pub fn get_properties(
184-
device: &BrightnessDevice,
185-
) -> Result<BTreeMap<&'static str, String>, brightness::Error> {
186-
use brightness::blocking::windows::BrightnessExt;
187-
Ok(btreemap! {
188-
"device_name" => device.device_name()?,
189-
"device_description" => device.device_description()?,
190-
"device_key" => device.device_registry_key()?,
191-
"device_path" => device.device_path()?,
192-
})
193-
}
198+
impl MonitorProperties {
199+
fn from_device(device: &BrightnessDevice) -> Self {
200+
#[cfg(windows)]
201+
use brightness::blocking::windows::BrightnessExt;
202+
Self {
203+
device_name: device.device_name().unwrap(),
204+
#[cfg(windows)]
205+
device_description: device.device_description().unwrap(),
206+
#[cfg(windows)]
207+
device_key: device.device_registry_key().unwrap(),
208+
#[cfg(windows)]
209+
device_path: device.device_path().unwrap(),
210+
}
211+
}
194212

195-
#[cfg(target_os = "linux")]
196-
pub fn get_properties(
197-
device: &BrightnessDevice,
198-
) -> Result<BTreeMap<&'static str, String>, brightness::Error> {
199-
Ok(btreemap! {
200-
"device_name" => device.device_name()?,
201-
})
213+
pub fn to_map(&self) -> HashMap<MonitorProperty, &str> {
214+
let mut map = HashMap::<_, &str>::new();
215+
map.insert(MonitorProperty::DeviceName, &self.device_name);
216+
#[cfg(windows)]
217+
{
218+
map.insert(MonitorProperty::DeviceDescription, &self.device_description);
219+
map.insert(MonitorProperty::DeviceKey, &self.device_key);
220+
map.insert(MonitorProperty::DevicePath, &self.device_path);
221+
}
222+
map
223+
}
202224
}

src/config.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! SSB Config file definition
2-
32
use crate::common::config_directory;
43
use anyhow::Context;
4+
use enum_iterator::Sequence;
55
use serde::{Deserialize, Serialize};
66
use std::fs;
77
use std::io::Write;
@@ -34,10 +34,33 @@ pub struct SsbConfig {
3434
pub overrides: Vec<MonitorOverride>,
3535
}
3636

37+
#[derive(Debug, Serialize, Deserialize, Copy, Clone, Eq, Hash, PartialEq, Sequence)]
38+
#[serde(rename_all = "snake_case")]
39+
pub enum MonitorProperty {
40+
DeviceName,
41+
#[cfg(windows)]
42+
DeviceDescription,
43+
#[cfg(windows)]
44+
DeviceKey,
45+
#[cfg(windows)]
46+
DevicePath,
47+
}
48+
49+
impl MonitorProperty {
50+
pub fn as_str(&self) -> &'static str {
51+
match self {
52+
MonitorProperty::DeviceName => "Device Name",
53+
MonitorProperty::DeviceDescription => "Device Description",
54+
MonitorProperty::DeviceKey => "Device Key",
55+
MonitorProperty::DevicePath => "Device Path",
56+
}
57+
}
58+
}
59+
3760
#[derive(Debug, Deserialize, Serialize, Validate, Clone)]
3861
pub struct MonitorOverride {
3962
pub pattern: String,
40-
pub key: String,
63+
pub key: MonitorProperty,
4164
#[validate]
4265
pub brightness: Option<BrightnessValues>,
4366
}

src/gui/app.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::controller::Message;
44
use crate::gui::brightness_settings::BrightnessSettingsPage;
55
use crate::gui::help::HelpPage;
66
use crate::gui::location_settings::LocationSettingsPage;
7+
use crate::gui::monitor_overrides::MonitorOverridePage;
78
use crate::gui::status::StatusPage;
89
use crate::gui::UserEvent;
910
use egui::{Align, Color32, Layout, ScrollArea};
@@ -24,6 +25,7 @@ pub struct SsbEguiApp {
2425
brightness_settings_page: BrightnessSettingsPage,
2526
pub location_settings_page: LocationSettingsPage,
2627
help_page: HelpPage,
28+
monitor_override_page: MonitorOverridePage,
2729
context: AppState,
2830
pub modal: Option<Box<dyn Modal>>,
2931
}
@@ -55,6 +57,7 @@ enum PageId {
5557
Status,
5658
BrightnessSettings,
5759
LocationSettings,
60+
MonitorOverrides,
5861
Help,
5962
}
6063

@@ -65,6 +68,7 @@ impl PageId {
6568
PageId::BrightnessSettings => "Brightness Settings",
6669
PageId::LocationSettings => "Location Settings",
6770
PageId::Help => "Help",
71+
PageId::MonitorOverrides => "Monitor Overrides",
6872
}
6973
}
7074

@@ -74,6 +78,7 @@ impl PageId {
7478
PageId::BrightnessSettings => "🔅",
7579
PageId::LocationSettings => "🌐",
7680
PageId::Help => "❔",
81+
PageId::MonitorOverrides => "💻",
7782
}
7883
}
7984
}
@@ -143,6 +148,7 @@ impl SsbEguiApp {
143148
selected_page: PageId::Status,
144149
brightness_settings_page: BrightnessSettingsPage::from_config(&config_read),
145150
location_settings_page: LocationSettingsPage::from_config(&config_read),
151+
monitor_override_page: MonitorOverridePage::from_config(&config_read),
146152
modal: None,
147153
context: AppState {
148154
main_loop,
@@ -221,6 +227,9 @@ impl SsbEguiApp {
221227
self.location_settings_page.render(ui, &mut self.context)
222228
}
223229
PageId::Help => self.help_page.render(ui, &mut self.context),
230+
PageId::MonitorOverrides => {
231+
self.monitor_override_page.render(ui, &mut self.context)
232+
}
224233
}
225234
});
226235
}

src/gui/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub mod app;
22
mod brightness_settings;
33
mod help;
44
mod location_settings;
5+
mod monitor_overrides;
56
mod status;
67

78
use crate::common::APP_NAME;

0 commit comments

Comments
 (0)