created brightness branch
This commit is contained in:
parent
83b396ae3e
commit
620e1feac5
2 changed files with 140 additions and 0 deletions
139
src/widgets/brightness.rs
Normal file
139
src/widgets/brightness.rs
Normal file
|
|
@ -0,0 +1,139 @@
|
||||||
|
use iced::{
|
||||||
|
Subscription, Task,
|
||||||
|
widget::{Column, button, text},
|
||||||
|
};
|
||||||
|
use iced_layershell::reexport::{Anchor, NewLayerShellSettings};
|
||||||
|
use ppd::{PpdProxyBlocking, Result};
|
||||||
|
use zbus::blocking::Connection;
|
||||||
|
|
||||||
|
use crate::widget::{Message, PanelWidget};
|
||||||
|
|
||||||
|
pub struct PowerManagementWidget<'a> {
|
||||||
|
connection: Option<Connected<'a>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Connected<'a> {
|
||||||
|
window: Option<iced::window::Id>,
|
||||||
|
current_mode: Box<str>,
|
||||||
|
modes: Box<[Box<str>]>,
|
||||||
|
proxy: PpdProxyBlocking<'a>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum PowerManagement {
|
||||||
|
ToggleWindow,
|
||||||
|
SetMode(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PowerManagementWidget<'_> {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
connection: Self::establish_connection().ok(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn establish_connection<'a>() -> Result<Connected<'a>> {
|
||||||
|
let conn = Connection::system().unwrap();
|
||||||
|
let proxy = PpdProxyBlocking::new(&conn)?;
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
pub mod battery;
|
pub mod battery;
|
||||||
|
pub mod brightness;
|
||||||
pub mod clock;
|
pub mod clock;
|
||||||
pub mod power_management;
|
pub mod power_management;
|
||||||
pub mod powerbutton;
|
pub mod powerbutton;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue