diff --git a/Cargo.lock b/Cargo.lock index a1964393..8fbf03ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3404,6 +3404,7 @@ dependencies = [ "font-loader", "hotwatch", "image 0.25.10", + "komorebi-bar-core", "komorebi-client", "komorebi-themes", "lazy_static", @@ -3434,6 +3435,9 @@ dependencies = [ [[package]] name = "komorebi-bar-core" version = "0.1.42" +dependencies = [ + "eframe", +] [[package]] name = "komorebi-client" diff --git a/crates/komorebi-bar-core/Cargo.toml b/crates/komorebi-bar-core/Cargo.toml index f5edd858..796901a9 100644 --- a/crates/komorebi-bar-core/Cargo.toml +++ b/crates/komorebi-bar-core/Cargo.toml @@ -3,3 +3,6 @@ name = "komorebi-bar-core" version = "0.1.42" description = "The status bar shared by the Windows and macOS komorebi bars: configuration, widgets and the platform seam" edition = "2024" + +[dependencies] +eframe = { workspace = true } diff --git a/crates/komorebi-bar-core/src/lib.rs b/crates/komorebi-bar-core/src/lib.rs index fa91b405..a679df2b 100644 --- a/crates/komorebi-bar-core/src/lib.rs +++ b/crates/komorebi-bar-core/src/lib.rs @@ -4,3 +4,6 @@ //! oracle in `tests/schema_oracle.rs`: the checked-in `schema.bar.json` of both bars must not //! change unless a reviewed revision says so. #![warn(clippy::all)] + +pub mod selected_frame; +pub mod ui; diff --git a/komorebi-bar/src/selected_frame.rs b/crates/komorebi-bar-core/src/selected_frame.rs similarity index 100% rename from komorebi-bar/src/selected_frame.rs rename to crates/komorebi-bar-core/src/selected_frame.rs diff --git a/komorebi-bar/src/ui.rs b/crates/komorebi-bar-core/src/ui.rs similarity index 100% rename from komorebi-bar/src/ui.rs rename to crates/komorebi-bar-core/src/ui.rs diff --git a/komorebi-bar/Cargo.toml b/komorebi-bar/Cargo.toml index 12058736..803957e0 100644 --- a/komorebi-bar/Cargo.toml +++ b/komorebi-bar/Cargo.toml @@ -6,6 +6,7 @@ edition = "2024" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +komorebi-bar-core = { path = "../crates/komorebi-bar-core" } komorebi-client = { path = "../komorebi-client", default-features = false } komorebi-themes = { path = "../komorebi-themes", default-features = false } diff --git a/komorebi-bar/src/main.rs b/komorebi-bar/src/main.rs index 40671e4e..e01ed678 100644 --- a/komorebi-bar/src/main.rs +++ b/komorebi-bar/src/main.rs @@ -1,8 +1,8 @@ mod bar; mod config; mod render; -mod selected_frame; -mod ui; +pub use komorebi_bar_core::selected_frame; +pub use komorebi_bar_core::ui; mod widgets; use crate::bar::Komobar; diff --git a/komorebi-for-mac/Cargo.lock b/komorebi-for-mac/Cargo.lock index 9e3aaf02..9fb7e5e2 100644 --- a/komorebi-for-mac/Cargo.lock +++ b/komorebi-for-mac/Cargo.lock @@ -3217,6 +3217,7 @@ dependencies = [ "hotwatch", "icns", "image 0.25.10", + "komorebi-bar-core", "komorebi-client", "komorebi-themes", "lazy_static", @@ -3239,6 +3240,13 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "komorebi-bar-core" +version = "0.1.42" +dependencies = [ + "eframe", +] + [[package]] name = "komorebi-client" version = "0.1.1" diff --git a/komorebi-for-mac/komorebi-bar/Cargo.toml b/komorebi-for-mac/komorebi-bar/Cargo.toml index 8ef61d14..0a95de41 100644 --- a/komorebi-for-mac/komorebi-bar/Cargo.toml +++ b/komorebi-for-mac/komorebi-bar/Cargo.toml @@ -6,6 +6,7 @@ edition = "2024" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +komorebi-bar-core = { path = "../../crates/komorebi-bar-core" } komorebi-client = { path = "../komorebi-client", default-features = false } komorebi-themes = { workspace = true } diff --git a/komorebi-for-mac/komorebi-bar/src/main.rs b/komorebi-for-mac/komorebi-bar/src/main.rs index 3396239b..c3b75b33 100644 --- a/komorebi-for-mac/komorebi-bar/src/main.rs +++ b/komorebi-for-mac/komorebi-bar/src/main.rs @@ -1,8 +1,8 @@ mod bar; mod config; mod render; -mod selected_frame; -mod ui; +pub use komorebi_bar_core::selected_frame; +pub use komorebi_bar_core::ui; mod widgets; use crate::bar::Komobar; diff --git a/komorebi-for-mac/komorebi-bar/src/selected_frame.rs b/komorebi-for-mac/komorebi-bar/src/selected_frame.rs deleted file mode 100644 index 6e5efce0..00000000 --- a/komorebi-for-mac/komorebi-bar/src/selected_frame.rs +++ /dev/null @@ -1,89 +0,0 @@ -use eframe::egui::Color32; -use eframe::egui::CursorIcon; -use eframe::egui::Frame; -use eframe::egui::Margin; -use eframe::egui::Response; -use eframe::egui::Sense; -use eframe::egui::Stroke; -use eframe::egui::Ui; - -/// Same as SelectableLabel, but supports all content -pub struct SelectableFrame { - selected: bool, - selected_fill: Option, -} - -impl SelectableFrame { - pub fn new(selected: bool) -> Self { - Self { - selected, - selected_fill: None, - } - } - - pub fn new_auto(selected: bool, selected_fill: Option) -> Self { - Self { - selected, - selected_fill, - } - } - - pub fn show(self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> Response { - let Self { - selected, - selected_fill, - } = self; - - Frame::NONE - .show(ui, |ui| { - let response = ui.interact(ui.max_rect(), ui.unique_id(), Sense::click()); - - if ui.is_rect_visible(response.rect) { - // take into account the stroke width - let inner_margin = Margin::symmetric( - ui.style().spacing.button_padding.x as i8 - 1, - ui.style().spacing.button_padding.y as i8 - 1, - ); - - // since the stroke is drawn inside the frame, we always reserve space for it - if selected && response.hovered() { - let visuals = ui.style().interact_selectable(&response, selected); - - Frame::NONE - .stroke(Stroke::new(1.0_f32, visuals.bg_stroke.color)) - .corner_radius(visuals.corner_radius) - .fill(selected_fill.unwrap_or(visuals.bg_fill)) - .inner_margin(inner_margin) - .show(ui, add_contents); - } else if response.hovered() || response.highlighted() || response.has_focus() { - let visuals = ui.style().interact_selectable(&response, selected); - - Frame::NONE - .stroke(Stroke::new(1.0_f32, visuals.bg_stroke.color)) - .corner_radius(visuals.corner_radius) - .fill(visuals.bg_fill) - .inner_margin(inner_margin) - .show(ui, add_contents); - } else if selected { - let visuals = ui.style().interact_selectable(&response, selected); - - Frame::NONE - .stroke(Stroke::new(1.0_f32, visuals.bg_fill)) - .corner_radius(visuals.corner_radius) - .fill(selected_fill.unwrap_or(visuals.bg_fill)) - .inner_margin(inner_margin) - .show(ui, add_contents); - } else { - Frame::NONE - .stroke(Stroke::new(1.0_f32, Color32::TRANSPARENT)) - .inner_margin(inner_margin) - .show(ui, add_contents); - } - } - - response - }) - .inner - .on_hover_cursor(CursorIcon::PointingHand) - } -} diff --git a/komorebi-for-mac/komorebi-bar/src/ui.rs b/komorebi-for-mac/komorebi-bar/src/ui.rs deleted file mode 100644 index 24d58444..00000000 --- a/komorebi-for-mac/komorebi-bar/src/ui.rs +++ /dev/null @@ -1,22 +0,0 @@ -use eframe::egui::Align; -use eframe::egui::Layout; -use eframe::egui::Response; -use eframe::egui::Ui; -use eframe::egui::Vec2; -use eframe::egui::Widget; - -pub struct CustomUi<'ui>(pub &'ui mut Ui); - -impl CustomUi<'_> { - pub fn add_sized_left_to_right( - &mut self, - max_size: impl Into, - widget: impl Widget, - ) -> Response { - let layout = - Layout::from_main_dir_and_cross_align(self.0.layout().main_dir(), Align::Center); - self.0 - .allocate_ui_with_layout(max_size.into(), layout, |ui| ui.add(widget)) - .inner - } -}