mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 01:44:25 +00:00
Side labels can show a short caption in place of the name: LabelKind::Short keeps the text label's column and colon while the label's text names the control; WarpFields shows H and V beside the distortion fields
This commit is contained in:
@@ -3,12 +3,12 @@
|
||||
|
||||
use craft_behavior::{EditEvent, IconId};
|
||||
use craft_ui::buttons::ToggleButton;
|
||||
use craft_ui::contract::{Bind, Entry, FieldLayout, Props as _, Show as _, ShowContainer as _, Sizing};
|
||||
use craft_ui::contract::{Bind, Entry, FieldLayout, LabelKind, Props as _, Show as _, ShowContainer as _, SideLabel, Sizing};
|
||||
use craft_ui::display::Divider;
|
||||
use craft_ui::form::Field;
|
||||
use craft_ui::layout_components::Row;
|
||||
use craft_ui::pickers::Select;
|
||||
use craft_ui::runtime::env::{Env, UnitKind};
|
||||
use craft_ui::runtime::env::{Env, Label, UnitKind};
|
||||
use craft_ui::text::NumberField;
|
||||
use egui::Ui;
|
||||
|
||||
@@ -32,6 +32,10 @@ craft_ui::words! {
|
||||
bend: "warpFields.bend" = "Bend";
|
||||
horizontal: "warpFields.horizontal" = "Horizontal distortion";
|
||||
vertical: "warpFields.vertical" = "Vertical distortion";
|
||||
/// The caption shown beside the Horizontal distortion field.
|
||||
horizontal_caption: "warpFields.horizontalCaption" = "H";
|
||||
/// The caption shown beside the Vertical distortion field.
|
||||
vertical_caption: "warpFields.verticalCaption" = "V";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -295,13 +299,17 @@ impl<'a> WarpFields<'a> {
|
||||
edits.push(WarpFieldsEdit::orientation(!values.vertical));
|
||||
}
|
||||
type FieldEdit = fn(EditEvent<f64>) -> WarpFieldsEdit;
|
||||
let fields: [(_, f64, FieldEdit); 3] = [
|
||||
(&words.bend, values.bend, WarpFieldsEdit::Bend),
|
||||
(&words.horizontal, values.horizontal, WarpFieldsEdit::Horizontal),
|
||||
(&words.vertical, values.vertical_distortion, WarpFieldsEdit::Vertical),
|
||||
let fields: [(&Label, Option<&Label>, f64, FieldEdit); 3] = [
|
||||
(&words.bend, None, values.bend, WarpFieldsEdit::Bend),
|
||||
(&words.horizontal, Some(&words.horizontal_caption), values.horizontal, WarpFieldsEdit::Horizontal),
|
||||
(&words.vertical, Some(&words.vertical_caption), values.vertical_distortion, WarpFieldsEdit::Vertical),
|
||||
];
|
||||
for (name, value, edit) in fields {
|
||||
Field::new(name.clone()).layout(caption).sizing(Sizing::Content).show(ui, |ui, _| {
|
||||
for (name, short, value, edit) in fields {
|
||||
let mut field = Field::new(name.clone()).layout(caption).sizing(Sizing::Content);
|
||||
if let Some(short) = short {
|
||||
field = field.side(SideLabel::new().kind(LabelKind::Short(short.clone())));
|
||||
}
|
||||
field.show(ui, |ui, _| {
|
||||
let field = NumberField::new(name.clone(), Bind::given(Some(value)))
|
||||
.range(DISTORTION_RANGE)
|
||||
.unit_kind(UnitKind::Percent)
|
||||
|
||||
@@ -348,11 +348,11 @@ impl Component for LabelLeaf {
|
||||
}
|
||||
}
|
||||
|
||||
/// The colon a beside text label draws after its name, unless the name ends in one. The label's node and the
|
||||
/// The colon a beside text label draws after its shown text, unless that text ends in one. The label's node and the
|
||||
/// control keep the name as given.
|
||||
pub(super) fn colon(env: &Env, input: &LabelIn<'_>) -> Option<String> {
|
||||
let text = env.view().resolve(input.text);
|
||||
let beside_text = input.side.layout == FieldLayout::Beside && input.side.kind == LabelKind::Text;
|
||||
let text = env.view().resolve(input.side.kind.shown(input.text));
|
||||
let beside_text = input.side.layout == FieldLayout::Beside && matches!(input.side.kind, LabelKind::Text | LabelKind::Short(_));
|
||||
if !beside_text || text.as_str().is_empty() || text.as_str().ends_with([':', '\u{ff1a}']) {
|
||||
return None;
|
||||
}
|
||||
|
||||
@@ -156,7 +156,7 @@ pub(crate) fn control_after(ui: &mut Ui, env: &Env, input: &LabelIn<'_>) -> Plac
|
||||
}
|
||||
let reset = scope::last_reset(&ctx, input.scope);
|
||||
let slot = if reset.is_some() && input.side.reset_dot { env.tokens.size.handle.sm } else { 0.0 };
|
||||
let mut leaf = LabelLeaf::new(input.text.clone(), slot);
|
||||
let mut leaf = LabelLeaf::new(input.side.kind.shown(input.text).clone(), slot);
|
||||
leaf.resettable = reset.is_some();
|
||||
leaf.colon = colon(env, input);
|
||||
match &input.side.kind {
|
||||
|
||||
@@ -20,6 +20,19 @@ pub enum LabelKind {
|
||||
/// An icon or a short text shown in place of the label; the text still names the control. Glyph labels share
|
||||
/// a column of their own, apart from the text labels'. The label's detail joins the description and is not shown.
|
||||
Glyph(GlyphLabel),
|
||||
/// A shorter text shown in place of the label ("H" for "Horizontal distortion"), as a text label in every other
|
||||
/// way: its column, colon, icon and detail. The label's text still names the control.
|
||||
Short(Label),
|
||||
}
|
||||
|
||||
impl LabelKind {
|
||||
/// The text a visible text label shows: the short text of `Short`, else `text`.
|
||||
pub(super) fn shown<'a>(&'a self, text: &'a Label) -> &'a Label {
|
||||
match self {
|
||||
LabelKind::Short(short) => short,
|
||||
_ => text,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What a glyph label shows.
|
||||
|
||||
@@ -149,7 +149,7 @@ pub(crate) fn run(ui: &mut Ui, env: &Env, input: &LabelIn<'_>, trailing: Option<
|
||||
return Placed { label: Rect::NOTHING, control: hidden_control(&ctx, input, flow, gap), trailing: Rect::NOTHING };
|
||||
}
|
||||
let slot = if reset.is_some() && input.side.reset_dot { slot_side } else { 0.0 };
|
||||
let mut leaf = LabelLeaf::new(input.text.clone(), slot);
|
||||
let mut leaf = LabelLeaf::new(input.side.kind.shown(input.text).clone(), slot);
|
||||
leaf.scrub = input.enabled && (input.side.scrub || scope::last_scrub(&ctx, input.scope));
|
||||
leaf.resettable = reset.is_some();
|
||||
leaf.differs = reset.is_some_and(|fact| fact.differs);
|
||||
|
||||
@@ -5,6 +5,7 @@ pub mod group;
|
||||
mod hit;
|
||||
mod inside;
|
||||
pub mod kit;
|
||||
mod short;
|
||||
mod tooltip;
|
||||
|
||||
use self::kit::{Dial, Field, Seen, Wide};
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
//! A short caption: the field shows the short text with its colon, and the long label names the control.
|
||||
|
||||
use craft_ui::contract::{Bind, LabelKind, Show as _, ShowContainer as _, SideLabel};
|
||||
use craft_ui::form::Field;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use craft_ui::text::NumberField;
|
||||
|
||||
const NAME: &str = "Horizontal distortion";
|
||||
|
||||
#[trial]
|
||||
fn a_short_caption_shows_in_place_of_the_label_and_the_label_names_the_control() {
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
Field::new(NAME).side(SideLabel::new().kind(LabelKind::Short("H".into()))).show(ui, |ui, _| {
|
||||
NumberField::new(NAME, Bind::given(Some(10.0))).show(ui);
|
||||
});
|
||||
});
|
||||
fixture.settle();
|
||||
let texts = fixture.texts();
|
||||
let caption = texts.iter().find(|run| run.text == "H").expect("the short caption is shown");
|
||||
assert!(
|
||||
texts.iter().any(|run| run.text.trim() == ":" && run.rect.min.x >= caption.rect.min.x),
|
||||
"the short caption takes the colon of a beside text label"
|
||||
);
|
||||
assert!(texts.iter().all(|run| run.text != NAME), "the long label is not shown");
|
||||
let control = fixture.by_label(NAME).expect("the long label names the control");
|
||||
assert!(control.rect.min.x >= caption.rect.max.x, "the control follows its caption");
|
||||
}
|
||||
Reference in New Issue
Block a user