fix(bar): load app icons for focused window widget

This commit adds the icon path to WindowDetails and then uses that path
to look up and load the icon in komorebi-bar.
This commit is contained in:
LGUG2Z committed 2025-10-01 11:19:43 -07:00
1 parent c9eeaf3999
commit 75373c2174
5 files changed
+99 -11

No files matched your search

Generated
+11
View File
@@ -2456,6 +2456,16 @@ dependencies = [
"cc",
]
[[package]]
name = "icns"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5ccfbad7e08da70a5b48a924994a5afd93125ce5d45a3b0ba0b8da7bda59a40"
dependencies = [
"byteorder",
"png 0.16.8",
]
[[package]]
name = "icu_collections"
version = "2.0.0"
@@ -2868,6 +2878,7 @@ dependencies = [
"egui-phosphor",
"font-loader",
"hotwatch",
"icns",
"image 0.25.8",
"komorebi-client",
"komorebi-themes",
+2 -1
View File
@@ -9,8 +9,8 @@ edition = "2024"
komorebi-client = { path = "../komorebi-client", default-features = false }
komorebi-themes = { workspace = true }
chrono-tz = { workspace = true }
chrono = { workspace = true }
chrono-tz = { workspace = true }
clap = { workspace = true }
color-eyre = { workspace = true }
crossbeam-channel = { workspace = true }
@@ -20,6 +20,7 @@ eframe = { workspace = true }
egui-phosphor = "0.10"
font-loader = "0.11"
hotwatch = { workspace = true }
icns = "0.3"
image = "0.25"
lazy_static = { workspace = true }
netdev = "0.36"
+36 -5
View File
@@ -28,6 +28,9 @@ use eframe::egui::Ui;
use eframe::egui::Vec2;
use eframe::egui::text::LayoutJob;
use eframe::egui::vec2;
use icns::IconFamily;
use icns::IconType;
use image::RgbaImage;
use komorebi_client::Container;
use komorebi_client::PathExt;
use komorebi_client::Rect;
@@ -42,6 +45,8 @@ use serde::Serialize;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::fs::File;
use std::io::BufReader;
use std::io::Result as IoResult;
use std::path::Path;
use std::rc::Rc;
@@ -1005,11 +1010,37 @@ impl From<&Window> for WindowInfo {
fn from(value: &Window) -> Self {
Self {
title: value.details.as_ref().map(|details| details.title.clone()),
icon: None,
// icon: ImageIcon::try_load(value.hwnd, || {
// windows_icons::get_icon_by_hwnd(value.hwnd)
// .or_else(|| windows_icons_fallback::get_icon_by_process_id(value.process_id()))
// }),
icon: value
.details
.as_ref()
.map(|details| {
ImageIcon::try_load(details.icon_path.as_path(), || {
icns_to_rgba(&details.icon_path.to_string_lossy())
})
})
.unwrap_or_default(),
}
}
}
pub fn icns_to_rgba(path: &str) -> Option<RgbaImage> {
let file = File::open(path).ok()?;
let icon_family = IconFamily::read(BufReader::new(file)).ok()?;
let icon = icon_family
.get_icon_with_type(IconType::RGBA32_512x512_2x)
.or_else(|_| icon_family.get_icon_with_type(IconType::RGBA32_512x512))
.or_else(|_| icon_family.get_icon_with_type(IconType::RGBA32_256x256))
.or_else(|_| icon_family.get_icon_with_type(IconType::RGBA32_128x128))
.or_else(|_| icon_family.get_icon_with_type(IconType::RGBA32_64x64))
.or_else(|_| icon_family.get_icon_with_type(IconType::RGBA32_32x32))
.or_else(|_| icon_family.get_icon_with_type(IconType::RGB24_128x128))
.or_else(|_| icon_family.get_icon_with_type(IconType::RGB24_32x32))
.ok()?;
let width = icon.width();
let height = icon.height();
let data = icon.data().to_vec();
RgbaImage::from_raw(width, height, data)
}
+5 -5
View File
@@ -154,8 +154,8 @@ impl ImageIcon {
pub enum ImageIconId {
/// Identifier based on a file system path.
Path(Arc<Path>),
/// Windows HWND handle.
Hwnd(isize),
/// Window ID
WindowId(u32),
}
impl From<&Path> for ImageIconId {
@@ -165,9 +165,9 @@ impl From<&Path> for ImageIconId {
}
}
impl From<isize> for ImageIconId {
impl From<u32> for ImageIconId {
#[inline]
fn from(value: isize) -> Self {
Self::Hwnd(value)
fn from(value: u32) -> Self {
Self::WindowId(value)
}
}
+45
View File
@@ -63,6 +63,8 @@ use objc2_core_graphics::kCGWindowBounds;
use objc2_core_graphics::kCGWindowName;
use objc2_core_graphics::kCGWindowOwnerName;
use objc2_core_graphics::kCGWindowOwnerPID;
use objc2_foundation::NSBundle;
use objc2_foundation::NSString;
use regex::Regex;
use serde::Deserialize;
use serde::Serialize;
@@ -74,6 +76,7 @@ use std::ffi::c_void;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Write;
use std::path::Path;
use std::path::PathBuf;
use std::ptr::NonNull;
use std::str::FromStr;
@@ -193,6 +196,7 @@ pub struct WindowDetails {
pub exe: String,
pub role: String,
pub subrole: String,
pub icon_path: PathBuf,
}
impl From<&Window> for WindowDetails {
@@ -202,6 +206,7 @@ impl From<&Window> for WindowDetails {
exe: value.exe().unwrap_or_default(),
role: value.role().unwrap_or_default(),
subrole: value.subrole().unwrap_or_default(),
icon_path: value.icon_path().unwrap_or_default(),
}
}
}
@@ -419,6 +424,18 @@ impl Window {
}
}
pub fn bundle_path(&self) -> Option<PathBuf> {
if let Ok(Some(path)) = self
.running_application()
.map(|app| unsafe { app.bundleURL() })
.map(|url| url.map(|url| url.to_file_path()))
{
path
} else {
None
}
}
pub fn path(&self) -> Option<PathBuf> {
if let Ok(Some(path)) = self
.running_application()
@@ -431,6 +448,34 @@ impl Window {
}
}
pub fn icon_path(&self) -> Option<PathBuf> {
if let Some(path) = self.bundle_path() {
unsafe {
if let Some(bundle) =
NSBundle::bundleWithPath(&NSString::from_str(&path.to_string_lossy()))
&& let Some(icon_file) =
bundle.objectForInfoDictionaryKey(&NSString::from_str("CFBundleIconFile"))
&& let Ok(icon_name) = icon_file.downcast::<NSString>()
{
let mut icon_path =
format!("{}/Contents/Resources/{}", path.display(), icon_name);
if !icon_path.ends_with(".icns") {
icon_path.push_str(".icns");
}
let path = Path::new(&icon_path);
if path.exists() {
return Some(PathBuf::from(path));
}
}
}
}
None
}
pub fn role(&self) -> Option<String> {
AccessibilityApi::copy_attribute_value::<CFString>(&self.element, kAXRoleAttribute)
.map(|s| s.to_string())