Timeline: Tab moves to the next value field (#361)

Tab in a value being typed (hot text) dropped the keyboard: egui moved
the focus to the next widget, and the inline editors asked for the
focus again every frame, which also reset what keys a field keeps for
itself.

As in After Effects, Tab now commits the value and types in the next
value field of the same panel (the next dimension, then the next
property's), Shift+Tab the previous one. The hot-text fields of each
panel register in drawing order; the dock tags each panel's Ui so the
order stays within it. `keep_focus` asks for the focus only when the
field doesn't have it.

Tests: tab_commits_a_value_and_moves_to_the_next (ui_inline_edit:
Timeline and Effect Controls).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bflatastic
2026-10-09 22:52:56 +07:00
co-authored by Claude Opus 5.5
parent 0171eb1e2e
commit 9c9e954ee2
4 changed files with 138 additions and 7 deletions
+3 -1
View File
@@ -159,7 +159,9 @@ impl EffectcraftApp {
panels::precomp::navigator(self, &mut child, nav);
content.min.y = nav.max.y;
}
let mut child = ui.new_child(egui::UiBuilder::new().max_rect(content).id_salt(("panel", p.id())));
let mut child = ui.new_child(
egui::UiBuilder::new().max_rect(content).id_salt(("panel", p.id())).ui_stack_info(egui::UiStackInfo::default().with_tag(crate::widgets::PANEL_TAG)),
);
child.set_clip_rect(content.intersect(ui.clip_rect()));
panels::show(self, &mut child, p, content);
if viewer && !self.ui.start_screen {
+66 -5
View File
@@ -6,7 +6,49 @@ use crate::automation::Registry;
use crate::icons::{self, Icon};
use crate::theme::Tokens;
/// "Hot text": a blue number you drag horizontally to scrub, click to type.
/// The [`egui::UiStackInfo`] tag of a dock panel's `Ui`: Tab moves between the hot-text fields
/// of one panel.
pub const PANEL_TAG: &str = "effectcraft.panel";
/// A panel's hot-text fields in drawing order (this pass's and the last one's), and a Tab
/// pressed while typing in one: (that field, Shift held, the pass).
#[derive(Clone, Default)]
struct TabOrder {
pass: u64,
current: Vec<egui::Id>,
last: Vec<egui::Id>,
pending: Option<(egui::Id, bool, u64)>,
}
fn tab_order_id(ui: &Ui) -> egui::Id {
let panel = ui.stack().iter().find(|s| s.tags().contains(PANEL_TAG)).map_or(egui::Id::NULL, |s| s.id);
egui::Id::new(("hot-tab-order", panel))
}
/// Puts hot-text field `id` in its panel's Tab order. True when a Tab in the field before it
/// (Shift+Tab: after it) moves the typing here.
fn tab_stop(ui: &Ui, id: egui::Id) -> bool {
let key = tab_order_id(ui);
let pass = ui.ctx().cumulative_pass_nr();
ui.data_mut(|d| {
let o = d.get_temp_mut_or_default::<TabOrder>(key);
if o.pass != pass {
o.last = std::mem::take(&mut o.current);
o.pass = pass;
}
o.current.push(id);
let Some((from, back, at)) = o.pending else { return false };
let i = o.last.iter().position(|f| *f == from);
let next = i.and_then(|i| if back { i.checked_sub(1) } else { i.checked_add(1) }).and_then(|i| o.last.get(i)).copied();
if next == Some(id) || next.is_none() || pass > at.saturating_add(2) {
o.pending = None;
}
next == Some(id)
})
}
/// "Hot text": a blue number you drag horizontally to scrub, click to type. Tab / Shift+Tab
/// while typing commits and types in the panel's next / previous one, as in After Effects.
/// Returns (response, Some(new value) when changed, drag finished).
pub fn hot_number_at(
ui: &mut Ui,
@@ -20,16 +62,28 @@ pub fn hot_number_at(
t: &Tokens,
) -> (Rect, Option<f64>, bool) {
let editing_id = id.with("editing");
let editing: Option<String> = ui.data(|d| d.get_temp(editing_id));
let mut editing: Option<String> = ui.data(|d| d.get_temp(editing_id));
if tab_stop(ui, id) && editing.is_none() {
editing = Some(format!("{value:.decimals$}"));
}
let text = format!("{value:.decimals$}{suffix}");
let galley = ui.painter().layout_no_wrap(text, Tokens::ui(12.0), t.hot_text);
let rect = Rect::from_min_size(rect_min, galley.size() + vec2(4.0, 4.0));
if let Some(mut buf) = editing {
let er = Rect::from_min_size(rect_min - vec2(2.0, 1.0), vec2((galley.size().x + 24.0).max(54.0), 18.0));
let mut child = ui.new_child(egui::UiBuilder::new().max_rect(er));
let r = child.add(egui::TextEdit::singleline(&mut buf).desired_width(er.width()).font(Tokens::ui(12.0)).margin(egui::Margin::symmetric(2, 1)));
// (Tab stays with the field: it moves to the next hot-text field, not the next widget.)
let r = child.add(
egui::TextEdit::singleline(&mut buf)
.id(id.with("text"))
.desired_width(er.width())
.font(Tokens::ui(12.0))
.margin(egui::Margin::symmetric(2, 1))
.lock_focus(true),
);
let tab = r.has_focus().then(|| ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Tab).then_some(i.modifiers.shift))).flatten();
let mut out = None;
if r.lost_focus() {
if r.lost_focus() || tab.is_some() {
let txt = buf.trim().trim_end_matches(suffix).trim().replace(',', ".");
if !ui.input(|i| i.key_pressed(egui::Key::Escape))
&& let Ok(v) = txt.parse::<f64>()
@@ -38,6 +92,11 @@ pub fn hot_number_at(
out = Some(v.clamp(range.0, range.1));
}
ui.data_mut(|d| d.remove::<String>(editing_id));
if let Some(back) = tab {
r.surrender_focus();
let (key, pass) = (tab_order_id(ui), ui.ctx().cumulative_pass_nr());
ui.data_mut(|d| d.get_temp_mut_or_default::<TabOrder>(key).pending = Some((id, back, pass)));
}
} else {
keep_focus(ui.ctx(), &r, &buf);
ui.data_mut(|d| d.insert_temp(editing_id, buf));
@@ -97,11 +156,13 @@ pub fn hot_int_at(ui: &mut Ui, rect_min: egui::Pos2, id: egui::Id, value: i64, s
/// Keep the keyboard on an inline editor that is open until it loses the focus. The frame it
/// opens (it has no focus yet) its whole `text` is selected, so typing replaces the value.
/// (Only then is the focus requested: a request resets what keys the field keeps for itself,
/// such as Tab with [`egui::TextEdit::lock_focus`].)
pub fn keep_focus(ctx: &egui::Context, field: &Response, text: &str) {
if !ctx.memory(|m| m.has_focus(field.id)) {
select_all(ctx, field.id, text);
field.request_focus();
}
field.request_focus();
}
/// Select all of the text field `id`'s `text`, so typing replaces it.