From ea7a9e818881f45e83a81c0b483a55a8182b9b8e Mon Sep 17 00:00:00 2001 From: Fu Wang Date: Mon, 18 Aug 2025 11:21:28 +0800 Subject: [PATCH 1/2] Add friendly device name to Brightness API Introduces a `friendly_device_name` method to both async and blocking Brightness traits and implementations for Linux and Windows. Example code updated to display the friendly device name instead of the technical device name, improving user-facing output. --- examples/list_displays_async.rs | 1 + examples/set_brightness_async.rs | 2 +- examples/set_brightness_blocking.rs | 2 +- src/blocking.rs | 7 +++++++ src/blocking/linux.rs | 4 ++++ src/blocking/windows.rs | 6 ++++++ src/lib.rs | 7 +++++++ src/linux.rs | 4 ++++ src/windows.rs | 4 ++++ 9 files changed, 35 insertions(+), 2 deletions(-) diff --git a/examples/list_displays_async.rs b/examples/list_displays_async.rs index 56c7568..a1c6825 100644 --- a/examples/list_displays_async.rs +++ b/examples/list_displays_async.rs @@ -18,6 +18,7 @@ async fn run() { async fn show_brightness(dev: &BrightnessDevice) -> Result<(), brightness::Error> { println!("Display {}", dev.device_name().await?); + println!("\tFriendly name = {}", dev.friendly_device_name().await?); println!("\tBrightness = {}%", dev.get().await?); show_platform_specific_info(dev).await?; Ok(()) diff --git a/examples/set_brightness_async.rs b/examples/set_brightness_async.rs index 84b3e1f..c410eca 100644 --- a/examples/set_brightness_async.rs +++ b/examples/set_brightness_async.rs @@ -24,7 +24,7 @@ async fn run(percentage: u32) { async fn show_brightness(dev: &BrightnessDevice) -> Result<(), brightness::Error> { println!( "Brightness of device {} is {}%", - dev.device_name().await?, + dev.friendly_device_name().await?, dev.get().await? ); Ok(()) diff --git a/examples/set_brightness_blocking.rs b/examples/set_brightness_blocking.rs index 9f9c376..f200053 100644 --- a/examples/set_brightness_blocking.rs +++ b/examples/set_brightness_blocking.rs @@ -23,7 +23,7 @@ fn run(percentage: u32) { fn show_brightness(dev: &BrightnessDevice) -> Result<(), brightness::Error> { println!( "Brightness of device {} is {}%", - dev.device_name()?, + dev.friendly_device_name()?, dev.get()? ); Ok(()) diff --git a/src/blocking.rs b/src/blocking.rs index 3618e3f..b1f3a1c 100644 --- a/src/blocking.rs +++ b/src/blocking.rs @@ -25,6 +25,9 @@ pub trait Brightness { /// Returns the device name. fn device_name(&self) -> Result; + /// Returns the user friendly device name. + fn friendly_device_name(&self) -> Result; + /// Returns the current brightness as a percentage. fn get(&self) -> Result; @@ -37,6 +40,10 @@ impl Brightness for BrightnessDevice { self.0.device_name() } + fn friendly_device_name(&self) -> Result { + self.0.friendly_device_name() + } + fn get(&self) -> Result { self.0.get() } diff --git a/src/blocking/linux.rs b/src/blocking/linux.rs index 337173f..307fff4 100644 --- a/src/blocking/linux.rs +++ b/src/blocking/linux.rs @@ -22,6 +22,10 @@ impl crate::blocking::Brightness for BlockingDeviceImpl { Ok(self.device.clone()) } + fn friendly_device_name(&self) -> Result { + Ok(self.device.clone()) + } + fn get(&self) -> Result { let max = read_value(&self.device, Value::Max)?; let actual = read_value(&self.device, Value::Actual)?; diff --git a/src/blocking/windows.rs b/src/blocking/windows.rs index 07327ad..b51f3b2 100644 --- a/src/blocking/windows.rs +++ b/src/blocking/windows.rs @@ -59,6 +59,7 @@ pub(crate) struct BlockingDeviceImpl { physical_monitor: WrappedPhysicalMonitor, file_handle: WrappedFileHandle, device_name: String, + friendly_name: String, /// Note: PHYSICAL_MONITOR.szPhysicalMonitorDescription == DISPLAY_DEVICEW.DeviceString /// Description is **not** unique. pub(crate) device_description: String, @@ -127,6 +128,10 @@ impl crate::blocking::Brightness for BlockingDeviceImpl { Ok(self.device_name.clone()) } + fn friendly_device_name(&self) -> Result { + Ok(self.friendly_name.clone()) + } + fn get(&self) -> Result { Ok(if self.is_internal() { ioctl_query_display_brightness(self)? @@ -194,6 +199,7 @@ pub(crate) fn brightness_devices() -> impl Iterator impl Future> + Send; + /// Returns the user friendly device name. + fn friendly_device_name(&self) -> impl Future> + Send; + /// Returns the current brightness as a percentage. fn get(&self) -> impl Future> + Send; @@ -95,6 +98,10 @@ mod r#async { self.0.device_name().await } + async fn friendly_device_name(&self) -> Result { + self.0.friendly_device_name().await + } + async fn get(&self) -> Result { self.0.get().await } diff --git a/src/linux.rs b/src/linux.rs index db0658f..2c039b4 100644 --- a/src/linux.rs +++ b/src/linux.rs @@ -22,6 +22,10 @@ impl crate::Brightness for AsyncDeviceImpl { Ok(self.device.clone()) } + async fn friendly_device_name(&self) -> Result { + Ok(self.device.clone()) + } + async fn get(&self) -> Result { let max = read_value(&self.device, Value::Max)?; let actual = read_value(&self.device, Value::Actual)?; diff --git a/src/windows.rs b/src/windows.rs index fae6c3b..96c19ef 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -25,6 +25,10 @@ impl crate::Brightness for AsyncDeviceImpl { self.0.device_name() } + async fn friendly_device_name(&self) -> Result { + self.0.friendly_device_name() + } + async fn get(&self) -> Result { let cloned = Arc::clone(&self.0); unblock(move || cloned.get()).await From 530e84974c4617ce485e913d97af210d1941a5df Mon Sep 17 00:00:00 2001 From: Fu Wang Date: Mon, 18 Aug 2025 12:03:48 +0800 Subject: [PATCH 2/2] Update the docs for friendly_device_name function to address the code review feedback. --- src/blocking.rs | 3 ++- src/lib.rs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/blocking.rs b/src/blocking.rs index b1f3a1c..8b29719 100644 --- a/src/blocking.rs +++ b/src/blocking.rs @@ -25,7 +25,8 @@ pub trait Brightness { /// Returns the device name. fn device_name(&self) -> Result; - /// Returns the user friendly device name. + /// Returns a human-readable device name suitable for display to users. + /// This may differ from the technical device identifier returned by `device_name()`. fn friendly_device_name(&self) -> Result; /// Returns the current brightness as a percentage. diff --git a/src/lib.rs b/src/lib.rs index 9436659..8a9adb9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -79,7 +79,8 @@ mod r#async { /// Returns the device name. fn device_name(&self) -> impl Future> + Send; - /// Returns the user friendly device name. + /// Returns a human-readable device name suitable for display to users, + /// which may differ from the technical device identifier returned by `device_name`. fn friendly_device_name(&self) -> impl Future> + Send; /// Returns the current brightness as a percentage.