Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: icon overrides #837

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions docs/Configuration guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,11 @@ Check [here](config) for an example config file for a fully configured bar in ea

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

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

> [!TIP]
> `monitors` is only required if you are following **2b** or **2c** (ie not the same bar across all monitors).
Expand Down Expand Up @@ -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).
For more information on styling, please see the [styling guide](styling-guide).
2 changes: 1 addition & 1 deletion docs/modules/Launcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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 |
| `favorites` | `string[]` | `[]` | List of app IDs (or classes) to always show at the start of the launcher. |
| `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). |
Expand Down
1 change: 1 addition & 0 deletions src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ impl Bar {
output_name: &self.monitor_name,
location: $location,
icon_theme: &icon_theme,
icon_overrides: &self.ironbar.config.borrow().icon_overrides,
}
};
}
Expand Down
7 changes: 7 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,13 @@ pub struct Config {
///
/// Providing this option overrides the single, global `bar` option.
pub monitors: Option<HashMap<String, MonitorConfig>>,

/// Map of app IDs (or classes) to icon names,
/// overriding the app's default icon.
///
/// **Default**: `{}`
#[serde(default)]
pub icon_overrides: HashMap<String, String>,
}

const fn default_layer() -> gtk_layer_shell::Layer {
Expand Down
14 changes: 10 additions & 4 deletions src/modules/focused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use color_eyre::Result;
use gtk::prelude::*;
use gtk::Label;
use serde::Deserialize;
use std::sync::Arc;
use tokio::sync::mpsc;
use tracing::debug;

Expand Down Expand Up @@ -132,8 +133,6 @@ impl Module<gtk::Box> for FocusedModule {
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
info: &ModuleInfo,
) -> Result<ModuleParts<gtk::Box>> {
let icon_theme = info.icon_theme;

let container = gtk::Box::new(info.bar_position.orientation(), 5);

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

{
let icon_theme = icon_theme.clone();
let icon_overrides = Arc::new(info.icon_overrides.clone());
let icon_theme = info.icon_theme.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(icon) = icon_overrides.get(&id) {
id = icon.clone();
}

match ImageProvider::parse(&id, &icon_theme, true, self.icon_size)
.map(|image| image.load_into_image(&icon))
{
Expand Down
14 changes: 12 additions & 2 deletions src/modules/launcher/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,23 @@ pub struct Item {
pub open_state: OpenState,
pub windows: IndexMap<usize, Window>,
pub name: String,
pub icon_override: String,
}

impl Item {
pub fn new(app_id: String, open_state: OpenState, favorite: bool) -> Self {
pub fn new(
app_id: String,
icon_override: String,
open_state: OpenState,
favorite: bool,
) -> Self {
Self {
app_id,
favorite,
open_state,
windows: IndexMap::new(),
name: String::new(),
icon_override,
}
}

Expand Down Expand Up @@ -108,6 +115,7 @@ impl From<ToplevelInfo> for Item {
open_state,
windows,
name,
icon_override: String::new(),
}
}
}
Expand Down Expand Up @@ -167,7 +175,9 @@ impl ItemButton {
}

if appearance.show_icons {
let input = if item.app_id.is_empty() {
let input = if !item.icon_override.is_empty() {
item.icon_override.clone()
} else if item.app_id.is_empty() {
item.name.clone()
} else {
item.app_id.clone()
Expand Down
26 changes: 21 additions & 5 deletions src/modules/launcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl Module<gtk::Box> for LauncherModule {

fn spawn_controller(
&self,
_info: &ModuleInfo,
info: &ModuleInfo,
context: &WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
mut rx: mpsc::Receiver<Self::ReceiveMessage>,
) -> crate::Result<()> {
Expand All @@ -149,18 +149,24 @@ impl Module<gtk::Box> for LauncherModule {
favorites
.iter()
.map(|app_id| {
let icon_override = info
.icon_overrides
.get(app_id)
.map_or_else(String::new, |v| v.to_string());

(
app_id.to_string(),
Item::new(app_id.to_string(), OpenState::Closed, true),
Item::new(app_id.to_string(), icon_override, OpenState::Closed, true),
)
})
.collect::<IndexMap<_, _>>()
});

let items = arc_mut!(items);

let items2 = Arc::clone(&items);

let icon_overrides = Arc::new(info.icon_overrides.clone());

let tx = context.tx.clone();
let tx2 = context.tx.clone();

Expand All @@ -180,7 +186,13 @@ impl Module<gtk::Box> for LauncherModule {
item.merge_toplevel(info.clone());
}
None => {
items.insert(info.app_id.clone(), Item::from(info.clone()));
let mut item = Item::from(info.clone());

if let Some(icon) = icon_overrides.get(&info.app_id) {
item.icon_override = icon.clone();
}

items.insert(info.app_id.clone(), item);
}
}
}
Expand Down Expand Up @@ -210,7 +222,11 @@ impl Module<gtk::Box> for LauncherModule {
let item = items.get_mut(&info.app_id);
match item {
None => {
let item: Item = info.into();
let mut item: Item = info.into();

if let Some(icon) = icon_overrides.get(&app_id) {
item.icon_override = icon.clone();
}

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

Expand Down
2 changes: 2 additions & 0 deletions src/modules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::fmt::Debug;
use std::rc::Rc;
use std::sync::Arc;
Expand Down Expand Up @@ -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 HashMap<String, String>,
}

#[derive(Debug, Clone)]
Expand Down