Footer buttons and media controls are Tab stops of their own; the text ruler is one Tab stop

FooterBar no longer makes its members one roving group, so each dialog or panel footer button is a Tab stop in
display order, as the old PanelFooter. Bar::tab_stops drops a bar's roving group; MediaControls uses it. The
TextRuler's markers, stops and column borders are one roving group whose Page keys move between them, as the
canvas guides; the arrows reach the ruler as nudges, and its own Page pick goes.
This commit is contained in:
aiongg
2026-11-12 14:32:42 +00:00
parent 0b82d9415e
commit fd2e9d2abb
10 changed files with 123 additions and 48 deletions
+8 -5
View File
@@ -15,7 +15,7 @@
//! ```
use super::length::DocUnit;
use craft_behavior::{Axis, GesturePhase, HitLevel, HitPriority, Metrics, Sense};
use craft_behavior::{Axis, GesturePhase, HitLevel, HitPriority, Metrics, NavShape, Roving, Sense};
use craft_ui::contract::{
Access, Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, Input, MeasureCx, Name, Numeric, Outcome, PaintUi, PartsCx, Props as _,
Role, WordSetCx as _,
@@ -309,10 +309,10 @@ pub struct TextRulerStyle {
/// reader's Increment, Decrement and SetValue report a lone [`GesturePhase::Commit`].
/// - Right-to-left paragraphs mirror the ruler: positions run from the right edge, and the stops' feet point the other
/// way.
/// - Keys: each marker and stop is a Tab stop. Page Down and Page Up move to the next or previous marker or stop, the
/// arrows nudge the focused one by the snap step (Shift larger, Alt finer), Delete and Backspace clear a stop and
/// focus the one before it. Each key is one edit. Each marker and stop is a slider named by its kind, with its value
/// in the unit; Increment, Decrement and SetValue act on it.
/// - Keys: the markers, stops and column borders are one Tab stop. Page Down and Page Up move to the next or previous
/// one, the arrows nudge the focused one by the snap step (Shift larger, Alt finer), Delete and Backspace clear a
/// stop and focus the one before it. Each key is one edit. Each marker and stop is a slider named by its kind, with
/// its value in the unit; Increment, Decrement and SetValue act on it.
#[derive(craft_ui::contract::Component, craft_ui::contract::Props)]
#[component(content_painter)]
pub struct TextRuler<'a> {
@@ -475,6 +475,7 @@ impl Component for TextRuler<'_> {
.priority(handle)
.cursor(craft_behavior::CursorIcon::ResizeHorizontal)
.focusable()
.nudges()
.focus_shape(FocusShape::Rect)
.value_part();
if matches!(item, Item::Stop(at) if self.selected == Some(at)) {
@@ -488,9 +489,11 @@ impl Component for TextRuler<'_> {
.priority(handle)
.cursor(craft_behavior::CursorIcon::ResizeHorizontal)
.focusable()
.nudges()
.focus_shape(FocusShape::Rect)
.value_part();
}
cx.roving(Roving::new(NavShape::Flat).page(Some(1)).home_end(false));
if self.kind.is_some() {
cx.part(SELECTOR, layout.corner)
.sense(Sense::CLICK | Sense::CONTEXT_CLICK)
-12
View File
@@ -119,15 +119,3 @@ pub(super) fn moved(values: &RulerValues, item: Item, to: f32, width: f32) -> (R
}
(next, item)
}
/// The item after (or before) `picked` in `items`, staying at the ends.
pub(super) fn pick(items: &[Item], picked: Option<Item>, forward: bool) -> Option<Item> {
let at = picked.and_then(|picked| items.iter().position(|item| *item == picked));
let to = match (at, forward) {
(None, true) => 0,
(None, false) => items.len().saturating_sub(1),
(Some(at), true) => (at + 1).min(items.len().saturating_sub(1)),
(Some(at), false) => at.saturating_sub(1),
};
items.get(to).copied()
}
+7 -15
View File
@@ -1,7 +1,7 @@
//! One pass of the ruler's input: the gestures on the items, the strip and the column borders, keys and the
//! screen reader's requests.
use super::items::{COLUMN, Item, STRIP, moved, pick, place, without};
use super::items::{COLUMN, Item, STRIP, moved, place, without};
use super::{ColumnBorder, ColumnDrag, RulerValues, SAME_POSITION, TabLeader, TabStop, TextRuler, dropped_off, snap_stop};
use crate::ui::length::{DocUnit, shown};
use craft_behavior::{CancelReason, GesturePhase, Granularity, Intent, NavMove, PointerButton, PressPhase, RulerUnits, Ticks};
@@ -64,10 +64,9 @@ fn snap_within(position: f32, step: f32, bounds: (f32, f32), free: bool) -> f32
}
/// The moves and nudges taken from the focused part this pass: how far they move it, in points (positive: further
/// from the start edge), and the Page Down and Page Up moves, which pick the next or previous item.
fn take_steps(cx: &mut Cx<'_, TextRuler<'_>>, g: &super::Geometry, step: f32) -> (f32, Vec<NavMove>) {
/// from the start edge).
fn take_steps(cx: &mut Cx<'_, TextRuler<'_>>, g: &super::Geometry, step: f32) -> f32 {
let mut by = 0.0;
let mut pages = Vec::new();
while let Some((mv, mods)) = cx.intents().take_move() {
let size = if mods.fine {
SAME_POSITION
@@ -77,7 +76,6 @@ fn take_steps(cx: &mut Cx<'_, TextRuler<'_>>, g: &super::Geometry, step: f32) ->
step
};
match mv {
NavMove::PageDown | NavMove::PageUp => pages.push(mv),
NavMove::Forward | NavMove::Up => by += size * g.sign(),
NavMove::Back | NavMove::Down => by -= size * g.sign(),
_ => {}
@@ -93,7 +91,7 @@ fn take_steps(cx: &mut Cx<'_, TextRuler<'_>>, g: &super::Geometry, step: f32) ->
};
by += direction.x * size * g.sign();
}
(by, pages)
by
}
impl TextRuler<'_> {
@@ -295,8 +293,7 @@ impl TextRuler<'_> {
cx.focus_part(COLUMN.at(index as u64));
}
/// Keys on the focused item: Page Down and Page Up move to the next and previous item, the arrows nudge it, Delete
/// and Backspace clear a stop; a screen reader's SetValue sets an item's position.
/// Keys on the focused item: the arrows nudge it, Delete and Backspace clear a stop; a screen reader's SetValue sets an item's position.
fn run_keys(&mut self, cx: &mut Cx<'_, Self>, g: &super::Geometry, step: f32, held: &mut Held) {
if cx.gesture().is_some() {
return;
@@ -318,7 +315,7 @@ impl TextRuler<'_> {
}
let Some(part) = cx.active_part() else { return };
if part.key == COLUMN {
let (by, _) = take_steps(cx, g, step);
let by = take_steps(cx, g, step);
if let (Some(index), true) = (part.index, by != 0.0)
&& let Some(position) = self.columns.get(index as usize).copied()
{
@@ -327,12 +324,7 @@ impl TextRuler<'_> {
return;
}
let Some(item) = Item::of_part(part) else { return };
let (by, pages) = take_steps(cx, g, step);
for mv in pages {
if let Some(next) = pick(&Item::all(&values), Some(item), mv == NavMove::PageDown) {
cx.focus_part(next.part());
}
}
let by = take_steps(cx, g, step);
if by != 0.0 {
let (value, now) = moved(&values, item, item.position(&values, g.width) + by, g.width);
cx.set(value);
+1
View File
@@ -2,4 +2,5 @@
mod editor;
mod notes;
mod ruler;
mod tabs;
+46
View File
@@ -0,0 +1,46 @@
//! The text ruler's keys: the ruler is one Tab stop, Page Down moves between its markers and stops, and the arrows
//! move the focused one.
use craft_text::ui::ruler::{RulerValues, TabAlign, TabLeader, TabStop, TextRuler};
use craft_ui::buttons::Button;
use craft_ui::contract::{Bind, Show as _, ShowContainer as _};
use craft_ui::layout_components::Stack;
use craft_ui::testing::{Fixture, trial};
use egui::{Key, Modifiers};
use std::cell::RefCell;
use std::rc::Rc;
/// Which of `labels` holds the focus.
fn focused<'a>(fixture: &Fixture, labels: &[&'a str]) -> Option<&'a str> {
let focused = craft_ui::testing::access::focused_key(fixture.tree());
labels.iter().copied().find(|label| fixture.by_label(label).is_some_and(|target| target.node == focused))
}
#[trial]
fn the_ruler_is_one_tab_stop_whose_page_keys_pick_and_arrows_move() {
let mut start = RulerValues::default();
start.stops = vec![TabStop::new(72.0, TabAlign::Left, TabLeader::None)];
let values = Rc::new(RefCell::new(start));
let shown = values.clone();
let mut fixture = Fixture::new(move |ui| {
Stack::new().show(ui, |ui, _| {
Button::new("Before").show(ui);
TextRuler::new("Ruler", Bind::bound(&mut *shown.borrow_mut()), 300.0).show(ui);
Button::new("After").show(ui);
});
});
fixture.idle(4);
let labels = ["Before", "First Line Indent", "Hanging Indent", "After"];
fixture.key(Key::Tab, Modifiers::NONE).idle(2);
assert_eq!(focused(&fixture, &labels), Some("Before"), "Tab reaches the first button");
fixture.key(Key::Tab, Modifiers::NONE).idle(2);
assert_eq!(focused(&fixture, &labels), Some("First Line Indent"), "Tab enters the ruler at its first marker");
fixture.key(Key::PageDown, Modifiers::NONE).idle(2);
assert_eq!(focused(&fixture, &labels), Some("Hanging Indent"), "Page Down moves to the next marker");
let before_move = values.borrow().clone();
fixture.key(Key::ArrowRight, Modifiers::NONE).idle(2);
assert_ne!(*values.borrow(), before_move, "Right moves the focused marker");
assert_eq!(focused(&fixture, &labels), Some("Hanging Indent"), "Right keeps the focus on the marker");
fixture.key(Key::Tab, Modifiers::NONE).idle(2);
assert_eq!(focused(&fixture, &labels), Some("After"), "Tab leaves the ruler: its markers and stops are one stop");
}
+6 -6
View File
@@ -325,11 +325,11 @@ impl Outcome for MediaControlsOutput {
}
}
/// PowerPoint's media controls under a video or sound on a slide: play or pause, back and forward a quarter second,
/// a seek track with the trim marks, and the time (`1:05.25` style in the mono role, digits through the units). The
/// controls are a named toolbar `Bar`, so a press on them never reaches the canvas under them. The seek track is a
/// `Slider` (one Tab stop; Left and Right a quarter second, Home and End) reporting an `EditEvent<f64>` gesture in
/// seconds.
/// PowerPoint's media controls under a video or sound on a slide: play or pause, back and forward a quarter second, a
/// seek track with the trim marks, and the time (`1:05.25` style in the mono role, digits through the units). The
/// controls are a named toolbar `Bar`, so a press on them never reaches the canvas under them; each button is its own
/// Tab stop, in reading order. The seek track is a `Slider` (one Tab stop; Left and Right a quarter second, Home and
/// End) reporting an `EditEvent<f64>` gesture in seconds.
#[derive(Pattern, Props)]
pub struct MediaControls {
label: Label,
@@ -358,7 +358,7 @@ impl MediaControls {
let playable = duration.is_finite() && duration > 0.0;
let position = if position.is_finite() { position.clamp(0.0, if playable { duration } else { 0.0 }) } else { 0.0 };
let (mut toggle, mut nudge, mut seek) = (false, None, EditEvents::new());
let shown = Bar::new(label, BarKind::Tool).show(ui, |ui, _| {
let shown = Bar::new(label, BarKind::Tool).tab_stops().show(ui, |ui, _| {
let play = IconId::new(if playing { "pause" } else { "play" });
toggle = clicked(&IconButton::new(play).label(words.play_pause_media.clone()).show(ui));
if clicked(&IconButton::new(IconId::new("chevron_left")).label(words.back_quarter.clone()).show(ui)) {
+3 -3
View File
@@ -12,7 +12,7 @@ use crate::runtime::env::{Env, EnvValue, Label};
use crate::runtime::focus;
use crate::runtime::keys;
use crate::style::{ComponentStyle, StyleCx};
use craft_behavior::{Metrics, NavShape, OwnerClass, Roving, Sense};
use craft_behavior::{Metrics, OwnerClass, Sense};
use craft_host::target::Os;
use craft_keymap::{Chord, Key};
use craft_theme::{
@@ -35,7 +35,7 @@ pub struct FooterBarStyle {
metrics: Metrics,
}
/// A footer region's content: its body is the caller's closure, and its members are one roving group.
/// A footer region's content: its body is the caller's closure, and each member is its own Tab stop, in display order.
#[derive(crate::contract::Component, crate::contract::Props)]
#[component(container)]
pub struct FooterBar {
@@ -537,7 +537,7 @@ impl FooterBar {
pub fn body() -> Self {
FooterBar {
common: Common::new(),
props: ContainerProps::new().sizing(Sizing::FillWidth).roving(Roving::new(NavShape::Flat)).layout(BodyLayout::row().align(RowAlign::End)),
props: ContainerProps::new().sizing(Sizing::FillWidth).layout(BodyLayout::row().align(RowAlign::End)),
content: Content::Body,
enter: false,
column: false,
@@ -0,0 +1,36 @@
//! A footer bar's buttons are Tab stops of their own: Tab moves between them and the arrows leave the focus where it is.
use craft_ui::buttons::Button;
use craft_ui::contract::{Show as _, ShowContainer as _};
use craft_ui::layout_components::Stack;
use craft_ui::sections::{DialogRole, FooterBar, FooterButton};
use craft_ui::testing::{Fixture, trial};
use egui::{Key, Modifiers};
/// Which of `labels` holds the focus.
fn focused<'a>(fixture: &Fixture, labels: &[&'a str]) -> Option<&'a str> {
let focused = craft_ui::testing::access::focused_key(fixture.tree());
labels.iter().copied().find(|label| fixture.by_label(label).is_some_and(|target| target.node == focused))
}
#[trial]
fn each_footer_button_is_its_own_tab_stop() {
let mut fixture = Fixture::new(|ui| {
Stack::new().show(ui, |ui, _| {
Button::new("Before").show(ui);
let _ =
FooterBar::buttons([FooterButton::new("OK", DialogRole::Accept), FooterButton::new("Cancel", DialogRole::Cancel)]).show_footer(ui);
});
});
fixture.idle(4);
let labels = ["Before", "OK", "Cancel"];
fixture.key(Key::Tab, Modifiers::NONE).idle(2);
assert_eq!(focused(&fixture, &labels), Some("Before"), "Tab reaches the first button");
fixture.key(Key::Tab, Modifiers::NONE).idle(2);
let first = focused(&fixture, &labels).filter(|label| *label != "Before").expect("Tab reaches a footer button");
fixture.key(Key::ArrowRight, Modifiers::NONE).idle(2);
assert_eq!(focused(&fixture, &labels), Some(first), "the arrows do not move between footer buttons");
fixture.key(Key::Tab, Modifiers::NONE).idle(2);
let second = focused(&fixture, &labels).expect("Tab stays among the labelled stops");
assert!(second != first && second != "Before", "Tab moves to the other footer button");
}
@@ -1,5 +1,6 @@
//! Reorderable sections.
mod footer_keys;
mod footer_row;
mod layout;
mod reorderable;
+15 -7
View File
@@ -73,13 +73,13 @@ impl Outcome for BarOutput {
}
}
/// A bar over a chrome surface whose body holds the members in reading order at the small control size. They are
/// one roving group. The bar is as tall as its tallest member plus the control padding, and takes the width it is
/// offered. A bar of one row lays its body out as a row; a bar of several rows as a stack whose rows the caller makes
/// with `Row`. A row too long for the bar first narrows its selects and elidable text, then scrolls, so a member past
/// the edge is reached by Tab and revealed. A rule between groups of members is a `Divider::vertical()`. A bar with
/// `commit_cancel` ends with a cancel and a commit button in its end region, outside the scroll; they run
/// `BarCommands`.
/// A bar over a chrome surface whose body holds the members in reading order at the small control size. They are one
/// roving group unless the bar is made of [`tab_stops`](Bar::tab_stops). The bar is as tall as its tallest member plus
/// the control padding, and takes the width it is offered. A bar of one row lays its body out as a row; a bar of
/// several rows as a stack whose rows the caller makes with `Row`. A row too long for the bar first narrows its selects
/// and elidable text, then scrolls, so a member past the edge is reached by Tab and revealed. A rule between groups of
/// members is a `Divider::vertical()`. A bar with `commit_cancel` ends with a cancel and a commit button in its end
/// region, outside the scroll; they run `BarCommands`.
#[derive(crate::contract::Component)]
#[component(container)]
pub struct Bar<'a> {
@@ -127,6 +127,14 @@ impl<'a> Bar<'a> {
self
}
/// Makes each member its own Tab stop, in reading order, as media controls are; the arrows no longer move between
/// them.
#[must_use]
pub fn tab_stops(mut self) -> Self {
self.props.roving = None;
self
}
/// Ends the bar with a cancel and a commit button named `cancel` and `commit`. They run `BarCommands::Cancel`
/// and `BarCommands::Commit`, so their tooltips carry the chords the registry binds.
#[must_use]