mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-22 00:21:43 +01:00
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<ImageIcon>`). 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 <command>` 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.
This commit is contained in:
1 parent
0c197e8652
commit
1bc806c9d9
5 files changed
+55
-65
No files matched your search
@@ -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<ImageIcon>;
|
||||
|
||||
/// The glyph the date widget shows as its calendar icon.
|
||||
fn calendar_icon(&self) -> &'static str;
|
||||
}
|
||||
|
||||
+39
-1
@@ -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::<Vec<&str>>()[0]])
|
||||
.args([
|
||||
"/C",
|
||||
"explorer.exe",
|
||||
label.split(' ').collect::<Vec<&str>>()[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<ImageIcon> {
|
||||
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<Cow<'_, Path>> {
|
||||
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)
|
||||
}
|
||||
@@ -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<Self> {
|
||||
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<Cow<'_, Path>> {
|
||||
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.
|
||||
///
|
||||
|
||||
@@ -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<ImageIcon> {
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -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<Self> {
|
||||
// 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<Self> {
|
||||
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<Cow<'_, Path>> {
|
||||
// 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.
|
||||
///
|
||||
|
||||
Reference in new issue
Block a user