mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
Keys: a bare Tab is egui's while every stop is a host stop
With only the app's own widgets registered as stops (a hosted canvas), a bare Tab from nothing or from a host stop that does not keep Tab was a framework move whose order held the host stops alone: the canvas took focus and Tab cycled on it, so the app's other egui widgets were unreachable. The key stage now leaves that Tab to egui (TabToEgui), whose order holds the host stops and the app's widgets. A Tab that toggles the chrome stays the framework's.
This commit is contained in:
@@ -277,10 +277,14 @@ pub(crate) fn is_canvas_member(ctx: &Context, rec: &StopRec) -> bool {
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether a Tab in `viewport` has nothing to leave and nowhere to go: egui held no focus at the end of its last pass
|
||||
/// and it registered no focus stop there. Read by the input hook, before the begin hook rotates the registrations.
|
||||
pub(crate) fn tab_from_nothing(ctx: &Context, viewport: ViewportId) -> bool {
|
||||
with_viewport(ctx, viewport, |state| state.focus_now.is_none() && state.stops.is_empty())
|
||||
/// Whether a bare Tab in `viewport` is egui's own focus move: the window registered no framework stop in its last pass,
|
||||
/// only stops of the app's own egui widgets (`Stop::host`) if any, which egui's Tab order holds with the app's other
|
||||
/// widgets; and egui held no focus at the end of that pass, or held it on one of those stops. A framework move would
|
||||
/// reach the host stops alone. Read by the input hook, before the begin hook rotates the registrations.
|
||||
pub(crate) fn tab_is_egui_s(ctx: &Context, viewport: ViewportId) -> bool {
|
||||
with_viewport(ctx, viewport, |state| {
|
||||
state.stops.iter().all(|rec| rec.stop.host) && state.focus_now.is_none_or(|id| state.stops.iter().any(|rec| rec.stop.root == id))
|
||||
})
|
||||
}
|
||||
|
||||
/// How many stops the running pass has registered so far.
|
||||
@@ -1004,8 +1008,8 @@ pub(crate) fn begin(ctx: &Context, tab_all_controls: bool, dialog_tab: TabReach)
|
||||
let (acts, announcements) = nav::begin(state, input);
|
||||
(acts, announcements, host_focus)
|
||||
});
|
||||
// egui's direction moves focus from the widget it holds, an app's own, or from nothing with a Tab the key stage
|
||||
// left to egui; never in a pass whose focus the framework moves.
|
||||
// egui's direction moves focus from the widget it holds, an app's own, or with a Tab the key stage left to egui
|
||||
// (from nothing or a host stop); never in a pass whose focus the framework moves.
|
||||
if !(host_focus || tab_to_egui) || !acts.is_empty() {
|
||||
egui_focus::clear_directional_focus(ctx);
|
||||
}
|
||||
|
||||
@@ -56,10 +56,12 @@ struct Strip<'a> {
|
||||
/// The edit set whose clipboard events and edit chords stay in egui's input: the focused stop's when it is the
|
||||
/// app's own widget (`host`), else the canvas's with nothing focused (`canvas`).
|
||||
egui_edits: EditSet,
|
||||
/// egui holds no focus and the window registered no focus stop in its last pass: a framework focus move has
|
||||
/// nowhere to go, so a bare Tab is egui's. Focus on a component's widget without a stop is focus: egui would take
|
||||
/// a Tab from it as a move to the next widget it shows, in a later pass.
|
||||
no_stops: bool,
|
||||
/// A bare Tab that the focus does not keep is egui's own move (`focus::tab_is_egui_s`): the window registered no
|
||||
/// framework stop in its last pass, only the app's own widgets' (`Stop::host`) if any, and nothing has the focus or
|
||||
/// one of those stops has it. egui's Tab order holds those stops and the app's other widgets; a framework move
|
||||
/// would reach the stops alone. Focus on a component's widget without a stop is focus: egui would take a Tab from
|
||||
/// it as a move to the next widget it shows, in a later pass. A Tab that toggles the chrome stays the framework's.
|
||||
egui_tab: bool,
|
||||
/// egui holds a label text selection, which a copy or cut event copies.
|
||||
label_selection: bool,
|
||||
/// A tool host claimed the lone Alt tap: it goes to the claimer on every OS, not to the menu bar.
|
||||
@@ -379,7 +381,7 @@ impl Strip<'_> {
|
||||
PressUse::CanvasSpace
|
||||
} else if (self.foreign && (bare_tab || escape)) || (self.host && escape && !self.press_captured) || canvas_tab || egui_edit {
|
||||
PressUse::Egui
|
||||
} else if bare_tab && self.no_stops {
|
||||
} else if bare_tab && self.egui_tab && !self.keeps_tab {
|
||||
PressUse::TabToEgui
|
||||
} else if bare_tab && !self.keeps_tab {
|
||||
PressUse::Tab
|
||||
@@ -442,7 +444,8 @@ impl Strip<'_> {
|
||||
// command with it either.
|
||||
PressUse::Egui => true,
|
||||
PressUse::TabToEgui => {
|
||||
// egui moves its own focus with it, from nothing focused into the app's own widgets (`focus::begin`).
|
||||
// egui moves its own focus with it, from nothing focused or a host stop through the app's own widgets
|
||||
// (`focus::begin`).
|
||||
batch.nav.tab_to_egui = true;
|
||||
true
|
||||
}
|
||||
@@ -535,7 +538,7 @@ enum PressUse {
|
||||
CanvasSpace,
|
||||
/// The app's own widget or popup, or the canvas with nothing focused, reads the key from egui.
|
||||
Egui,
|
||||
/// A bare Tab egui moves its own focus with.
|
||||
/// A bare Tab egui moves its own focus with, through the app's own widgets.
|
||||
TabToEgui,
|
||||
/// A bare Tab: a focus move or a chrome toggle, which `take_nav` decides.
|
||||
Tab,
|
||||
@@ -667,7 +670,10 @@ pub(crate) fn input_hook(ctx: &Context, input: &RawInput, events: &mut Vec<Arriv
|
||||
let lists_edits = |keys: &KeyUse| EditCommands::ALL.iter().any(|command| command.edit().is_some_and(|edit| keys.edits.contains(edit)));
|
||||
let edit_chords = if keys.as_ref().or(canvas.as_ref()).is_some_and(lists_edits) { commands::edit_chords(ctx) } else { Arc::default() };
|
||||
let window_shortcuts = commands::window_shortcuts(ctx);
|
||||
let no_stops = keys.is_none() && !record.foreign && focus::tab_from_nothing(ctx, viewport);
|
||||
// A window with a document canvas whose root env has Tab toggle the chrome: the begin hook decides that Tab
|
||||
// (`take_nav`), so it is never egui's.
|
||||
let chrome_tab = focus::canvas_stop(ctx, viewport).is_some() && Env::root_for(ctx, viewport).is_some_and(|env| env.tab_toggles_chrome);
|
||||
let egui_tab = (keys.is_none() || record.host) && !record.foreign && !chrome_tab && focus::tab_is_egui_s(ctx, viewport);
|
||||
let latin = &input.latin_layout_chars;
|
||||
let requested_paste = commands::board::has_pending_paste(ctx, viewport);
|
||||
let mut requested_content = None::<commands::ClipContent>;
|
||||
@@ -700,7 +706,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &RawInput, events: &mut Vec<Arriv
|
||||
host: record.host,
|
||||
canvas,
|
||||
egui_edits: keys.filter(|_| record.host).or(canvas).map_or(EditSet::NONE, |keys| keys.edits),
|
||||
no_stops,
|
||||
egui_tab,
|
||||
label_selection: record.label_selection,
|
||||
alt_claimed: !stage.alt_claims.is_empty(),
|
||||
in_menu: record.menu.is_some(),
|
||||
|
||||
@@ -327,6 +327,39 @@ fn tab_from_nothing_focused_in_a_window_without_stops_moves_egui_focus() {
|
||||
assert!(focused.is_some() && focused != first && !reached, "egui focus on that widget is focus: the Tab is not egui's and moves nothing");
|
||||
}
|
||||
|
||||
/// A window whose only stop is the app's own canvas, between two of the app's own buttons: a bare Tab moves egui
|
||||
/// focus through egui's order, from nothing focused and from the canvas alike, and a canvas that keeps Tab keeps it.
|
||||
#[trial]
|
||||
fn tab_moves_egui_focus_through_the_app_s_widgets_while_every_stop_is_a_host_stop() {
|
||||
let (first, canvas, last) = (named_id("host first"), named_id("host canvas"), named_id("host last"));
|
||||
let keeps = Rc::new(Cell::new(false));
|
||||
let reached = Rc::new(Cell::new(false));
|
||||
let (keep, seen) = (Rc::clone(&keeps), Rc::clone(&reached));
|
||||
let mut show = move |ui: &mut Ui| {
|
||||
seen.set(seen.get() || key_reached(ui.ctx(), Key::Tab));
|
||||
fixture::host_button(ui, first, Rect::from_min_size(Pos2::new(100.0, 300.0), egui::vec2(80.0, 30.0)));
|
||||
fixture::host_button(ui, canvas, Rect::from_min_size(Pos2::new(220.0, 300.0), egui::vec2(80.0, 30.0)));
|
||||
host_canvas(ui.ctx(), canvas, if keep.get() { KeyUse::CANVAS.with_keep(KeepKeys::Tab) } else { KeyUse::CANVAS });
|
||||
fixture::host_button(ui, last, Rect::from_min_size(Pos2::new(340.0, 300.0), egui::vec2(80.0, 30.0)));
|
||||
};
|
||||
let mut frames = Frames::new();
|
||||
frames.idle(2, &mut show);
|
||||
let tab = |frames: &mut Frames, show: &mut dyn FnMut(&mut Ui)| {
|
||||
reached.set(false);
|
||||
frames.frame(vec![key_press(Key::Tab, Modifiers::NONE), key_release(Key::Tab, Modifiers::NONE)], &mut *show);
|
||||
frames.frame(Vec::new(), &mut *show);
|
||||
(egui_focused(&frames.ctx), reached.get())
|
||||
};
|
||||
assert_eq!(tab(&mut frames, &mut show), (Some(first), true), "Tab from nothing gives egui focus to the first widget in egui's order");
|
||||
assert_eq!(tab(&mut frames, &mut show), (Some(canvas), true), "the next Tab reaches the canvas");
|
||||
assert_eq!(tab(&mut frames, &mut show), (Some(last), true), "Tab from the canvas moves on through egui's order");
|
||||
keeps.set(true);
|
||||
request(&frames.ctx, FocusTarget::Id(canvas));
|
||||
frames.idle(2, &mut show);
|
||||
assert_eq!(egui_focused(&frames.ctx), Some(canvas));
|
||||
assert_eq!(tab(&mut frames, &mut show), (Some(canvas), true), "a canvas that keeps Tab gets it from egui and keeps the focus");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn escape_ends_an_egui_drag_on_a_host_widget() {
|
||||
let area = named_id("host drag area");
|
||||
|
||||
Reference in New Issue
Block a user