TransportBar centres its buttons and keeps the button editor at the end edge

Bar::align sets the body row's alignment; Bar::trailing gets the bar's body
context so an end-region group can read and write the state the body reads.
A centred or end-aligned row in a body that scrolls along x measures its band
from the view, as the shrink step does.
This commit is contained in:
aiongg
2026-11-12 14:32:46 +00:00
parent 9c8af0ac52
commit efc09443fa
10 changed files with 125 additions and 55 deletions
+2 -2
View File
@@ -10,7 +10,7 @@ use craft_ui::bars::{Bar, BarKind};
use craft_ui::buttons::IconButton;
use craft_ui::canvas::RulerText;
use craft_ui::choice::Checkbox;
use craft_ui::contract::{Bind, Entry, Outcome, Props as _, Show as _, ShowContainer as _, clicked};
use craft_ui::contract::{Bind, BodyCx, Entry, Outcome, Props as _, Show as _, ShowContainer as _, clicked};
use craft_ui::display::Divider;
use craft_ui::runtime::env::{Env, Label, UnitKind};
use egui::{Response, Ui};
@@ -170,7 +170,7 @@ impl<'a, W: TransformWarp> TransformBar<'a, W> {
let mut unit_picked = None;
let mut switched = false;
// Reset warp and the mode switch stay in view after commit and cancel while the fields scroll.
let trailing = |ui: &mut Ui| {
let trailing = |ui: &mut Ui, _: &mut BodyCx<'_>| {
if warping {
let reset = IconButton::new(IconId::new("undo")).label(words.reset_warp.clone()).show(ui);
reset_warp = clicked(&reset);
+45 -38
View File
@@ -7,14 +7,16 @@ use craft_behavior::{EditEvents, IconId};
use craft_ui::bars::{Bar, BarKind};
use craft_ui::buttons::{Button, IconButton, ToggleButton};
use craft_ui::choice::Checkbox;
use craft_ui::contract::{Bind, Held, NoValue, Outcome, Pattern, PopupKind, Props, Show as _, ShowContainer as _, clicked};
use craft_ui::contract::{Bind, BodyCx, Held, NoValue, Outcome, Pattern, PopupKind, Props, Show as _, ShowContainer as _, clicked};
use craft_ui::display::{Divider, Text};
use craft_ui::layout::RowAlign;
use craft_ui::popover::Popover;
use craft_ui::runtime::env::{Env, Label};
use craft_ui::runtime::memory::Remember as _;
use craft_ui::style::TypeRole;
use craft_ui::value_drag::Slider;
use egui::{Response, Ui};
use std::cell::RefCell;
/// How far the arrows and the seek keys move the position, in seconds.
const NUDGE: f64 = 0.25;
@@ -214,9 +216,9 @@ impl Outcome for TransportBarOutput {
#[derive(Clone, Debug, Default)]
struct EditorOpen(bool);
/// A monitor's transport row (Premiere's, as FilmCraft draws it): a tool `Bar` of icon buttons, Pause in place of
/// Play while playing, Loop pressed while on, and with `editable` a "+" button editor at the end (a popover with a
/// checkbox per button and Reset).
/// A monitor's transport row (Premiere's, as FilmCraft draws it): a tool `Bar` of icon buttons centred in the room the
/// editor leaves, Pause in place of Play while playing, Loop pressed while on, and with `editable` a "+" button editor
/// at the end edge (a popover with a checkbox per button and Reset).
///
/// - Keyboard and AccessKit are the bar's: buttons named by their tooltips, Space and Enter press. The keys in the
/// tooltips (Space, I, O, Left, Right) are the app's shortcuts, not handled here.
@@ -250,12 +252,47 @@ impl<'a> TransportBar<'a> {
pub fn show(self, ui: &mut Ui) -> TransportBarOutput {
let TransportBar { label, buttons, settings, playing, looping, editable } = self;
let words = Env::of(ui).words::<TransportWords>();
let mut held = settings.map(Held::new);
// The editor in the end region writes the settings the body reads; in a measuring run it runs first.
let held = RefCell::new(settings.map(Held::new));
let editor = editable && held.borrow().is_some();
let (mut pressed, mut changed) = (None, false);
let shown = Bar::new(label.clone(), BarKind::Tool).show(ui, |ui, cx| {
let before = held.as_mut().and_then(|held| held.read(cx)).unwrap_or_default();
let trailing = |ui: &mut Ui, cx: &mut BodyCx<'_>| {
let mut held = held.borrow_mut();
let Some(held) = held.as_mut() else { return };
let before = held.read(cx).unwrap_or_default();
let mut next = before.clone();
for button in buttons.iter().filter(|button| !before.hidden.contains(button)) {
let opener = IconButton::new(IconId::new("plus")).label(words.button_editor.clone()).popup(PopupKind::Dialog).show(ui);
let mut open = cx.keep::<EditorOpen>().0;
if clicked(&opener) {
open = !open;
}
Popover::new(words.button_editor.clone(), Bind::bound(&mut open)).opened_by(&opener).show(ui, |ui, _| {
for button in buttons {
let mut visible = !next.hidden.contains(button);
let edit = Checkbox::new(Bind::bound(&mut visible)).label(button.label(&words).to_owned()).show(ui);
if !edit.events.is_empty() {
next.hidden.retain(|hidden| hidden != button);
if !visible {
next.hidden.push(button.clone());
}
}
}
Divider::horizontal().show(ui);
if clicked(&Button::new(words.reset.clone()).show(ui)) {
next.hidden.clear();
}
});
cx.keep::<EditorOpen>().0 = open;
if next != before {
changed = true;
held.write(cx, &before, next, label.as_str());
}
};
let bar = Bar::new(label.clone(), BarKind::Tool).align(RowAlign::Center);
let bar = if editor { bar.trailing(trailing) } else { bar };
let shown = bar.show(ui, |ui, cx| {
let shown = held.borrow_mut().as_mut().and_then(|held| held.read(cx)).unwrap_or_default();
for button in buttons.iter().filter(|button| !shown.hidden.contains(button)) {
let name = button.label(&words).to_owned();
if *button == TransportButton::Loop {
let mut on = looping;
@@ -267,36 +304,6 @@ impl<'a> TransportBar<'a> {
pressed = Some(button.clone());
}
}
if editable && held.is_some() {
let opener = IconButton::new(IconId::new("plus")).label(words.button_editor.clone()).popup(PopupKind::Dialog).show(ui);
let mut open = cx.keep::<EditorOpen>().0;
if clicked(&opener) {
open = !open;
}
Popover::new(words.button_editor.clone(), Bind::bound(&mut open)).opened_by(&opener).show(ui, |ui, _| {
for button in buttons {
let mut visible = !next.hidden.contains(button);
let edit = Checkbox::new(Bind::bound(&mut visible)).label(button.label(&words).to_owned()).show(ui);
if !edit.events.is_empty() {
next.hidden.retain(|hidden| hidden != button);
if !visible {
next.hidden.push(button.clone());
}
}
}
Divider::horizontal().show(ui);
if clicked(&Button::new(words.reset.clone()).show(ui)) {
next.hidden.clear();
}
});
cx.keep::<EditorOpen>().0 = open;
}
if next != before {
changed = true;
if let Some(held) = held.as_mut() {
held.write(cx, &before, next, label.as_str());
}
}
});
TransportBarOutput { response: shown.output.response, pressed, settings_changed: changed }
}
+1
View File
@@ -5,3 +5,4 @@ mod lanes;
mod panel;
mod ruler;
mod timecode;
mod transport;
+50
View File
@@ -0,0 +1,50 @@
//! The transport bar: its "+" editor sits at the end edge, and unchecking a button there hides it from the row and
//! reports the settings changed.
use std::cell::RefCell;
use std::rc::Rc;
use craft_timeline::ui::{TransportBar, TransportBarSettings, TransportButton};
use craft_ui::contract::{Bind, Role};
use craft_ui::testing::{Fixture, trial};
use egui::vec2;
#[derive(Default)]
struct Seen {
settings: TransportBarSettings,
changed: bool,
}
fn bar() -> (Fixture, Rc<RefCell<Seen>>) {
let seen = Rc::new(RefCell::new(Seen::default()));
let shown = seen.clone();
let mut fixture = Fixture::new(move |ui| {
let seen = &mut *shown.borrow_mut();
let out = TransportBar::new("Program transport", &TransportButton::PROGRAM).settings(Bind::bound(&mut seen.settings)).show(ui);
seen.changed |= out.settings_changed;
});
fixture.room(vec2(900.0, 80.0));
fixture.settle();
(fixture, seen)
}
#[trial]
fn unchecking_a_button_in_the_end_edge_editor_hides_it_from_the_row() {
let (mut fixture, seen) = bar();
let row = fixture.by_role(Role::TOOLBAR).first().expect("the bar").rect;
let opener = fixture.by_label("Button Editor").expect("the editor's opener");
assert!(row.max.x - opener.rect.max.x < 16.0, "the opener sits at the end edge: {:?} in {:?}", opener.rect, row);
let in_row = |fixture: &Fixture| fixture.by_role(Role::BUTTON).iter().filter(|button| row.contains(button.center())).count();
let buttons = in_row(&fixture);
fixture.click(&opener);
fixture.settle();
let first = fixture.by_role(Role::CHECK_BOX).first().cloned().expect("a checkbox per button");
fixture.click(&first);
fixture.settle();
let seen = seen.borrow();
assert_eq!(seen.settings.hidden, [TransportButton::AddMarker], "the unchecked button is hidden");
assert!(seen.changed, "the bar reports the settings changed");
drop(seen);
assert_eq!(in_row(&fixture), buttons - 1, "the row left the hidden button out");
}
+5 -3
View File
@@ -832,9 +832,11 @@ pub(crate) fn run_body_measured<R>(
RowAlign::Start | RowAlign::SpaceBetween => None,
RowAlign::Center | RowAlign::End => {
let last = this_pass(&ctx, sizing, id);
// Without a measuring run the block is placed as `Start`.
last.filter(|last| last.x > 0.0 && last.x < room.width()).map(|last| {
let spare = room.width() - last.x;
// In a body that scrolls along x the band is the width its view shows, not the scroll region.
let band = if Env::of(ui).unbounded.0 { shrink_room(ui) } else { room.width() };
// Without a measuring run, or in a band without an end, the block is placed as `Start`.
last.filter(|last| band.is_finite() && last.x > 0.0 && last.x < band).map(|last| {
let spare = band - last.x;
let lead = if *align == RowAlign::Center { spare / 2.0 } else { spare };
Flow::new(room, rtl).take_start(lead).1.rect()
})
+1 -1
View File
@@ -73,7 +73,7 @@ pub fn options_bar(ui: &mut Ui, viewer: &mut Viewer) {
let mut more = None;
let Viewer { env, more_open, variants, preview, workspace: shown, .. } = viewer;
Bar::new("Appearance", BarKind::Options)
.trailing(|ui| {
.trailing(|ui, _| {
IconButton::command(IconId::new("undo"), GalleryCommands::ResetLayout).show(ui);
choose(ui, "Workspace", shown, &workspaces);
})
+1 -1
View File
@@ -293,7 +293,7 @@ fn status_bar(ui: &mut Ui, state: &mut MockState) {
let layer_count = words.layer_count.text(layers, &env.format().integer(layers));
let mut chosen = None;
let _ = Bar::new("Status", BarKind::Status)
.trailing(|ui| {
.trailing(|ui, _| {
Link::new(words.cpu_rendering.clone()).tone(Tone::Warn).show(ui);
})
.show(ui, |ui, _| {
+15 -5
View File
@@ -6,6 +6,7 @@ use crate::contract::{
Access, BodyCx, BodyLayout, Common, Component, Container, ContainerProps, Cx, EndRegion, Gap, InBar, NoValue, Outcome, Pad, Politeness,
Props as _, Role, Scroll, Show as _, ShowContainer as _, Sizing, WordSetCx as _,
};
use crate::layout::RowAlign;
use crate::layout_components::Row;
use crate::runtime::access::Name;
use crate::runtime::commands::{CommandSet as _, Commands as _};
@@ -17,7 +18,7 @@ use egui::{Response, Ui};
use std::num::NonZeroU8;
/// The trailing group's content.
type TrailingFn<'a> = Box<dyn FnMut(&mut Ui) + 'a>;
type TrailingFn<'a> = Box<dyn FnMut(&mut Ui, &mut BodyCx<'_>) + 'a>;
/// What a bar is for; it picks the bar's role, spacing and name.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@@ -129,6 +130,14 @@ impl<'a> Bar<'a> {
self
}
/// Places the members of a bar of one row along it by `align` (`Start` by default): a transport bar centres its
/// buttons in the room its end region leaves.
#[must_use]
pub fn align(mut self, align: RowAlign) -> Self {
self.props.layout = self.props.layout.clone().align(align);
self
}
/// Makes each member its own Tab stop, in reading order, as media controls are; the arrows no longer move between
/// them.
#[must_use]
@@ -147,9 +156,10 @@ impl<'a> Bar<'a> {
}
/// Ends the bar with a group of controls that stays at the reading end, outside the scroll, while the members
/// before it scroll. The group sits after the cancel and commit buttons.
/// before it scroll. The group sits after the cancel and commit buttons and gets the bar's body context, so it can
/// read and write the state the body reads. In a measuring run it runs before the body.
#[must_use]
pub fn trailing(mut self, group: impl FnMut(&mut Ui) + 'a) -> Self {
pub fn trailing(mut self, group: impl FnMut(&mut Ui, &mut BodyCx<'_>) + 'a) -> Self {
self.trailing = Some(Box::new(group));
self.props = self.props.end(EndRegion::new().gap(Gap::Stack));
self
@@ -215,7 +225,7 @@ impl Component for Bar<'_> {
impl Container for Bar<'_> {
/// The cancel button and the commit button, then the trailing group, on one row as the body's members are, so a
/// rule in the group is as tall as the row.
fn end(&mut self, ui: &mut Ui, _: &mut BodyCx<'_>) {
fn end(&mut self, ui: &mut Ui, cx: &mut BodyCx<'_>) {
let (commit_cancel, trailing) = (&self.commit_cancel, &mut self.trailing);
Row::new().show(ui, |ui, _| {
if let Some((commit, cancel)) = commit_cancel {
@@ -223,7 +233,7 @@ impl Container for Bar<'_> {
IconButton::command(IconId::new("check"), BarCommands::Commit).label(commit.clone()).show(ui);
}
if let Some(group) = trailing {
group(ui);
group(ui, cx);
}
});
}
+3 -3
View File
@@ -77,7 +77,7 @@ fn bar_captioned(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
let themes = [Entry::new(0u8, "ArtCraft"), Entry::new(1, "Paper")];
let modes = [Entry::new(0u8, "Light"), Entry::new(1, "Dark"), Entry::new(2, "System")];
Bar::new("Appearance", BarKind::Options)
.trailing(|ui| {
.trailing(|ui, _| {
Field::new("Workspace").sizing(Sizing::Content).show(ui, |ui, _| Segmented::new("Workspace", Bind::given(0u8), &modes).show(ui));
})
.show(ui, |ui, _| {
@@ -96,7 +96,7 @@ fn status_bar(ui: &mut Ui, data: &mut StoryData, message: StatusMessage, width:
let shown = crate::testing::memory::scoped(ui, |ui| {
ui.set_max_width(width.min(ui.available_width()));
Bar::new("Status", BarKind::Status)
.trailing(|ui| {
.trailing(|ui, _| {
Link::new("CPU Rendering").tone(Tone::Warn).show(ui);
})
.show(ui, |ui, _| {
@@ -133,7 +133,7 @@ fn status_progress(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
crate::testing::memory::scoped(ui, |ui| {
ui.set_max_width(STATUS_WIDTH.min(ui.available_width()));
Bar::new("Status", BarKind::Status)
.trailing(|ui| {
.trailing(|ui, _| {
StatusProgress::new("Blur", Some(0.45)).cancel("Cancel").show(ui);
})
.show(ui, |_, _| {})
+2 -2
View File
@@ -176,7 +176,7 @@ fn status_fixture(message: &'static str, cancelled: Rc<Cell<bool>>, seen: Rc<Cel
Fixture::new(move |ui| {
let (mut progress, mut link) = (Rect::NOTHING, Rect::NOTHING);
let shown = Bar::new("Status", BarKind::Status)
.trailing(|ui| {
.trailing(|ui, _| {
let output = StatusProgress::new("Blur", Some(0.45)).cancel("Cancel").show(ui);
if output.cancelled {
cancelled.set(true);
@@ -238,7 +238,7 @@ fn readout_texts(width: f32) -> Vec<(String, bool)> {
let canvas = egui::Rect::from_min_max(egui::Pos2::ZERO, egui::Pos2::new(800.0, 600.0));
let view = CanvasView::new(canvas, Point::new(1000.0, 750.0), 0.6667);
Bar::new("Status", BarKind::Status)
.trailing(|ui| {
.trailing(|ui, _| {
Link::new("CPU Rendering").tone(Tone::Warn).show(ui);
})
.show(ui, |ui, _| {