mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
ui-contract: an open anchored layer keeps the side it took only while its host asks for the same side; asked for another, it takes that side's placement
This commit is contained in:
@@ -247,6 +247,7 @@ pub(crate) fn host(ctx: &Context, layer: &Layer<'_>, body: &mut dyn FnMut(&mut U
|
||||
entry.parent = parent;
|
||||
entry.anchor = matches!(overlay.place, Place::Anchored { .. }).then_some(anchor);
|
||||
entry.side = placed.side;
|
||||
entry.asked = placed.asked;
|
||||
entry.exiting = layer.exiting;
|
||||
entry.cover = cover;
|
||||
entry.region = layer.region;
|
||||
@@ -325,6 +326,7 @@ fn host_in_popup(ctx: &Context, layer: &Layer<'_>, popup: PopupRoom, body: &mut
|
||||
entry.parent = parent;
|
||||
entry.anchor = matches!(overlay.place, Place::Anchored { .. }).then_some(anchor);
|
||||
entry.side = placed.side;
|
||||
entry.asked = placed.asked;
|
||||
entry.exiting = layer.exiting;
|
||||
entry.scoped = scope.is_some();
|
||||
entry.absorbs = overlay.dismiss.outside == Outside::Absorb;
|
||||
@@ -406,6 +408,7 @@ fn show_in_popup(
|
||||
overlay::end_layer(&ctx);
|
||||
let mut entry = OpenEntry::new(layer.id, overlay.rank, overlay.keys, shown.rect, shown.layer);
|
||||
entry.side = placed.side;
|
||||
entry.asked = placed.asked;
|
||||
entry.exiting = layer.exiting;
|
||||
entry.cover = cover;
|
||||
entry.scoped = scope.is_some();
|
||||
|
||||
@@ -256,6 +256,8 @@ pub(super) struct Placed {
|
||||
pub(super) at: Pos2,
|
||||
/// The side of the anchor an anchored layer took by its size; `None` while its size is unknown.
|
||||
pub(super) side: Option<Side>,
|
||||
/// The side the layer asked for when it took `side`.
|
||||
pub(super) asked: Option<Side>,
|
||||
/// The most the body may take from `at`: what the viewport leaves, within the fit's cap.
|
||||
pub(super) room: egui::Vec2,
|
||||
}
|
||||
@@ -263,7 +265,7 @@ pub(super) struct Placed {
|
||||
/// The pure placement of the anchored forms; the centre or the given position for floats, kept inside the safe rect
|
||||
/// within the overlay's bounds (`within_bounds`) by the keep rule moves follow. An anchored layer takes its side by its
|
||||
/// size: below its anchor, above when the room below is too short and the room above is larger, and keeps that side
|
||||
/// while it stays open and the content fits there or that side is the larger. A layer that `covers` its anchor
|
||||
/// while it stays open, asks for the same side, and the content fits there or that side is the larger. A layer that `covers` its anchor
|
||||
/// ([`layer_covers_anchor`]) and fits neither side keeps its height within the bounds and slides over the anchor.
|
||||
/// Before its size is known (a popup's first, hidden pass) it takes no side and its body is offered `content_room`, so
|
||||
/// the size it measures decides the side.
|
||||
@@ -275,11 +277,12 @@ pub(super) fn placement(ctx: &Context, layer: &Layer<'_>, covers: bool, size: Op
|
||||
let kept = layer_bounds(layer, bounds);
|
||||
let Some(size) = size else {
|
||||
let room = content_room(layer.overlay, covers, bounds, kept, anchor, rtl);
|
||||
return Placed { at: anchor.left_bottom().clamp(kept.min, kept.max), side: None, room };
|
||||
return Placed { at: anchor.left_bottom().clamp(kept.min, kept.max), side: None, asked: None, room };
|
||||
};
|
||||
// The side taken in an earlier pass is the preferred one while the layer stays open: it stays while the
|
||||
// content fits there or that side is the larger, so a size measured later (a body that grew) still flips it.
|
||||
let taken = overlay::entry(ctx, layer.id).and_then(|entry| entry.side).unwrap_or(*side);
|
||||
// The side taken in an earlier pass is the preferred one while the layer stays open and asks for the same side:
|
||||
// it stays while the content fits there or that side is the larger, so a size measured later (a body that
|
||||
// grew) still flips it. A host that asks for another side gets that side's placement.
|
||||
let taken = overlay::entry(ctx, layer.id).filter(|entry| entry.asked == Some(*side)).and_then(|entry| entry.side).unwrap_or(*side);
|
||||
let request = PlaceRequest::new(anchor, size, kept)
|
||||
.side(taken)
|
||||
.align(*align)
|
||||
@@ -293,14 +296,14 @@ pub(super) fn placement(ctx: &Context, layer: &Layer<'_>, covers: bool, size: Op
|
||||
// The room is what the side the layer took leaves for a body as large as the cap, or for a layer that covers
|
||||
// its anchor, what the bounds leave.
|
||||
let room = craft_behavior::overlay::place(&request.kept(Some(placed.side)).size(cap)).rect.size();
|
||||
Placed { at: placed.rect.min, side: Some(placed.side), room }
|
||||
Placed { at: placed.rect.min, side: Some(placed.side), asked: Some(*side), room }
|
||||
}
|
||||
Place::Float(float) => {
|
||||
let kept = within_bounds(layer.overlay, bounds);
|
||||
let size = size.unwrap_or(egui::Vec2::ZERO);
|
||||
let min = float.top_left(size).unwrap_or_else(|| kept.center() - size / 2.0);
|
||||
let at = crate::contract::body::keep_inside(Rect::from_min_size(min, size), kept).min;
|
||||
Placed { at, side: None, room: (kept.max - at).max(egui::Vec2::ZERO).min(cap) }
|
||||
Placed { at, side: None, asked: None, room: (kept.max - at).max(egui::Vec2::ZERO).min(cap) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,6 +157,8 @@ pub struct OpenEntry {
|
||||
pub rect: Rect,
|
||||
/// The side of the anchor it took by its size; `None` for a float and while its size is unknown.
|
||||
pub side: Option<Side>,
|
||||
/// The side its host asked for when it took `side`: a later pass keeps `side` only while the host asks for the same.
|
||||
pub(crate) asked: Option<Side>,
|
||||
/// It is fading out: not `top`, not counted by `any_open`, taking no input.
|
||||
pub exiting: bool,
|
||||
/// The layer's egui id and the cover under it, listed in the order.
|
||||
@@ -217,6 +219,7 @@ impl OpenEntry {
|
||||
peers: Vec::new(),
|
||||
rect,
|
||||
side: None,
|
||||
asked: None,
|
||||
exiting: false,
|
||||
layer,
|
||||
cover: None,
|
||||
|
||||
@@ -295,3 +295,29 @@ fn a_float_places_its_corner_at_the_position() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An anchored layer that stays open takes the side its host asks for now: asked for End after Above, it hangs at the
|
||||
/// anchor's end; asked for Above again, it sits above. Guards against a layer that kept the side an earlier pass took
|
||||
/// while it stayed open, whatever side the host asked for since.
|
||||
#[trial]
|
||||
fn an_open_layer_takes_the_side_its_host_asks_for_now() {
|
||||
let anchor = Rect::from_min_size(Pos2::new(200.0, 300.0), Vec2::new(40.0, 20.0));
|
||||
let (shown, asked, log, seen) = (Cell::new(Rect::NOTHING), Cell::new(Side::Above), RefCell::new(Vec::new()), RefCell::new(Seen::default()));
|
||||
let mut show = |ui: &mut Ui| {
|
||||
let overlay = Overlay::popover().place(Place::anchored(Anchor::Rect(anchor), asked.get(), Align::Start));
|
||||
let props = ContainerProps::new().mount(Mount::Layer(overlay)).sizing(Sizing::Content);
|
||||
let out = Pane { common: Common::new(), props, log: &log }.id_salt("bar").show(ui, |ui, _| {
|
||||
Probe::new(&seen).size(Vec2::new(60.0, 20.0)).show(ui);
|
||||
});
|
||||
shown.set(out.output.rect);
|
||||
};
|
||||
let mut frames = Frames::new();
|
||||
frames.idle(4, &mut show);
|
||||
assert!(shown.get().bottom() <= anchor.top() + SLACK, "asked for Above, the layer sits above: {:?}", shown.get());
|
||||
asked.set(Side::End);
|
||||
frames.idle(4, &mut show);
|
||||
assert!(shown.get().left() >= anchor.right() - SLACK, "asked for End, the layer hangs at the anchor's end: {:?}", shown.get());
|
||||
asked.set(Side::Above);
|
||||
frames.idle(4, &mut show);
|
||||
assert!(shown.get().bottom() <= anchor.top() + SLACK, "asked for Above again, the layer sits above: {:?}", shown.get());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user