refactor(battery): use channel subscription
This commit is contained in:
parent
ff6e9e3483
commit
8d9bb40b1b
5 changed files with 127 additions and 174 deletions
|
|
@ -392,31 +392,28 @@ impl Application for CosmicBatteryApplet {
|
||||||
Subscription::batch(vec![
|
Subscription::batch(vec![
|
||||||
self.applet_helper.theme_subscription(0).map(Message::Theme),
|
self.applet_helper.theme_subscription(0).map(Message::Theme),
|
||||||
device_subscription(0).map(
|
device_subscription(0).map(
|
||||||
|(
|
|DeviceDbusEvent::Update {
|
||||||
_,
|
icon_name,
|
||||||
DeviceDbusEvent::Update {
|
percent,
|
||||||
icon_name,
|
time_to_empty,
|
||||||
percent,
|
}| Message::Update {
|
||||||
time_to_empty,
|
|
||||||
},
|
|
||||||
)| Message::Update {
|
|
||||||
icon_name,
|
icon_name,
|
||||||
percent,
|
percent,
|
||||||
time_to_empty,
|
time_to_empty,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
kbd_backlight_subscription(0).map(|event| match event {
|
kbd_backlight_subscription(0).map(|event| match event {
|
||||||
(_, KeyboardBacklightUpdate::Update(b)) => Message::UpdateKbdBrightness(b),
|
KeyboardBacklightUpdate::Update(b) => Message::UpdateKbdBrightness(b),
|
||||||
(_, KeyboardBacklightUpdate::Init(tx, b)) => Message::InitKbdBacklight(tx, b),
|
KeyboardBacklightUpdate::Init(tx, b) => Message::InitKbdBacklight(tx, b),
|
||||||
}),
|
}),
|
||||||
screen_backlight_subscription(0).map(|e| match e {
|
screen_backlight_subscription(0).map(|e| match e {
|
||||||
(_, ScreenBacklightUpdate::Update(b)) => Message::UpdateScreenBrightness(b),
|
ScreenBacklightUpdate::Update(b) => Message::UpdateScreenBrightness(b),
|
||||||
(_, ScreenBacklightUpdate::Init(tx, b)) => Message::InitScreenBacklight(tx, b),
|
ScreenBacklightUpdate::Init(tx, b) => Message::InitScreenBacklight(tx, b),
|
||||||
}),
|
}),
|
||||||
power_profile_subscription(0).map(|event| match event {
|
power_profile_subscription(0).map(|event| match event {
|
||||||
(_, PowerProfileUpdate::Update { profile }) => Message::Profile(profile),
|
PowerProfileUpdate::Update { profile } => Message::Profile(profile),
|
||||||
(_, PowerProfileUpdate::Init(tx, p)) => Message::InitProfile(p, tx),
|
PowerProfileUpdate::Init(tx, p) => Message::InitProfile(p, tx),
|
||||||
(_, PowerProfileUpdate::Error(e)) => Message::Errored(e), // TODO: handle error
|
PowerProfileUpdate::Error(e) => Message::Errored(e), // TODO: handle error
|
||||||
}),
|
}),
|
||||||
self.timeline
|
self.timeline
|
||||||
.as_subscription()
|
.as_subscription()
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,7 @@ use std::{
|
||||||
str::{self, FromStr},
|
str::{self, FromStr},
|
||||||
};
|
};
|
||||||
|
|
||||||
use cosmic::iced;
|
use cosmic::iced::{self, futures::SinkExt, subscription};
|
||||||
use iced::subscription;
|
|
||||||
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
|
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
|
||||||
|
|
||||||
const BACKLIGHT_SYSDIR: &str = "/sys/class/backlight";
|
const BACKLIGHT_SYSDIR: &str = "/sys/class/backlight";
|
||||||
|
|
@ -77,21 +76,14 @@ pub async fn backlight() -> io::Result<Option<Backlight>> {
|
||||||
|
|
||||||
pub fn screen_backlight_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
|
pub fn screen_backlight_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
|
||||||
id: I,
|
id: I,
|
||||||
) -> iced::Subscription<(I, ScreenBacklightUpdate)> {
|
) -> iced::Subscription<ScreenBacklightUpdate> {
|
||||||
subscription::unfold(id, State::Ready, move |state| start_listening_loop(id, state))
|
subscription::channel(id, 50, move |mut output| async move {
|
||||||
}
|
let mut state = State::Ready;
|
||||||
|
|
||||||
async fn start_listening_loop<I: Copy + Debug>(
|
loop {
|
||||||
id: I,
|
state = start_listening(state, &mut output).await;
|
||||||
mut state: State,
|
|
||||||
) -> ((I, ScreenBacklightUpdate), State) {
|
|
||||||
loop {
|
|
||||||
let (update, new_state) = start_listening(id, state).await;
|
|
||||||
state = new_state;
|
|
||||||
if let Some(update) = update {
|
|
||||||
return (update, state);
|
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum State {
|
pub enum State {
|
||||||
|
|
@ -104,46 +96,43 @@ pub enum State {
|
||||||
Finished,
|
Finished,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn start_listening<I: Copy>(
|
async fn start_listening(
|
||||||
id: I,
|
|
||||||
state: State,
|
state: State,
|
||||||
) -> (Option<(I, ScreenBacklightUpdate)>, State) {
|
output: &mut futures::channel::mpsc::Sender<ScreenBacklightUpdate>,
|
||||||
|
) -> State {
|
||||||
match state {
|
match state {
|
||||||
State::Ready => {
|
State::Ready => {
|
||||||
let conn = match zbus::Connection::system().await {
|
let conn = match zbus::Connection::system().await {
|
||||||
Ok(conn) => conn,
|
Ok(conn) => conn,
|
||||||
Err(_) => return (None, State::Finished),
|
Err(_) => return State::Finished,
|
||||||
};
|
};
|
||||||
let screen_proxy = match LogindSessionProxy::builder(&conn).build().await {
|
let screen_proxy = match LogindSessionProxy::builder(&conn).build().await {
|
||||||
Ok(p) => p,
|
Ok(p) => p,
|
||||||
Err(_) => return (None, State::Finished),
|
Err(_) => return State::Finished,
|
||||||
};
|
};
|
||||||
let backlight = match backlight().await {
|
let backlight = match backlight().await {
|
||||||
Ok(Some(b)) => b,
|
Ok(Some(b)) => b,
|
||||||
_ => return (None, State::Finished),
|
_ => return State::Finished,
|
||||||
};
|
};
|
||||||
let (tx, rx) = unbounded_channel();
|
let (tx, rx) = unbounded_channel();
|
||||||
|
|
||||||
let b = (backlight.brightness().await.unwrap_or_default() as f64
|
let b = (backlight.brightness().await.unwrap_or_default() as f64
|
||||||
/ backlight.max_brightness().await.unwrap_or(1) as f64)
|
/ backlight.max_brightness().await.unwrap_or(1) as f64)
|
||||||
.clamp(0., 1.);
|
.clamp(0., 1.);
|
||||||
(
|
_ = output.send(ScreenBacklightUpdate::Init(tx, b)).await;
|
||||||
Some((id, ScreenBacklightUpdate::Init(tx, b))),
|
|
||||||
State::Waiting(backlight, screen_proxy, rx),
|
State::Waiting(backlight, screen_proxy, rx)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
State::Waiting(backlight, proxy, mut rx) => match rx.recv().await {
|
State::Waiting(backlight, proxy, mut rx) => match rx.recv().await {
|
||||||
Some(req) => match req {
|
Some(req) => match req {
|
||||||
ScreenBacklightRequest::Get => {
|
ScreenBacklightRequest::Get => {
|
||||||
let msg = if let Some(max_brightness) = backlight.max_brightness().await {
|
if let Some(max_brightness) = backlight.max_brightness().await {
|
||||||
let value = (backlight.brightness().await.unwrap_or_default() as f64
|
let value = (backlight.brightness().await.unwrap_or_default() as f64
|
||||||
/ max_brightness as f64)
|
/ max_brightness as f64)
|
||||||
.clamp(0., 1.);
|
.clamp(0., 1.);
|
||||||
Some((id, ScreenBacklightUpdate::Update(value)))
|
_ = output.send(ScreenBacklightUpdate::Update(value)).await;
|
||||||
} else {
|
}
|
||||||
None
|
State::Waiting(backlight, proxy, rx)
|
||||||
};
|
|
||||||
(msg, State::Waiting(backlight, proxy, rx))
|
|
||||||
}
|
}
|
||||||
ScreenBacklightRequest::Set(value) => {
|
ScreenBacklightRequest::Set(value) => {
|
||||||
if let Some(max_brightness) = backlight.max_brightness().await {
|
if let Some(max_brightness) = backlight.max_brightness().await {
|
||||||
|
|
@ -151,10 +140,10 @@ async fn start_listening<I: Copy>(
|
||||||
let value = value.round() as u32;
|
let value = value.round() as u32;
|
||||||
let _ = backlight.set_brightness(&proxy, value).await;
|
let _ = backlight.set_brightness(&proxy, value).await;
|
||||||
}
|
}
|
||||||
(None, State::Waiting(backlight, proxy, rx))
|
State::Waiting(backlight, proxy, rx)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None => (None, State::Finished),
|
None => State::Finished,
|
||||||
},
|
},
|
||||||
State::Finished => iced::futures::future::pending().await,
|
State::Finished => iced::futures::future::pending().await,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,7 @@
|
||||||
//!
|
//!
|
||||||
//! …consequently `zbus-xmlgen` did not generate code for the above interfaces.
|
//! …consequently `zbus-xmlgen` did not generate code for the above interfaces.
|
||||||
|
|
||||||
use cosmic::iced;
|
use cosmic::iced::{self, futures::SinkExt, subscription};
|
||||||
use cosmic::iced::subscription;
|
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
use tokio::sync::mpsc::UnboundedReceiver;
|
use tokio::sync::mpsc::UnboundedReceiver;
|
||||||
|
|
@ -113,9 +112,13 @@ pub async fn set_power_profile(daemon: PowerDaemonProxy<'_>, power: Power) -> Re
|
||||||
|
|
||||||
pub fn power_profile_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
|
pub fn power_profile_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
|
||||||
id: I,
|
id: I,
|
||||||
) -> iced::Subscription<(I, PowerProfileUpdate)> {
|
) -> iced::Subscription<PowerProfileUpdate> {
|
||||||
subscription::unfold(id, State::Ready, move |state| {
|
subscription::channel(id, 50, move |mut output| async move {
|
||||||
start_listening_loop(id, state)
|
let mut state = State::Ready;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
state = start_listening(state, &mut output).await;
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -126,25 +129,18 @@ pub enum State {
|
||||||
Finished,
|
Finished,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn start_listening_loop<I: Copy + Debug>(
|
async fn start_listening(
|
||||||
id: I,
|
state: State,
|
||||||
mut state: State,
|
output: &mut futures::channel::mpsc::Sender<PowerProfileUpdate>,
|
||||||
) -> ((I, PowerProfileUpdate), State) {
|
) -> State {
|
||||||
loop {
|
|
||||||
let (update, new_state) = start_listening(id, state).await;
|
|
||||||
state = new_state;
|
|
||||||
if let Some(update) = update {
|
|
||||||
return (update, state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn start_listening<I: Copy>(id: I, state: State) -> (Option<(I, PowerProfileUpdate)>, State) {
|
|
||||||
match state {
|
match state {
|
||||||
State::Ready => {
|
State::Ready => {
|
||||||
let conn = match Connection::system().await.map_err(|e| e.to_string()) {
|
let conn = match Connection::system().await.map_err(|e| e.to_string()) {
|
||||||
Ok(conn) => conn,
|
Ok(conn) => conn,
|
||||||
Err(e) => return (Some((id, PowerProfileUpdate::Error(e))), State::Finished),
|
Err(e) => {
|
||||||
|
_ = output.send(PowerProfileUpdate::Error(e)).await;
|
||||||
|
return State::Finished;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
|
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
|
||||||
|
|
||||||
|
|
@ -154,10 +150,9 @@ async fn start_listening<I: Copy>(id: I, state: State) -> (Option<(I, PowerProfi
|
||||||
{
|
{
|
||||||
Ok(p) => p,
|
Ok(p) => p,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return (
|
_ = output.send(PowerProfileUpdate::Error(e)).await;
|
||||||
Some((id, PowerProfileUpdate::Error(e))),
|
|
||||||
State::Waiting(conn, rx),
|
return State::Waiting(conn, rx);
|
||||||
)
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let profile = match get_power_profile(power_proxy)
|
let profile = match get_power_profile(power_proxy)
|
||||||
|
|
@ -166,17 +161,12 @@ async fn start_listening<I: Copy>(id: I, state: State) -> (Option<(I, PowerProfi
|
||||||
{
|
{
|
||||||
Ok(p) => p,
|
Ok(p) => p,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return (
|
_ = output.send(PowerProfileUpdate::Error(e)).await;
|
||||||
Some((id, PowerProfileUpdate::Error(e))),
|
return State::Waiting(conn, rx);
|
||||||
State::Waiting(conn, rx),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
_ = output.send(PowerProfileUpdate::Init(profile, tx)).await;
|
||||||
(
|
State::Waiting(conn, rx)
|
||||||
Some((id, PowerProfileUpdate::Init(profile, tx))),
|
|
||||||
State::Waiting(conn, rx),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
State::Waiting(conn, mut rx) => {
|
State::Waiting(conn, mut rx) => {
|
||||||
let power_proxy = match PowerDaemonProxy::new(&conn)
|
let power_proxy = match PowerDaemonProxy::new(&conn)
|
||||||
|
|
@ -185,32 +175,24 @@ async fn start_listening<I: Copy>(id: I, state: State) -> (Option<(I, PowerProfi
|
||||||
{
|
{
|
||||||
Ok(p) => p,
|
Ok(p) => p,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return (
|
_ = output.send(PowerProfileUpdate::Error(e)).await;
|
||||||
Some((id, PowerProfileUpdate::Error(e))),
|
return State::Waiting(conn, rx);
|
||||||
State::Waiting(conn, rx),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
match rx.recv().await {
|
match rx.recv().await {
|
||||||
Some(PowerProfileRequest::Get) => {
|
Some(PowerProfileRequest::Get) => {
|
||||||
if let Ok(profile) = get_power_profile(power_proxy).await {
|
if let Ok(profile) = get_power_profile(power_proxy).await {
|
||||||
(
|
_ = output.send(PowerProfileUpdate::Update { profile }).await;
|
||||||
Some((id, PowerProfileUpdate::Update { profile })),
|
|
||||||
State::Waiting(conn, rx),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
(None, State::Waiting(conn, rx))
|
|
||||||
}
|
}
|
||||||
|
State::Waiting(conn, rx)
|
||||||
}
|
}
|
||||||
Some(PowerProfileRequest::Set(profile)) => {
|
Some(PowerProfileRequest::Set(profile)) => {
|
||||||
let _ = set_power_profile(power_proxy, profile).await;
|
let _ = set_power_profile(power_proxy, profile).await;
|
||||||
(
|
_ = output.send(PowerProfileUpdate::Update { profile }).await;
|
||||||
Some((id, PowerProfileUpdate::Update { profile })),
|
State::Waiting(conn, rx)
|
||||||
State::Waiting(conn, rx),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
None => (None, State::Finished),
|
None => State::Finished,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
State::Finished => iced::futures::future::pending().await,
|
State::Finished => iced::futures::future::pending().await,
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,12 @@
|
||||||
//! This code was generated by `zbus-xmlgen` `2.0.1` from DBus introspection data.
|
//! This code was generated by `zbus-xmlgen` `2.0.1` from DBus introspection data.
|
||||||
//! Source: `Interface '/org/freedesktop/UPower/devices/DisplayDevice' from service 'org.freedesktop.UPower' on system bus`.
|
//! Source: `Interface '/org/freedesktop/UPower/devices/DisplayDevice' from service 'org.freedesktop.UPower' on system bus`.
|
||||||
|
|
||||||
use cosmic::iced::{self, subscription};
|
use cosmic::iced::{
|
||||||
|
self,
|
||||||
|
futures::{SinkExt, StreamExt},
|
||||||
|
subscription,
|
||||||
|
};
|
||||||
|
|
||||||
use futures::StreamExt;
|
|
||||||
use std::{fmt::Debug, hash::Hash};
|
use std::{fmt::Debug, hash::Hash};
|
||||||
use zbus::dbus_proxy;
|
use zbus::dbus_proxy;
|
||||||
|
|
||||||
|
|
@ -152,8 +155,14 @@ trait Device {
|
||||||
|
|
||||||
pub fn device_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
|
pub fn device_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
|
||||||
id: I,
|
id: I,
|
||||||
) -> iced::Subscription<(I, DeviceDbusEvent)> {
|
) -> iced::Subscription<DeviceDbusEvent> {
|
||||||
subscription::unfold(id, State::Ready, move |state| start_listening_loop(id, state))
|
subscription::channel(id, 50, move |mut output| async move {
|
||||||
|
let mut state = State::Ready;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
state = start_listening(state, &mut output).await;
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
@ -174,45 +183,32 @@ async fn display_device() -> zbus::Result<DeviceProxy<'static>> {
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn start_listening_loop<I: Copy + Debug>(
|
async fn start_listening(
|
||||||
id: I,
|
state: State,
|
||||||
mut state: State,
|
output: &mut futures::channel::mpsc::Sender<DeviceDbusEvent>,
|
||||||
) -> ((I, DeviceDbusEvent), State) {
|
) -> State {
|
||||||
loop {
|
|
||||||
let (update, new_state) = start_listening(id, state).await;
|
|
||||||
state = new_state;
|
|
||||||
if let Some(update) = update {
|
|
||||||
return (update, state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn start_listening<I: Copy>(id: I, state: State) -> (Option<(I, DeviceDbusEvent)>, State) {
|
|
||||||
match state {
|
match state {
|
||||||
State::Ready => {
|
State::Ready => {
|
||||||
if let Ok(device) = display_device().await {
|
if let Ok(device) = display_device().await {
|
||||||
return (
|
_ = output
|
||||||
Some((
|
.send(DeviceDbusEvent::Update {
|
||||||
id,
|
icon_name: device
|
||||||
DeviceDbusEvent::Update {
|
.cached_icon_name()
|
||||||
icon_name: device
|
.unwrap_or_default()
|
||||||
.cached_icon_name()
|
.unwrap_or_default(),
|
||||||
.unwrap_or_default()
|
percent: device
|
||||||
.unwrap_or_default(),
|
.cached_percentage()
|
||||||
percent: device
|
.unwrap_or_default()
|
||||||
.cached_percentage()
|
.unwrap_or_default(),
|
||||||
.unwrap_or_default()
|
time_to_empty: device
|
||||||
.unwrap_or_default(),
|
.cached_time_to_empty()
|
||||||
time_to_empty: device
|
.unwrap_or_default()
|
||||||
.cached_time_to_empty()
|
.unwrap_or_default(),
|
||||||
.unwrap_or_default()
|
})
|
||||||
.unwrap_or_default(),
|
.await;
|
||||||
},
|
return State::Waiting(device);
|
||||||
)),
|
|
||||||
State::Waiting(device),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
(None, State::Finished)
|
State::Finished
|
||||||
}
|
}
|
||||||
State::Waiting(device) => {
|
State::Waiting(device) => {
|
||||||
let mut stream = futures::stream_select!(
|
let mut stream = futures::stream_select!(
|
||||||
|
|
@ -221,10 +217,9 @@ async fn start_listening<I: Copy>(id: I, state: State) -> (Option<(I, DeviceDbus
|
||||||
device.receive_time_to_empty_changed().await.map(|_| ()),
|
device.receive_time_to_empty_changed().await.map(|_| ()),
|
||||||
);
|
);
|
||||||
match stream.next().await {
|
match stream.next().await {
|
||||||
Some(_) => (
|
Some(_) => {
|
||||||
Some((
|
_ = output
|
||||||
id,
|
.send(DeviceDbusEvent::Update {
|
||||||
DeviceDbusEvent::Update {
|
|
||||||
icon_name: device
|
icon_name: device
|
||||||
.cached_icon_name()
|
.cached_icon_name()
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
|
|
@ -237,11 +232,12 @@ async fn start_listening<I: Copy>(id: I, state: State) -> (Option<(I, DeviceDbus
|
||||||
.cached_time_to_empty()
|
.cached_time_to_empty()
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
},
|
})
|
||||||
)),
|
.await;
|
||||||
State::Waiting(device),
|
|
||||||
),
|
State::Waiting(device)
|
||||||
None => (None, State::Finished),
|
}
|
||||||
|
None => State::Finished,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
State::Finished => iced::futures::future::pending().await,
|
State::Finished => iced::futures::future::pending().await,
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,7 @@
|
||||||
//! This code was generated by `zbus-xmlgen` `2.0.1` from DBus introspection data.
|
//! This code was generated by `zbus-xmlgen` `2.0.1` from DBus introspection data.
|
||||||
//! Source: `Interface '/org/freedesktop/UPower/KbdBacklight' from service 'org.freedesktop.UPower' on system bus`.
|
//! Source: `Interface '/org/freedesktop/UPower/KbdBacklight' from service 'org.freedesktop.UPower' on system bus`.
|
||||||
|
|
||||||
use cosmic::iced;
|
use cosmic::iced::{self, futures::SinkExt, subscription};
|
||||||
use iced::subscription;
|
|
||||||
use std::{fmt::Debug, hash::Hash};
|
use std::{fmt::Debug, hash::Hash};
|
||||||
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
|
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
|
||||||
use zbus::dbus_proxy;
|
use zbus::dbus_proxy;
|
||||||
|
|
@ -35,21 +34,14 @@ trait KbdBacklight {
|
||||||
|
|
||||||
pub fn kbd_backlight_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
|
pub fn kbd_backlight_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
|
||||||
id: I,
|
id: I,
|
||||||
) -> iced::Subscription<(I, KeyboardBacklightUpdate)> {
|
) -> iced::Subscription<KeyboardBacklightUpdate> {
|
||||||
subscription::unfold(id, State::Ready, move |state| start_listening_loop(id, state))
|
subscription::channel(id, 50, move |mut output| async move {
|
||||||
}
|
let mut state = State::Ready;
|
||||||
|
|
||||||
async fn start_listening_loop<I: Copy + Debug>(
|
loop {
|
||||||
id: I,
|
state = start_listening(state, &mut output).await;
|
||||||
mut state: State,
|
|
||||||
) -> ((I, KeyboardBacklightUpdate), State) {
|
|
||||||
loop {
|
|
||||||
let (update, new_state) = start_listening(id, state).await;
|
|
||||||
state = new_state;
|
|
||||||
if let Some(update) = update {
|
|
||||||
return (update, state);
|
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
@ -62,38 +54,35 @@ pub enum State {
|
||||||
Finished,
|
Finished,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn start_listening<I: Copy>(
|
async fn start_listening(
|
||||||
id: I,
|
|
||||||
state: State,
|
state: State,
|
||||||
) -> (Option<(I, KeyboardBacklightUpdate)>, State) {
|
output: &mut futures::channel::mpsc::Sender<KeyboardBacklightUpdate>,
|
||||||
|
) -> State {
|
||||||
match state {
|
match state {
|
||||||
State::Ready => {
|
State::Ready => {
|
||||||
let conn = match zbus::Connection::system().await {
|
let conn = match zbus::Connection::system().await {
|
||||||
Ok(conn) => conn,
|
Ok(conn) => conn,
|
||||||
Err(_) => return (None, State::Finished),
|
Err(_) => return State::Finished,
|
||||||
};
|
};
|
||||||
let kbd_proxy = match KbdBacklightProxy::builder(&conn).build().await {
|
let kbd_proxy = match KbdBacklightProxy::builder(&conn).build().await {
|
||||||
Ok(p) => p,
|
Ok(p) => p,
|
||||||
Err(_) => return (None, State::Finished),
|
Err(_) => return State::Finished,
|
||||||
};
|
};
|
||||||
let (tx, rx) = unbounded_channel();
|
let (tx, rx) = unbounded_channel();
|
||||||
|
|
||||||
let b = kbd_proxy.get_brightness().await.unwrap_or_default() as f64
|
let b = kbd_proxy.get_brightness().await.unwrap_or_default() as f64
|
||||||
/ kbd_proxy.get_max_brightness().await.unwrap_or(1) as f64;
|
/ kbd_proxy.get_max_brightness().await.unwrap_or(1) as f64;
|
||||||
(
|
_ = output.send(KeyboardBacklightUpdate::Init(tx, b)).await;
|
||||||
Some((id, KeyboardBacklightUpdate::Init(tx, b))),
|
|
||||||
State::Waiting(kbd_proxy, rx),
|
State::Waiting(kbd_proxy, rx)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
State::Waiting(proxy, mut rx) => match rx.recv().await {
|
State::Waiting(proxy, mut rx) => match rx.recv().await {
|
||||||
Some(req) => match req {
|
Some(req) => match req {
|
||||||
KeyboardBacklightRequest::Get => {
|
KeyboardBacklightRequest::Get => {
|
||||||
let b = proxy.get_brightness().await.unwrap_or_default() as f64
|
let b = proxy.get_brightness().await.unwrap_or_default() as f64
|
||||||
/ proxy.get_max_brightness().await.unwrap_or(1) as f64;
|
/ proxy.get_max_brightness().await.unwrap_or(1) as f64;
|
||||||
(
|
_ = output.send(KeyboardBacklightUpdate::Update(b)).await;
|
||||||
Some((id, KeyboardBacklightUpdate::Update(b))),
|
State::Waiting(proxy, rx)
|
||||||
State::Waiting(proxy, rx),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
KeyboardBacklightRequest::Set(value) => {
|
KeyboardBacklightRequest::Set(value) => {
|
||||||
if let Ok(max_brightness) = proxy.get_max_brightness().await {
|
if let Ok(max_brightness) = proxy.get_max_brightness().await {
|
||||||
|
|
@ -102,10 +91,10 @@ async fn start_listening<I: Copy>(
|
||||||
let _ = proxy.set_brightness(value).await;
|
let _ = proxy.set_brightness(value).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
(None, State::Waiting(proxy, rx))
|
State::Waiting(proxy, rx)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None => (None, State::Finished),
|
None => State::Finished,
|
||||||
},
|
},
|
||||||
State::Finished => iced::futures::future::pending().await,
|
State::Finished => iced::futures::future::pending().await,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue