mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 10:48:15 +01:00
Form dialogs: RadioLayout::Column heads a document dialog's radio options one per line; full-width text rows span the form and a multi-line field opens at its start, nothing selected
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4fe5bbd953
commit
94cfd1df00
6 files changed
+121
-9
No files matched your search
@@ -610,8 +610,8 @@ fn prepare(core: &mut CxCore<'_>, ui: &mut Ui, decl: &TextPart, state: &mut Text
|
||||
/// plain click selects the whole text once it is released (`TextState::select_on_click`).
|
||||
fn arrive(ctx: &egui::Context, widget: egui::Id, decl: &TextPart, state: &mut TextState, by_pointer: bool, select_all: bool) {
|
||||
let len = decl.shown.chars().count();
|
||||
// A dialog that opens on this field selects its whole text whatever the codec's caret, so typing replaces it
|
||||
// (photocraft#15540).
|
||||
// A dialog that opens on this single-line field selects its whole text whatever the codec's caret, so typing
|
||||
// replaces it (photocraft#15540).
|
||||
let opened = !decl.options.keep_caret_on_open
|
||||
&& crate::runtime::focus::focused_rec(ctx)
|
||||
.and_then(|rec| crate::runtime::focus::origin(ctx, &rec))
|
||||
@@ -620,6 +620,11 @@ fn arrive(ctx: &egui::Context, widget: egui::Id, decl: &TextPart, state: &mut Te
|
||||
state.select_on_click = true;
|
||||
return;
|
||||
}
|
||||
// A multi-line field opens on its first line with nothing selected, so the text reads from its start.
|
||||
if opened && !select_all && decl.options.multiline() {
|
||||
adapter::select(ctx, widget, 0, 0);
|
||||
return;
|
||||
}
|
||||
match if opened || select_all { Caret::All } else { decl.caret } {
|
||||
Caret::All => adapter::select(ctx, widget, 0, len),
|
||||
// A bound caret is where the app put it: focus arriving keeps it.
|
||||
|
||||
@@ -12,7 +12,7 @@ use super::host_requests::HostRequests;
|
||||
use super::param::{Param, ParamKind, edited_rows, open_fields, set_color_field};
|
||||
use super::preview::{PagerStep, PaneOverlay, PanePager, PreviewPane, PreviewState, PreviewView, PreviewZoom};
|
||||
|
||||
use super::rows::{ParamRows, ParamRowsOutput};
|
||||
use super::rows::{ParamRows, ParamRowsOutput, RadioLayout};
|
||||
|
||||
use crate::contract::{
|
||||
Bind, BodyCx, CanvasGesture, CollectionEvent, NoValue, Outcome, Remember as _, Sampler as _, SaveKey, Show as _, ShowCollection as _,
|
||||
@@ -382,6 +382,10 @@ pub struct ParamDialog<'a> {
|
||||
/// wide as its text, and a group row's caption heads it ([`CaptionColumn`]).
|
||||
#[prop]
|
||||
caption_column: bool,
|
||||
/// In a document dialog ([`ParamLayout::Document`]), how the radio rows arrange their caption and options:
|
||||
/// [`RadioLayout::Line`] by default.
|
||||
#[prop]
|
||||
radios: RadioLayout,
|
||||
/// Whether the title row has a close action.
|
||||
#[prop]
|
||||
closable: bool,
|
||||
@@ -518,6 +522,7 @@ impl<'a> ParamDialog<'a> {
|
||||
reset_to: None,
|
||||
layout: None,
|
||||
caption_column: false,
|
||||
radios: RadioLayout::Line,
|
||||
closable: true,
|
||||
close_unchanged: false,
|
||||
previews: false,
|
||||
@@ -644,6 +649,7 @@ impl<'a> ParamDialog<'a> {
|
||||
reset_to,
|
||||
layout,
|
||||
caption_column,
|
||||
radios,
|
||||
closable,
|
||||
close_unchanged,
|
||||
previews,
|
||||
@@ -775,6 +781,7 @@ impl<'a> ParamDialog<'a> {
|
||||
// controls filling the rest of the row.
|
||||
labels: if layout == ParamLayout::Document && !tabs.is_empty() { Some(LabelScheme::Inspector) } else { layout.labels() },
|
||||
caption_column: caption_column && layout == ParamLayout::Document,
|
||||
radios,
|
||||
canvas,
|
||||
read_only,
|
||||
};
|
||||
@@ -1254,6 +1261,8 @@ struct DialogRows<'p> {
|
||||
labels: Option<LabelScheme>,
|
||||
/// The rows' captions share one column ([`ParamDialog::caption_column`]).
|
||||
caption_column: bool,
|
||||
/// How radio rows arrange their options ([`ParamDialog::radios`]).
|
||||
radios: RadioLayout,
|
||||
canvas: Option<egui::Id>,
|
||||
/// The dialog shows values only.
|
||||
read_only: bool,
|
||||
@@ -1265,6 +1274,7 @@ impl DialogRows<'_> {
|
||||
let mut rows = ParamRows::new(label.clone(), params, Bind::bound(values))
|
||||
.preview_colors(self.preview_colors)
|
||||
.sliders(self.sliders)
|
||||
.radios(self.radios)
|
||||
.read_only(self.read_only || read_only);
|
||||
if let Some(source) = self.patterns {
|
||||
rows = rows.patterns(source);
|
||||
|
||||
@@ -8,7 +8,7 @@ pub(crate) mod rows;
|
||||
|
||||
pub use fields::{DialogFields, FieldValue};
|
||||
pub use param::{CustomContext, CustomRow, Enable, NumberUnit, Param, ParamKind, encode, open_fields, set_color_field};
|
||||
pub use rows::{ParamRows, ParamRowsOutput};
|
||||
pub use rows::{ParamRows, ParamRowsOutput, RadioLayout};
|
||||
pub use words::ParamWords;
|
||||
|
||||
pub(crate) mod dialog;
|
||||
|
||||
@@ -181,6 +181,20 @@ pub struct ParamRows<'a> {
|
||||
sliders: SliderFieldLayout,
|
||||
labels: Option<LabelScheme>,
|
||||
heading: Option<Label>,
|
||||
radios: RadioLayout,
|
||||
}
|
||||
|
||||
/// How a radio row of a document dialog (`LabelScheme::Inline`) arranges its caption and options. Rows with
|
||||
/// [`options_in_row`](Param::options_in_row) or [`stacked_options`](Param::stacked_options) keep their own arrangement,
|
||||
/// and other label schemes always put the caption over a column.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub enum RadioLayout {
|
||||
/// The caption and its options share one line, wrapping when the room is short, so a document dialog stays short.
|
||||
#[default]
|
||||
Line,
|
||||
/// The caption heads the options, one per line (DeckCraft's Set Up Show and Paste Special).
|
||||
Column,
|
||||
}
|
||||
|
||||
impl<'a> ParamRows<'a> {
|
||||
@@ -198,9 +212,16 @@ impl<'a> ParamRows<'a> {
|
||||
sliders: SliderFieldLayout::Inline,
|
||||
labels: None,
|
||||
heading: None,
|
||||
radios: RadioLayout::Line,
|
||||
}
|
||||
}
|
||||
|
||||
/// How a document dialog's radio rows arrange their caption and options ([`RadioLayout`]).
|
||||
pub fn radios(mut self, layout: RadioLayout) -> Self {
|
||||
self.radios = layout;
|
||||
self
|
||||
}
|
||||
|
||||
/// The heading the rows stand under, when the host shows one. In a `LabelScheme::Inline` form a radio row of the
|
||||
/// same name leaves its caption out, as production's Pages to Print options follow their heading.
|
||||
pub fn heading(mut self, heading: impl Into<Label>) -> Self {
|
||||
@@ -258,7 +279,7 @@ impl<'a> ParamRows<'a> {
|
||||
|
||||
/// Shows the rows across the width offered.
|
||||
pub fn show(self, ui: &mut Ui) -> ParamRowsOutput {
|
||||
let ParamRows { label, params, mut fields, mut seed, read_only, canvas, patterns, preview_colors, sliders, labels, heading } = self;
|
||||
let ParamRows { label, params, mut fields, mut seed, read_only, canvas, patterns, preview_colors, sliders, labels, heading, radios } = self;
|
||||
let env = Env::of(ui);
|
||||
let words = env.words::<ParamWords>();
|
||||
let units = env.units();
|
||||
@@ -266,6 +287,7 @@ impl<'a> ParamRows<'a> {
|
||||
let mut pass = Pass::new(&words, &units, patterns, canvas);
|
||||
pass.previews = preview_colors;
|
||||
pass.sliders = sliders;
|
||||
pass.radios = radios;
|
||||
pass.shaded = labels == Some(LabelScheme::Inline);
|
||||
pass.heading = heading.map(|heading| plain(&heading).as_str().to_owned());
|
||||
let mut changed = false;
|
||||
@@ -397,6 +419,8 @@ struct Pass<'p> {
|
||||
previews: &'p [(String, Srgba)],
|
||||
/// How range rows arrange their slider and number field (`ParamRows::sliders`).
|
||||
sliders: SliderFieldLayout,
|
||||
/// How a document dialog's radio rows arrange their options (`ParamRows::radios`).
|
||||
radios: RadioLayout,
|
||||
/// The pass's edits of the whole values, lifted from the rows' controls.
|
||||
events: EditEvents<DialogFields>,
|
||||
/// A number or length field holds text that does not parse.
|
||||
@@ -567,6 +591,7 @@ impl<'p> Pass<'p> {
|
||||
canvas,
|
||||
previews: &[],
|
||||
sliders: SliderFieldLayout::Inline,
|
||||
radios: RadioLayout::Line,
|
||||
events: EditEvents::new(),
|
||||
unparsed: false,
|
||||
help_link: None,
|
||||
@@ -1239,7 +1264,7 @@ impl<'p> Pass<'p> {
|
||||
/// [`options_in_row`](Param::options_in_row) has no title and stands its options side by side. In a form whose
|
||||
/// rows share a label column, the options start in the control column ([`Line::show_uncaptioned`]).
|
||||
fn radio_options(&mut self, ui: &mut Ui, line: &Line<'_>, options: &[(String, Label)], details: &[Label]) {
|
||||
let inline = Env::of(ui).view().get::<LabelScheme>() == LabelScheme::Inline;
|
||||
let inline = Env::of(ui).view().get::<LabelScheme>() == LabelScheme::Inline && self.radios == RadioLayout::Line;
|
||||
if line.param.option().options_in_row() {
|
||||
line.show_uncaptioned(ui, |ui| self.radio_group(ui, line, options, details, true));
|
||||
return;
|
||||
@@ -1510,7 +1535,8 @@ impl<'p> Pass<'p> {
|
||||
fn text(&mut self, ui: &mut Ui, line: &Line<'_>, default: &str) {
|
||||
let value = self.work.text(line.key).unwrap_or(default).to_owned();
|
||||
let browse = line.param.browse.clone();
|
||||
line.show(ui, |ui| {
|
||||
let across = line.param.option().full_width();
|
||||
line.show_body(ui, |ui| {
|
||||
let typed = match &browse {
|
||||
Some(kind) => {
|
||||
let mut row = PathRow::new(line.name.clone(), Bind::given(value.clone()));
|
||||
@@ -1544,6 +1570,9 @@ impl<'p> Pass<'p> {
|
||||
if let Some(hint) = line.param.hint_text() {
|
||||
field = field.placeholder(hint.clone());
|
||||
}
|
||||
if across {
|
||||
field = field.sizing(Sizing::FillWidth);
|
||||
}
|
||||
field.show(ui).value().cloned()
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! Stories of `ParamRows`.
|
||||
|
||||
use super::{DialogFields, ParamRows};
|
||||
use super::{DialogFields, ParamRows, RadioLayout};
|
||||
use crate::contract::LabelScheme;
|
||||
use crate::runtime::requests::PathKind;
|
||||
|
||||
use crate::contract::Show as _;
|
||||
@@ -206,6 +207,25 @@ fn param_rows_with_radio_details(ui: &mut Ui, data: &mut StoryData) -> StoryOutp
|
||||
rows_story(ui, data, ¶ms, |_| {})
|
||||
}
|
||||
|
||||
/// A document form's radio rows in each `RadioLayout`: the caption beside its options on one line, then the caption
|
||||
/// over its options, one per line.
|
||||
#[story(ParamRows<'static>, covers = [], drive = Drive::Never)]
|
||||
fn param_rows_document_radio_layouts(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let kind = || {
|
||||
ParamKind::radio(
|
||||
vec![("theme".to_owned(), "Use Destination Theme".into()), ("source".to_owned(), "Keep Source Formatting".into())],
|
||||
Some("theme".to_owned()),
|
||||
)
|
||||
};
|
||||
let (line, column) = ([Param::new("line", "Paste as", kind())], [Param::new("column", "As", kind())]);
|
||||
let _ = ParamRows::new("Line", &line, data.bind("line", DialogFields::new())).labels(LabelScheme::Inline).show(ui);
|
||||
ParamRows::new("Column", &column, data.bind("column", DialogFields::new()))
|
||||
.labels(LabelScheme::Inline)
|
||||
.radios(RadioLayout::Column)
|
||||
.show(ui)
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Rows disabled by their values: Black Ink shows checked while Paper is on, Gap takes input only with dashes, and
|
||||
/// Constrain only while exactly one of Width and Height is on.
|
||||
#[story(ParamRows<'static>, covers = [], drive = Drive::Never)]
|
||||
|
||||
@@ -6,7 +6,8 @@ use craft_ui::contract::{Bind, LabelScheme, Role};
|
||||
use craft_ui::dialog::DialogHost;
|
||||
use craft_ui::patterns::blend_keys::{BlendKeyEntry, BlendShortcut};
|
||||
use craft_ui::patterns::form_dialogs::{
|
||||
DialogFields, FieldValue, HostedParamDialog, NumberUnit, Param, ParamDialog, ParamKind, ParamRows, open_fields, set_color_field, set_param_field,
|
||||
DialogFields, FieldValue, HostedParamDialog, NumberUnit, Param, ParamDialog, ParamKind, ParamRows, RadioLayout, open_fields, set_color_field,
|
||||
set_param_field,
|
||||
};
|
||||
use craft_ui::patterns::presets::{PresetFns, PresetRow, PresetSource};
|
||||
use craft_ui::runtime::env::{Label, Scale, UnitKind};
|
||||
@@ -984,6 +985,28 @@ fn typing_in_a_freshly_opened_parameter_dialog_replaces_its_first_field() {
|
||||
assert_eq!(values.borrow().number("radius"), Some(7.0), "typing replaced the radius");
|
||||
}
|
||||
|
||||
/// DeckCraft's comment box: a dialog that opens on a multi-line field leaves its text unselected with the caret at the
|
||||
/// start, so typing adds before the text instead of replacing it; a full-width multi-line row's field spans the form,
|
||||
/// wider than a captioned row's field.
|
||||
#[trial]
|
||||
fn a_dialog_opening_on_a_multi_line_field_starts_at_its_first_line_across_the_form() {
|
||||
let values = Rc::new(RefCell::new(DialogFields::new()));
|
||||
let shown = values.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let params = vec![
|
||||
Param::new("text", "Comment", ParamKind::text("first line\nsecond line")).multiline(4).full_width(),
|
||||
Param::new("name", "A long caption for a name", ParamKind::text("")),
|
||||
];
|
||||
let _ = ParamDialog::new("comment", "New Comment", params).values(Bind::bound(&mut *shown.borrow_mut())).show(ui);
|
||||
});
|
||||
fixture.settle();
|
||||
let width = |label: &str| fixture.by_label(label).unwrap_or_else(|| panic!("the {label} field")).rect.width();
|
||||
let (text, name) = (width("Comment"), width("A long caption for a name"));
|
||||
assert!(text > name, "the full-width multi-line field spans the form: {text} against {name}");
|
||||
fixture.type_text("New ").settle();
|
||||
assert_eq!(values.borrow().text("text"), Some("New first line\nsecond line"), "typing went in at the start, the text kept");
|
||||
}
|
||||
|
||||
/// A fixture showing `params` as rows over `values`, logging the edit events of every pass as `(phase, gesture)`. While
|
||||
/// `twice` is on, every frame runs a second pass.
|
||||
fn logged_rows(params: Vec<Param>, values: &Rc<RefCell<DialogFields>>, twice: bool) -> (Fixture, Rc<RefCell<Vec<EditEvent<DialogFields>>>>) {
|
||||
@@ -1539,6 +1562,31 @@ fn inline_texts(params: Vec<Param>) -> Vec<(String, egui::Rect)> {
|
||||
fixture.texts().iter().map(|text| (text.text.clone(), text.rect)).collect()
|
||||
}
|
||||
|
||||
/// DeckCraft's Paste Special: under `RadioLayout::Column` a document form's radio row puts its caption over its
|
||||
/// options, one per line, where the default line arrangement sets the caption and options on one line.
|
||||
#[trial]
|
||||
fn a_document_forms_radio_column_heads_its_options_one_per_line() {
|
||||
let texts = |layout: RadioLayout| {
|
||||
let ways = vec![("theme".to_owned(), Label::from("Use Destination Theme")), ("text".to_owned(), Label::from("Keep Text Only"))];
|
||||
let params = vec![Param::new("as", "As", ParamKind::radio(ways, Some("theme".to_owned())))];
|
||||
let values = Rc::new(RefCell::new(DialogFields::new()));
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let _ = ParamRows::new("Form", ¶ms, Bind::bound(&mut *values.borrow_mut())).labels(LabelScheme::Inline).radios(layout).show(ui);
|
||||
});
|
||||
fixture.settle();
|
||||
let texts: Vec<(String, egui::Rect)> = fixture.texts().iter().map(|text| (text.text.clone(), text.rect)).collect();
|
||||
let at =
|
||||
|name: &str| texts.iter().find(|(text, _)| text.trim() == name).map(|(_, rect)| *rect).unwrap_or_else(|| panic!("{name}: {texts:?}"));
|
||||
(at("As"), at("Use Destination Theme"), at("Keep Text Only"))
|
||||
};
|
||||
let (caption, first, second) = texts(RadioLayout::Line);
|
||||
assert!((caption.center().y - first.center().y).abs() < 3.0, "the line arrangement sets the caption beside the options");
|
||||
assert!(second.left() > first.right(), "the line arrangement sets the options side by side");
|
||||
let (caption, first, second) = texts(RadioLayout::Column);
|
||||
assert!(caption.bottom() <= first.top() + 1.0, "the caption heads the options");
|
||||
assert!(second.top() >= first.bottom(), "the options stand one per line");
|
||||
}
|
||||
|
||||
/// The rect of the first text that contains `name`.
|
||||
fn text_at(texts: &[(String, egui::Rect)], name: &str) -> Option<egui::Rect> {
|
||||
texts.iter().find(|(text, _)| text.contains(name)).map(|(_, rect)| *rect)
|
||||
|
||||
Reference in new issue
Block a user