From d1b4af47105e6904d4790f8c9f5fde3929731fe0 Mon Sep 17 00:00:00 2001 From: BowDown097 Date: Thu, 16 Jan 2025 21:39:06 -0800 Subject: [PATCH] refactor: make icon overrides bar-level and apply to focused module --- docs/Configuration guide.md | 3 ++- docs/modules/Launcher.md | 1 - src/bar.rs | 1 + src/config/mod.rs | 7 +++++++ src/modules/focused.rs | 11 ++++++++++- src/modules/launcher/mod.rs | 11 ++--------- src/modules/mod.rs | 2 ++ 7 files changed, 24 insertions(+), 12 deletions(-) diff --git a/docs/Configuration guide.md b/docs/Configuration guide.md index 25e1b22c..2b22cd9a 100644 --- a/docs/Configuration guide.md +++ b/docs/Configuration guide.md @@ -309,6 +309,7 @@ The following table lists each of the bar-level bar config options: | `exclusive_zone` | `boolean` | `true` unless `start_hidden` is enabled. | Whether the bar should reserve an exclusive zone around it. | | `popup_gap` | `integer` | `5` | The gap between the bar and popup window. | | `icon_theme` | `string` | `null` | Name of the GTK icon theme to use. Leave blank to use default. | +| `icon_overrides` | `map` | `null` | Map of app IDs (or classes) to icon names, overriding the app's default icon. | | `start_hidden` | `boolean` | `false`, or `true` if `autohide` set | Whether the bar should be hidden when the application starts. Enabled by default when `autohide` is set. | | `autohide` | `integer` | `null` | The duration in milliseconds before the bar is hidden after the cursor leaves. Leave unset to disable auto-hide behaviour. | | `start` | `Module[]` | `[]` | Array of left or top modules. | @@ -353,4 +354,4 @@ For information on the `Script` type, and embedding scripts in strings, see [her | `name` | `string` | `null` | Sets the unique widget name, allowing you to style it using `#name`. | | `class` | `string` | `null` | Sets one or more CSS classes, allowing you to style it using `.class`. | -For more information on styling, please see the [styling guide](styling-guide). \ No newline at end of file +For more information on styling, please see the [styling guide](styling-guide). diff --git a/docs/modules/Launcher.md b/docs/modules/Launcher.md index e00371da..2c934327 100644 --- a/docs/modules/Launcher.md +++ b/docs/modules/Launcher.md @@ -16,7 +16,6 @@ Optionally displays a launchable set of favourites. | | Type | Default | Description | |-----------------------------|---------------------------------------------|----------|--------------------------------------------------------------------------------------------------------------------------| | `favorites` | `string[]` | `[]` | List of app IDs (or classes) to always show at the start of the launcher. | -| `icon_overrides` | `map` | `{}` | Map of app IDs (or classes) to icon names, overriding the app's default icon. | | `show_names` | `boolean` | `false` | Whether to show app names on the button label. Names will still show on tooltips when set to false. | | `show_icons` | `boolean` | `true` | Whether to show app icons on the button. | | `icon_size` | `integer` | `32` | Size to render icon at (image icons only). | diff --git a/src/bar.rs b/src/bar.rs index 518cadbf..e54489a6 100644 --- a/src/bar.rs +++ b/src/bar.rs @@ -268,6 +268,7 @@ impl Bar { output_name: &self.monitor_name, location: $location, icon_theme: &icon_theme, + icon_overrides: &config.icon_overrides, } }; } diff --git a/src/config/mod.rs b/src/config/mod.rs index b4d7371a..7d22a514 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -290,6 +290,12 @@ pub struct BarConfig { /// **Default**: `null` pub icon_theme: Option, + /// Map of app IDs (or classes) to icon names, + /// overriding the app's default icon. + /// + /// **Default**; `null` + pub icon_overrides: Option>, + /// An array of modules to append to the start of the bar. /// Depending on the orientation, this is either the top of the left edge. /// @@ -338,6 +344,7 @@ impl Default for BarConfig { start_hidden: None, autohide: None, icon_theme: None, + icon_overrides: None, start: Some(vec![ModuleConfig::Label( LabelModule::new("ℹ️ Using default config".to_string()).into(), )]), diff --git a/src/modules/focused.rs b/src/modules/focused.rs index 250493e3..e8f9da3f 100644 --- a/src/modules/focused.rs +++ b/src/modules/focused.rs @@ -133,6 +133,7 @@ impl Module for FocusedModule { info: &ModuleInfo, ) -> Result> { let icon_theme = info.icon_theme; + let icon_overrides = info.icon_overrides; let container = gtk::Box::new(info.bar_position.orientation(), 5); @@ -153,9 +154,17 @@ impl Module for FocusedModule { { let icon_theme = icon_theme.clone(); + let icon_overrides = icon_overrides.clone(); glib_recv!(context.subscribe(), data => { - if let Some((name, id)) = data { + if let Some((name, mut id)) = data { if self.show_icon { + + if let Some(ref overrides) = icon_overrides { + if let Some(icon) = overrides.get(&id) { + id = icon.clone(); + } + } + match ImageProvider::parse(&id, &icon_theme, true, self.icon_size) .map(|image| image.load_into_image(&icon)) { diff --git a/src/modules/launcher/mod.rs b/src/modules/launcher/mod.rs index c302d504..6f6c1bd6 100644 --- a/src/modules/launcher/mod.rs +++ b/src/modules/launcher/mod.rs @@ -15,7 +15,6 @@ use gtk::prelude::*; use gtk::{Button, Orientation}; use indexmap::IndexMap; use serde::Deserialize; -use std::collections::HashMap; use std::process::{Command, Stdio}; use std::sync::Arc; use tokio::sync::{broadcast, mpsc}; @@ -30,12 +29,6 @@ pub struct LauncherModule { /// **Default**: `null` favorites: Option>, - /// Map of app IDs (or classes) to icon names, - /// overriding the app's default icon. - /// - /// **Default**; `null` - icon_overrides: Option>, - /// Whether to show application names on the bar. /// /// **Default**: `false` @@ -156,7 +149,7 @@ impl Module for LauncherModule { favorites .iter() .map(|app_id| { - let icon_override = self + let icon_override = _info .icon_overrides .as_ref() .and_then(|overrides| overrides.get(app_id)) @@ -173,7 +166,7 @@ impl Module for LauncherModule { let items = arc_mut!(items); let items2 = Arc::clone(&items); - let icon_overrides = arc_mut!(self.icon_overrides.clone()); + let icon_overrides = arc_mut!(_info.icon_overrides.clone()); let icon_overrides2 = Arc::clone(&icon_overrides); let tx = context.tx.clone(); diff --git a/src/modules/mod.rs b/src/modules/mod.rs index dfcce622..35289146 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use std::fmt::Debug; use std::rc::Rc; use std::sync::Arc; @@ -71,6 +72,7 @@ pub struct ModuleInfo<'a> { pub monitor: &'a Monitor, pub output_name: &'a str, pub icon_theme: &'a IconTheme, + pub icon_overrides: &'a Option>, } #[derive(Debug, Clone)]