From 715527b138d6b0535dc24e9d7a2a03661eaeebef Mon Sep 17 00:00:00 2001 From: Daniel Bulant Date: Sat, 11 Jan 2025 10:29:37 +0100 Subject: [PATCH] code cleanup --- src/bar/battery.rs | 2 +- src/bar/mod.rs | 34 +++++++++++----------------------- src/bar/spotify.rs | 7 ++----- src/bar/time.rs | 4 ++-- src/main.rs | 7 ++----- src/widgets.rs | 13 +++++++++++++ 6 files changed, 31 insertions(+), 36 deletions(-) create mode 100644 src/widgets.rs diff --git a/src/bar/battery.rs b/src/bar/battery.rs index cf19c58..2f13e7d 100644 --- a/src/bar/battery.rs +++ b/src/bar/battery.rs @@ -1,7 +1,7 @@ use battery::State; use cushy::{styles::components::TextColor, widget::MakeWidget}; -use crate::theme::{TEXT_BATTERY, TEXT_TEMP, WIDGET_PADDING}; +use crate::theme::{TEXT_BATTERY, WIDGET_PADDING}; const BATTERY_LOW: &str = "󱃍"; diff --git a/src/bar/mod.rs b/src/bar/mod.rs index bb0f716..7bc2acc 100644 --- a/src/bar/mod.rs +++ b/src/bar/mod.rs @@ -8,16 +8,18 @@ use cushy::{ styles::{ components::{ BaseLineHeight, BaseTextSize, CornerRadius, DefaultBackgroundColor, FontWeight, - WidgetBackground, }, FontFamilyList, }, - value::{Dynamic, Source}, + value::Dynamic, widget::MakeWidget, Application, Open, }; -use crate::theme::{BG_DEFAULT, CORNER_RADIUS, DEFAULT_FONT_WEIGHT, TEXT_FONT, TEXT_SIZE}; +use crate::{ + theme::{BG_DEFAULT, CORNER_RADIUS, DEFAULT_FONT_WEIGHT, TEXT_FONT, TEXT_SIZE}, + widgets::WidgetExt, +}; mod battery; mod spotify; @@ -29,27 +31,13 @@ pub fn start_bar(app: &mut impl Application) -> cushy::Result { monitor_size.height = UPx::new(40); monitor_size.width = UPx::new((monitor_size.width.get() as f64 / 1.25) as _); let size = Dynamic::new(monitor_size); - let mut window = ((time::time_widget() - .expand_vertically() - .with(&WidgetBackground, BG_DEFAULT) - .pad()) - .and( - spotify::spotify_controls() - .expand_vertically() - .with(&WidgetBackground, BG_DEFAULT) - .pad(), - ) + let mut window = ((time::time_widget().bar_pill()) + .and(spotify::spotify_controls().bar_pill()) + .into_columns() + .centered() + .expand_horizontally()) + .and(battery().bar_pill()) .into_columns() - .centered() - .expand_horizontally()) - .and( - battery() - .expand_vertically() - .with(&WidgetBackground, BG_DEFAULT) - .pad(), - ) - .into_columns() - .centered() .expand_horizontally() .width(monitor_size.width) .height(Lp::points(30)) diff --git a/src/bar/spotify.rs b/src/bar/spotify.rs index 266fdae..73c4baf 100644 --- a/src/bar/spotify.rs +++ b/src/bar/spotify.rs @@ -6,16 +6,13 @@ use std::{ use crate::{ rt::tokio_runtime, - theme::{BG_DEFAULT, CORNER_RADIUS, TEXT_SPOTIFY, WIDGET_PADDING}, + theme::{CORNER_RADIUS, TEXT_SPOTIFY, WIDGET_PADDING}, vibrancy::Vibrancy, }; use cushy::{ figures::{units::Lp, Size, Zero}, kludgine::{AnyTexture, LazyTexture}, - styles::{ - components::{TextColor, WidgetBackground}, - Color, CornerRadii, Dimension, DimensionRange, - }, + styles::{components::TextColor, Color, CornerRadii, Dimension, DimensionRange}, value::{Destination, Dynamic, Source}, widget::MakeWidget, widgets::{image::ImageCornerRadius, label::Displayable, Image}, diff --git a/src/bar/time.rs b/src/bar/time.rs index 2b08195..85f8018 100644 --- a/src/bar/time.rs +++ b/src/bar/time.rs @@ -1,13 +1,13 @@ use chrono::{Local, Timelike}; use cushy::{ - styles::components::{TextColor, WidgetBackground}, + styles::components::TextColor, value::{Destination, Dynamic, MapEach}, widget::MakeWidget, }; use crate::{ rt::tokio_runtime, - theme::{BG_DEFAULT, TEXT_CLOCK, WIDGET_PADDING}, + theme::{TEXT_CLOCK, WIDGET_PADDING}, }; const FORMAT: &'static str = " %H:%M %p 󰃭 %a %d "; diff --git a/src/main.rs b/src/main.rs index b7b10aa..f662e30 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ mod menu; pub mod rt; mod theme; mod vibrancy; +pub mod widgets; fn main() -> cushy::Result { let args = Args::parse(); @@ -19,11 +20,7 @@ fn main() -> cushy::Result { Commands::Bar => start_bar(app).unwrap(), Commands::Menu => start_menu(app).unwrap(), Commands::Power => { - let win = "Hello world!".open(app).unwrap(); - // std::thread::spawn(move || { - // std::thread::sleep(std::time::Duration::from_secs(2)); - // win.request_close(); - // }); + "Hello world!".open(app).unwrap(); } }); diff --git a/src/widgets.rs b/src/widgets.rs new file mode 100644 index 0000000..8b9d398 --- /dev/null +++ b/src/widgets.rs @@ -0,0 +1,13 @@ +use cushy::{styles::components::WidgetBackground, widget::MakeWidget, widgets::Container}; + +use crate::theme::BG_DEFAULT; + +pub trait WidgetExt: MakeWidget { + fn bar_pill(self) -> Container { + self.expand_vertically() + .with(&WidgetBackground, BG_DEFAULT) + .pad() + } +} + +impl WidgetExt for T {}