mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 00:34:25 +00:00
Runtime: egui moves focus among an app's own widgets with the arrows and the Tab it keeps
This commit is contained in:
@@ -347,6 +347,11 @@ pub(crate) fn host_focus(ctx: &Context, id: egui::Id) {
|
||||
ctx.memory_mut(|memory| memory.request_focus(id));
|
||||
}
|
||||
|
||||
/// A host's own clickable egui widget `id` at `rect`, a focus stop for egui's own focus moves.
|
||||
pub(crate) fn host_button(ui: &Ui, id: egui::Id, rect: Rect) {
|
||||
ui.interact(rect, id, egui::Sense::click());
|
||||
}
|
||||
|
||||
/// A host's own egui drag area `id` at `rect`; returns whether egui reports it dragged and whether its drag stopped
|
||||
/// this pass.
|
||||
pub(crate) fn host_drag_area(ui: &Ui, id: egui::Id, rect: Rect) -> (bool, bool) {
|
||||
|
||||
@@ -54,8 +54,10 @@ pub(crate) fn surrender(ctx: &Context, id: Id) {
|
||||
ctx.memory_mut(|memory| memory.surrender_focus(id));
|
||||
}
|
||||
|
||||
/// H7: egui's begin pass set a focus direction from an unmodified arrow; the runtime's focus service is the
|
||||
/// only mover, so the direction is cleared before any component runs.
|
||||
/// H7: egui's begin pass set a focus direction from an unmodified arrow or the Tab it kept. The focus service clears
|
||||
/// it before any component runs unless egui focus is on an app's own widget (`FocusedRecord::host_focus`) and the
|
||||
/// service moves no focus in the pass: egui would otherwise move focus off a framework stop that the same arrow
|
||||
/// reaches as an intent.
|
||||
pub(crate) fn clear_directional_focus(ctx: &Context) {
|
||||
ctx.memory_mut(|memory| memory.move_focus(FocusDirection::None));
|
||||
}
|
||||
|
||||
@@ -76,6 +76,9 @@ pub(crate) struct FocusedRecord {
|
||||
/// drag is captured: an app's own focused widget or popup keeps the keys a focused component keeps (Tab, Escape,
|
||||
/// the clipboard events). Written by the end hook after its focus moves.
|
||||
pub(crate) foreign: bool,
|
||||
/// egui focus is on a widget no framework component made, and no framework press or drag is captured: egui
|
||||
/// moves focus among the app's own widgets with the arrows and the Tab it keeps. Written by the end hook.
|
||||
pub(crate) host_focus: bool,
|
||||
/// egui holds a label text selection: a copy or cut event is egui's, which copies the selection. Written by the
|
||||
/// end hook.
|
||||
pub(crate) label_selection: bool,
|
||||
@@ -642,8 +645,16 @@ pub(crate) fn begin(ctx: &Context, tab_all_controls: bool) {
|
||||
tab_all_controls,
|
||||
frame: crate::runtime::egui::pass::frame_nr(ctx, viewport(ctx)),
|
||||
};
|
||||
egui_focus::clear_directional_focus(ctx);
|
||||
let (acts, announcements) = with(ctx, |state| nav::begin(state, input));
|
||||
let (acts, announcements, host_focus) = with(ctx, |state| {
|
||||
let host_focus = state.record.host_focus;
|
||||
let (acts, announcements) = nav::begin(state, input);
|
||||
(acts, announcements, host_focus)
|
||||
});
|
||||
// egui's direction moves focus from the widget it holds: only from an app's own widget, and not in a pass whose
|
||||
// focus the framework moves.
|
||||
if !host_focus || !acts.is_empty() {
|
||||
egui_focus::clear_directional_focus(ctx);
|
||||
}
|
||||
apply(ctx, acts);
|
||||
for text in announcements {
|
||||
crate::runtime::frame::announce(ctx, text);
|
||||
@@ -661,11 +672,12 @@ pub(crate) fn end(ctx: &Context) {
|
||||
// Escape over a captured framework press or drag cancels it (`frame::begin_pass`), whatever egui has focused.
|
||||
let captured = crate::runtime::pointer::press_captured(ctx) || crate::runtime::drag::running(ctx);
|
||||
let unmade = with(ctx, |state| state.focus_now.filter(|_| state.stop_of_focus().is_none()));
|
||||
let foreign =
|
||||
!captured && (egui_focus::popup_open(ctx) || unmade.is_some_and(|id| !crate::runtime::pointer::made_by_component(ctx, viewport, id)));
|
||||
let host_focus = !captured && unmade.is_some_and(|id| !crate::runtime::pointer::made_by_component(ctx, viewport, id));
|
||||
let foreign = host_focus || (!captured && egui_focus::popup_open(ctx));
|
||||
let label_selection = egui_focus::label_selection(ctx);
|
||||
with(ctx, |state| {
|
||||
state.record.foreign = foreign;
|
||||
state.record.host_focus = host_focus;
|
||||
state.record.label_selection = label_selection;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -585,6 +585,7 @@ pub(super) fn end(state: &mut State, focused: Option<Id>, frame: u64) -> Vec<Act
|
||||
keys,
|
||||
menu_bar: std::mem::take(&mut state.menu_bar),
|
||||
foreign: false,
|
||||
host_focus: false,
|
||||
label_selection: false,
|
||||
};
|
||||
acts
|
||||
|
||||
@@ -111,6 +111,8 @@ fn a_focused_egui_field_keeps_tab_escape_and_the_clipboard() {
|
||||
assert_eq!(host.borrow().text, "x", "the paste reaches the focused field");
|
||||
press_key(&mut frames, &mut show, Key::Tab);
|
||||
assert_eq!(seen(&host), (vec![Key::Tab], 0), "the field's Tab is egui's");
|
||||
frames.frame(Vec::new(), &mut show);
|
||||
assert_eq!(egui_focused(&frames.ctx), Some(named_id("button")), "egui's Tab moves focus on to the next widget");
|
||||
fixture::host_focus(&frames.ctx, field());
|
||||
frames.frame(Vec::new(), &mut show);
|
||||
press_key(&mut frames, &mut show, Key::Escape);
|
||||
@@ -157,6 +159,37 @@ fn escape_cancels_a_framework_drag_while_an_egui_field_has_focus() {
|
||||
frames.frame(vec![release(inside)], &mut show);
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn arrows_and_tab_move_egui_focus_among_host_buttons() {
|
||||
let (left, right) = (named_id("host left"), named_id("host right"));
|
||||
let (_host, mut base) = host_window();
|
||||
let mut show = move |ui: &mut Ui| {
|
||||
base(ui);
|
||||
fixture::host_button(ui, left, Rect::from_min_size(Pos2::new(300.0, 300.0), egui::vec2(80.0, 30.0)));
|
||||
fixture::host_button(ui, right, Rect::from_min_size(Pos2::new(420.0, 300.0), egui::vec2(80.0, 30.0)));
|
||||
};
|
||||
let mut frames = Frames::new();
|
||||
frames.frame(Vec::new(), &mut show);
|
||||
fixture::host_focus(&frames.ctx, left);
|
||||
frames.idle(2, &mut show);
|
||||
press_key(&mut frames, &mut show, Key::ArrowRight);
|
||||
assert_eq!(egui_focused(&frames.ctx), Some(right), "ArrowRight moves egui focus to the host's next button");
|
||||
press_key(&mut frames, &mut show, Key::ArrowLeft);
|
||||
assert_eq!(egui_focused(&frames.ctx), Some(left), "ArrowLeft moves it back");
|
||||
press_key(&mut frames, &mut show, Key::Tab);
|
||||
assert_eq!(egui_focused(&frames.ctx), Some(right), "Tab moves egui focus on");
|
||||
frames.frame(vec![key_press(Key::Tab, Modifiers::SHIFT)], &mut show);
|
||||
frames.frame(Vec::new(), &mut show);
|
||||
assert_eq!(egui_focused(&frames.ctx), Some(left), "Shift+Tab moves it back");
|
||||
|
||||
request(&frames.ctx, FocusTarget::Id(named_id("button")));
|
||||
frames.idle(2, &mut show);
|
||||
assert_eq!(egui_focused(&frames.ctx), Some(named_id("button")));
|
||||
press_key(&mut frames, &mut show, Key::ArrowRight);
|
||||
frames.frame(Vec::new(), &mut show);
|
||||
assert_eq!(egui_focused(&frames.ctx), Some(named_id("button")), "egui's arrows leave a framework stop's focus alone");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn escape_ends_an_egui_drag_on_a_host_widget() {
|
||||
let area = named_id("host drag area");
|
||||
|
||||
@@ -155,7 +155,7 @@ An app with its own eframe `App` calls the hooks in `craft::ui::shell::embed`. `
|
||||
| F10, Alt+letter | Taken while a framework menu bar is registered: F10 (`window.focusMenuBar`, bound and yielded as F6 is) focuses the bar, Alt+letter opens a menu (not on macOS). |
|
||||
| Edit chords | Taken while a framework editor is focused. Otherwise egui's copy, cut and paste events become the ⌘C/X/V chord for the app's commands, unless an app's own widget has the focus (below). While egui holds a label text selection, copy and cut stay egui's, and egui copies the selection. |
|
||||
| IME | Taken while a framework editor owns the IME: its events and `PlatformOutput.ime` are the runtime's. Otherwise they stay egui's, for the app's own `TextEdit` or IME code. |
|
||||
| An app's own focused widget | While egui focus is on a widget no framework component made, or an egui popup is open, and no framework press or drag is captured, Tab, Shift+Tab, Escape and egui's clipboard events stay in egui's input for it, and the runtime routes nothing for them. |
|
||||
| An app's own focused widget | While egui focus is on a widget no framework component made, or an egui popup is open, and no framework press or drag is captured, Tab, Shift+Tab, Escape and egui's clipboard events stay in egui's input for it, and the runtime routes nothing for them. While egui focus is on such a widget, the arrows and Tab move egui focus among the app's widgets as egui does; while a framework stop has the focus, egui's arrow moves are off. |
|
||||
| Releases | The release of every key the runtime took is taken too. |
|
||||
| Focus surrender | egui's own surrender is off; the runtime moves focus on press. A press a framework component takes moves focus to it; a press no component takes drops egui focus, unless it lands on the widget that holds egui focus (an app's own focused field keeps it). |
|
||||
| Zoom keys | egui's keyboard zoom is off. The interface scale is `RootEnv::scale`. |
|
||||
|
||||
Reference in New Issue
Block a user