pub mod audio; pub mod bluetooth; pub mod clock; pub mod icon; pub mod network; pub mod notifications; pub mod power; pub mod separator; pub mod sysmon; pub mod tray; pub mod window; pub mod workspaces; use std::collections::HashMap; use iced::{Element, Event, mouse}; use slowshell_commons::panels::{PanelEdge, PanelOrientation}; use slowshell_config::Config; use slowshell_core::{ Store, listeners::ListenerAction, message::{EventFilter, ItemEffect, ItemMessage}, types::{ToUstr, Ustr}, }; use slowshell_widgets::EventWrapper; pub trait Component: Send { fn events(&self) -> Vec { Vec::new() } fn watch(&mut self, store: &mut Store, options: Option<&ComponentOptions>) { let _ = (store, options); } fn stop(&mut self, store: &Store, options: Option<&ComponentOptions>) { let _ = (store, options); } fn update( &mut self, config: &Config, store: &mut Store, event: &ListenerAction, options: Option<&ComponentOptions>, ) -> anyhow::Result { let _ = (config, store, event, options); Ok(ItemEffect::None) } fn check_view(&self, store: &Store, options: Option<&ComponentOptions>) -> bool { let _ = (store, options); true } fn view<'a>( &self, config: &Config, store: &Store, ctx: &ComponentContext, options: Option<&ComponentOptions>, ) -> Element<'a, ItemMessage>; } pub struct ComponentOptions { inner: HashMap, } impl ComponentOptions { pub fn number(&self, id: impl Into) -> Option { self .inner .get(&id.into()) .and_then(|s| s.as_float().map(|x| x as f32)) } pub fn int(&self, id: impl Into) -> Option { self .inner .get(&id.into()) .and_then(|s| s.as_integer().map(|x| x as i32)) } pub fn str(&self, id: impl Into) -> Option<&str> { self.inner.get(&id.into()).and_then(|s| s.as_str()) } pub fn bool(&self, id: impl Into) -> Option { self.inner.get(&id.into()).and_then(|b| b.as_bool()) } pub fn table(&self, id: impl Into) -> Option<&toml::Table> { self .inner .get(&id.into()) .and_then(|value| value.as_table()) } pub fn strings(&self, id: impl Into) -> Option> { self .inner .get(&id.into()) .and_then(|value| value.as_str()) .map(|s| s.split(",").collect::>()) } } impl From> for ComponentOptions { fn from(value: HashMap) -> Self { ComponentOptions { inner: value } } } pub struct ComponentContext<'a> { pub panel_name: &'a str, pub position: PanelEdge, pub orientation: PanelOrientation, pub monitor: &'a str, } impl<'a> ComponentContext<'a> { pub fn resolve_style(&self, config: &slowshell_config::Config) -> slowshell_config::style::Style { if self.orientation == PanelOrientation::Horizontal { config.style("component") } else { config.style_any(&["component.vertical", "component"]) } } } pub fn vertical_text<'a, Message: 'a>( content: &str, font_size: f32, color: iced::Color, ) -> Element<'a, Message> { use iced::widget::{column, text}; let items: Vec> = content .chars() .map(|ch| text(ch.to_string()).size(font_size).color(color).into()) .collect(); column(items).spacing(0).align_x(iced::Alignment::Center).into() } pub struct MenuConfig { pub content: String, pub panel_name: Option, pub position: PanelEdge, } pub fn popup_open_action(config: &MenuConfig, cursor: iced::Point) -> ListenerAction { let mut payload: HashMap = HashMap::new(); payload.insert("content".to_ustr(), config.content.to_ustr()); let (x, y) = match config.position { PanelEdge::Top { .. } | PanelEdge::Bottom { .. } => ( format!("fixed,{}", cursor.x), if let Some(panel) = &config.panel_name { format!("panel,{}", panel) } else { format!("fixed,0.0") }, ), PanelEdge::Left { width } => { payload.insert("edge".to_ustr(), "left".to_ustr()); ( format!("fixed,{}", width as f32), format!("fixed,{}", cursor.y), ) } PanelEdge::Right { .. } => { payload.insert("edge".to_ustr(), "right".to_ustr()); ( format!("fixed,0.0"), format!("fixed,{}", cursor.y), ) } }; payload.insert("x".to_ustr(), x.to_ustr()); payload.insert("y".to_ustr(), y.to_ustr()); ListenerAction::Payload { name: "popup.open".to_ustr(), payload: Some(payload), } } pub fn menu_trigger<'a>( content: Element<'a, ItemMessage>, config: MenuConfig, ) -> Element<'a, ItemMessage> { let action = move |event: &Event, layout: iced::advanced::layout::Layout<'_>, cursor: mouse::Cursor| { if !cursor.is_over(layout.bounds()) { return None; } let Event::Mouse(mouse::Event::ButtonPressed(button)) = event else { return None; }; if !matches!(button, mouse::Button::Left | mouse::Button::Right) { return None; } let Some(point) = cursor.position() else { return None; }; Some(ItemMessage::Action(popup_open_action(&config, point))) }; EventWrapper::new(content, action).into() } pub type ComponentFactory = Box Box + Send + Sync>; pub type Components = HashMap; pub fn register_all(components: &mut Components) { components.insert( "core/audio".into(), Box::new(|| Box::new(audio::Audio::new())), ); components.insert( "core/sound".into(), Box::new(|| Box::new(audio::Audio::new())), ); components.insert( "core/clock".into(), Box::new(|| Box::new(clock::Clock::new())), ); components.insert( "core/power".into(), Box::new(|| Box::new(power::Power::new())), ); components.insert( "core/system".into(), Box::new(|| Box::new(sysmon::SystemMon::new())), ); components.insert( "core/network".into(), Box::new(|| Box::new(network::Network::new())), ); components.insert( "core/bluetooth".into(), Box::new(|| Box::new(bluetooth::Bluetooth::new())), ); components.insert( "core/notifications".into(), Box::new(|| Box::new(notifications::Notifications::new())), ); components.insert( "core/icon".into(), Box::new(|| Box::new(icon::IconComp::default())), ); components.insert( "core/tray".into(), Box::new(|| Box::new(tray::SystemTray::new())), ); components.insert( "core/separator".into(), Box::new(|| Box::new(separator::Separator)), ); components.insert( "core/workspaces".into(), Box::new(|| Box::new(workspaces::Workspaces)), ); components.insert("core/window".into(), Box::new(|| Box::new(window::Window))); } slowshell_registry::register_resources!( component: Style( slowshell_config::style! { "color" => "text", "color.faded" => "subtext", "color.invert" => "base", "color.primary" => "primary", "font.size" => 12, "icon.size" => 14, "spacing" => 5, "padding.x" => 9, "padding.y" => 5, } ) ); pub fn register_vertical_style() { slowshell_config::style::new_default_style( "component.vertical".into(), slowshell_config::style! { "color" => "text", "color.faded" => "subtext", "color.invert" => "base", "color.primary" => "primary", "font.size" => 15, "icon.size" => 18, "spacing" => 8, "padding.x" => 5, "padding.y" => 4, }, ); }