mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 12:03:42 +01:00
A memoized view in a row sits on the row's line
A memoized view's content starts a layout room of its own (Env::start_room), so the view sat at the top of the unwrapped Row around it while every other one-line child sits with its centre on the row's line. Since craft-menu's bar became a memoized view, PhotoCraft's menu titles sat about 4 pt above the icon beside them (photocraft#16113). The view's room now moves down by the whole pixels that put the centre of its extent (this pass's measuring run, else its last run) on the row's line. The room keeps its size, so a kept run replays moved with it; a view taller than the line reaches stays at the row's top, as place_in_cell keeps a child of more than one line.
This commit is contained in:
1 parent
fb8aae1df6
commit
02be56cfbb
3 files changed
+60
-1
No files matched your search
@@ -77,6 +77,7 @@ impl Lazy {
|
||||
None => sub(ui.scope_id(), ("lazy", self.key)),
|
||||
};
|
||||
let available = flow_region(ui);
|
||||
let available = if matches!(key, Key::Memo(_)) { on_row_line(ui, id, available) } else { available };
|
||||
let rtl = ui.layout().prefer_right_to_left();
|
||||
let sizing = ui.is_sizing_pass();
|
||||
// The child is not part of its body's transcript: in a real run it counts there by its extent, whether it
|
||||
@@ -275,6 +276,24 @@ fn place_view(ui: &mut Ui, rect: Rect) -> Rect {
|
||||
rect
|
||||
}
|
||||
|
||||
/// The room of the memoized view `id` in the flow region `room` of `ui`. Its content starts a layout room of its own
|
||||
/// (`Env::start_room`), so it would sit at the top of the unwrapped `Row` around it; as a child placed in flow
|
||||
/// (`place_in_cell`), a view of one line sits with its centre on the row's line instead: its room moves down by the
|
||||
/// whole pixels that put the centre of the extent it took (in this pass's measuring run, else its last run) there. A
|
||||
/// view taller than that reaches, and a view no run showed yet, stays at the row's top. The room keeps its size, so a
|
||||
/// kept run replays moved with it.
|
||||
fn on_row_line(ui: &Ui, id: egui::Id, room: Rect) -> Rect {
|
||||
let Some(centre) = Env::of(ui).row_line.and_then(|line| line.centre) else {
|
||||
return room;
|
||||
};
|
||||
let Some(size) = lazy::last_extent(ui.ctx(), id) else {
|
||||
return room;
|
||||
};
|
||||
let pixels_per_point = ui.ctx().pixels_per_point();
|
||||
let above = ((centre - size.y / 2.0) * pixels_per_point).round() / pixels_per_point;
|
||||
if above.is_finite() && above > 0.0 { room.translate(Vec2::new(0.0, above)) } else { room }
|
||||
}
|
||||
|
||||
/// What a child came to in a pass.
|
||||
pub(super) enum Shown<R> {
|
||||
/// It ran and returned this.
|
||||
|
||||
@@ -433,6 +433,12 @@ pub(crate) fn shown(ctx: &Context, owner: Option<Id>, id: Id, sizing: bool, meas
|
||||
});
|
||||
}
|
||||
|
||||
/// The extent the child `id` took in the last measuring or real run that showed it (this pass's measuring run once it
|
||||
/// ran), at whatever room it had; `None` before any run showed it.
|
||||
pub(crate) fn last_extent(ctx: &Context, id: Id) -> Option<Vec2> {
|
||||
with(ctx, |store, _, _| store.entries.get(&id).and_then(|entry| entry.sizes[1].map(|measured| measured.size)))
|
||||
}
|
||||
|
||||
/// Registers what the skipped child `id` registered with the focus service in the last real run that showed it, in
|
||||
/// the real run that skips it.
|
||||
pub(crate) fn replay(ctx: &Context, id: Id) {
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
|
||||
use craft_behavior::{ColumnWidth, Proposal, Sense};
|
||||
use craft_ui::contract::{
|
||||
BodyCx, Common, Component, Container, ContainerProps, Cx, EndRegion, MeasureCx, Props as _, Role, Show as _, ShowContainer as _, Sizing,
|
||||
BodyCx, Common, Component, Container, ContainerProps, Cx, EndRegion, MeasureCx, MemoProps, MemoView, Props as _, Role, Show as _,
|
||||
ShowContainer as _, Sizing,
|
||||
};
|
||||
use craft_ui::layout::{BodyLayout, RowAlign};
|
||||
use craft_ui::runtime::env::{Direction, with_env};
|
||||
@@ -322,6 +323,39 @@ fn the_end_region_shares_the_members_line() {
|
||||
assert!((end.center().y - member.center().y).abs() < SLACK, "the end region's probe {end:?} is off the members' line {member:?}");
|
||||
}
|
||||
|
||||
/// A memoized view of one short block.
|
||||
#[derive(MemoProps)]
|
||||
struct Short {}
|
||||
|
||||
fn short(_: &mut Short, ui: &mut Ui) -> Rect {
|
||||
Block::new(Vec2::new(40.0, 10.0)).show(ui).rect
|
||||
}
|
||||
|
||||
/// A memoized view in a row sits on the row's line, as the one-line child beside it does, also while it replays. Its
|
||||
/// content starts a layout room of its own, which left it at the row's top (a menu bar beside an app icon,
|
||||
/// storytold/photocraft#16113).
|
||||
#[trial]
|
||||
fn a_memoized_view_sits_on_the_rows_line() {
|
||||
let seen = Rc::new(Cell::new((Rect::NOTHING, Rect::NOTHING)));
|
||||
let shown = Rc::clone(&seen);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
Panel::new().layout(BodyLayout::row()).id_salt("row").show(ui, |ui, _| {
|
||||
let tall = Block::new(Vec2::new(20.0, 30.0)).show(ui).rect;
|
||||
let view = MemoView::new("short", Short {}, short).show(ui);
|
||||
if !is_measuring(ui.ctx()) {
|
||||
shown.set((tall, view.unwrap_or(Rect::NOTHING)));
|
||||
}
|
||||
});
|
||||
});
|
||||
fixture.room(Vec2::splat(400.0)).settle();
|
||||
for frame in 0..3 {
|
||||
let (tall, view) = seen.get();
|
||||
assert!(view.height() > 0.0, "frame {frame}: the view showed: {view:?}");
|
||||
assert!((view.center().y - tall.center().y).abs() < SLACK, "frame {frame}: the view {view:?} is off the line of {tall:?}");
|
||||
fixture.settle();
|
||||
}
|
||||
}
|
||||
|
||||
/// A Row in right-to-left holding a vertically scrolling container (content-sized, then filling its height) beside a
|
||||
/// probe: the container, the probe inside it and the probe beside it are finite, and the container lies in the row.
|
||||
#[trial]
|
||||
|
||||
Reference in new issue
Block a user