Skip to content

Commit

Permalink
feat(launcher): icon overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
BowDown097 committed Jan 11, 2025
1 parent b296726 commit 9c74ec8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
3 changes: 2 additions & 1 deletion docs/modules/Launcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ 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. |
| `icon_overrides` | `map<string, string>` | `{}` | 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). |
Expand Down
9 changes: 7 additions & 2 deletions src/modules/launcher/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ 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 +110,7 @@ impl From<ToplevelInfo> for Item {
open_state,
windows,
name,
icon_override: String::new(),
}
}
}
Expand Down Expand Up @@ -167,7 +170,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
40 changes: 36 additions & 4 deletions src/modules/launcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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};
Expand All @@ -29,6 +30,12 @@ pub struct LauncherModule {
/// **Default**: `null`
favorites: Option<Vec<String>>,

/// Map of app IDs (or classes) to icon names,
/// overriding the app's default icon.
///
/// **Default**; `null`
icon_overrides: Option<HashMap<String, String>>,

/// Whether to show application names on the bar.
///
/// **Default**: `false`
Expand Down Expand Up @@ -149,24 +156,33 @@ impl Module<gtk::Box> for LauncherModule {
favorites
.iter()
.map(|app_id| {
let icon_override = self
.icon_overrides
.as_ref()
.and_then(|overrides| 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_mut!(self.icon_overrides.clone());
let icon_overrides2 = Arc::clone(&icon_overrides);

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

let wl = context.client::<wayland::Client>();
spawn(async move {
let items = items2;
let icon_overrides = icon_overrides2;
let tx = tx2;

let mut wlrx = wl.subscribe_toplevels();
Expand All @@ -180,7 +196,16 @@ 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());
let icon_overrides = lock!(icon_overrides);

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

items.insert(info.app_id.clone(), item);
}
}
}
Expand Down Expand Up @@ -210,7 +235,14 @@ 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();
let icon_overrides = lock!(icon_overrides);

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

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

Expand Down

0 comments on commit 9c74ec8

Please sign in to comment.