Add 'input.keyboard.track_layout' config option
Browse files at this commit907e0a03efcce6ba35aa6405e130f316bc217c5aParent: eb7d20c3 changed files +57 -4
- resources/default-config.kdl +5 -0
- src/config.rs +13 -0
- src/niri.rs +39 -4
resources/default-config.kdlmodified+5 -0
| @@ -16,6 +16,11 @@ input { | ||
| 16 | 16 | // Delay is in milliseconds before the repeat starts. Rate is in characters per second. |
| 17 | 17 | // repeat-delay 600 |
| 18 | 18 | // repeat-rate 25 |
| 19 | + | |
| 20 | + // Niri can remember the keyboard layout globally (the default) or per-window. | |
| 21 | + // - "global" - layout change is global for all windows. | |
| 22 | + // - "window" - layout is tracked for each window individually. | |
| 23 | + // track-layout "global" | |
| 19 | 24 | } |
| 20 | 25 | |
| 21 | 26 | // Next sections include libinput settings. |
src/config.rsmodified+13 -0
| @@ -62,6 +62,8 @@ pub struct Keyboard { | ||
| 62 | 62 | pub repeat_delay: u16, |
| 63 | 63 | #[knuffel(child, unwrap(argument), default = 25)] |
| 64 | 64 | pub repeat_rate: u8, |
| 65 | + #[knuffel(child, unwrap(argument), default)] | |
| 66 | + pub track_layout: TrackLayout, | |
| 65 | 67 | } |
| 66 | 68 | |
| 67 | 69 | #[derive(knuffel::Decode, Debug, Default, PartialEq, Eq)] |
| @@ -78,6 +80,15 @@ pub struct Xkb { | ||
| 78 | 80 | pub options: Option<String>, |
| 79 | 81 | } |
| 80 | 82 | |
| 83 | +#[derive(knuffel::DecodeScalar, Debug, Default, PartialEq, Eq)] | |
| 84 | +pub enum TrackLayout { | |
| 85 | + /// The layout change is global. | |
| 86 | + #[default] | |
| 87 | + Global, | |
| 88 | + /// The layout change is window local. | |
| 89 | + Window, | |
| 90 | +} | |
| 91 | + | |
| 81 | 92 | // FIXME: Add the rest of the settings. |
| 82 | 93 | #[derive(knuffel::Decode, Debug, Default, PartialEq)] |
| 83 | 94 | pub struct Touchpad { |
| @@ -514,6 +525,7 @@ mod tests { | ||
| 514 | 525 | keyboard { |
| 515 | 526 | repeat-delay 600 |
| 516 | 527 | repeat-rate 25 |
| 528 | + track-layout "window" | |
| 517 | 529 | xkb { |
| 518 | 530 | layout "us,ru" |
| 519 | 531 | options "grp:win_space_toggle" |
| @@ -587,6 +599,7 @@ mod tests { | ||
| 587 | 599 | }, |
| 588 | 600 | repeat_delay: 600, |
| 589 | 601 | repeat_rate: 25, |
| 602 | + track_layout: TrackLayout::Window, | |
| 590 | 603 | }, |
| 591 | 604 | touchpad: Touchpad { |
| 592 | 605 | tap: true, |
src/niri.rsmodified+39 -4
| @@ -1,4 +1,4 @@ | ||
| 1 | -use std::cell::RefCell; | |
| 1 | +use std::cell::{Cell, RefCell}; | |
| 2 | 2 | use std::collections::{HashMap, HashSet}; |
| 3 | 3 | use std::ffi::OsString; |
| 4 | 4 | use std::path::PathBuf; |
| @@ -29,7 +29,7 @@ use smithay::desktop::utils::{ | ||
| 29 | 29 | under_from_surface_tree, update_surface_primary_scanout_output, OutputPresentationFeedback, |
| 30 | 30 | }; |
| 31 | 31 | use smithay::desktop::{layer_map_for_output, PopupManager, Space, Window, WindowSurfaceType}; |
| 32 | -use smithay::input::keyboard::XkbConfig; | |
| 32 | +use smithay::input::keyboard::{Layout as KeyboardLayout, XkbConfig, XkbContextHandler}; | |
| 33 | 33 | use smithay::input::pointer::{CursorIcon, CursorImageAttributes, CursorImageStatus, MotionEvent}; |
| 34 | 34 | use smithay::input::{Seat, SeatState}; |
| 35 | 35 | use smithay::output::Output; |
| @@ -76,7 +76,7 @@ use smithay::wayland::text_input::TextInputManagerState; | ||
| 76 | 76 | use smithay::wayland::virtual_keyboard::VirtualKeyboardManagerState; |
| 77 | 77 | |
| 78 | 78 | use crate::backend::{Backend, RenderResult, Tty, Winit}; |
| 79 | -use crate::config::Config; | |
| 79 | +use crate::config::{Config, TrackLayout}; | |
| 80 | 80 | use crate::cursor::{CursorManager, CursorTextureCache, RenderCursor, XCursor}; |
| 81 | 81 | #[cfg(feature = "dbus")] |
| 82 | 82 | use crate::dbus::gnome_shell_screenshot::{NiriToScreenshot, ScreenshotToNiri}; |
| @@ -382,8 +382,43 @@ impl State { | ||
| 382 | 382 | }; |
| 383 | 383 | |
| 384 | 384 | let keyboard = self.niri.seat.get_keyboard().unwrap(); |
| 385 | - if keyboard.current_focus() != focus { | |
| 385 | + let current_focus = keyboard.current_focus(); | |
| 386 | + if current_focus != focus { | |
| 387 | + if self.niri.config.borrow().input.keyboard.track_layout == TrackLayout::Window { | |
| 388 | + let current_layout = | |
| 389 | + keyboard.with_kkb_state(self, |context| context.active_layout()); | |
| 390 | + | |
| 391 | + let mut new_layout = current_layout; | |
| 392 | + // Store the currently active layout for the surface. | |
| 393 | + if let Some(current_focus) = current_focus.as_ref() { | |
| 394 | + with_states(current_focus, |data| { | |
| 395 | + let cell = data | |
| 396 | + .data_map | |
| 397 | + .get_or_insert::<Cell<KeyboardLayout>, _>(Cell::default); | |
| 398 | + cell.set(current_layout); | |
| 399 | + }); | |
| 400 | + } | |
| 401 | + | |
| 402 | + if let Some(focus) = focus.as_ref() { | |
| 403 | + new_layout = with_states(focus, |data| { | |
| 404 | + let cell = data.data_map.get_or_insert::<Cell<KeyboardLayout>, _>(|| { | |
| 405 | + // The default layout is effectively the first layout in the | |
| 406 | + // keymap, so use it for new windows. | |
| 407 | + Cell::new(KeyboardLayout::default()) | |
| 408 | + }); | |
| 409 | + cell.get() | |
| 410 | + }); | |
| 411 | + } | |
| 412 | + if new_layout != current_layout && focus.is_some() { | |
| 413 | + keyboard.set_focus(self, None, SERIAL_COUNTER.next_serial()); | |
| 414 | + keyboard.with_kkb_state(self, |mut context| { | |
| 415 | + context.set_layout(new_layout); | |
| 416 | + }); | |
| 417 | + } | |
| 418 | + } | |
| 419 | + | |
| 386 | 420 | keyboard.set_focus(self, focus, SERIAL_COUNTER.next_serial()); |
| 421 | + | |
| 387 | 422 | // FIXME: can be more granular. |
| 388 | 423 | self.niri.queue_redraw_all(); |
| 389 | 424 | } |