mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-07-01 10:41:03 +02:00
Merge pull request #191 from body20002/flatpak_icons
Add Support For Flatpak Icons
This commit is contained in:
commit
ac04cc27ce
2 changed files with 158 additions and 55 deletions
28
Cargo.toml
28
Cargo.toml
|
@ -26,7 +26,13 @@ ipc = ["dep:serde_json"]
|
||||||
|
|
||||||
http = ["dep:reqwest"]
|
http = ["dep:reqwest"]
|
||||||
|
|
||||||
"config+all" = ["config+json", "config+yaml", "config+toml", "config+corn", "config+ron"]
|
"config+all" = [
|
||||||
|
"config+json",
|
||||||
|
"config+yaml",
|
||||||
|
"config+toml",
|
||||||
|
"config+corn",
|
||||||
|
"config+ron",
|
||||||
|
]
|
||||||
"config+json" = ["universal-config/json"]
|
"config+json" = ["universal-config/json"]
|
||||||
"config+yaml" = ["universal-config/yaml"]
|
"config+yaml" = ["universal-config/yaml"]
|
||||||
"config+toml" = ["universal-config/toml"]
|
"config+toml" = ["universal-config/toml"]
|
||||||
|
@ -58,7 +64,15 @@ workspaces = ["futures-util"]
|
||||||
gtk = "0.17.0"
|
gtk = "0.17.0"
|
||||||
gtk-layer-shell = "0.6.0"
|
gtk-layer-shell = "0.6.0"
|
||||||
glib = "0.17.10"
|
glib = "0.17.10"
|
||||||
tokio = { version = "1.28.2", features = ["macros", "rt-multi-thread", "time", "process", "sync", "io-util", "net"] }
|
tokio = { version = "1.28.2", features = [
|
||||||
|
"macros",
|
||||||
|
"rt-multi-thread",
|
||||||
|
"time",
|
||||||
|
"process",
|
||||||
|
"sync",
|
||||||
|
"io-util",
|
||||||
|
"net",
|
||||||
|
] }
|
||||||
tracing = "0.1.37"
|
tracing = "0.1.37"
|
||||||
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
|
||||||
tracing-error = "0.2.0"
|
tracing-error = "0.2.0"
|
||||||
|
@ -73,7 +87,9 @@ notify = { version = "6.0.1", default-features = false }
|
||||||
wayland-client = "0.30.2"
|
wayland-client = "0.30.2"
|
||||||
wayland-protocols = { version = "0.30.0", features = ["unstable", "client"] }
|
wayland-protocols = { version = "0.30.0", features = ["unstable", "client"] }
|
||||||
wayland-protocols-wlr = { version = "0.1.0", features = ["client"] }
|
wayland-protocols-wlr = { version = "0.1.0", features = ["client"] }
|
||||||
smithay-client-toolkit = { version = "0.17.0", default-features = false, features = ["calloop"] }
|
smithay-client-toolkit = { version = "0.17.0", default-features = false, features = [
|
||||||
|
"calloop",
|
||||||
|
] }
|
||||||
universal-config = { version = "0.4.0", default_features = false }
|
universal-config = { version = "0.4.0", default_features = false }
|
||||||
ctrlc = "3.4.0"
|
ctrlc = "3.4.0"
|
||||||
|
|
||||||
|
@ -117,7 +133,9 @@ hyprland = { version = "=0.3.1", optional = true }
|
||||||
futures-util = { version = "0.3.21", optional = true }
|
futures-util = { version = "0.3.21", optional = true }
|
||||||
|
|
||||||
# shared
|
# shared
|
||||||
regex = { version = "1.8.4", default-features = false, features = ["std"], optional = true } # music, sys_info
|
regex = { version = "1.8.4", default-features = false, features = [
|
||||||
|
"std",
|
||||||
|
], optional = true } # music, sys_info
|
||||||
|
|
||||||
[patch.crates-io]
|
[patch.crates-io]
|
||||||
stray = { git = "https://github.com/jakestanger/stray", branch = "fix/connection-errors" }
|
stray = { git = "https://github.com/jakestanger/stray", branch = "fix/connection-errors" }
|
||||||
|
|
|
@ -1,16 +1,33 @@
|
||||||
use std::collections::HashMap;
|
use lazy_static::lazy_static;
|
||||||
use std::fs::File;
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::io;
|
use std::fs;
|
||||||
use std::io::BufRead;
|
use std::path::{Path, PathBuf};
|
||||||
use std::path::PathBuf;
|
use std::sync::Mutex;
|
||||||
use walkdir::WalkDir;
|
use tracing::warn;
|
||||||
|
use walkdir::{DirEntry, WalkDir};
|
||||||
|
|
||||||
/// Gets directories that should contain `.desktop` files
|
use crate::lock;
|
||||||
|
|
||||||
|
type DesktopFile = HashMap<String, Vec<String>>;
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref DESKTOP_FILES: Mutex<HashMap<PathBuf, DesktopFile>> =
|
||||||
|
Mutex::new(HashMap::new());
|
||||||
|
|
||||||
|
/// These are the keys that in the cache
|
||||||
|
static ref DESKTOP_FILES_LOOK_OUT_KEYS: HashSet<&'static str> =
|
||||||
|
HashSet::from(["Name", "StartupWMClass", "Exec", "Icon"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Finds directories that should contain `.desktop` files
|
||||||
/// and exist on the filesystem.
|
/// and exist on the filesystem.
|
||||||
fn find_application_dirs() -> Vec<PathBuf> {
|
fn find_application_dirs() -> Vec<PathBuf> {
|
||||||
let mut dirs = vec![PathBuf::from("/usr/share/applications")];
|
let mut dirs = vec![
|
||||||
let user_dir = dirs::data_local_dir();
|
PathBuf::from("/usr/share/applications"), // system installed apps
|
||||||
|
PathBuf::from("/var/lib/flatpak/exports/share/applications"), // flatpak apps
|
||||||
|
];
|
||||||
|
|
||||||
|
let user_dir = dirs::data_local_dir(); // user installed apps
|
||||||
if let Some(mut user_dir) = user_dir {
|
if let Some(mut user_dir) = user_dir {
|
||||||
user_dir.push("applications");
|
user_dir.push("applications");
|
||||||
dirs.push(user_dir);
|
dirs.push(user_dir);
|
||||||
|
@ -19,55 +36,123 @@ fn find_application_dirs() -> Vec<PathBuf> {
|
||||||
dirs.into_iter().filter(|dir| dir.exists()).collect()
|
dirs.into_iter().filter(|dir| dir.exists()).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attempts to locate a `.desktop` file for an app id
|
/// Finds all the desktop files
|
||||||
/// (or app class).
|
fn find_desktop_files() -> Vec<PathBuf> {
|
||||||
///
|
|
||||||
/// A simple case-insensitive check is performed on filename == `app_id`.
|
|
||||||
pub fn find_desktop_file(app_id: &str) -> Option<PathBuf> {
|
|
||||||
let dirs = find_application_dirs();
|
let dirs = find_application_dirs();
|
||||||
|
dirs.into_iter()
|
||||||
for dir in dirs {
|
.flat_map(|dir| {
|
||||||
let mut walker = WalkDir::new(dir).max_depth(5).into_iter();
|
WalkDir::new(dir)
|
||||||
|
.max_depth(5)
|
||||||
let entry = walker.find(|entry| {
|
.into_iter()
|
||||||
entry.as_ref().map_or(false, |entry| {
|
.filter_map(Result::ok)
|
||||||
let file_name = entry.file_name().to_string_lossy().to_lowercase();
|
.map(DirEntry::into_path)
|
||||||
let test_name = format!("{}.desktop", app_id.to_lowercase());
|
.filter(|file| file.is_file() && file.extension().unwrap_or_default() == "desktop")
|
||||||
file_name == test_name
|
})
|
||||||
})
|
.collect()
|
||||||
});
|
|
||||||
|
|
||||||
if let Some(Ok(entry)) = entry {
|
|
||||||
let path = entry.path().to_owned();
|
|
||||||
return Some(path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses a desktop file into a flat hashmap of keys/values.
|
/// Attempts to locate a `.desktop` file for an app id
|
||||||
fn parse_desktop_file(path: PathBuf) -> io::Result<HashMap<String, String>> {
|
pub fn find_desktop_file(app_id: &str) -> Option<PathBuf> {
|
||||||
let file = File::open(path)?;
|
// this is necessary to invalidate the cache
|
||||||
let lines = io::BufReader::new(file).lines();
|
let files = find_desktop_files();
|
||||||
|
|
||||||
let mut map = HashMap::new();
|
if let Some(path) = find_desktop_file_by_filename(app_id, &files) {
|
||||||
|
return Some(path);
|
||||||
for line in lines.flatten() {
|
|
||||||
if let Some((key, value)) = line.split_once('=') {
|
|
||||||
map.insert(key.to_string(), value.to_string());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(map)
|
find_desktop_file_by_filedata(app_id, &files)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Finds the correct desktop file using a simple condition check
|
||||||
|
fn find_desktop_file_by_filename(app_id: &str, files: &[PathBuf]) -> Option<PathBuf> {
|
||||||
|
let app_id = app_id.to_lowercase();
|
||||||
|
|
||||||
|
files
|
||||||
|
.iter()
|
||||||
|
.find(|file| {
|
||||||
|
let file_name: String = file
|
||||||
|
.file_name()
|
||||||
|
.expect("file name doesn't end with ...")
|
||||||
|
.to_string_lossy()
|
||||||
|
.to_lowercase();
|
||||||
|
|
||||||
|
file_name.contains(&app_id)
|
||||||
|
|| app_id
|
||||||
|
.split(&[' ', ':', '@', '.', '_'][..])
|
||||||
|
.any(|part| file_name.contains(part)) // this will attempt to find flatpak apps that are like this
|
||||||
|
// `com.company.app` or `com.app.something`
|
||||||
|
})
|
||||||
|
.map(ToOwned::to_owned)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Finds the correct desktop file using the keys in `DESKTOP_FILES_LOOK_OUT_KEYS`
|
||||||
|
fn find_desktop_file_by_filedata(app_id: &str, files: &[PathBuf]) -> Option<PathBuf> {
|
||||||
|
let app_id = &app_id.to_lowercase();
|
||||||
|
let mut desktop_files_cache = lock!(DESKTOP_FILES);
|
||||||
|
|
||||||
|
files
|
||||||
|
.iter()
|
||||||
|
.filter_map(|file| {
|
||||||
|
let Some(parsed_desktop_file) = parse_desktop_file(file) else { return None };
|
||||||
|
|
||||||
|
desktop_files_cache.insert(file.clone(), parsed_desktop_file.clone());
|
||||||
|
Some((file.clone(), parsed_desktop_file))
|
||||||
|
})
|
||||||
|
.find(|(_, desktop_file)| {
|
||||||
|
desktop_file
|
||||||
|
.values()
|
||||||
|
.flatten()
|
||||||
|
.any(|value| value.to_lowercase().contains(app_id))
|
||||||
|
})
|
||||||
|
.map(|(path, _)| path)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parses a desktop file into a hashmap of keys/vector(values).
|
||||||
|
fn parse_desktop_file(path: &Path) -> Option<DesktopFile> {
|
||||||
|
let Ok(file) = fs::read_to_string(path) else {
|
||||||
|
warn!("Couldn't Open File: {}", path.display());
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut desktop_file: DesktopFile = DesktopFile::new();
|
||||||
|
|
||||||
|
file.lines()
|
||||||
|
.filter_map(|line| {
|
||||||
|
let Some((key, value)) = line.split_once('=') else { return None };
|
||||||
|
|
||||||
|
let key = key.trim();
|
||||||
|
let value = value.trim();
|
||||||
|
|
||||||
|
if DESKTOP_FILES_LOOK_OUT_KEYS.contains(key) {
|
||||||
|
Some((key, value))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.for_each(|(key, value)| {
|
||||||
|
desktop_file
|
||||||
|
.entry(key.to_string())
|
||||||
|
.or_insert_with(Vec::new)
|
||||||
|
.push(value.to_string());
|
||||||
|
});
|
||||||
|
|
||||||
|
Some(desktop_file)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attempts to get the icon name from the app's `.desktop` file.
|
/// Attempts to get the icon name from the app's `.desktop` file.
|
||||||
pub fn get_desktop_icon_name(app_id: &str) -> Option<String> {
|
pub fn get_desktop_icon_name(app_id: &str) -> Option<String> {
|
||||||
find_desktop_file(app_id).and_then(|file| {
|
let Some(path) = find_desktop_file(app_id) else { return None };
|
||||||
let map = parse_desktop_file(file);
|
|
||||||
map.map_or(None, |map| {
|
let mut desktop_files_cache = lock!(DESKTOP_FILES);
|
||||||
map.get("Icon").map(std::string::ToString::to_string)
|
|
||||||
})
|
let desktop_file = match desktop_files_cache.get(&path) {
|
||||||
})
|
Some(desktop_file) => desktop_file,
|
||||||
|
_ => desktop_files_cache
|
||||||
|
.entry(path.clone())
|
||||||
|
.or_insert_with(|| parse_desktop_file(&path).expect("desktop_file")),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut icons = desktop_file.get("Icon").into_iter().flatten();
|
||||||
|
|
||||||
|
icons.next().map(std::string::ToString::to_string)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue