sysfs_class/
brightness.rs

1use crate::SysClass;
2use std::io::Result;
3
4pub trait Brightness: SysClass {
5    trait_method!(brightness parse_file u64);
6
7    trait_method!(max_brightness parse_file u64);
8
9    set_trait_method!("brightness", set_brightness u64);
10
11    /// Sets the `new` brightness level if it is less than the current brightness.
12    ///
13    /// Returns the brightness level that was set at the time of exiting the function.
14    fn set_if_lower_than(&self, percent: u64) -> Result<()> {
15        let max_brightness = self.max_brightness()?;
16        let current = self.brightness()?;
17
18        let new = max_brightness * percent / 100;
19        if new < current {
20            self.set_brightness(new)
21        } else {
22            Ok(())
23        }
24    }
25}