1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-09-15 19:26:58 +02:00

feat: support glob patterns for keyboard layout icons (#949)

* Simplistic globbing for matching keyboard layout icons

Update the logic for determining the display text for the current keyboard layout.
Instead of a direct map lookup, iterate through the layout map to support wildcard matching.

Patterns ending with `*` will match any language string starting with the characters before the `*`. This allows grouping similar layouts (e.g., `English`, `English (Colemak-DH ISO)`) under a single pattern like `English*`.

* Use `IndexMap` instead of `HashMap` for keyboard layout icons map

This enables users to choose which globs to prioritize via ordering in the config

* Enable feature `serde` for `indexmap`

* Document wildcard matching for keyboard layouts

* Enable `indexmap2` feature flag for `schemars`

* Add missing period

* use string slices

* Fix formatting
This commit is contained in:
Alan 2025-07-21 00:19:58 +03:00 committed by GitHub
commit 5520562a18
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 21 additions and 8 deletions

1
Cargo.lock generated
View file

@ -2969,6 +2969,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82d20c4491bc164fa2f6c5d44565947a52ad80b9505d8e36f8d54c27c739fcd0" checksum = "82d20c4491bc164fa2f6c5d44565947a52ad80b9505d8e36f8d54c27c739fcd0"
dependencies = [ dependencies = [
"dyn-clone", "dyn-clone",
"indexmap",
"ref-cast", "ref-cast",
"schemars_derive", "schemars_derive",
"serde", "serde",

View file

@ -132,7 +132,7 @@ tracing-appender = "0.2.3"
strip-ansi-escapes = "0.2.0" strip-ansi-escapes = "0.2.0"
color-eyre = "0.6.5" color-eyre = "0.6.5"
serde = { version = "1.0.219", features = ["derive"] } serde = { version = "1.0.219", features = ["derive"] }
indexmap = "2.10.0" indexmap = { version = "2.10.0", features = ["serde"] }
dirs = "6.0.0" dirs = "6.0.0"
walkdir = "2.5.0" walkdir = "2.5.0"
notify = { version = "8.1.0", default-features = false } notify = { version = "8.1.0", default-features = false }
@ -191,7 +191,8 @@ rustix = { version = "1.0.7", default-features = false, features = ["std", "fs",
serde_json = { version = "1.0.140", optional = true } # ipc, niri serde_json = { version = "1.0.140", optional = true } # ipc, niri
# schema # schema
schemars = { version = "1.0.4", optional = true }
schemars = { version = "1.0.4", optional = true, features = ["indexmap2"] }
[build-dependencies] [build-dependencies]
clap = { version = "4.5.41", features = ["derive"] } clap = { version = "4.5.41", features = ["derive"] }

View file

@ -25,7 +25,7 @@ Displays the toggle state of the capslock, num lock and scroll lock keys, and th
| `icons.num_off` | `string` or [image](images) | `''` | Icon to show for disabled num lock indicator. | | `icons.num_off` | `string` or [image](images) | `''` | Icon to show for disabled num lock indicator. |
| `icons.scroll_on` | `string` or [image](images) | `` | Icon to show for enabled scroll lock indicator. | | `icons.scroll_on` | `string` or [image](images) | `` | Icon to show for enabled scroll lock indicator. |
| `icons.scroll_off` | `string` or [image](images) | `''` | Icon to show for disabled scroll lock indicator. | | `icons.scroll_off` | `string` or [image](images) | `''` | Icon to show for disabled scroll lock indicator. |
| `icons.layout_map` | `Map<string, string or image>` | `{}` | Map of icons or labels to show for a particular keyboard layout. Layouts use their actual name if not present in the map. | | `icons.layout_map` | `Map<string, string or image>` | `{}` | Map of icons or labels to show for a particular keyboard layout. Layouts use their actual name if not present in the map. Layouts are matched in the order they appear in the map. If a pattern to match ends with a `*`, it acts as a wildcard, matching any layout name that begins with the part before the `*`. |
| `seat` | `string` | `seat0` | ID of the Wayland seat to attach to. | | `seat` | `string` | `seat0` | ID of the Wayland seat to attach to. |
<details> <details>

View file

@ -1,8 +1,7 @@
use std::collections::HashMap;
use color_eyre::Result; use color_eyre::Result;
use color_eyre::eyre::Report; use color_eyre::eyre::Report;
use gtk::prelude::*; use gtk::prelude::*;
use indexmap::IndexMap;
use serde::Deserialize; use serde::Deserialize;
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tracing::{debug, trace}; use tracing::{debug, trace};
@ -129,7 +128,7 @@ struct Icons {
/// } /// }
/// ``` /// ```
#[serde(default)] #[serde(default)]
layout_map: HashMap<String, String>, layout_map: IndexMap<String, String>,
} }
impl Default for Icons { impl Default for Icons {
@ -141,7 +140,7 @@ impl Default for Icons {
num_off: String::new(), num_off: String::new(),
scroll_on: default_icon_scroll(), scroll_on: default_icon_scroll(),
scroll_off: String::new(), scroll_off: String::new(),
layout_map: HashMap::new(), layout_map: IndexMap::new(),
} }
} }
} }
@ -338,7 +337,19 @@ impl Module<gtk::Box> for KeyboardModule {
} }
} }
KeyboardUpdate::Layout(KeyboardLayoutUpdate(language)) => { KeyboardUpdate::Layout(KeyboardLayoutUpdate(language)) => {
let text = icons.layout_map.get(&language).unwrap_or(&language); let text = icons
.layout_map
.iter()
.find_map(|(pattern, display_text)| {
let is_match = if pattern.ends_with("*") {
language.starts_with(&pattern[..pattern.len() - 1])
} else {
pattern == &language
};
is_match.then(|| display_text)
})
.unwrap_or(&language);
layout_button.set_label(text); layout_button.set_label(text);
} }
}); });