diff --git a/crates/komorebi-bar-core/src/platform.rs b/crates/komorebi-bar-core/src/platform.rs index 107f1fb0..c7c2316e 100644 --- a/crates/komorebi-bar-core/src/platform.rs +++ b/crates/komorebi-bar-core/src/platform.rs @@ -38,6 +38,9 @@ pub trait BarPlatform: Send + Sync { /// Runs a command line in the bar's shell session (Windows: its PowerShell session). fn exec_shell_command(&self, command: &str) -> color_eyre::Result<()>; + /// Sends one message to the komorebi daemon. + fn send_komorebi_message(&self, message: &SocketMessage) -> io::Result<()>; + /// Sends messages to the komorebi daemon as one batch. fn send_komorebi_batch(&self, messages: Vec) -> io::Result<()>; diff --git a/komorebi-bar/src/widgets/komorebi_layout.rs b/crates/komorebi-bar-core/src/widgets/komorebi_layout.rs similarity index 86% rename from komorebi-bar/src/widgets/komorebi_layout.rs rename to crates/komorebi-bar-core/src/widgets/komorebi_layout.rs index 04d09d06..8ac4218f 100644 --- a/komorebi-bar/src/widgets/komorebi_layout.rs +++ b/crates/komorebi-bar-core/src/widgets/komorebi_layout.rs @@ -1,7 +1,9 @@ use crate::config::DisplayFormat; use crate::render::RenderConfig; use crate::selected_frame::SelectableFrame; -use crate::widgets::komorebi::KomorebiLayoutConfig; +use crate::platform::platform; +use crate::widgets::komorebi_config::KomorebiLayout; +use crate::widgets::komorebi_config::KomorebiLayoutConfig; use eframe::egui::Context; use eframe::egui::CornerRadius; use eframe::egui::FontId; @@ -13,8 +15,7 @@ use eframe::egui::StrokeKind; use eframe::egui::Ui; use eframe::egui::Vec2; use eframe::egui::vec2; -pub use komorebi_bar_core::widgets::komorebi_config::KomorebiLayout; -use komorebi_client::SocketMessage; +use komorebi_protocol::message::SocketMessage; /// What the layout widget does with a layout kind (the kind itself lives in the core). pub trait KomorebiLayoutExt { @@ -60,7 +61,7 @@ impl KomorebiLayoutExt for KomorebiLayout { match self { KomorebiLayout::Default(option) => { if let Some(ws_idx) = workspace_idx - && komorebi_client::send_message(&SocketMessage::WorkspaceLayout( + && platform().send_komorebi_message(&SocketMessage::WorkspaceLayout( monitor_idx, ws_idx, *option, @@ -71,7 +72,7 @@ impl KomorebiLayoutExt for KomorebiLayout { } } KomorebiLayout::Monocle => { - if komorebi_client::send_batch([ + if platform().send_komorebi_batch(vec![ SocketMessage::FocusMonitorAtCursor, SocketMessage::ToggleMonocle, ]) @@ -81,7 +82,7 @@ impl KomorebiLayoutExt for KomorebiLayout { } } KomorebiLayout::Floating => { - if komorebi_client::send_batch([ + if platform().send_komorebi_batch(vec![ SocketMessage::FocusMonitorAtCursor, SocketMessage::ToggleTiling, ]) @@ -91,7 +92,7 @@ impl KomorebiLayoutExt for KomorebiLayout { } } KomorebiLayout::Paused => { - if komorebi_client::send_message(&SocketMessage::TogglePause).is_err() { + if platform().send_komorebi_message(&SocketMessage::TogglePause).is_err() { tracing::error!("could not send message to komorebi: TogglePause"); } } @@ -118,44 +119,44 @@ impl KomorebiLayoutExt for KomorebiLayout { match self { KomorebiLayout::Default(layout) => match layout { - komorebi_client::DefaultLayout::BSP => { + komorebi_layouts::DefaultLayout::BSP => { painter.line_segment([c - vec2(0.0, r), c + vec2(0.0, r)], stroke); painter.line_segment([c, c + vec2(r, 0.0)], stroke); painter.line_segment([c + vec2(r / 2.0, 0.0), c + vec2(r / 2.0, r)], stroke); } - komorebi_client::DefaultLayout::Columns => { + komorebi_layouts::DefaultLayout::Columns => { painter.line_segment([c - vec2(r / 2.0, r), c + vec2(-r / 2.0, r)], stroke); painter.line_segment([c - vec2(0.0, r), c + vec2(0.0, r)], stroke); painter.line_segment([c - vec2(-r / 2.0, r), c + vec2(r / 2.0, r)], stroke); } - komorebi_client::DefaultLayout::Rows => { + komorebi_layouts::DefaultLayout::Rows => { painter.line_segment([c - vec2(r, r / 2.0), c + vec2(r, -r / 2.0)], stroke); painter.line_segment([c - vec2(r, 0.0), c + vec2(r, 0.0)], stroke); painter.line_segment([c - vec2(r, -r / 2.0), c + vec2(r, r / 2.0)], stroke); } - komorebi_client::DefaultLayout::VerticalStack => { + komorebi_layouts::DefaultLayout::VerticalStack => { painter.line_segment([c - vec2(0.0, r), c + vec2(0.0, r)], stroke); painter.line_segment([c, c + vec2(r, 0.0)], stroke); } - komorebi_client::DefaultLayout::RightMainVerticalStack => { + komorebi_layouts::DefaultLayout::RightMainVerticalStack => { painter.line_segment([c - vec2(0.0, r), c + vec2(0.0, r)], stroke); painter.line_segment([c - vec2(r, 0.0), c], stroke); } - komorebi_client::DefaultLayout::HorizontalStack => { + komorebi_layouts::DefaultLayout::HorizontalStack => { painter.line_segment([c - vec2(r, 0.0), c + vec2(r, 0.0)], stroke); painter.line_segment([c, c + vec2(0.0, r)], stroke); } - komorebi_client::DefaultLayout::UltrawideVerticalStack => { + komorebi_layouts::DefaultLayout::UltrawideVerticalStack => { painter.line_segment([c - vec2(r / 2.0, r), c + vec2(-r / 2.0, r)], stroke); painter.line_segment([c + vec2(r / 2.0, 0.0), c + vec2(r, 0.0)], stroke); painter.line_segment([c - vec2(-r / 2.0, r), c + vec2(r / 2.0, r)], stroke); } - komorebi_client::DefaultLayout::Grid => { + komorebi_layouts::DefaultLayout::Grid => { painter.line_segment([c - vec2(r, 0.0), c + vec2(r, 0.0)], stroke); painter.line_segment([c - vec2(0.0, r), c + vec2(0.0, r)], stroke); } // TODO: @CtByte can you think of a nice icon to draw here? - komorebi_client::DefaultLayout::Scrolling => { + komorebi_layouts::DefaultLayout::Scrolling => { painter.line_segment([c - vec2(r / 2.0, r), c + vec2(-r / 2.0, r)], stroke); painter.line_segment([c - vec2(0.0, r), c + vec2(0.0, r)], stroke); painter.line_segment([c - vec2(-r / 2.0, r), c + vec2(r / 2.0, r)], stroke); @@ -244,18 +245,18 @@ impl KomorebiLayoutExt for KomorebiLayout { ); let mut layout_options = layout_config.options.clone().unwrap_or(vec![ - KomorebiLayout::Default(komorebi_client::DefaultLayout::BSP), - KomorebiLayout::Default(komorebi_client::DefaultLayout::Columns), - KomorebiLayout::Default(komorebi_client::DefaultLayout::Rows), - KomorebiLayout::Default(komorebi_client::DefaultLayout::VerticalStack), + KomorebiLayout::Default(komorebi_layouts::DefaultLayout::BSP), + KomorebiLayout::Default(komorebi_layouts::DefaultLayout::Columns), + KomorebiLayout::Default(komorebi_layouts::DefaultLayout::Rows), + KomorebiLayout::Default(komorebi_layouts::DefaultLayout::VerticalStack), KomorebiLayout::Default( - komorebi_client::DefaultLayout::RightMainVerticalStack, + komorebi_layouts::DefaultLayout::RightMainVerticalStack, ), - KomorebiLayout::Default(komorebi_client::DefaultLayout::HorizontalStack), + KomorebiLayout::Default(komorebi_layouts::DefaultLayout::HorizontalStack), KomorebiLayout::Default( - komorebi_client::DefaultLayout::UltrawideVerticalStack, + komorebi_layouts::DefaultLayout::UltrawideVerticalStack, ), - KomorebiLayout::Default(komorebi_client::DefaultLayout::Grid), + KomorebiLayout::Default(komorebi_layouts::DefaultLayout::Grid), //KomorebiLayout::Custom, KomorebiLayout::Monocle, KomorebiLayout::Floating, diff --git a/crates/komorebi-bar-core/src/widgets/mod.rs b/crates/komorebi-bar-core/src/widgets/mod.rs index 0a231ba1..a07b36fe 100644 --- a/crates/komorebi-bar-core/src/widgets/mod.rs +++ b/crates/komorebi-bar-core/src/widgets/mod.rs @@ -4,6 +4,7 @@ pub mod cpu; pub mod date; pub mod icons; pub mod komorebi_config; +pub mod komorebi_layout; pub mod memory; pub mod network_config; pub mod platform_config; diff --git a/komorebi-bar/src/os.rs b/komorebi-bar/src/os.rs index 9dd97e41..1dcffa38 100644 --- a/komorebi-bar/src/os.rs +++ b/komorebi-bar/src/os.rs @@ -43,6 +43,10 @@ impl BarPlatform for WindowsBar { crate::bar::exec_powershell(command) } + fn send_komorebi_message(&self, message: &SocketMessage) -> io::Result<()> { + komorebi_client::send_message(message) + } + fn send_komorebi_batch(&self, messages: Vec) -> io::Result<()> { komorebi_client::send_batch(messages) } diff --git a/komorebi-bar/src/widgets/mod.rs b/komorebi-bar/src/widgets/mod.rs index bdb5f341..a8306d3d 100644 --- a/komorebi-bar/src/widgets/mod.rs +++ b/komorebi-bar/src/widgets/mod.rs @@ -5,7 +5,7 @@ pub use komorebi_bar_core::widgets::cpu; pub use komorebi_bar_core::widgets::date; pub mod keyboard; pub mod komorebi; -mod komorebi_layout; +pub use komorebi_bar_core::widgets::komorebi_layout; pub mod media; pub use komorebi_bar_core::widgets::memory; pub mod network; diff --git a/komorebi-for-mac/komorebi-bar/src/os.rs b/komorebi-for-mac/komorebi-bar/src/os.rs index 56d4efab..cccfcccd 100644 --- a/komorebi-for-mac/komorebi-bar/src/os.rs +++ b/komorebi-for-mac/komorebi-bar/src/os.rs @@ -46,6 +46,10 @@ impl BarPlatform for MacBar { Err(io::Error::new(io::ErrorKind::Unsupported, "no shell session on macOS").into()) } + fn send_komorebi_message(&self, message: &SocketMessage) -> io::Result<()> { + komorebi_client::send_message(message) + } + fn send_komorebi_batch(&self, messages: Vec) -> io::Result<()> { komorebi_client::send_batch(messages) } diff --git a/komorebi-for-mac/komorebi-bar/src/widgets/komorebi_layout.rs b/komorebi-for-mac/komorebi-bar/src/widgets/komorebi_layout.rs deleted file mode 100644 index 5c51684b..00000000 --- a/komorebi-for-mac/komorebi-bar/src/widgets/komorebi_layout.rs +++ /dev/null @@ -1,291 +0,0 @@ -use crate::config::DisplayFormat; -use crate::render::RenderConfig; -use crate::selected_frame::SelectableFrame; -use crate::widgets::komorebi::KomorebiLayoutConfig; -pub use komorebi_bar_core::widgets::komorebi_config::KomorebiLayout; -use eframe::egui::Context; -use eframe::egui::CornerRadius; -use eframe::egui::FontId; -use eframe::egui::Frame; -use eframe::egui::Label; -use eframe::egui::Sense; -use eframe::egui::Stroke; -use eframe::egui::StrokeKind; -use eframe::egui::Ui; -use eframe::egui::Vec2; -use eframe::egui::vec2; -use komorebi_client::SocketMessage; - -/// What the layout widget does with a layout kind (the kind itself lives in the core). -pub trait KomorebiLayoutExt { - fn is_default(&mut self) -> bool; - fn on_click( - &mut self, - show_options: &bool, - monitor_idx: usize, - workspace_idx: Option, - ) -> bool; - fn on_click_option(&mut self, monitor_idx: usize, workspace_idx: Option); - fn show_icon(&mut self, is_selected: bool, font_id: FontId, ctx: &Context, ui: &mut Ui); - fn show( - &mut self, - ctx: &Context, - ui: &mut Ui, - render_config: &mut RenderConfig, - layout_config: &KomorebiLayoutConfig, - workspace_idx: Option, - ); -} - -impl KomorebiLayoutExt for KomorebiLayout { - fn is_default(&mut self) -> bool { - matches!(self, KomorebiLayout::Default(_)) - } - - fn on_click( - &mut self, - show_options: &bool, - monitor_idx: usize, - workspace_idx: Option, - ) -> bool { - if self.is_default() { - !show_options - } else { - self.on_click_option(monitor_idx, workspace_idx); - false - } - } - - fn on_click_option(&mut self, monitor_idx: usize, workspace_idx: Option) { - match self { - KomorebiLayout::Default(option) => { - if let Some(ws_idx) = workspace_idx - && komorebi_client::send_message(&SocketMessage::WorkspaceLayout( - monitor_idx, - ws_idx, - *option, - )) - .is_err() - { - tracing::error!("could not send message to komorebi: WorkspaceLayout"); - } - } - KomorebiLayout::Monocle => { - if komorebi_client::send_batch([ - SocketMessage::FocusMonitorAtCursor, - SocketMessage::ToggleMonocle, - ]) - .is_err() - { - tracing::error!("could not send message to komorebi: ToggleMonocle"); - } - } - KomorebiLayout::Floating => { - if komorebi_client::send_batch([ - SocketMessage::FocusMonitorAtCursor, - SocketMessage::ToggleTiling, - ]) - .is_err() - { - tracing::error!("could not send message to komorebi: ToggleTiling"); - } - } - KomorebiLayout::Paused => { - if komorebi_client::send_message(&SocketMessage::TogglePause).is_err() { - tracing::error!("could not send message to komorebi: TogglePause"); - } - } - KomorebiLayout::Custom => {} - } - } - - fn show_icon(&mut self, is_selected: bool, font_id: FontId, ctx: &Context, ui: &mut Ui) { - // paint custom icons for the layout - let size = Vec2::splat(font_id.size); - let (response, painter) = ui.allocate_painter(size, Sense::hover()); - let color = if is_selected { - ctx.style().visuals.selection.stroke.color - } else { - ui.style().visuals.text_color() - }; - let stroke = Stroke::new(1.0_f32, color); - let mut rect = response.rect; - let rounding = CornerRadius::same((rect.width() * 0.1) as u8); - rect = rect.shrink(stroke.width); - let c = rect.center(); - let r = rect.width() / 2.0; - painter.rect_stroke(rect, rounding, stroke, StrokeKind::Outside); - - match self { - KomorebiLayout::Default(layout) => match layout { - komorebi_client::DefaultLayout::BSP => { - painter.line_segment([c - vec2(0.0, r), c + vec2(0.0, r)], stroke); - painter.line_segment([c, c + vec2(r, 0.0)], stroke); - painter.line_segment([c + vec2(r / 2.0, 0.0), c + vec2(r / 2.0, r)], stroke); - } - komorebi_client::DefaultLayout::Columns => { - painter.line_segment([c - vec2(r / 2.0, r), c + vec2(-r / 2.0, r)], stroke); - painter.line_segment([c - vec2(0.0, r), c + vec2(0.0, r)], stroke); - painter.line_segment([c - vec2(-r / 2.0, r), c + vec2(r / 2.0, r)], stroke); - } - komorebi_client::DefaultLayout::Rows => { - painter.line_segment([c - vec2(r, r / 2.0), c + vec2(r, -r / 2.0)], stroke); - painter.line_segment([c - vec2(r, 0.0), c + vec2(r, 0.0)], stroke); - painter.line_segment([c - vec2(r, -r / 2.0), c + vec2(r, r / 2.0)], stroke); - } - komorebi_client::DefaultLayout::VerticalStack => { - painter.line_segment([c - vec2(0.0, r), c + vec2(0.0, r)], stroke); - painter.line_segment([c, c + vec2(r, 0.0)], stroke); - } - komorebi_client::DefaultLayout::RightMainVerticalStack => { - painter.line_segment([c - vec2(0.0, r), c + vec2(0.0, r)], stroke); - painter.line_segment([c - vec2(r, 0.0), c], stroke); - } - komorebi_client::DefaultLayout::HorizontalStack => { - painter.line_segment([c - vec2(r, 0.0), c + vec2(r, 0.0)], stroke); - painter.line_segment([c, c + vec2(0.0, r)], stroke); - } - komorebi_client::DefaultLayout::UltrawideVerticalStack => { - painter.line_segment([c - vec2(r / 2.0, r), c + vec2(-r / 2.0, r)], stroke); - painter.line_segment([c + vec2(r / 2.0, 0.0), c + vec2(r, 0.0)], stroke); - painter.line_segment([c - vec2(-r / 2.0, r), c + vec2(r / 2.0, r)], stroke); - } - komorebi_client::DefaultLayout::Grid => { - painter.line_segment([c - vec2(r, 0.0), c + vec2(r, 0.0)], stroke); - painter.line_segment([c - vec2(0.0, r), c + vec2(0.0, r)], stroke); - } - // TODO: @CtByte can you think of a nice icon to draw here? - komorebi_client::DefaultLayout::Scrolling => { - painter.line_segment([c - vec2(r / 2.0, r), c + vec2(-r / 2.0, r)], stroke); - painter.line_segment([c - vec2(0.0, r), c + vec2(0.0, r)], stroke); - painter.line_segment([c - vec2(-r / 2.0, r), c + vec2(r / 2.0, r)], stroke); - } - }, - KomorebiLayout::Monocle => {} - KomorebiLayout::Floating => { - let mut rect_left = response.rect; - rect_left.set_width(rect.width() * 0.5); - rect_left.set_height(rect.height() * 0.5); - let mut rect_right = rect_left; - rect_left = rect_left.translate(Vec2::new( - rect.width() * 0.1 + stroke.width, - rect.width() * 0.1 + stroke.width, - )); - rect_right = rect_right.translate(Vec2::new( - rect.width() * 0.35 + stroke.width, - rect.width() * 0.35 + stroke.width, - )); - painter.rect_filled(rect_left, rounding, color); - painter.rect_stroke(rect_right, rounding, stroke, StrokeKind::Outside); - } - KomorebiLayout::Paused => { - let mut rect_left = response.rect; - rect_left.set_width(rect.width() * 0.25); - rect_left.set_height(rect.height() * 0.8); - let mut rect_right = rect_left; - rect_left = rect_left.translate(Vec2::new( - rect.width() * 0.2 + stroke.width, - rect.width() * 0.1 + stroke.width, - )); - rect_right = rect_right.translate(Vec2::new( - rect.width() * 0.55 + stroke.width, - rect.width() * 0.1 + stroke.width, - )); - painter.rect_filled(rect_left, rounding, color); - painter.rect_filled(rect_right, rounding, color); - } - KomorebiLayout::Custom => { - painter.line_segment([c - vec2(0.0, r), c + vec2(0.0, r)], stroke); - painter.line_segment([c + vec2(0.0, r / 2.0), c + vec2(r, r / 2.0)], stroke); - painter.line_segment([c - vec2(0.0, r / 3.0), c - vec2(r, r / 3.0)], stroke); - } - } - } - - fn show( - &mut self, - ctx: &Context, - ui: &mut Ui, - render_config: &mut RenderConfig, - layout_config: &KomorebiLayoutConfig, - workspace_idx: Option, - ) { - let monitor_idx = render_config.monitor_idx; - let font_id = render_config.icon_font_id.clone(); - let mut show_options = RenderConfig::load_show_komorebi_layout_options(); - let format = layout_config.display.unwrap_or(DisplayFormat::IconAndText); - - if !self.is_default() { - show_options = false; - } - - render_config.apply_on_widget(false, ui, |ui| { - let layout_frame = SelectableFrame::new(false) - .show(ui, |ui| { - if let DisplayFormat::Icon | DisplayFormat::IconAndText = format { - self.show_icon(true, font_id.clone(), ctx, ui); - } - - if let DisplayFormat::Text | DisplayFormat::IconAndText = format { - ui.add(Label::new(self.to_string()).selectable(false)); - } - }) - .on_hover_text(self.to_string()); - - if layout_frame.clicked() { - show_options = self.on_click(&show_options, monitor_idx, workspace_idx); - } - - if show_options && let Some(workspace_idx) = workspace_idx { - Frame::NONE.show(ui, |ui| { - ui.add( - Label::new(egui_phosphor::regular::ARROW_FAT_LINES_RIGHT.to_string()) - .selectable(false), - ); - - let mut layout_options = layout_config.options.clone().unwrap_or(vec![ - KomorebiLayout::Default(komorebi_client::DefaultLayout::BSP), - KomorebiLayout::Default(komorebi_client::DefaultLayout::Columns), - KomorebiLayout::Default(komorebi_client::DefaultLayout::Rows), - KomorebiLayout::Default(komorebi_client::DefaultLayout::VerticalStack), - KomorebiLayout::Default( - komorebi_client::DefaultLayout::RightMainVerticalStack, - ), - KomorebiLayout::Default(komorebi_client::DefaultLayout::HorizontalStack), - KomorebiLayout::Default( - komorebi_client::DefaultLayout::UltrawideVerticalStack, - ), - KomorebiLayout::Default(komorebi_client::DefaultLayout::Grid), - //KomorebiLayout::Custom, - KomorebiLayout::Monocle, - KomorebiLayout::Floating, - KomorebiLayout::Paused, - ]); - - for layout_option in &mut layout_options { - let is_selected = self == layout_option; - - if SelectableFrame::new(is_selected) - .show(ui, |ui| { - layout_option.show_icon(is_selected, font_id.clone(), ctx, ui) - }) - .on_hover_text(match layout_option { - KomorebiLayout::Default(layout) => layout.to_string(), - KomorebiLayout::Monocle => "Toggle monocle".to_string(), - KomorebiLayout::Floating => "Toggle tiling".to_string(), - KomorebiLayout::Paused => "Toggle pause".to_string(), - KomorebiLayout::Custom => "Custom".to_string(), - }) - .clicked() - { - layout_option.on_click_option(monitor_idx, Some(workspace_idx)); - show_options = false; - }; - } - }); - } - }); - - RenderConfig::store_show_komorebi_layout_options(show_options); - } -} diff --git a/komorebi-for-mac/komorebi-bar/src/widgets/mod.rs b/komorebi-for-mac/komorebi-bar/src/widgets/mod.rs index d301b556..e34ad182 100644 --- a/komorebi-for-mac/komorebi-bar/src/widgets/mod.rs +++ b/komorebi-for-mac/komorebi-bar/src/widgets/mod.rs @@ -4,7 +4,7 @@ pub use komorebi_bar_core::widgets::battery; pub use komorebi_bar_core::widgets::cpu; pub use komorebi_bar_core::widgets::date; pub mod komorebi; -mod komorebi_layout; +pub use komorebi_bar_core::widgets::komorebi_layout; pub use komorebi_bar_core::widgets::memory; pub mod network; pub use komorebi_bar_core::widgets::storage;