From 3a2094e80e879830548ef3bf53d5bc008597c2b1 Mon Sep 17 00:00:00 2001 From: darkwater Date: Thu, 4 Sep 2025 13:02:18 +0200 Subject: [PATCH] Add `Memory::move_focus` (#7476) * [X] I have followed the instructions in the PR template This allows us to programatically move focus around. Another way would be to inject arrow key inputs, but that requires the backend to implement a way to do that, and could have unintended side-effects. Co-authored-by: Emil Ernerfeldt --- crates/egui/src/lib.rs | 2 +- crates/egui/src/memory/mod.rs | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/egui/src/lib.rs b/crates/egui/src/lib.rs index 1fc5f6ca..75f80457 100644 --- a/crates/egui/src/lib.rs +++ b/crates/egui/src/lib.rs @@ -499,7 +499,7 @@ pub use self::{ layers::{LayerId, Order}, layout::*, load::SizeHint, - memory::{Memory, Options, Theme, ThemePreference}, + memory::{FocusDirection, Memory, Options, Theme, ThemePreference}, painter::Painter, response::{InnerResponse, Response}, sense::Sense, diff --git a/crates/egui/src/memory/mod.rs b/crates/egui/src/memory/mod.rs index 349ebcea..1e8cc4c9 100644 --- a/crates/egui/src/memory/mod.rs +++ b/crates/egui/src/memory/mod.rs @@ -140,8 +140,9 @@ impl Default for Memory { } } +/// A direction in which to move the keyboard focus. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -enum FocusDirection { +pub enum FocusDirection { /// Select the widget closest above the current focused widget. Up, @@ -888,6 +889,11 @@ impl Memory { } } + /// Move keyboard focus in a specific direction. + pub fn move_focus(&mut self, direction: FocusDirection) { + self.focus_mut().focus_direction = direction; + } + /// Returns true if /// - this layer is the top-most modal layer or above it /// - there is no modal layer