mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 17:55:45 +01:00
Merge pull request #3798 from dotCooCoo/fix/button-wrap-bounded-detail
Wrap Button detail text at its assigned width
This commit is contained in:
3 files changed
+169
-15
No files matched your search
@@ -7,11 +7,11 @@ use crate::contract::{
|
||||
};
|
||||
use crate::display::{ChordWrite, DisplayWords, Kbd, Picture, Spinner};
|
||||
use crate::list::ThumbAspect;
|
||||
use crate::primitives::{HAlign, IconSpec, PaintShape};
|
||||
use crate::primitives::{HAlign, IconSpec, PaintShape, Wrap};
|
||||
use crate::runtime::commands::{CommandRef, ResolvedCommand};
|
||||
use crate::runtime::env::Label;
|
||||
use crate::style::{ComponentStyle, DataColor, FocusLook, IconLook, StyleColor, StyleCx, SurfaceLook, TextLook, TypeRole};
|
||||
use craft_behavior::{Insets, Metrics, PartKey, PointerButton, PressPhase, Sense};
|
||||
use craft_behavior::{Insets, Metrics, PartKey, PointerButton, PressPhase, Proposal, Sense};
|
||||
use craft_host::target::Os;
|
||||
use craft_keymap::{Mods, Trigger};
|
||||
use craft_theme::{
|
||||
@@ -192,10 +192,14 @@ pub struct Button<'a> {
|
||||
/// that names an item at the reading start).
|
||||
#[prop]
|
||||
align: HAlign,
|
||||
/// A muted caption-role second line, wrapped under the icon and label. The face grows to fit it. It is the
|
||||
/// accessible description unless `description` is set.
|
||||
/// A muted caption under the icon and label of an inline button. It is the accessible description unless
|
||||
/// `description` is set.
|
||||
#[prop(into)]
|
||||
detail: Option<Label>,
|
||||
/// Whether the detail wraps in layouts that measure the button at its assigned width. By default the detail
|
||||
/// uses its natural width.
|
||||
#[prop]
|
||||
wrap_detail: bool,
|
||||
/// Whether a command button shows its command's chord after the label.
|
||||
#[prop]
|
||||
chord: ButtonChord,
|
||||
@@ -226,6 +230,7 @@ impl<'a> Button<'a> {
|
||||
least_width: None,
|
||||
align: HAlign::Center,
|
||||
detail: None,
|
||||
wrap_detail: false,
|
||||
chord: ButtonChord::Hidden,
|
||||
popup: None,
|
||||
expanded: None,
|
||||
@@ -264,6 +269,7 @@ impl<'a> Button<'a> {
|
||||
/// The face showing `label` as it is, with `chord` after it: in the label role, unless `style` names another (a
|
||||
/// side menu's row).
|
||||
fn face_of<'s>(&'s self, label: &'s str, style: &ButtonStyle, chord: Option<&'s str>) -> FaceSpec<'s> {
|
||||
let detail_wrap = if self.wrap_detail { Wrap::Wrap } else { Wrap::Line };
|
||||
FaceSpec {
|
||||
arrangement: self.arrangement,
|
||||
label,
|
||||
@@ -275,7 +281,7 @@ impl<'a> Button<'a> {
|
||||
mnemonic: None,
|
||||
align: self.align,
|
||||
chord,
|
||||
detail: self.detail.as_ref().map(Label::as_str),
|
||||
detail: self.detail.as_ref().map(|detail| (detail.as_str(), detail_wrap)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -362,11 +368,23 @@ impl Component for Button<'_> {
|
||||
|
||||
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
|
||||
let chord = self.chord_text(cx.command(), || (cx.env.shell().os, cx.words::<DisplayWords>()));
|
||||
let shown = face::measure(cx, &self.spec(cx.resolved.label, cx.style, chord.as_deref()));
|
||||
match &self.room_for {
|
||||
let spec = self.spec(cx.resolved.label, cx.style, chord.as_deref());
|
||||
let shown = face::measure(cx, &spec);
|
||||
let mut size = match &self.room_for {
|
||||
Some(room) => shown.max(face::measure(cx, &self.face_of(room.as_str(), cx.style, chord.as_deref()))),
|
||||
None => shown,
|
||||
};
|
||||
if cx.proposal == Proposal::Min
|
||||
&& self.arrangement == Arrangement::Inline
|
||||
&& let Some(detail) = spec.detail_run().filter(|run| run.wrap == Wrap::Wrap)
|
||||
{
|
||||
// The title reservation and style floor can widen the face before its caption wraps.
|
||||
let metrics = cx.style.metrics;
|
||||
let floor = metrics.least_width.unwrap_or(metrics.min_size.x) - metrics.padding.start - metrics.padding.end;
|
||||
let width = cx.room.filter(|room| room.is_finite()).unwrap_or_else(|| size.x.max(floor));
|
||||
size.y += cx.layout_at(&detail, width).size().y - cx.least(&detail).size().y;
|
||||
}
|
||||
size
|
||||
}
|
||||
|
||||
fn sense(&self) -> Sense {
|
||||
|
||||
@@ -4,8 +4,10 @@
|
||||
use super::{ActiveLook, ControlPlace, GroupLevel, RibbonLook};
|
||||
use crate::contract::{Component, Cx, MeasureCx, PaintUi, Styled as _};
|
||||
use crate::list::ThumbAspect;
|
||||
use crate::primitives::{HAlign, IconSpec, TextLayout, TextRun};
|
||||
use crate::style::{FocusLook, GlyphRole, IconLook, StyleColor, StyleCx, SurfaceKind, SurfaceLook, TextLook, TypeRole, UiTokens, VariantParts};
|
||||
use crate::primitives::{HAlign, IconSpec, TextLayout, TextRun, Wrap};
|
||||
use crate::style::{
|
||||
ComponentStyle as _, FocusLook, GlyphRole, IconLook, StyleColor, StyleCx, SurfaceKind, SurfaceLook, TextLook, TypeRole, UiTokens, VariantParts,
|
||||
};
|
||||
use craft_behavior::{Flow, Insets, Metrics, Proposal};
|
||||
use craft_theme::{
|
||||
Stateful, States, Variant,
|
||||
@@ -106,8 +108,8 @@ pub(crate) struct FaceSpec<'a> {
|
||||
pub align: HAlign,
|
||||
/// The command's chord, shown after the label of an inline face and dropped before the label elides.
|
||||
pub chord: Option<&'a str>,
|
||||
/// A caption-role second line under the icon and label of an inline face, wrapped to the face's width.
|
||||
pub detail: Option<&'a str>,
|
||||
/// A caption under the icon and label of an inline face, with its wrapping policy.
|
||||
pub detail: Option<(&'a str, Wrap)>,
|
||||
}
|
||||
|
||||
impl FaceSpec<'_> {
|
||||
@@ -154,8 +156,8 @@ impl FaceSpec<'_> {
|
||||
})
|
||||
}
|
||||
|
||||
fn detail_run(&self) -> Option<TextRun<'_>> {
|
||||
self.detail.filter(|detail| !detail.is_empty()).map(|detail| TextRun::new(detail).role(TypeRole::Caption))
|
||||
pub(super) fn detail_run(&self) -> Option<TextRun<'_>> {
|
||||
self.detail.filter(|(detail, _)| !detail.is_empty()).map(|(detail, wrap)| TextRun::new(detail).role(TypeRole::Caption).wrap(wrap))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -476,7 +478,21 @@ pub(crate) fn measure<C: Component>(cx: &MeasureCx<'_, C>, spec: &FaceSpec<'_>)
|
||||
let (icon, gap) = (measured_icon(cx), cx.gap());
|
||||
let trail = arrow_room(cx.env.get::<ControlPlace>(), cx.env.get::<RibbonLook>()).unwrap_or(icon);
|
||||
let fixed = spec.icons_width(icon, trail) + gap * spec.gaps() as f32;
|
||||
let room = cx.room.unwrap_or(f32::INFINITY);
|
||||
let detail = spec.detail_run();
|
||||
let wraps = detail.as_ref().is_some_and(|run| run.wrap == Wrap::Wrap);
|
||||
let room = cx.room.or_else(|| {
|
||||
if !wraps {
|
||||
return None;
|
||||
}
|
||||
let width = match cx.proposal {
|
||||
Proposal::Width(width) => width,
|
||||
Proposal::Size(size) => size.x,
|
||||
_ => return None,
|
||||
};
|
||||
let pad = cx.style.metrics().padding;
|
||||
width.is_finite().then(|| (width - pad.start - pad.end).max(0.0))
|
||||
});
|
||||
let room = room.unwrap_or(f32::INFINITY);
|
||||
let (text, chord) = if spec.label.is_empty() {
|
||||
(Vec2::ZERO, None)
|
||||
} else if cx.proposal == Proposal::Min {
|
||||
@@ -493,7 +509,7 @@ pub(crate) fn measure<C: Component>(cx: &MeasureCx<'_, C>, spec: &FaceSpec<'_>)
|
||||
}
|
||||
};
|
||||
let row = Vec2::new(text.x + fixed + chord.map_or(0.0, |width| width + gap), text.y.max(icon));
|
||||
let Some(detail) = spec.detail_run() else { return row };
|
||||
let Some(detail) = detail else { return row };
|
||||
let detail = if cx.proposal == Proposal::Min { cx.least(&detail).size() } else { cx.layout_at(&detail, room).size() };
|
||||
Vec2::new(row.x.max(detail.x), row.y + gap + detail.y)
|
||||
}
|
||||
|
||||
@@ -27,6 +27,126 @@ use egui::{Event, Id, Key, Modifiers, Pos2, Rect, Response, Ui, pos2, vec2};
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::rc::Rc;
|
||||
|
||||
pub(crate) mod detail_wrapping {
|
||||
use super::{Button, Cell, Fixture, Frames, IconId, Key, Modifiers, Rc, Rect, vec2};
|
||||
use craft_behavior::Proposal;
|
||||
use craft_ui::contract::{LeastWidth, Props as _, Role, Show as _, ShowContainer as _, Sizing};
|
||||
use craft_ui::layout_components::Stack;
|
||||
use craft_ui::testing::{Target, focus::AccessActionKind, trial};
|
||||
|
||||
const DETAIL: &str = "Learn the essential tools. Choose a skill to see the tutorials for that skill.";
|
||||
|
||||
fn wrapped_detail(fixture: &mut Fixture, title: &str) -> Target {
|
||||
let shapes = fixture.frame_shapes();
|
||||
let detail = shapes
|
||||
.iter()
|
||||
.find_map(|shape| match shape {
|
||||
egui::Shape::Text(text) if text.galley.text() == DETAIL => Some(text),
|
||||
_ => None,
|
||||
})
|
||||
.expect("the whole detail paints");
|
||||
let button = fixture.by_label(title).expect("the button is named");
|
||||
assert!(detail.galley.rows.len() > 1, "the detail wraps across lines");
|
||||
assert!(button.rect.expand(0.5).contains_rect(detail.visual_bounding_rect()), "the detail fits inside the button");
|
||||
button
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_button_detail_stays_on_one_line_without_the_opt_in() {
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
Button::new("Quick guide").detail(DETAIL).sizing(Sizing::FillWidth).show(ui);
|
||||
});
|
||||
fixture.room(vec2(172.0, 220.0)).settle();
|
||||
let rows = fixture.frame_shapes().iter().find_map(|shape| match shape {
|
||||
egui::Shape::Text(text) if text.galley.text() == DETAIL => Some(text.galley.rows.len()),
|
||||
_ => None,
|
||||
});
|
||||
assert_eq!(rows, Some(1), "ordinary button detail remains a single line");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_wrapped_button_detail_fits_its_face_and_keeps_one_activation_target() {
|
||||
const TITLE: &str = "Quick guide";
|
||||
let activations = Rc::new(Cell::new(0));
|
||||
let seen = Rc::clone(&activations);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let button = Button::new(TITLE).icon(IconId::new("copy")).detail(DETAIL).wrap_detail(true).sizing(Sizing::FillWidth);
|
||||
if button.show(ui).clicked() {
|
||||
seen.set(seen.get() + 1);
|
||||
}
|
||||
});
|
||||
fixture.room(vec2(172.0, 220.0)).settle();
|
||||
let button = wrapped_detail(&mut fixture, TITLE);
|
||||
assert_eq!(fixture.tree().description_of(button.node), Some(DETAIL));
|
||||
assert_eq!(fixture.by_role(Role::BUTTON).len(), 1);
|
||||
|
||||
fixture.click(&button).settle();
|
||||
assert_eq!(activations.get(), 1);
|
||||
fixture.access(AccessActionKind::Focus, &button);
|
||||
assert_eq!(fixture.tree().focused_key(), button.node);
|
||||
fixture.key(Key::Enter, Modifiers::NONE).settle();
|
||||
assert_eq!(activations.get(), 2, "clicking and pressing Enter each activate the button once");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_wrapped_button_detail_measures_at_an_explicit_width() {
|
||||
let width = 134.0;
|
||||
for proposal in [Proposal::Width(width), Proposal::Size(vec2(width, 100.0))] {
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut button = Button::new("Quick guide").detail(DETAIL).wrap_detail(true);
|
||||
let height = button.measure_in(ui, proposal).y;
|
||||
let room = Rect::from_min_size(ui.cursor().min, vec2(width, height));
|
||||
button.show_at(ui, room);
|
||||
});
|
||||
fixture.room(vec2(440.0, 220.0)).settle();
|
||||
wrapped_detail(&mut fixture, "Quick guide");
|
||||
}
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_wrapped_button_detail_keeps_explicit_unbounded_queries_unwrapped() {
|
||||
let sizes = |width| {
|
||||
let mut frames = Frames::new();
|
||||
let mut out = [egui::Vec2::ZERO; 4];
|
||||
frames.idle(1, |ui| {
|
||||
let room = Rect::from_min_size(ui.cursor().min, vec2(width, 380.0));
|
||||
Stack::new().fill().show_at(ui, room, |ui, _| {
|
||||
let mut button = Button::new("Quick guide").detail(DETAIL).wrap_detail(true);
|
||||
out = [Proposal::Ideal, Proposal::Width(f32::INFINITY), Proposal::Size(vec2(f32::INFINITY, 300.0)), Proposal::Width(width)]
|
||||
.map(|proposal| button.measure_in(ui, proposal));
|
||||
});
|
||||
});
|
||||
out
|
||||
};
|
||||
let narrow = sizes(134.0);
|
||||
let wide = sizes(580.0);
|
||||
assert_eq!(narrow[..3], wide[..3], "the unbounded queries are independent of their parent");
|
||||
assert!(narrow[3].y > narrow[0].y, "the explicit finite width wraps the caption");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_wrapped_button_detail_measures_its_minimum_height_at_its_style_width_floor() {
|
||||
let mut frames = Frames::new();
|
||||
frames.idle(1, |ui| {
|
||||
let mut button = Button::new("Quick guide").detail(DETAIL).wrap_detail(true).least_width(LeastWidth::SideMenu);
|
||||
let minimum = button.measure_in(ui, Proposal::Min);
|
||||
let at_width = button.measure_in(ui, Proposal::Width(minimum.x));
|
||||
assert!((minimum.y - at_width.y).abs() < 0.5, "the minimum height follows the returned width: {minimum:?}, {at_width:?}");
|
||||
});
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_wrapped_button_detail_measures_its_minimum_height_at_its_reserved_title_width() {
|
||||
let mut frames = Frames::new();
|
||||
frames.idle(1, |ui| {
|
||||
let mut button = Button::new("I").detail(DETAIL).wrap_detail(true).room_for("WWWWWWWWW");
|
||||
let minimum = button.measure_in(ui, Proposal::Min);
|
||||
let at_width = button.measure_in(ui, Proposal::Width(minimum.x));
|
||||
assert!((minimum.y - at_width.y).abs() < 0.5, "the caption uses the reserved width: {minimum:?}, {at_width:?}");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// The button kinds that take an arrangement.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum Kind {
|
||||
|
||||
Reference in new issue
Block a user