From 1bc806c9d9253ed5e803db328a893aa071327d70 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sat, 31 Oct 2026 10:30:21 -0700 Subject: [PATCH] refactor(bar): converge applications.rs of both bars behind BarPlatform::command_icon The two applications.rs differed in how an application icon is found for a command string. The shared file now asks the platform (new hook `BarPlatform::command_icon(&str) -> Option`). Windows os.rs gets the former `UserCommand::get_executable` logic verbatim (as the private `executable_of`) and the windows-icons lookup; macOS returns None, exactly what its commented-out version did. `1.0` vs `1.0_f32` unified. The two files are now byte-identical. No schema text changes. Known and preserved: `UserCommand::launch_if_ready` spawns `cmd /C ` in both bars, so launching an application from the macOS bar is broken (it was before). Verified: schema harness macOS and Windows byte-identical; Windows clippy -D warnings --locked clean; macOS fake-CC check --locked 0 errors. Not run: the bars. --- crates/komorebi-bar-core/src/platform.rs | 5 +++ komorebi-bar/src/os.rs | 40 ++++++++++++++++++- komorebi-bar/src/widgets/applications.rs | 34 +--------------- komorebi-for-mac/komorebi-bar/src/os.rs | 5 +++ .../komorebi-bar/src/widgets/applications.rs | 36 ++--------------- 5 files changed, 55 insertions(+), 65 deletions(-) diff --git a/crates/komorebi-bar-core/src/platform.rs b/crates/komorebi-bar-core/src/platform.rs index 8e0d56e4..8176adf4 100644 --- a/crates/komorebi-bar-core/src/platform.rs +++ b/crates/komorebi-bar-core/src/platform.rs @@ -3,6 +3,7 @@ //! A shell implements [`BarPlatform`] and calls [`install`] first thing in `main`; the widgets //! reach it through [`platform`] when they are built or clicked. +use crate::widgets::icons::ImageIcon; use std::io; use std::sync::OnceLock; @@ -29,6 +30,10 @@ pub trait BarPlatform: Send + Sync { /// The GitHub API URL that says which release of this platform's komorebi is the latest. fn latest_release_api_url(&self) -> &'static str; + /// The icon of the program a command string would start (Windows: the executable's icon; + /// macOS: none). + fn command_icon(&self, command: &str) -> Option; + /// The glyph the date widget shows as its calendar icon. fn calendar_icon(&self) -> &'static str; } diff --git a/komorebi-bar/src/os.rs b/komorebi-bar/src/os.rs index dcce88d4..e59e1680 100644 --- a/komorebi-bar/src/os.rs +++ b/komorebi-bar/src/os.rs @@ -1,8 +1,12 @@ +use crate::widgets::ImageIcon; use egui_phosphor::regular::CALENDAR_DOTS; use komorebi_bar_core::platform::BarPlatform; use komorebi_bar_core::platform::SystemTool; +use std::borrow::Cow; use std::io; +use std::path::Path; use std::process::Command; +use which::which; pub struct WindowsBar; @@ -16,7 +20,11 @@ impl BarPlatform for WindowsBar { SystemTool::NetworkSettings => Command::new("cmd.exe").args(["/C", "ncpa"]).spawn(), SystemTool::Url(url) => Command::new("explorer.exe").args([url]).spawn(), SystemTool::StorageLocation(label) => Command::new("cmd.exe") - .args(["/C", "explorer.exe", label.split(' ').collect::>()[0]]) + .args([ + "/C", + "explorer.exe", + label.split(' ').collect::>()[0], + ]) .spawn(), } .map(|_| ()) @@ -29,4 +37,34 @@ impl BarPlatform for WindowsBar { fn calendar_icon(&self) -> &'static str { CALENDAR_DOTS } + + fn command_icon(&self, command: &str) -> Option { + let path = executable_of(command)?; + ImageIcon::try_load(path.as_ref(), || { + let path_str = path.to_str()?; + windows_icons::get_icon_by_path(path_str) + .or_else(|| windows_icons_fallback::get_icon_by_path(path_str)) + }) + } +} + +/// Attempts to resolve the executable path from the command string. +/// +/// Resolution logic: +/// - Splits the command by ".exe" and checks if the first part is an existing file. +/// - If not, attempts to locate the binary using [`which`] on this name. +/// - If still unresolved, takes the first word (separated by whitespace) and attempts +/// to find it in the system `PATH` using [`which`]. +/// +/// Returns `None` if no executable path can be determined. +fn executable_of(command: &str) -> Option> { + if let Some(binary) = command.split(".exe").next().map(Path::new) { + if binary.is_file() { + return Some(Cow::Borrowed(binary)); + } else if let Ok(binary) = which(binary) { + return Some(Cow::Owned(binary)); + } + } + + which(command.split(' ').next()?).ok().map(Cow::Owned) } diff --git a/komorebi-bar/src/widgets/applications.rs b/komorebi-bar/src/widgets/applications.rs index 20bc1fe3..f899f4d7 100644 --- a/komorebi-bar/src/widgets/applications.rs +++ b/komorebi-bar/src/widgets/applications.rs @@ -1,4 +1,5 @@ use super::ImageIcon; +use crate::platform::platform; use crate::render::RenderConfig; use crate::selected_frame::SelectableFrame; use crate::widgets::widget::BarWidget; @@ -20,14 +21,11 @@ use eframe::egui::vec2; use komorebi_client::PathExt; use serde::Deserialize; use serde::Serialize; -use std::borrow::Cow; -use std::path::Path; use std::process::Command; use std::sync::Arc; use std::time::Duration; use std::time::Instant; use tracing; -use which::which; /// Minimum interval between consecutive application launches to prevent accidental spamming. const MIN_LAUNCH_INTERVAL: Duration = Duration::from_millis(800); @@ -273,13 +271,7 @@ impl Icon { /// - Returns `None` if the executable path is unavailable or icon extraction fails. #[inline] pub fn try_from_command(command: &UserCommand) -> Option { - let path = command.get_executable()?; - let image_icon = ImageIcon::try_load(path.as_ref(), || { - let path_str = path.to_str()?; - windows_icons::get_icon_by_path(path_str) - .or_else(|| windows_icons_fallback::get_icon_by_path(path_str)) - })?; - Some(Icon::Image(image_icon)) + platform().command_icon(&command.command).map(Icon::Image) } /// Renders the icon in the given [`Ui`] using the provided [`IconConfig`]. @@ -364,28 +356,6 @@ impl UserCommand { } } - /// Attempts to resolve the executable path from the command string. - /// - /// Resolution logic: - /// - Splits the command by ".exe" and checks if the first part is an existing file. - /// - If not, attempts to locate the binary using [`which`] on this name. - /// - If still unresolved, takes the first word (separated by whitespace) and attempts - /// to find it in the system `PATH` using [`which`]. - /// - /// Returns `None` if no executable path can be determined. - #[inline] - pub fn get_executable(&self) -> Option> { - if let Some(binary) = self.command.split(".exe").next().map(Path::new) { - if binary.is_file() { - return Some(Cow::Borrowed(binary)); - } else if let Ok(binary) = which(binary) { - return Some(Cow::Owned(binary)); - } - } - - which(self.command.split(' ').next()?).ok().map(Cow::Owned) - } - /// Attempts to launch the specified command in a separate thread if enough time has passed /// since the last launch. This prevents repeated launches from rapid consecutive clicks. /// diff --git a/komorebi-for-mac/komorebi-bar/src/os.rs b/komorebi-for-mac/komorebi-bar/src/os.rs index 2dd84e6e..68a3978f 100644 --- a/komorebi-for-mac/komorebi-bar/src/os.rs +++ b/komorebi-for-mac/komorebi-bar/src/os.rs @@ -1,3 +1,4 @@ +use crate::widgets::ImageIcon; use egui_phosphor::regular::CALENDAR; use komorebi_bar_core::platform::BarPlatform; use komorebi_bar_core::platform::SystemTool; @@ -38,4 +39,8 @@ impl BarPlatform for MacBar { fn calendar_icon(&self) -> &'static str { CALENDAR } + + fn command_icon(&self, _command: &str) -> Option { + None + } } diff --git a/komorebi-for-mac/komorebi-bar/src/widgets/applications.rs b/komorebi-for-mac/komorebi-bar/src/widgets/applications.rs index e2d75250..f899f4d7 100644 --- a/komorebi-for-mac/komorebi-bar/src/widgets/applications.rs +++ b/komorebi-for-mac/komorebi-bar/src/widgets/applications.rs @@ -1,4 +1,5 @@ use super::ImageIcon; +use crate::platform::platform; use crate::render::RenderConfig; use crate::selected_frame::SelectableFrame; use crate::widgets::widget::BarWidget; @@ -269,15 +270,8 @@ impl Icon { /// - Returns [`Icon::Image`] if an icon is successfully extracted. /// - Returns `None` if the executable path is unavailable or icon extraction fails. #[inline] - pub fn try_from_command(_command: &UserCommand) -> Option { - // let path = command.get_executable()?; - // let image_icon = ImageIcon::try_load(path.as_ref(), || { - // let path_str = path.to_str()?; - // windows_icons::get_icon_by_path(path_str) - // .or_else(|| windows_icons_fallback::get_icon_by_path(path_str)) - // })?; - // Some(Icon::Image(image_icon)) - None + pub fn try_from_command(command: &UserCommand) -> Option { + platform().command_icon(&command.command).map(Icon::Image) } /// Renders the icon in the given [`Ui`] using the provided [`IconConfig`]. @@ -310,7 +304,7 @@ impl Icon { #[inline] pub fn draw_fallback(ui: &mut Ui, icon_size: Vec2) { let (response, painter) = ui.allocate_painter(icon_size, Sense::hover()); - let stroke = Stroke::new(1.0, ui.style().visuals.text_color()); + let stroke = Stroke::new(1.0_f32, ui.style().visuals.text_color()); let mut rect = response.rect; let rounding = CornerRadius::same((rect.width() * 0.1) as u8); rect = rect.shrink(stroke.width); @@ -362,28 +356,6 @@ impl UserCommand { } } - // /// Attempts to resolve the executable path from the command string. - // /// - // /// Resolution logic: - // /// - Splits the command by ".exe" and checks if the first part is an existing file. - // /// - If not, attempts to locate the binary using [`which`] on this name. - // /// - If still unresolved, takes the first word (separated by whitespace) and attempts - // /// to find it in the system `PATH` using [`which`]. - // /// - // /// Returns `None` if no executable path can be determined. - // #[inline] - // pub fn get_executable(&self) -> Option> { - // if let Some(binary) = self.command.split(".exe").next().map(Path::new) { - // if binary.is_file() { - // return Some(Cow::Borrowed(binary)); - // } else if let Ok(binary) = which(binary) { - // return Some(Cow::Owned(binary)); - // } - // } - // - // which(self.command.split(' ').next()?).ok().map(Cow::Owned) - // } - /// Attempts to launch the specified command in a separate thread if enough time has passed /// since the last launch. This prevents repeated launches from rapid consecutive clicks. ///