Add `Memory::move_focus` (#7476)

<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/main/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to test and add commits to your PR.
* Remember to run `cargo fmt` and `cargo clippy`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

* [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 <emil.ernerfeldt@gmail.com>
This commit is contained in:
darkwater 2025-09-04 13:02:18 +02:00 committed by GitHub
parent 80d61a7c53
commit 3a2094e80e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 2 deletions

View File

@ -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,

View File

@ -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