refactor(bar-core): move the komorebi layout widget into the core

The two komorebi_layout.rs were identical except for the position of one
`pub use`. The widget moves to
crates/komorebi-bar-core/src/widgets/komorebi_layout.rs together with the
KomorebiLayoutExt trait; the shells re-export the module. Its three
komorebi_client calls go through the platform seam: new hook
`BarPlatform::send_komorebi_message` (next to send_komorebi_batch), which
both os.rs implement as `komorebi_client::send_message`, so each bar sends
exactly what it sent. DefaultLayout and SocketMessage come from
komorebi-layouts and komorebi-protocol (what komorebi_client re-exports).
About 290 lines per bar are gone.

Verified: native bar-core tests 3+6; schema harness macOS and Windows
byte-identical (3,774 / 9,091 lines); Windows clippy -D warnings --locked
clean; macOS fake-CC check --locked 0 errors (files touched first). Not
run: the bars; clicking the layout widget is unverified on both.
This commit is contained in:
LGUG2Z committed 2026-10-31 10:56:04 -07:00
1 parent ef89899bd9
commit 91840eb34f
8 files changed
+39 -317

No files matched your search

+3
View File
@@ -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<SocketMessage>) -> io::Result<()>;
@@ -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,
@@ -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;
+4
View File
@@ -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<SocketMessage>) -> io::Result<()> {
komorebi_client::send_batch(messages)
}
+1 -1
View File
@@ -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;
+4
View File
@@ -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<SocketMessage>) -> io::Result<()> {
komorebi_client::send_batch(messages)
}
@@ -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<usize>,
) -> bool;
fn on_click_option(&mut self, monitor_idx: usize, workspace_idx: Option<usize>);
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<usize>,
);
}
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<usize>,
) -> 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<usize>) {
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<usize>,
) {
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);
}
}
@@ -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;