mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
Notice cards are as wide as their text up to the card width
This commit is contained in:
@@ -587,10 +587,12 @@ impl Component for Notice<'_> {
|
||||
let texts = self.texts(&cx.env, &cx.words::<NoticeWords>(), cx.format(), cx.resolved.label);
|
||||
let natural = self.plan(cx, &texts, f32::INFINITY).needs;
|
||||
let width = match (self.layout, cx.room) {
|
||||
// The card is the card width wide, its padding included, where the room allows.
|
||||
// The card is as wide as its text, up to the card width (padding included) where the room allows. A card
|
||||
// that hosts content (a progress bar) has no text width of its own and takes the card width.
|
||||
(NoticeLayout::Card, room) => {
|
||||
let padding = cx.style.metrics.padding;
|
||||
let width = cx.style.card_width - padding.start - padding.end;
|
||||
let cap = cx.style.card_width - padding.start - padding.end;
|
||||
let width = if self.content_sizes.is_empty() { natural.min(cap) } else { cap };
|
||||
room.map_or(width, |room| width.min(room))
|
||||
}
|
||||
(NoticeLayout::Centred, None) => natural.min(cx.style.reading_width),
|
||||
|
||||
@@ -167,3 +167,20 @@ fn a_wide_notice_wraps_its_message_beside_its_actions() {
|
||||
assert!(text.top() >= action.top(), "the message begins within the action line: {text:?}, {action:?}");
|
||||
assert!(text.top() < action.bottom() && action.top() < text.bottom(), "the actions share the message's first line");
|
||||
}
|
||||
|
||||
/// A card with a short message is as wide as its text, not the card width; a long one wraps at the card width.
|
||||
#[trial]
|
||||
fn a_card_is_as_wide_as_its_text_up_to_the_card_width() {
|
||||
let width = |message: String| {
|
||||
let seen = Rc::new(Cell::new(0.0_f32));
|
||||
let out = seen.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
out.set(Notice::new(Tone::Neutral, message.clone()).layout(NoticeLayout::Card).show(ui).response.rect.width());
|
||||
});
|
||||
fixture.settle();
|
||||
seen.get()
|
||||
};
|
||||
let (short, long) = (width("No recent files".to_owned()), width("Couldn't read the file. ".repeat(20)));
|
||||
assert!(short < long, "a short card is narrower than a long one: {short} vs {long}");
|
||||
assert!(long < 400.0, "a long card wraps at the card width: {long}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user