progress
This commit is contained in:
parent
620e1feac5
commit
bd5868593d
4 changed files with 185 additions and 150 deletions
|
|
@ -2,138 +2,31 @@ use iced::{
|
|||
Subscription, Task,
|
||||
widget::{Column, button, text},
|
||||
};
|
||||
use iced_layershell::reexport::{Anchor, NewLayerShellSettings};
|
||||
use ppd::{PpdProxyBlocking, Result};
|
||||
use zbus::blocking::Connection;
|
||||
use iced_layershell::reexport::{Anchor, NewLayerShellSettings, WithConnection};
|
||||
use zbus::Connection;
|
||||
|
||||
use crate::widget::{Message, PanelWidget};
|
||||
|
||||
pub struct PowerManagementWidget<'a> {
|
||||
connection: Option<Connected<'a>>,
|
||||
use brightness::Brightness;
|
||||
|
||||
struct BrightnessWidget {
|
||||
conn: Option<BrightnessConnection>,
|
||||
}
|
||||
|
||||
struct Connected<'a> {
|
||||
window: Option<iced::window::Id>,
|
||||
current_mode: Box<str>,
|
||||
modes: Box<[Box<str>]>,
|
||||
proxy: PpdProxyBlocking<'a>,
|
||||
struct BrightnessConnection {
|
||||
device: brightness::BrightnessDevice,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum PowerManagement {
|
||||
ToggleWindow,
|
||||
SetMode(String),
|
||||
}
|
||||
|
||||
impl PowerManagementWidget<'_> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
connection: Self::establish_connection().ok(),
|
||||
}
|
||||
impl PanelWidget for BrightnessWidget {
|
||||
fn update(&mut self, message: &Message) -> Task<Message> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn establish_connection<'a>() -> Result<Connected<'a>> {
|
||||
let conn = Connection::system().unwrap();
|
||||
let proxy = PpdProxyBlocking::new(&conn)?;
|
||||
fn subscribe(&self) -> Subscription<Message> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
let modes: Box<[Box<str>]> = proxy
|
||||
.profiles()?
|
||||
.iter()
|
||||
.map(|f| f.profile.clone().into_boxed_str())
|
||||
.collect();
|
||||
|
||||
let current_mode = proxy.active_profile()?.into_boxed_str();
|
||||
|
||||
Ok(Connected {
|
||||
current_mode,
|
||||
modes,
|
||||
proxy,
|
||||
window: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl PanelWidget for PowerManagementWidget<'_> {
|
||||
fn update(&mut self, message: &crate::widget::Message) -> iced::Task<crate::widget::Message> {
|
||||
let Message::PowerManagement(msg) = message else {
|
||||
return Task::none();
|
||||
};
|
||||
|
||||
let Some(conn) = &mut self.connection else {
|
||||
return Task::none();
|
||||
};
|
||||
|
||||
match msg {
|
||||
PowerManagement::ToggleWindow => match conn.window {
|
||||
Some(child) => {
|
||||
conn.window = None;
|
||||
Task::done(Message::RemoveWindow(child))
|
||||
}
|
||||
None => {
|
||||
let id = iced::window::Id::unique();
|
||||
conn.window = Some(id);
|
||||
|
||||
Task::done(Message::NewLayerShell {
|
||||
settings: NewLayerShellSettings {
|
||||
size: Some((220, 400)),
|
||||
anchor: Anchor::Top,
|
||||
layer: iced_layershell::reexport::Layer::Overlay,
|
||||
keyboard_interactivity:
|
||||
iced_layershell::reexport::KeyboardInteractivity::OnDemand,
|
||||
exclusive_zone: None,
|
||||
..Default::default()
|
||||
},
|
||||
id,
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
PowerManagement::SetMode(mode) => {
|
||||
let _ = conn.proxy.set_active_profile(mode.into());
|
||||
|
||||
conn.current_mode = conn.proxy.active_profile().unwrap().into_boxed_str();
|
||||
Task::none()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn subscribe(&self) -> iced::Subscription<crate::widget::Message> {
|
||||
Subscription::none()
|
||||
}
|
||||
|
||||
fn view(&self) -> Option<iced::Element<'_, crate::widget::Message>> {
|
||||
let Some(conn) = &self.connection else {
|
||||
return None;
|
||||
};
|
||||
|
||||
let output = button(text!("{}", conn.current_mode))
|
||||
.on_press(Message::PowerManagement(PowerManagement::ToggleWindow))
|
||||
.into();
|
||||
|
||||
Some(output)
|
||||
}
|
||||
|
||||
fn render_window(
|
||||
&self,
|
||||
id: iced::window::Id,
|
||||
) -> Option<iced::Element<'_, crate::widget::Message>> {
|
||||
let Some(conn) = &self.connection else {
|
||||
return None;
|
||||
};
|
||||
|
||||
if conn.window != Some(id) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let output = Column::with_children(conn.modes.iter().map(|f| {
|
||||
button(text!("{}", f))
|
||||
.on_press(Message::PowerManagement(PowerManagement::SetMode(
|
||||
f.clone().into_string(),
|
||||
)))
|
||||
.into()
|
||||
}))
|
||||
.into();
|
||||
|
||||
Some(output)
|
||||
fn view(&self) -> Option<iced::Element<'_, Message>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ use iced::{
|
|||
widget::{Column, button, text},
|
||||
};
|
||||
use iced_layershell::reexport::{Anchor, NewLayerShellSettings};
|
||||
use ppd::{PpdProxyBlocking, Result};
|
||||
use zbus::blocking::Connection;
|
||||
use ppd::{PpdProxy, Result};
|
||||
use zbus::Connection;
|
||||
|
||||
use crate::widget::{Message, PanelWidget};
|
||||
|
||||
|
|
@ -16,7 +16,7 @@ struct Connected<'a> {
|
|||
window: Option<iced::window::Id>,
|
||||
current_mode: Box<str>,
|
||||
modes: Box<[Box<str>]>,
|
||||
proxy: PpdProxyBlocking<'a>,
|
||||
proxy: PpdProxy<'a>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
|
@ -27,22 +27,21 @@ pub enum PowerManagement {
|
|||
|
||||
impl PowerManagementWidget<'_> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
connection: Self::establish_connection().ok(),
|
||||
}
|
||||
Self { connection: None }
|
||||
}
|
||||
|
||||
fn establish_connection<'a>() -> Result<Connected<'a>> {
|
||||
let conn = Connection::system().unwrap();
|
||||
let proxy = PpdProxyBlocking::new(&conn)?;
|
||||
async fn establish_connection<'a>() -> Result<Connected<'a>> {
|
||||
let conn = Connection::session().await?;
|
||||
let proxy = PpdProxy::new(&conn).await?;
|
||||
|
||||
let modes: Box<[Box<str>]> = proxy
|
||||
.profiles()?
|
||||
.profiles()
|
||||
.await?
|
||||
.iter()
|
||||
.map(|f| f.profile.clone().into_boxed_str())
|
||||
.collect();
|
||||
|
||||
let current_mode = proxy.active_profile()?.into_boxed_str();
|
||||
let current_mode = proxy.active_profile().await?.into_boxed_str();
|
||||
|
||||
Ok(Connected {
|
||||
current_mode,
|
||||
|
|
@ -90,8 +89,8 @@ impl PanelWidget for PowerManagementWidget<'_> {
|
|||
|
||||
PowerManagement::SetMode(mode) => {
|
||||
let _ = conn.proxy.set_active_profile(mode.into());
|
||||
conn.current_mode = mode.clone().into_boxed_str();
|
||||
|
||||
conn.current_mode = conn.proxy.active_profile().unwrap().into_boxed_str();
|
||||
Task::none()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue