separated app to separate file

This commit is contained in:
Jiří Maxmilián Stříbrný 2026-03-20 20:44:12 +01:00
parent 80d13f40e8
commit 23fe371656
2 changed files with 75 additions and 73 deletions

73
src/app.rs Normal file
View file

@ -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<Box<dyn PanelWidget>>,
}
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<Message> {
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<Message> {
Subscription::batch(self.widgets.iter().map(|w| w.subscribe()))
}
}

View file

@ -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<Box<dyn widget::PanelWidget>>,
}
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<Message> {
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<Message> {
Subscription::batch(self.widgets.iter().map(|w| w.subscribe()))
}
}