pretty keymap labels

This commit is contained in:
galister committed 2026-11-23 18:20:49 +09:00
1 parent 3be2324023
commit b8d25c4bb4
4 files changed
+38 -8

No files matched your search

+3 -4
View File
@@ -121,12 +121,11 @@ impl Layout {
button_state = KeyButtonData::KeymapSwitch {
layouts: keyboard_layouts.to_vec(),
};
let keymap_name = keymap
let layout_name = keymap
.as_ref()
.and_then(|m| m.get_name())
.unwrap_or("Layout");
.map_or("Layout", |m| m.get_layout_code().as_ref());
return Some(KeyData {
label: vec![keymap_name.into()],
label: vec![layout_name.into()],
button_state,
cap_type: KeyCapType::Other,
});
+27 -2
View File
@@ -5,7 +5,7 @@ use idmap_derive::IntegerId;
use libc::{input_event, timeval};
use regex::Regex;
use serde::Deserialize;
use std::{cell::LazyCell, sync::LazyLock};
use std::{cell::LazyCell, rc::Rc, sync::LazyLock};
use strum::{EnumIter, EnumString, FromRepr};
use xkbcommon::xkb;
@@ -331,6 +331,24 @@ pub const DEFAULT_XKB_MODEL: &str = "pc105";
#[derive(Clone)]
pub struct XkbKeymap {
pub inner: xkb::Keymap,
layout_code: Rc<str>,
}
fn make_layout_code(layout: &str, variant: &str) -> Rc<str> {
if layout.is_empty() {
"US".into()
} else if variant.is_empty() {
layout.to_string().to_uppercase().into()
} else {
let variant = match variant {
"nodeadkeys" => "NDK",
"colemak_dh" => "CMK-DH",
"altgr-intl" => "ALTGR",
_ => variant,
};
format!("{layout} {variant}").to_uppercase().into()
}
}
impl Default for XkbKeymap {
@@ -362,13 +380,20 @@ impl XkbKeymap {
None,
xkb::COMPILE_NO_FLAGS,
)
.map(|inner| Self { inner })
.map(|inner| Self {
inner,
layout_code: make_layout_code(layout, variant),
})
}
pub fn get_name(&self) -> Option<&str> {
self.inner.layouts().next()
}
pub fn get_layout_code(&self) -> &Rc<str> {
&self.layout_code
}
pub fn label_for_key(&self, key: VirtualKey, modifier: KeyModifier) -> String {
let mut state = xkb::State::new(&self.inner);
if modifier > 0
+4 -1
View File
@@ -121,7 +121,10 @@ impl Dispatch<WlKeyboard, ()> for MonitorState {
log::info!("wayland keymap: {l}");
}
state.keymap = Some(XkbKeymap { inner: keymap });
state.keymap = Some(XkbKeymap {
inner: keymap,
layout_code: "Wayland".into(),
});
}
Ok(None) => {
log::error!("Could not load keymap: no keymap");
+4 -1
View File
@@ -31,5 +31,8 @@ pub fn get_keymap_x11() -> anyhow::Result<XkbKeymap> {
}
let keymap = keymap_new_from_device(&context, &conn, device_id, xkb::KEYMAP_COMPILE_NO_FLAGS);
Ok(XkbKeymap { inner: keymap })
Ok(XkbKeymap {
inner: keymap,
layout_code: "X11".into(),
})
}