forge
Explore
Log in

Don't you dare go Hollow

Track keyboard focus component

Browse files at this commit

118773e17d689b95b4eefd105e3878fd3fe1aab1Ivan MolodetskikhParent: 741bee4
src/niri.rsmodified+56 -15
@@ -193,10 +193,7 @@ pub struct Niri {
193 pub seat: Seat<State>,193 pub seat: Seat<State>,
194 /// Scancodes of the keys to suppress.194 /// Scancodes of the keys to suppress.
195 pub suppressed_keys: HashSet<u32>,195 pub suppressed_keys: HashSet<u32>,
196- // This is always a toplevel surface focused as far as niri's logic is concerned, even when196+ pub keyboard_focus: KeyboardFocus,
197- // popup grabs are active (which means the real keyboard focus is on a popup descending from
198- // this toplevel surface).
199- pub keyboard_focus: Option<WlSurface>,
200 pub idle_inhibiting_surfaces: HashSet<WlSurface>,197 pub idle_inhibiting_surfaces: HashSet<WlSurface>,
201 pub is_fdo_idle_inhibited: Arc<AtomicBool>,198 pub is_fdo_idle_inhibited: Arc<AtomicBool>,
202 199
@@ -283,6 +280,18 @@ pub struct PopupGrabState {
283 pub grab: PopupGrab<State>,280 pub grab: PopupGrab<State>,
284 }281 }
285 282
283+// The surfaces here are always toplevel surfaces focused as far as niri's logic is concerned, even
284+// when popup grabs are active (which means the real keyboard focus is on a popup descending from
285+// that toplevel surface).
286+#[derive(Debug, Clone, PartialEq, Eq)]
287+pub enum KeyboardFocus {
288+ // Layout is focused by default if there's nothing else to focus.
289+ Layout { surface: Option<WlSurface> },
290+ LayerShell { surface: WlSurface },
291+ LockScreen { surface: Option<WlSurface> },
292+ ScreenshotUi,
293+}
294+
286 #[derive(Clone, PartialEq, Eq)]295 #[derive(Clone, PartialEq, Eq)]
287 pub struct PointerFocus {296 pub struct PointerFocus {
288 pub output: Output,297 pub output: Output,
@@ -329,6 +338,30 @@ impl Default for SurfaceFrameThrottlingState {
329 }338 }
330 }339 }
331 340
341+impl KeyboardFocus {
342+ pub fn surface(&self) -> Option<&WlSurface> {
343+ match self {
344+ KeyboardFocus::Layout { surface } => surface.as_ref(),
345+ KeyboardFocus::LayerShell { surface } => Some(surface),
346+ KeyboardFocus::LockScreen { surface } => surface.as_ref(),
347+ KeyboardFocus::ScreenshotUi => None,
348+ }
349+ }
350+
351+ pub fn into_surface(self) -> Option<WlSurface> {
352+ match self {
353+ KeyboardFocus::Layout { surface } => surface,
354+ KeyboardFocus::LayerShell { surface } => Some(surface),
355+ KeyboardFocus::LockScreen { surface } => surface,
356+ KeyboardFocus::ScreenshotUi => None,
357+ }
358+ }
359+
360+ pub fn is_layout(&self) -> bool {
361+ matches!(self, KeyboardFocus::Layout { .. })
362+ }
363+}
364+
332 pub struct State {365 pub struct State {
333 pub backend: Backend,366 pub backend: Backend,
334 pub niri: Niri,367 pub niri: Niri,
@@ -562,9 +595,11 @@ impl State {
562 595
563 pub fn update_keyboard_focus(&mut self) {596 pub fn update_keyboard_focus(&mut self) {
564 let focus = if self.niri.is_locked() {597 let focus = if self.niri.is_locked() {
565- self.niri.lock_surface_focus()598+ KeyboardFocus::LockScreen {
599+ surface: self.niri.lock_surface_focus(),
600+ }
566 } else if self.niri.screenshot_ui.is_open() {601 } else if self.niri.screenshot_ui.is_open() {
567- None602+ KeyboardFocus::ScreenshotUi
568 } else if let Some(output) = self.niri.layout.active_output() {603 } else if let Some(output) = self.niri.layout.active_output() {
569 let mon = self.niri.layout.monitor_for_output(output).unwrap();604 let mon = self.niri.layout.monitor_for_output(output).unwrap();
570 let layers = layer_map_for_output(output);605 let layers = layer_map_for_output(output);
@@ -577,7 +612,9 @@ impl State {
577 .map(|l| (&g.root, l.layer()))612 .map(|l| (&g.root, l.layer()))
578 });613 });
579 let grab_on_layer = |layer: Layer| {614 let grab_on_layer = |layer: Layer| {
580- layer_grab.and_then(move |(s, l)| if l == layer { Some(s.clone()) } else { None })615+ layer_grab
616+ .and_then(move |(s, l)| if l == layer { Some(s.clone()) } else { None })
617+ .map(|surface| KeyboardFocus::LayerShell { surface })
581 };618 };
582 619
583 let layout_focus = || {620 let layout_focus = || {
@@ -585,11 +622,15 @@ impl State {
585 .layout622 .layout
586 .focus()623 .focus()
587 .map(|win| win.toplevel().expect("no x11 support").wl_surface().clone())624 .map(|win| win.toplevel().expect("no x11 support").wl_surface().clone())
625+ .map(|surface| KeyboardFocus::Layout {
626+ surface: Some(surface),
627+ })
588 };628 };
589 let layer_focus = |surface: &LayerSurface| {629 let layer_focus = |surface: &LayerSurface| {
590 surface630 surface
591 .can_receive_keyboard_focus()631 .can_receive_keyboard_focus()
592 .then(|| surface.wl_surface().clone())632 .then(|| surface.wl_surface().clone())
633+ .map(|surface| KeyboardFocus::LayerShell { surface })
593 };634 };
594 635
595 let mut surface = grab_on_layer(Layer::Overlay);636 let mut surface = grab_on_layer(Layer::Overlay);
@@ -608,9 +649,9 @@ impl State {
608 surface = surface.or_else(layout_focus);649 surface = surface.or_else(layout_focus);
609 }650 }
610 651
611- surface652+ surface.unwrap_or(KeyboardFocus::Layout { surface: None })
612 } else {653 } else {
613- None654+ KeyboardFocus::Layout { surface: None }
614 };655 };
615 656
616 let keyboard = self.niri.seat.get_keyboard().unwrap();657 let keyboard = self.niri.seat.get_keyboard().unwrap();
@@ -622,7 +663,7 @@ impl State {
622 );663 );
623 664
624 if let Some(grab) = self.niri.popup_grab.as_mut() {665 if let Some(grab) = self.niri.popup_grab.as_mut() {
625- if Some(&grab.root) != focus.as_ref() {666+ if Some(&grab.root) != focus.surface() {
626 trace!(667 trace!(
627 "grab root {:?} is not the new focus {:?}, ungrabbing",668 "grab root {:?} is not the new focus {:?}, ungrabbing",
628 grab.root,669 grab.root,
@@ -646,7 +687,7 @@ impl State {
646 687
647 let mut new_layout = current_layout;688 let mut new_layout = current_layout;
648 // Store the currently active layout for the surface.689 // Store the currently active layout for the surface.
649- if let Some(current_focus) = self.niri.keyboard_focus.as_ref() {690+ if let Some(current_focus) = self.niri.keyboard_focus.surface() {
650 with_states(current_focus, |data| {691 with_states(current_focus, |data| {
651 let cell = data692 let cell = data
652 .data_map693 .data_map
@@ -655,7 +696,7 @@ impl State {
655 });696 });
656 }697 }
657 698
658- if let Some(focus) = focus.as_ref() {699+ if let Some(focus) = focus.surface() {
659 new_layout = with_states(focus, |data| {700 new_layout = with_states(focus, |data| {
660 let cell = data.data_map.get_or_insert::<Cell<KeyboardLayout>, _>(|| {701 let cell = data.data_map.get_or_insert::<Cell<KeyboardLayout>, _>(|| {
661 // The default layout is effectively the first layout in the702 // The default layout is effectively the first layout in the
@@ -665,7 +706,7 @@ impl State {
665 cell.get()706 cell.get()
666 });707 });
667 }708 }
668- if new_layout != current_layout && focus.is_some() {709+ if new_layout != current_layout && focus.surface().is_some() {
669 keyboard.set_focus(self, None, SERIAL_COUNTER.next_serial());710 keyboard.set_focus(self, None, SERIAL_COUNTER.next_serial());
670 keyboard.with_xkb_state(self, |mut context| {711 keyboard.with_xkb_state(self, |mut context| {
671 context.set_layout(new_layout);712 context.set_layout(new_layout);
@@ -674,7 +715,7 @@ impl State {
674 }715 }
675 716
676 self.niri.keyboard_focus.clone_from(&focus);717 self.niri.keyboard_focus.clone_from(&focus);
677- keyboard.set_focus(self, focus, SERIAL_COUNTER.next_serial());718+ keyboard.set_focus(self, focus.into_surface(), SERIAL_COUNTER.next_serial());
678 719
679 // FIXME: can be more granular.720 // FIXME: can be more granular.
680 self.niri.queue_redraw_all();721 self.niri.queue_redraw_all();
@@ -1169,7 +1210,7 @@ impl Niri {
1169 gamma_control_manager_state,1210 gamma_control_manager_state,
1170 1211
1171 seat,1212 seat,
1172- keyboard_focus: None,1213+ keyboard_focus: KeyboardFocus::Layout { surface: None },
1173 idle_inhibiting_surfaces: HashSet::new(),1214 idle_inhibiting_surfaces: HashSet::new(),
1174 is_fdo_idle_inhibited: Arc::new(AtomicBool::new(false)),1215 is_fdo_idle_inhibited: Arc::new(AtomicBool::new(false)),
1175 cursor_manager,1216 cursor_manager,
src/protocols/foreign_toplevel.rsmodified+1 -1
@@ -106,7 +106,7 @@ pub fn refresh(state: &mut State) {
106 .lock()106 .lock()
107 .unwrap();107 .unwrap();
108 108
109- if state.niri.keyboard_focus.as_ref() == Some(wl_surface) {109+ if state.niri.keyboard_focus.surface() == Some(wl_surface) {
110 focused = Some((window.clone(), output.cloned()));110 focused = Some((window.clone(), output.cloned()));
111 } else {111 } else {
112 refresh_toplevel(protocol_state, wl_surface, &role, output, false);112 refresh_toplevel(protocol_state, wl_surface, &role, output, false);