dialogs-w12: net change of 4 commits

- dialogs-w12: written from its sheet
- dialogs-w12: repair 1
- dialogs-w12: review fixes (review-1)
- dialogs-w12: review fixes (review-1b)
This commit is contained in:
Martin Ball
2026-11-18 18:18:25 +00:00
parent f57d0a1a51
commit 3220cbf9d9
5 changed files with 128 additions and 50 deletions
@@ -485,12 +485,12 @@ impl Cells<'_> {
/// The reference point picker; a pick moves the reference point to that point of the box.
fn reference(&mut self, ui: &mut Ui, cx: &mut BodyCx<'_>, pass: &Pass, compact: bool) {
let anchor = pass.before.state.anchor_of_pivot().unwrap_or(Anchor9::Centre);
let mut picker = ReferencePoint::new(self.words.reference.clone(), Bind::given(Some(anchor))).enabled(self.enabled); // P3(dialogs-w12): dg.F30
let mut picker = ReferencePoint::new(self.words.reference.clone(), Bind::given(Some(anchor))).enabled(self.enabled);
if compact {
picker = picker.compact();
}
let shown = picker.show(ui);
let Some(picked) = shown.value().copied().flatten() else { return }; // P3(dialogs-w12): dg.F30
let Some(picked) = shown.value().copied().flatten() else { return };
let before = pass.before;
let state = BoxState::new(before.state.quad, before.state.point_at(picked));
let event = EditEvent::set(Mixed::Same(before), TransformRow { state, skew_v: before.skew_v }, self.name.clone(), None);
@@ -164,31 +164,48 @@ pub struct CanvasSize<'a> {
/// The bytes one pixel takes, for the file size readouts.
#[prop(quantity = "bytes per pixel")]
bytes_per_pixel: f64,
/// The held OK of a hosted dialog ([`Hold`]).
#[prop(name = "hold_ok", vis = "pub(crate)")]
hold: Hold<CanvasSizeValues>,
held: Option<&'a mut HeldAnswer<CanvasSizeValues>>,
}
impl<'a> CanvasSize<'a> {
/// The dialog over `values`.
pub fn new(values: Bind<'a, CanvasSizeValues>) -> Self {
CanvasSize { values, open: None, bytes_per_pixel: 4.0, hold: Hold::default() }
CanvasSize { values, open: None, bytes_per_pixel: 4.0, held: None }
}
/// Files OK's answer in `held` for the host instead of closing; the host answers it.
pub(crate) fn held(mut self, held: &'a mut HeldAnswer<CanvasSizeValues>) -> Self {
self.held = Some(held);
self
}
/// Shows the dialog.
pub fn show(self, ui: &mut Ui) -> CanvasSizeOutput {
let CanvasSize { values, open, bytes_per_pixel, hold } = self;
let CanvasSize { values, open, bytes_per_pixel, mut held } = self;
let kit = Kit::new(ui);
let words = kit.words();
let title = words.canvas_size_title.clone();
show_form(
let hold = match held.as_deref() {
Some(held) => Hold {
on: true,
finish: held.is_accepted(),
waiting: held.is_waiting(),
refusal: held.refusal().map(|(reason, at)| (reason.clone(), at.clone())),
},
None => Hold::default(),
};
let finish = hold.finish;
let mut current: Option<CanvasSizeValues> = None;
// PLAN: `show_form` still takes a `Hold` here; dialogs-w5 moves it onto `Option<&mut HeldAnswer<S>>`, and this call then passes `held` directly.
let shown = show_form(
ui,
title,
open,
values,
hold,
|ui, state| {
current = Some(state.clone());
let size = state.size.original();
let has_background = state.has_background;
state.extension = state.fill();
@@ -206,7 +223,6 @@ impl<'a> CanvasSize<'a> {
}
let growth = (f64::from(now.width) - f64::from(size.width), f64::from(now.height) - f64::from(size.height));
Field::new(words.anchor.clone()).id_salt("anchor").show(ui, |ui, _| {
// P3(dialogs-w12): dg.F30
let picked = ReferencePoint::new(words.anchor.clone(), Bind::given(Some(state.anchor))).growth(growth).show(ui);
if let Some(anchor) = picked.value().copied().flatten() {
state.anchor = anchor;
@@ -221,8 +237,24 @@ impl<'a> CanvasSize<'a> {
}
});
},
CanvasSizeValues::result,
)
CanvasSizeValues::clone,
);
let SizingOutput { response, closed, result, held: filed } = shown;
let mut result = result.map(|values| values.result());
if let Some(held) = held.as_deref_mut() {
if let Some(values) = filed {
held.file(values);
}
// The refusal ends once the values differ from the ones it refused.
if held.refusal().is_some_and(|(_, at)| Some(at) != current.as_ref()) {
held.clear_refusal();
}
// The host accepted the held answer: the pass that closes the dialog answers it.
if finish && result.is_none() && matches!(closed, Some(CloseReason::Activated)) {
result = held.finish().map(|values| values.result());
}
}
SizingOutput { response, closed, result, held: None }
}
}
@@ -260,9 +292,7 @@ pub struct HostedCanvasSize {
/// Whether OK files its answer for the host instead of closing ([`held_ok`](HostedCanvasSize::held_ok)).
hold: bool,
/// The OK the host holds back until it answers `answer_ok`.
held: HeldAnswer<CanvasSizeResult>,
/// The values the host refused the held OK for: the refusal shows until they change.
refused_at: Option<CanvasSizeValues>,
held: HeldAnswer<CanvasSizeValues>,
#[cfg(feature = "testing")]
facts: crate::testing::StoryOutput,
}
@@ -280,7 +310,6 @@ impl HostedCanvasSize {
build: Box::new(build),
hold: false,
held: HeldAnswer::new(),
refused_at: None,
#[cfg(feature = "testing")]
facts: crate::testing::StoryOutput::default(),
}
@@ -306,15 +335,12 @@ impl HostedCanvasSize {
/// The OK filed by a held dialog, once.
pub fn take_ok(&mut self) -> Option<CanvasSizeResult> {
self.held.take()
self.held.take().map(|values| values.result())
}
/// The host's answer to the held OK: `Ok` closes the dialog with the held answer in the next pass; `Err` enables
/// OK again and shows the label under the rows until a value changes. Without a held OK it does nothing.
pub fn answer_ok(&mut self, answer: Result<(), Label>) {
if answer.is_err() {
self.refused_at = Some(self.values.clone());
}
self.held.answer(answer);
}
}
@@ -326,17 +352,19 @@ impl StackDialog for HostedCanvasSize {
/// host, as its button does, and answers none; once the host accepted the held answer, it answers that.
fn confirm(&mut self, _ctx: &egui::Context) -> Option<Self::Result> {
if self.held.is_accepted() {
return self.held.finish();
return self.held.finish().map(|values| values.result());
}
if self.held.is_waiting() {
return None;
}
let result = size_valid(self.values.pixels()).then(|| self.values.result())?;
if self.hold {
self.held.file(result);
if !size_valid(self.values.pixels()) {
return None;
}
Some(result)
if self.hold {
self.held.file(self.values.clone());
return None;
}
Some(self.values.result())
}
fn held_state(&self) -> Option<HeldOk> {
@@ -384,29 +412,16 @@ impl StackDialog for HostedCanvasSize {
}
fn show(&mut self, ui: &mut Ui, _: &DialogStack) -> Option<DialogResult<CanvasSizeResult>> {
let finish = self.held.is_accepted();
let hold = Hold {
on: self.hold,
finish,
waiting: self.held.is_waiting(),
refusal: self.held.refusal().zip(self.refused_at.as_ref()).map(|((reason, _), at)| (reason.clone(), at.clone())),
let built = (self.build)(Bind::bound(&mut self.values));
let built = match self.hold.then_some(&mut self.held) {
Some(held) => built.held(held),
None => built,
};
let mut shown = (self.build)(Bind::bound(&mut self.values)).hold_ok(hold).opened(Bind::bound(&mut self.open)).show(ui);
let shown = built.opened(Bind::bound(&mut self.open)).show(ui);
#[cfg(feature = "testing")]
{
self.facts = crate::contract::Outcome::story_facts(&shown);
}
if let Some(result) = shown.held.take() {
self.held.file(result);
}
if self.refused_at.as_ref().is_some_and(|at| *at != self.values) {
self.refused_at = None;
self.held.clear_refusal();
}
// The host accepted the held answer: the pass that closes the dialog answers it.
if finish && shown.result.is_none() && matches!(shown.closed, Some(CloseReason::Activated)) {
return Some(self.held.finish().map_or(DialogResult::Cancel, DialogResult::Ok));
}
shown.hosted()
}
@@ -262,7 +262,6 @@ fn rows(ui: &mut Ui, words: &WatermarkWords, units: &Units, now: &mut WatermarkS
}
}
Field::new(words.position.clone()).id_salt("position").show(ui, |ui, _| {
// P3(dialogs-w12): dg.F30
let picked = ReferencePoint::new(words.position.clone(), Bind::given(Some(now.position))).show(ui);
if let Some(position) = picked.value().cloned().flatten() {
now.position = position;
@@ -36,6 +36,11 @@ const ARROW_HEAD: f32 = 3.5 / 12.0;
/// The head's half width, as a share of the arrow's extent.
const ARROW_HEAD_HALF_WIDTH: f32 = 3.5 / 12.0;
/// The length of an alignment cell's short bar, as a share of the long bar.
const BAR_SHORT: f32 = 0.6;
/// The distance between an alignment cell's bars, as a share of the long bar.
const BAR_GAP: f32 = 0.25;
/// The style of a reference point picker.
#[derive(Clone, Debug, PartialEq, ComponentStyle)]
#[non_exhaustive]
@@ -60,11 +65,17 @@ pub struct ReferencePointStyle {
arrow: f32,
/// The unpicked dots and the growth arrows.
arrow_ink: Stateful<StyleColor>,
/// The length of a text bar of an alignment cell.
#[allow(dead_code)] // P3(dialogs-w12): dg.F30
/// The length of a long text bar of an alignment cell.
bar: f32,
/// The length of an alignment cell's short text bar.
bar_short: f32,
/// The distance between an alignment cell's text bars.
bar_gap: f32,
/// The fill of the picked alignment cell.
picked_fill: Stateful<StyleColor>,
/// The corner radius of the picked alignment cell's fill.
fill_radius: egui::CornerRadius,
/// The ink of an alignment cell's text bars.
#[allow(dead_code)] // P3(dialogs-w12): dg.F30
bar_ink: Stateful<StyleColor>,
}
@@ -130,7 +141,7 @@ impl<'a> ReferencePoint<'a> {
}
fn position_of(value: Option<&Option<Anchor9>>) -> Option<usize> {
let value = value.and_then(Option::as_ref); // P3(dialogs-w12): dg.F30
let value = value.and_then(Option::as_ref);
value.and_then(|value| Anchor9::ALL.iter().position(|point| point == value))
}
@@ -186,6 +197,10 @@ impl Component for ReferencePoint<'_> {
arrow: cx.tokens.size.icon.sm,
arrow_ink: look(cx.color(colors.text.muted)),
bar: cx.tokens.size.icon.sm,
bar_short: cx.tokens.size.icon.sm * BAR_SHORT,
bar_gap: cx.tokens.size.icon.sm * BAR_GAP,
picked_fill: look(cx.color(colors.state.selected)),
fill_radius: UiTokens::corner(cx.tokens.shape.radius.control),
bar_ink: look(cx.color(colors.text.muted)),
}
}
@@ -219,8 +234,12 @@ impl Component for ReferencePoint<'_> {
fn update(&mut self, cx: &mut Cx<'_, Self>) {
let current = Self::position_of(cx.value());
let activated = (0..Anchor9::ALL.len()).find(|at| cx.part_activated(Self::part(*at)).is_some());
if let Some(at) = activated.filter(|at| current != Some(*at)) {
cx.set(Some(Anchor9::ALL[at])); // P3(dialogs-w12): dg.F30
if let Some(at) = activated {
if current != Some(at) {
cx.set(Some(Anchor9::ALL[at]));
} else if self.kind == ReferencePointKind::Alignment {
cx.set(None);
}
}
}
@@ -236,7 +255,40 @@ impl Component for ReferencePoint<'_> {
fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
let current = Self::position_of(cx.value());
debug_assert!(!(self.compact && self.growth.is_some()), "the compact picker shows no growth arrows");
debug_assert!(
self.kind == ReferencePointKind::Point || (!self.compact && self.growth.is_none()),
"only the point picker has growth arrows and a compact size"
);
let draw = ui.draw();
if self.kind == ReferencePointKind::Alignment {
let color = cx.resolve(&cx.style.bar_ink);
let gap = cx.style.bar_gap;
for (at, point) in Anchor9::ALL.iter().enumerate() {
let cell = Self::cell_rect(cx.rect, at);
if current == Some(at) {
draw.fill(&PaintShape::Rect(cell, cx.style.fill_radius), cx.resolve(&cx.style.picked_fill));
}
let inset = ((cell.width() - cx.style.bar) / 2.0).max(0.0);
let inner = cell.shrink(inset);
let (column, row) = point.cell();
// The middle bar's height; the other two sit one gap above and below it.
let middle = match row {
0 => inner.top() + gap,
1 => inner.center().y,
_ => inner.bottom() - gap,
};
for (line, length) in [cx.style.bar, cx.style.bar_short, cx.style.bar].into_iter().enumerate() {
let start = match column {
0 => inner.left(),
1 => inner.center().x - length / 2.0,
_ => inner.right() - length,
};
let y = middle + (line as f32 - 1.0) * gap;
draw.stroke(&PaintShape::Path(vec![Pos2::new(start, y), Pos2::new(start + length, y)]), StrokeWidth::Medium, color);
}
}
return;
}
for (at, point) in Anchor9::ALL.iter().enumerate() {
let cell = Self::cell_rect(cx.rect, at);
let (side, ink) = if current == Some(at) { (cx.style.picked_dot, &cx.style.ink) } else { (cx.style.dot, &cx.style.arrow_ink) };
@@ -1,6 +1,6 @@
//! Stories of `ReferencePoint`.
use super::ReferencePoint;
use super::{ReferencePoint, ReferencePointKind};
use crate::contract::{Bind, Show as _, Validity};
use crate::runtime::env::Message;
use crate::testing::{Drive, Fixture, StoryData, StoryOutput, story};
@@ -39,6 +39,18 @@ fn reference_point_growth(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
ReferencePoint::new("Reference point", Bind::bound(data.value("grown", Some(Anchor9::TopLeft)))).growth((200.0, -100.0)).show(ui).into()
}
/// The alignment of text in a cell: three bars in each cell, the pick's cell filled; a click on the pick clears it.
#[story(ReferencePoint<'static>, covers = [], drive = Drive::Custom(pick_top_left))]
fn reference_point_alignment(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
ReferencePoint::new("Alignment", Bind::bound(data.value("alignment", Some(Anchor9::Centre)))).kind(ReferencePointKind::Alignment).show(ui).into()
}
/// The alignment picker with no alignment picked: no cell is filled.
#[story(ReferencePoint<'static>, covers = [], drive = Drive::Never)]
fn reference_point_alignment_unset(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
ReferencePoint::new("Alignment", Bind::bound(data.value("alignment_unset", None::<Anchor9>))).kind(ReferencePointKind::Alignment).show(ui).into()
}
/// The options bar's size: the grid is an icon square and every point is a dot.
#[story(ReferencePoint<'static>, covers = [], drive = Drive::Never)]
fn reference_point_compact(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {