diff --git a/src/app.rs b/src/app.rs new file mode 100644 index 0000000..4cd19af --- /dev/null +++ b/src/app.rs @@ -0,0 +1,73 @@ +use crate::widget::{Message, PanelWidget}; +use crate::widgets::battery::BatteryWidget; +use crate::widgets::clock::ClockWidget; +use crate::widgets::spacer::Spacer; + +use iced::Color; +use iced::Element; +use iced::Subscription; +use iced::Task; + +pub struct App { + widgets: Vec>, +} + +impl App { + pub const WINDOW_HEIGHT: u32 = 32; + + pub fn new() -> Self { + Self { + widgets: vec![ + Box::new(ClockWidget::new()), + Box::new(Spacer::new(iced::Length::Fill)), + Box::new(BatteryWidget::new()), + ], + } + } + + pub fn name() -> String { + "wayland_panel".into() + } + + pub fn view(&self, id: iced::window::Id) -> Element<'_, Message> { + if let Some(elem) = self + .widgets + .iter() + .find(|widget| widget.own_window(id)) + .map(|widget| widget.view(id)) + { + return elem; + } + + iced::widget::Row::with_children(self.widgets.iter().map(|widget| widget.view(id))) + .padding(iced::Padding::from([0, 5])) + .height(iced::Length::Fill) + .width(iced::Length::Fill) + .align_y(iced::Alignment::Center) + .into() + } + + pub fn update(&mut self, message: Message) -> Task { + let mut output = Vec::new(); + for widget in &mut self.widgets { + output.push(widget.update(&message)); + } + + Task::batch(output) + } + + pub fn style(&self, theme: &iced::Theme) -> iced::theme::Style { + iced::theme::Style { + background_color: Color::TRANSPARENT, + text_color: theme.palette().text, + } + } + + pub fn theme(&self, _id: iced::window::Id) -> iced::Theme { + iced::Theme::GruvboxDark + } + + pub fn subscription(&self) -> iced::Subscription { + Subscription::batch(self.widgets.iter().map(|w| w.subscribe())) + } +} diff --git a/src/main.rs b/src/main.rs index 218fed1..2989820 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,12 @@ -use iced::Color; -use iced::Element; -use iced::Subscription; -use iced::Task; use iced_layershell::daemon; use iced_layershell::reexport::Anchor; use iced_layershell::settings::LayerShellSettings; use iced_layershell::settings::Settings; +mod app; mod widget; mod widgets; - -use crate::widget::Message; -use crate::widgets::battery::BatteryWidget; -use crate::widgets::clock::ClockWidget; -use crate::widgets::spacer::Spacer; +use app::App; pub fn main() -> Result<(), iced_layershell::Error> { daemon(App::new, App::name, App::update, App::view) @@ -32,67 +25,3 @@ pub fn main() -> Result<(), iced_layershell::Error> { }) .run() } - -struct App { - widgets: Vec>, -} - -impl App { - const WINDOW_HEIGHT: u32 = 32; - - fn new() -> Self { - Self { - widgets: vec![ - Box::new(ClockWidget::new()), - Box::new(Spacer::new(iced::Length::Fill)), - Box::new(BatteryWidget::new()), - ], - } - } - - fn name() -> String { - "wayland_panel".into() - } - - fn view(&self, id: iced::window::Id) -> Element<'_, Message> { - if let Some(elem) = self - .widgets - .iter() - .find(|widget| widget.own_window(id)) - .map(|widget| widget.view(id)) - { - return elem; - } - - iced::widget::Row::with_children(self.widgets.iter().map(|widget| widget.view(id))) - .padding(iced::Padding::from([0, 5])) - .height(iced::Length::Fill) - .width(iced::Length::Fill) - .align_y(iced::Alignment::Center) - .into() - } - - fn update(&mut self, message: Message) -> Task { - let mut output = Vec::new(); - for widget in &mut self.widgets { - output.push(widget.update(&message)); - } - - Task::batch(output) - } - - fn style(&self, theme: &iced::Theme) -> iced::theme::Style { - iced::theme::Style { - background_color: Color::TRANSPARENT, - text_color: theme.palette().text, - } - } - - fn theme(&self, _id: iced::window::Id) -> iced::Theme { - iced::Theme::GruvboxDark - } - - fn subscription(&self) -> iced::Subscription { - Subscription::batch(self.widgets.iter().map(|w| w.subscribe())) - } -}