power management start
This commit is contained in:
parent
3789f5eb7b
commit
99b2e565b9
7 changed files with 281 additions and 2 deletions
|
|
@ -19,9 +19,9 @@ impl App {
|
|||
pub fn new() -> Self {
|
||||
Self {
|
||||
widgets: vec![
|
||||
Box::new(ClockWidget::new()),
|
||||
Box::new(Spacer::new(iced::Length::Fill)),
|
||||
Box::new(ShutdownWidget::new()),
|
||||
Box::new(Spacer::new(iced::Length::Fixed(5.))),
|
||||
Box::new(ClockWidget::new()),
|
||||
Box::new(Spacer::new(iced::Length::Fill)),
|
||||
Box::new(BatteryWidget::new()),
|
||||
],
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ use iced::Subscription;
|
|||
use iced::Task;
|
||||
use iced_layershell::to_layer_message;
|
||||
|
||||
use crate::widgets::power_management::PowerManagement;
|
||||
use crate::widgets::powerbutton::ShutdownEvents;
|
||||
|
||||
pub trait PanelWidget {
|
||||
|
|
@ -20,4 +21,5 @@ pub enum Message {
|
|||
Battery(Option<f64>),
|
||||
Time,
|
||||
ShutdownEvent(ShutdownEvents),
|
||||
PowerManagement(PowerManagement),
|
||||
}
|
||||
|
|
|
|||
69
src/widgets/focused_window.rs
Normal file
69
src/widgets/focused_window.rs
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
use iced::{Subscription, Task, futures::SinkExt};
|
||||
use niri_ipc::{Event, Request, Response, Window, socket::Socket};
|
||||
|
||||
use crate::widget::{Message, PanelWidget};
|
||||
|
||||
pub struct FocusedWindowWidget {
|
||||
focused_window: Option<u64>,
|
||||
}
|
||||
|
||||
impl FocusedWindowWidget {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
focused_window: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PanelWidget for FocusedWindowWidget {
|
||||
fn update(&mut self, message: &Message) -> iced::Task<Message> {
|
||||
let Message::FocusChanged(i) = message else {
|
||||
return Task::none();
|
||||
};
|
||||
|
||||
self.focused_window = *i;
|
||||
Task::none()
|
||||
}
|
||||
|
||||
fn subscribe(&self) -> Subscription<Message> {
|
||||
Subscription::run(|| {
|
||||
iced::stream::channel(16, async move |mut tx| {
|
||||
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
||||
use tokio::net::UnixStream;
|
||||
|
||||
let socket_path = std::env::var("NIRI_SOCKET").unwrap();
|
||||
let mut stream = UnixStream::connect(socket_path).await.unwrap();
|
||||
|
||||
// Send the EventStream request as newline-delimited JSON
|
||||
let request = serde_json::to_string(&niri_ipc::Request::EventStream).unwrap();
|
||||
stream
|
||||
.write_all(format!("{request}\n").as_bytes())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let mut reader = BufReader::new(stream);
|
||||
let mut line = String::new();
|
||||
|
||||
loop {
|
||||
line.clear();
|
||||
reader.read_line(&mut line).await.unwrap();
|
||||
|
||||
// First line back is the Reply, subsequent lines are Events
|
||||
if let Ok(event) = serde_json::from_str::<niri_ipc::Event>(line.trim())
|
||||
&& let niri_ipc::Event::WindowFocusChanged { id } = event
|
||||
{
|
||||
let _ = tx.send(Message::FocusChanged(id)).await;
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
fn view(&self, _id: iced::window::Id) -> iced::Element<'_, Message> {
|
||||
iced::widget::text!(
|
||||
"{}",
|
||||
self.focused_window
|
||||
.map_or("None".into(), |f| format!("{}", f))
|
||||
)
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
pub mod battery;
|
||||
pub mod clock;
|
||||
pub mod power_management;
|
||||
pub mod powerbutton;
|
||||
pub mod spacer;
|
||||
|
|
|
|||
52
src/widgets/power_management.rs
Normal file
52
src/widgets/power_management.rs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
use iced::{Subscription, Task};
|
||||
use ppd::PpdProxy;
|
||||
use zbus::connection::Connection;
|
||||
|
||||
use crate::widget::{Message, PanelWidget};
|
||||
|
||||
pub struct PowerManagementWidget {
|
||||
window: Option<iced::window::Id>,
|
||||
current_mode: usize,
|
||||
names: Vec<Box<str>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum PowerManagement {
|
||||
ToggleWindow,
|
||||
SetMode(SetMode),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum SetMode {
|
||||
Saver,
|
||||
Medium,
|
||||
Performant,
|
||||
}
|
||||
|
||||
impl PowerManagementWidget {
|
||||
pub fn new() -> Self {
|
||||
let conn = Connection::system().await.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
};
|
||||
|
||||
Task::none()
|
||||
}
|
||||
|
||||
fn subscribe(&self) -> iced::Subscription<crate::widget::Message> {
|
||||
Subscription::none()
|
||||
}
|
||||
|
||||
fn view(&self, id: iced::window::Id) -> iced::Element<'_, crate::widget::Message> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn has_window(&self, id: iced::window::Id) -> bool {
|
||||
self.window == Some(id)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue