Skip to content

Commit 49ab7e0

Browse files
authored
Merge pull request #837 from BowDown097/master
feat: icon overrides
2 parents 2e59bc9 + c2beda8 commit 49ab7e0

File tree

9 files changed

+77
-18
lines changed

9 files changed

+77
-18
lines changed

Diff for: docs/Configuration guide.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,11 @@ Check [here](config) for an example config file for a fully configured bar in ea
280280

281281
The following table lists each of the top-level bar config options:
282282

283-
| Name | Type | Default | Description |
284-
|--------------------|-----------------------------------------|---------|---------------------------------------------------------------|
285-
| `ironvar_defaults` | `Map<string, string>` | `{}` | Map of [ironvar](ironvars) keys against their default values. |
286-
| `monitors` | `Map<string, BarConfig or BarConfig[]>` | `null` | Map of monitor names against bar configs. |
283+
| Name | Type | Default | Description |
284+
|--------------------|-----------------------------------------|---------|-------------------------------------------------------------------------------|
285+
| `ironvar_defaults` | `Map<string, string>` | `{}` | Map of [ironvar](ironvars) keys against their default values. |
286+
| `monitors` | `Map<string, BarConfig or BarConfig[]>` | `null` | Map of monitor names against bar configs. |
287+
| `icon_overrides` | `Map<string, string>` | `{}` | Map of app IDs (or classes) to icon names, overriding the app's default icon. |
287288

