Timecode caret: Left or Right with the whole text selected starts from its last digit (TextOptions::arrows_from_end)

This commit is contained in:
aiongg
2026-11-12 14:32:41 +00:00
parent 7d0bf2b01a
commit 1e1f36fbea
4 changed files with 37 additions and 4 deletions
+3 -2
View File
@@ -181,8 +181,8 @@ impl Outcome for TimecodeFieldOutput {
/// A time as text: Premiere's timecode field.
///
/// - Click (or Tab) to type: the text is selected; Enter or a click away commits through [`parse_time`] relative to
/// the current value; Esc cancels. A time that does not parse commits nothing and is reported on
/// - Click (or Tab) to type: the text is selected, and Left or Right on it start from its last digit; Enter or a click
/// away commits through [`parse_time`] relative to the current value; Esc cancels. A time that does not parse commits nothing and is reported on
/// [`TimecodeFieldOutput::error`].
/// - A drag on the field while it is not in edit, or on its side label, scrubs one frame per point, Shift ten, as a
/// gesture of `EditEvent<Tick>`s; a click without a drag edits, and in edit a drag selects text.
@@ -208,6 +208,7 @@ impl<'a> TimecodeField<'a> {
options.select_on_focus = true;
options.submits = true;
options.steps_value = true;
options.arrows_from_end = true;
TimecodeField { common: Common::new(), value, format, min: Tick::ZERO, options }.label(label)
}
+17 -2
View File
@@ -1,10 +1,11 @@
//! The timecode field: a drag scrubs a field that is not in edit.
//! The timecode field: a drag scrubs a field that is not in edit, and the arrows on the selected text start from its
//! end.
use craft_timeline::ui::TimecodeField;
use craft_timeline::{FrameRate, Tick, TimeFormat};
use craft_ui::contract::{Bind, Role, Show as _};
use craft_ui::testing::{Fixture, Story, StoryData, StoryOutput, trial};
use egui::{Ui, Vec2};
use egui::{Key, Modifiers, Ui, Vec2};
/// One and a half seconds: "00:00:01:15" at 30 frames per second.
fn start() -> Tick {
@@ -33,3 +34,17 @@ fn a_drag_on_a_timecode_field_moves_the_time() {
fixture.settle();
assert!(time(&mut fixture) > start(), "a drag to the right moved the time on");
}
#[trial]
fn left_on_the_selected_timecode_starts_from_its_last_digit() {
let mut fixture = fixture();
let target = fixture.by_role(Role::SPIN_BUTTON).first().copied().expect("the field");
fixture.click(&target);
fixture.settle();
fixture.key(Key::ArrowLeft, Modifiers::NONE);
fixture.key(Key::Backspace, Modifiers::NONE);
fixture.key(Key::Enter, Modifiers::NONE);
fixture.settle();
let edited = time(&mut fixture);
assert!(edited != start() && edited > Tick::from_seconds_f64(1.0), "Backspace took a digit of the frames, not nothing at the start");
}
@@ -271,6 +271,16 @@ pub(crate) fn select(ctx: &Context, id: Id, anchor: usize, caret: usize) {
text_edit::select(ctx, id, anchor, caret);
}
/// `TextOptions::arrows_from_end`: before the widget reads a plain Left or Right, a selection of the whole text
/// collapses to its end.
fn arrows_from_end(ctx: &Context, widget: Id, len: usize) {
let arrow = ctx.input(|input| input.modifiers.is_none() && (input.key_pressed(egui::Key::ArrowLeft) || input.key_pressed(egui::Key::ArrowRight)));
let whole = selection(ctx, widget).is_some_and(|(caret, anchor)| caret.min(anchor) == 0 && caret.max(anchor) == len);
if arrow && len > 0 && whole {
select(ctx, widget, len, len);
}
}
/// The modifiers held in this pass, which are the Enter press's.
fn held_mods(ctx: &Context) -> Mods {
let os = crate::runtime::windows::os_of(ctx);
@@ -305,6 +315,9 @@ pub(crate) fn run(state: &mut TextState, mut input: AdapterIn<'_>) -> AdapterOut
display.insert_str(at, &p.text);
at..at + p.text.len()
});
if input.focused && input.options.arrows_from_end {
arrows_from_end(ctx, widget, base.chars().count());
}
let separated = input.focused && !input.read_only && state.preedit.is_none() && egui_input::take_separator(ctx, input.options.commit_on);
let filter = egui_focus::event_filter(input.keys.keep, false);
let edit = text_edit::Edit {
@@ -80,6 +80,9 @@ pub struct TextOptions {
pub mixed: bool,
/// The text sits in the middle of the part instead of at its start (numbers).
pub centred: bool,
/// A plain Left or Right with the whole text selected starts from the end of the text, not from the selection's
/// start: a field whose last digits are what the user edits (a timecode's frames).
pub arrows_from_end: bool,
}
impl Default for TextOptions {
@@ -104,6 +107,7 @@ impl Default for TextOptions {
submits: false,
mixed: false,
centred: false,
arrows_from_end: false,
}
}
}