Skip to content

Commit

Permalink
show active group
Browse files Browse the repository at this point in the history
  • Loading branch information
ids1024 committed Nov 18, 2024
1 parent 53caef6 commit 7b16ffc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
28 changes: 22 additions & 6 deletions client-toolkit/examples/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct AppData {
keyboard: Option<wl_keyboard::WlKeyboard>,
keymap_state: KeymapState,
keymap: Option<xkb::Keymap>,
group: Option<u32>,
}

impl ProvidesRegistryState for AppData {
Expand Down Expand Up @@ -143,12 +144,13 @@ impl KeyboardHandler for AppData {
impl KeymapHandler for AppData {
fn group(
&mut self,
conn: &Connection,
qh: &QueueHandle<Self>,
keyboard: &wl_keyboard::WlKeyboard,
keymap: &zcosmic_keymap_v1::ZcosmicKeymapV1,
_conn: &Connection,
_qh: &QueueHandle<Self>,
_keyboard: &wl_keyboard::WlKeyboard,
_keymap: &zcosmic_keymap_v1::ZcosmicKeymapV1,
group: u32,
) {
self.group = Some(group);
}
}

Expand All @@ -166,15 +168,31 @@ fn main() {
keymap_state,
keyboard: None,
keymap: None,
group: None,
};

while app_data.keymap.is_none() {
event_queue.blocking_dispatch(&mut app_data).unwrap();
}

let keyboard = app_data.keyboard.as_ref().unwrap();
let cosmic_keymap = app_data.keymap_state.get_keymap(&keyboard, &qh).unwrap();

while app_data.group.is_none() {
event_queue.blocking_dispatch(&mut app_data).unwrap();
}
let group = app_data.group.unwrap();

let keymap = app_data.keymap.as_ref().unwrap();
for (n, name) in keymap.layouts().enumerate() {
// Bold active layout
if n as u32 == group {
print!("\x1b[1m");
}
println!("{}: {}", n, name);
if n as u32 == group {
print!("\x1b[22m");
}
}
print!("Choose layout: ");

Expand All @@ -183,8 +201,6 @@ fn main() {
io::stdin().read_line(&mut line).unwrap();
let index = u32::from_str(line.trim()).unwrap();

let keyboard = app_data.keyboard.as_ref().unwrap();
let cosmic_keymap = app_data.keymap_state.get_keymap(&keyboard, &qh).unwrap();
cosmic_keymap.set_group(index);

event_queue.roundtrip(&mut app_data).unwrap();
Expand Down
20 changes: 14 additions & 6 deletions client-toolkit/src/keymap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmic_protocols::keymap::v1::client::{zcosmic_keymap_v1, zcosmic_keymap_manager_v1};
use cosmic_protocols::keymap::v1::client::{zcosmic_keymap_manager_v1, zcosmic_keymap_v1};
use sctk::registry::RegistryState;
use wayland_client::{protocol::wl_keyboard, Connection, Dispatch, QueueHandle};

Expand Down Expand Up @@ -29,13 +29,21 @@ impl KeymapState {
Self { keymap_manager }
}

pub fn get_keymap<D>(&self, keyboard: &wl_keyboard::WlKeyboard, qh: &QueueHandle<D>) -> Option<zcosmic_keymap_v1::ZcosmicKeymapV1>
pub fn get_keymap<D>(
&self,
keyboard: &wl_keyboard::WlKeyboard,
qh: &QueueHandle<D>,
) -> Option<zcosmic_keymap_v1::ZcosmicKeymapV1>
where
D: Dispatch<zcosmic_keymap_v1::ZcosmicKeymapV1, KeymapUserData> + 'static,
{
Some(self.keymap_manager.as_ref()?.get_keymap(keyboard, qh, KeymapUserData {
keyboard: keyboard.clone()
}))
Some(self.keymap_manager.as_ref()?.get_keymap(
keyboard,
qh,
KeymapUserData {
keyboard: keyboard.clone(),
},
))
}
}

Expand All @@ -44,7 +52,7 @@ where
D: Dispatch<zcosmic_keymap_manager_v1::ZcosmicKeymapManagerV1, ()>,
{
fn event(
state: &mut D,
_: &mut D,
_: &zcosmic_keymap_manager_v1::ZcosmicKeymapManagerV1,
event: zcosmic_keymap_manager_v1::Event,
_: &(),
Expand Down

0 comments on commit 7b16ffc

Please sign in to comment.