288289
> [!TIP]
289290
> `monitors` is only required if you are following **2b** or **2c** (ie not the same bar across all monitors).
@@ -353,4 +354,4 @@ For information on the `Script` type, and embedding scripts in strings, see [her
353354
| `name` | `string` | `null` | Sets the unique widget name, allowing you to style it using `#name`. |
354355
| `class` | `string` | `null` | Sets one or more CSS classes, allowing you to style it using `.class`. |
355356

356-
For more information on styling, please see the [styling guide](styling-guide).
357+
For more information on styling, please see the [styling guide](styling-guide).

Diff for: docs/modules/Launcher.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Optionally displays a launchable set of favourites.
1515
1616
| | Type | Default | Description |
1717
|-----------------------------|---------------------------------------------|----------|--------------------------------------------------------------------------------------------------------------------------|
18-
| `favorites` | `string[]` | `[]` | List of app IDs (or classes) to always show at the start of the launcher |
18+
| `favorites` | `string[]` | `[]` | List of app IDs (or classes) to always show at the start of the launcher. |
1919
| `show_names` | `boolean` | `false` | Whether to show app names on the button label. Names will still show on tooltips when set to false. |
2020
| `show_icons` | `boolean` | `true` | Whether to show app icons on the button. |
2121
| `icon_size` | `integer` | `32` | Size to render icon at (image icons only). |

Diff for: src/bar.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use gtk::gdk::Monitor;
88
use gtk::prelude::*;
99
use gtk::{Application, ApplicationWindow, IconTheme, Orientation, Window, WindowType};
1010
use gtk_layer_shell::LayerShell;
11+
use std::collections::HashMap;
1112
use std::rc::Rc;
13+
use std::sync::Arc;
1214
use std::time::Duration;
1315
use tracing::{debug, info};
1416

@@ -23,6 +25,7 @@ pub struct Bar {
2325
name: String,
2426
monitor_name: String,
2527
monitor_size: (i32, i32),
28+
icon_overrides: Arc<HashMap<String, String>>,
2629
position: BarPosition,
2730

2831
ironbar: Rc<Ironbar>,
@@ -43,6 +46,7 @@ impl Bar {
4346
app: &Application,
4447
monitor_name: String,
4548
monitor_size: (i32, i32),
49+
icon_overrides: Arc<HashMap<String, String>>,
4650
config: BarConfig,
4751
ironbar: Rc<Ironbar>,
4852
) -> Self {
@@ -92,6 +96,7 @@ impl Bar {
9296
name,
9397
monitor_name,
9498
monitor_size,
99+
icon_overrides,
95100
position,
96101
ironbar,
97102
window,
@@ -268,6 +273,7 @@ impl Bar {
268273
output_name: &self.monitor_name,
269274
location: $location,
270275
icon_theme: &icon_theme,
276+
icon_overrides: self.icon_overrides.clone(),
271277
}
272278
};
273279
}
@@ -398,9 +404,17 @@ pub fn create_bar(
398404
monitor: &Monitor,
399405
monitor_name: String,
400406
monitor_size: (i32, i32),
407+
icon_overrides: Arc<HashMap<String, String>>,
401408
config: BarConfig,
402409
ironbar: Rc<Ironbar>,
403410
) -> Result<Bar> {
404-
let bar = Bar::new(app, monitor_name, monitor_size, config, ironbar);
411+
let bar = Bar::new(
412+
app,
413+
monitor_name,
414+
monitor_size,
415+
icon_overrides,
416+
config,
417+
ironbar,
418+
);
405419
bar.init(monitor)
406420
}

Diff for: src/config/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,13 @@ pub struct Config {
388388
///
389389
/// Providing this option overrides the single, global `bar` option.
390390
pub monitors: Option<HashMap<String, MonitorConfig>>,
391+
392+
/// Map of app IDs (or classes) to icon names,
393+
/// overriding the app's default icon.
394+
///
395+
/// **Default**: `{}`
396+
#[serde(default)]
397+
pub icon_overrides: HashMap<String, String>,
391398
}
392399

393400
const fn default_layer() -> gtk_layer_shell::Layer {

Diff for: src/main.rs

+4
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ fn load_output_bars(
383383
};
384384

385385
let config = ironbar.config.borrow();
386+
let icon_overrides = Arc::new(config.icon_overrides.clone());
386387
let display = get_display();
387388

388389
// let pos = output.logical_position.unwrap_or_default();
@@ -406,6 +407,7 @@ fn load_output_bars(
406407
&monitor,
407408
monitor_name.to_string(),
408409
output_size,
410+
icon_overrides,
409411
config.clone(),
410412
ironbar.clone(),
411413
)?]
@@ -418,6 +420,7 @@ fn load_output_bars(
418420
&monitor,
419421
monitor_name.to_string(),
420422
output_size,
423+
icon_overrides.clone(),
421424
config.clone(),
422425
ironbar.clone(),
423426
)
@@ -428,6 +431,7 @@ fn load_output_bars(
428431
&monitor,
429432
monitor_name.to_string(),
430433
output_size,
434+
icon_overrides,
431435
config.bar.clone(),
432436
ironbar.clone(),
433437
)?],

Diff for: src/modules/focused.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,6 @@ impl Module<gtk::Box> for FocusedModule {
132132
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
133133
info: &ModuleInfo,
134134
) -> Result<ModuleParts<gtk::Box>> {
135-
let icon_theme = info.icon_theme;
136-
137135
let container = gtk::Box::new(info.bar_position.orientation(), 5);
138136

139137
let icon = gtk::Image::new();
@@ -152,10 +150,17 @@ impl Module<gtk::Box> for FocusedModule {
152150
container.add(&label);
153151

154152
{
155-
let icon_theme = icon_theme.clone();
153+
let icon_overrides = info.icon_overrides.clone();
154+
let icon_theme = info.icon_theme.clone();
155+
156156
glib_recv!(context.subscribe(), data => {
157-
if let Some((name, id)) = data {
157+
if let Some((name, mut id)) = data {
158158
if self.show_icon {
159+
160+
if let Some(icon) = icon_overrides.get(&id) {
161+
id = icon.clone();
162+
}
163+
159164
match ImageProvider::parse(&id, &icon_theme, true, self.icon_size)
160165
.map(|image| image.load_into_image(&icon))
161166
{

Diff for: src/modules/launcher/item.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,23 @@ pub struct Item {
2424
pub open_state: OpenState,
2525
pub windows: IndexMap<usize, Window>,
2626
pub name: String,
27+
pub icon_override: String,
2728
}
2829

2930
impl Item {
30-
pub fn new(app_id: String, open_state: OpenState, favorite: bool) -> Self {
31+
pub fn new(
32+
app_id: String,
33+
icon_override: String,
34+
open_state: OpenState,
35+
favorite: bool,
36+
) -> Self {
3137
Self {
3238
app_id,
3339
favorite,
3440
open_state,
3541
windows: IndexMap::new(),
3642
name: String::new(),
43+
icon_override,
3744
}
3845
}
3946

@@ -108,6 +115,7 @@ impl From<ToplevelInfo> for Item {
108115
open_state,
109116
windows,
110117
name,
118+
icon_override: String::new(),
111119
}
112120
}
113121
}
@@ -167,7 +175,9 @@ impl ItemButton {
167175
}
168176

169177
if appearance.show_icons {
170-
let input = if item.app_id.is_empty() {
178+
let input = if !item.icon_override.is_empty() {
179+
item.icon_override.clone()
180+
} else if item.app_id.is_empty() {
171181
item.name.clone()
172182
} else {
173183
item.app_id.clone()

Diff for: src/modules/launcher/mod.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl Module<gtk::Box> for LauncherModule {
138138

139139
fn spawn_controller(
140140
&self,
141-
_info: &ModuleInfo,
141+
info: &ModuleInfo,
142142
context: &WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
143143
mut rx: mpsc::Receiver<Self::ReceiveMessage>,
144144
) -> crate::Result<()> {
@@ -149,18 +149,24 @@ impl Module<gtk::Box> for LauncherModule {
149149
favorites
150150
.iter()
151151
.map(|app_id| {
152+
let icon_override = info
153+
.icon_overrides
154+
.get(app_id)
155+
.map_or_else(String::new, |v| v.to_string());
156+
152157
(
153158
app_id.to_string(),
154-
Item::new(app_id.to_string(), OpenState::Closed, true),
159+
Item::new(app_id.to_string(), icon_override, OpenState::Closed, true),
155160
)
156161
})
157162
.collect::<IndexMap<_, _>>()
158163
});
159164

160165
let items = arc_mut!(items);
161-
162166
let items2 = Arc::clone(&items);
163167

168+
let icon_overrides = info.icon_overrides.clone();
169+
164170
let tx = context.tx.clone();
165171
let tx2 = context.tx.clone();
166172

@@ -180,7 +186,13 @@ impl Module<gtk::Box> for LauncherModule {
180186
item.merge_toplevel(info.clone());
181187
}
182188
None => {
183-
items.insert(info.app_id.clone(), Item::from(info.clone()));
189+
let mut item = Item::from(info.clone());
190+
191+
if let Some(icon) = icon_overrides.get(&info.app_id) {
192+
item.icon_override = icon.clone();
193+
}
194+
195+
items.insert(info.app_id.clone(), item);
184196
}
185197
}
186198
}
@@ -210,7 +222,11 @@ impl Module<gtk::Box> for LauncherModule {
210222
let item = items.get_mut(&info.app_id);
211223
match item {
212224
None => {
213-
let item: Item = info.into();
225+
let mut item: Item = info.into();
226+
227+
if let Some(icon) = icon_overrides.get(&app_id) {
228+
item.icon_override = icon.clone();
229+
}
214230

215231
items.insert(app_id.clone(), item.clone());
216232

Diff for: src/modules/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::HashMap;
12
use std::fmt::Debug;
23
use std::rc::Rc;
34
use std::sync::Arc;
@@ -71,6 +72,7 @@ pub struct ModuleInfo<'a> {
7172
pub monitor: &'a Monitor,
7273
pub output_name: &'a str,
7374
pub icon_theme: &'a IconTheme,
75+
pub icon_overrides: Arc<HashMap<String, String>>,
7476
}
7577

7678
#[derive(Debug, Clone)]

0 commit comments

Comments
 (0)