mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:24:24 +00:00
A fill body in a shrinking row counts at its content; a thumbnail slider narrows
A body that fills the rest of a bounded row reported only the room its siblings left, so the row did not see that its content needed more and cut it at the view. It now counts at its content width, and the units narrow to leave it that. ThumbnailSlider measures its track at the proposal it is given, and a slider's track narrows to a short field's length at its least.
This commit is contained in:
@@ -233,9 +233,10 @@ impl<'b> BodyHooks<'b> {
|
||||
(_, ShowAt::Flow) => {
|
||||
let room = crate::layout::flow_room(ui);
|
||||
// A row that shrinks its children to fit gives a body among them a width: it measures and shows
|
||||
// in that room.
|
||||
// in that room. A body that fills a bounded row takes the rest of it instead.
|
||||
let fills = self.sizing(props) != Sizing::Content && room.x.is_finite();
|
||||
let cap = match Env::of(ui).shrink {
|
||||
Some(crate::layout::Shrink::Apply(row)) => crate::layout::shrink_cap(ui.ctx(), row, id),
|
||||
Some(crate::layout::Shrink::Apply(row)) if !fills => crate::layout::shrink_cap(ui.ctx(), row, id),
|
||||
_ => None,
|
||||
};
|
||||
let room = Vec2::new(cap.map_or(room.x, |cap| room.x.min(cap)), props.room_height.unwrap_or(room.y));
|
||||
|
||||
@@ -442,6 +442,8 @@ pub(crate) trait Erased {
|
||||
fn in_flow(&self) -> bool;
|
||||
/// Whether the shape fills the width of the room it is shown in (a body not sized from its content).
|
||||
fn fills_width(&self) -> bool;
|
||||
/// How much wider than the size it measured to the shape's content wants to be (`ShapeHooks::overflow`).
|
||||
fn overflow(&self) -> f32;
|
||||
fn sized(&self, ui: &Ui, id: Id, size: Vec2) -> Vec2;
|
||||
fn arena(&self) -> bool;
|
||||
fn extent(&mut self, ui: &Ui, at: ShowAt, id: Id, scx: &StyleCx<'_>) -> Vec2;
|
||||
@@ -1121,8 +1123,8 @@ pub(super) fn run(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, mut tip: Option<T
|
||||
// room (`mount_room`). A body that fills the width of a bounded room takes the room or its share of it (`sized`),
|
||||
// so it is no unit.
|
||||
let in_flow = matches!(at, ShowAt::Flow) && t.fills().is_none();
|
||||
let unit_body = || t.in_flow() && !(t.fills_width() && crate::layout::flow_room(ui).x.is_finite());
|
||||
let shrink = env.shrink.filter(|_| in_flow && (shape == ShapeKind::Leaf || (shape == ShapeKind::Body && unit_body())));
|
||||
let fill_body = shape == ShapeKind::Body && in_flow && t.in_flow() && t.fills_width() && crate::layout::flow_room(ui).x.is_finite();
|
||||
let shrink = env.shrink.filter(|_| in_flow && (shape == ShapeKind::Leaf || (shape == ShapeKind::Body && t.in_flow() && !fill_body)));
|
||||
let cap = match shrink {
|
||||
Some(crate::layout::Shrink::Apply(row)) if shape == ShapeKind::Leaf => crate::layout::shrink_cap(&ctx, row, id),
|
||||
_ => None,
|
||||
@@ -1219,6 +1221,12 @@ pub(super) fn run(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, mut tip: Option<T
|
||||
Some(crate::layout::Shrink::Least(row)) if shape == ShapeKind::Body => crate::layout::report_least(&ctx, row, id, size.x),
|
||||
_ => {}
|
||||
}
|
||||
// A body that fills the rest of a shrinking row needs at least its content: the row counts it at that width, so
|
||||
// the units narrow to leave it, and it takes what they leave.
|
||||
if let Some(crate::layout::Shrink::Count(row)) = env.shrink.filter(|_| fill_body) {
|
||||
let content = measured.x + t.overflow();
|
||||
crate::layout::report_shrink(&ctx, row, id, size.x.min(content), content, Some(content));
|
||||
}
|
||||
// A component shown at a rect fills it: the rect is its size, and a leaf that measures nothing (a content
|
||||
// painter) still shows there.
|
||||
let inert = match at {
|
||||
|
||||
@@ -171,6 +171,10 @@ impl<C: Component> Erased for Typed<'_, '_, C> {
|
||||
self.hooks.fills_width(self.comp.get_ref())
|
||||
}
|
||||
|
||||
fn overflow(&self) -> f32 {
|
||||
self.hooks.overflow()
|
||||
}
|
||||
|
||||
fn sized(&self, ui: &Ui, id: Id, size: Vec2) -> Vec2 {
|
||||
self.hooks.sized(self.comp.get_ref(), ui, id, size)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
//! A footer row: a row of view modes, a thumbnail slider and a footer bar narrows its slider to leave the bar its
|
||||
//! actions.
|
||||
|
||||
use craft_behavior::IconId;
|
||||
use craft_ui::choice::Segmented;
|
||||
use craft_ui::contract::{Bind, Entry, RowAlign, Show as _, ShowContainer as _};
|
||||
use craft_ui::layout_components::Row;
|
||||
use craft_ui::sections::{FooterAction, FooterBar};
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use craft_ui::tile_grid::{ThumbnailSlider, TileZoom};
|
||||
use egui::{Rect, Vec2};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// The slider's rect and the rects of the footer bar's actions, in a footer row `width` wide.
|
||||
fn footer(width: f32) -> (Rect, Vec<Rect>) {
|
||||
let seen = Rc::new(RefCell::new((Rect::NOTHING, Vec::new())));
|
||||
let out = Rc::clone(&seen);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let modes = [Entry::new(0u8, "List").icon(IconId::new("list")), Entry::new(1, "Icons").icon(IconId::new("grid"))];
|
||||
let mut zoom = 1.0;
|
||||
Row::new().fill_width().align(RowAlign::End).show(ui, |ui, _| {
|
||||
Segmented::new("View", Bind::given(0u8), &modes).show(ui);
|
||||
let slider = ThumbnailSlider::new("Size", Bind::bound(&mut zoom), TileZoom::Ladder).show(ui).response.rect;
|
||||
let actions = ["new_folder", "trash", "more"].map(|icon| FooterAction::new(IconId::new(icon), icon));
|
||||
let bar = FooterBar::actions(actions).show_footer(ui);
|
||||
*out.borrow_mut() = (slider, bar.responses.iter().map(|response| response.rect).collect());
|
||||
});
|
||||
});
|
||||
fixture.room(Vec2::new(width, 60.0)).settle();
|
||||
seen.borrow().clone()
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_footer_row_narrows_its_slider_to_leave_the_footer_bar_its_actions() {
|
||||
let (wide, _) = footer(900.0);
|
||||
let (slider, actions) = footer(300.0);
|
||||
let last = actions.last().copied().unwrap_or(Rect::NOTHING);
|
||||
assert!(last.max.x <= 300.0 + 0.5, "the last action ends at {} past a 300-point row", last.max.x);
|
||||
assert!(slider.width() < wide.width() - 0.5, "the slider kept its width {} in a narrow row", slider.width());
|
||||
assert!(slider.max.x <= actions[0].min.x + 0.5, "the slider {slider:?} runs into the actions {actions:?}");
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
//! Reorderable sections.
|
||||
|
||||
mod footer_row;
|
||||
mod layout;
|
||||
mod reorderable;
|
||||
|
||||
@@ -7,7 +7,7 @@ use crate::contract::{
|
||||
};
|
||||
use crate::runtime::env::Label;
|
||||
use crate::style::{ComponentStyle, StyleCx};
|
||||
use craft_behavior::{IconId, Metrics, Mixed, NavShape, PartKey, Proposal, Roving, Sense};
|
||||
use craft_behavior::{IconId, Metrics, Mixed, NavShape, PartKey, Roving, Sense};
|
||||
use egui::{Rect, Vec2};
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
@@ -162,7 +162,7 @@ impl Component for ThumbnailSlider<'_> {
|
||||
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
|
||||
let button = cx.face(IconButton::new(IconId::new(SMALLER_ICON)));
|
||||
let shown = cx.value().copied().map_or(Mixed::Mixed, Mixed::Same);
|
||||
let track = cx.child("slider", &mut self.track(shown, cx.label()), Proposal::Ideal);
|
||||
let track = cx.child("slider", &mut self.track(shown, cx.label()), cx.proposal);
|
||||
let width = 2.0 * (button.x + cx.gap()) + track.x;
|
||||
Vec2::new(cx.room.map_or(width, |room| width.min(room)), button.y.max(track.y))
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ use crate::primitives::{PaintShape, StrokeWidth};
|
||||
use crate::runtime::env::Label;
|
||||
use crate::runtime::focus::{ActivateKeys, KeepKeys, KeyUse};
|
||||
use crate::style::{ComponentStyle, FocusLook, StyleColor, StyleCx, TextLook, TrackLook};
|
||||
use craft_behavior::{Flow, HitLevel, HitPriority, Metrics, PartKey, PressPhase, Sense};
|
||||
use craft_behavior::{Flow, HitLevel, HitPriority, Metrics, PartKey, PressPhase, Proposal, Sense};
|
||||
use craft_theme::Stateful;
|
||||
use egui::{Pos2, Rect, Vec2};
|
||||
use std::ops::RangeInclusive;
|
||||
@@ -166,7 +166,8 @@ impl Component for Slider<'_> {
|
||||
let accent = &colors.accent;
|
||||
let length = cx.tokens.size.length.track;
|
||||
SliderStyle {
|
||||
metrics: Metrics::new().min_size(Vec2::new(length, control.height)),
|
||||
// Where a row shrinks it, the track narrows to a short field's length.
|
||||
metrics: Metrics::new().min_size(Vec2::new(length, control.height)).least_width(cx.tokens.size.length.short),
|
||||
focus: cx.focus_look(),
|
||||
mark_text: track::mark_text(cx),
|
||||
track: cx.track(),
|
||||
@@ -180,7 +181,8 @@ impl Component for Slider<'_> {
|
||||
|
||||
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
|
||||
self.marks.lay_out(&cx.style.mark_text, cx.style.space, |label| cx.env.resolve(label), |run| cx.layout(run).size());
|
||||
Vec2::new(cx.style.length, cx.icon() + self.marks.below(&cx.style.track))
|
||||
let along = if cx.proposal == Proposal::Min { 0.0 } else { cx.style.length };
|
||||
Vec2::new(along, cx.icon() + self.marks.below(&cx.style.track))
|
||||
}
|
||||
|
||||
fn sense(&self) -> Sense {
|
||||
|
||||
Reference in New Issue
Block a user