Overlay placement: a context menu that fits neither side covers its anchor (photocraft#15552)

PlaceRequest gains covers_anchor. Below or above the anchor, an overlay with it
that fits on neither side but fits the bounds keeps the preferred side and its
height and slides over the anchor; only one taller than the bounds is capped.
The overlay host sets it for a layer hanging from the pointer or from the rect
a context request named, so placement's room and the first pass's content room
are the cap within the bounds, and the menu shows every row without scroll
strips. Dropdowns, menu-bar menus and submenus keep their rule.
This commit is contained in:
aiongg
2026-11-21 07:48:59 +00:00
parent fb45d473df
commit 2126170565
5 changed files with 123 additions and 25 deletions
+35 -5
View File
@@ -97,6 +97,10 @@ pub struct PlaceRequest {
/// Beside the anchor, the overlay lines up with the anchor's other end when it runs past the bounds from the
/// aligned end and fits from the other, instead of sliding: a submenu of a low row opens upward.
pub flips_across: bool,
/// Below or above the anchor, an overlay that fits on neither side but fits the bounds keeps its height and slides
/// over the anchor from the preferred side; only one taller than the bounds is capped. For a context menu, whose
/// anchor is a point or a row.
pub covers_anchor: bool,
}
impl PlaceRequest {
@@ -113,6 +117,7 @@ impl PlaceRequest {
bounds,
kept: None,
flips_across: false,
covers_anchor: false,
}
}
@@ -163,6 +168,12 @@ impl PlaceRequest {
self.flips_across = on;
self
}
/// Below or above the anchor, slides over the anchor when the overlay fits neither side but fits the bounds.
pub const fn covers_anchor(mut self, on: bool) -> Self {
self.covers_anchor = on;
self
}
}
/// Where `place` put the overlay.
@@ -200,9 +211,10 @@ fn room(anchor: Rect, bounds: Rect, side: Side, rtl: bool, gap: f32) -> f32 {
}
/// Places an overlay: the preferred side unless the other has more room and the content does not fit, shifted to
/// stay inside the bounds, its height (or width, beside the anchor) capped to the room. Beside the anchor, a request that
/// flips across lines up with the anchor's other end when it runs past the bounds from the aligned end and fits from the
/// other. The rect is always finite and inside the bounds.
/// stay inside the bounds, its height (or width, beside the anchor) capped to the room. Below or above the anchor, a
/// request that covers its anchor and fits neither side keeps the preferred side and its height up to the bounds' and
/// slides over the anchor. Beside the anchor, a request that flips across lines up with the anchor's other end when it
/// runs past the bounds from the aligned end and fits from the other. The rect is always finite and inside the bounds.
pub fn place(request: &PlaceRequest) -> Placement {
let anchor = clean(request.anchor);
let bounds = clean(request.bounds);
@@ -213,12 +225,15 @@ pub fn place(request: &PlaceRequest) -> Placement {
let need = if request.side.vertical() { wanted.y } else { wanted.x };
let preferred = room(anchor, bounds, request.side, rtl, gap);
let other = room(anchor, bounds, request.side.flipped(), rtl, gap);
let covers = request.covers_anchor && request.side.vertical();
let side = match request.kept {
Some(kept) => kept,
None if preferred >= need || preferred >= other => request.side,
None if covers && other < need => request.side,
None => request.side.flipped(),
};
let available = room(anchor, bounds, side, rtl, gap).max(0.0);
// Covering the anchor, the overlay may take the bounds' height: the shift inside them moves it over the anchor.
let available = if covers { bounds.height() } else { room(anchor, bounds, side, rtl, gap).max(0.0) };
let size = if side.vertical() {
Vec2::new(wanted.x.min(bounds.width()), wanted.y.min(available).min(bounds.height()))
} else {
@@ -275,6 +290,20 @@ mod tests {
assert_eq!(neither.bottom(), BOUNDS.bottom(), "fits neither way: slides up to stay inside");
}
#[test]
fn a_menu_that_covers_its_anchor_and_fits_neither_side_keeps_its_height_and_slides_over_the_anchor() {
let point = rect(300.0, 400.0, 0.0, 0.0);
let request = PlaceRequest::new(point, Vec2::new(200.0, 450.0), BOUNDS).covers_anchor(true);
let placed = place(&request).rect;
assert_eq!(placed.height(), 450.0, "fits the bounds: its whole height");
assert!(BOUNDS.contains_rect(placed) && placed.top() < point.top(), "slides up over the point: {placed:?}");
assert_eq!(place(&request.size(Vec2::new(200.0, 700.0))).rect.height(), BOUNDS.height(), "taller than the bounds: capped");
let above = place(&request.size(Vec2::new(200.0, 350.0)));
assert!(above.side == Side::Above && above.rect.bottom() == point.top(), "fits above: opens above the point: {above:?}");
let without = place(&request.covers_anchor(false));
assert!(without.side == Side::Above && without.rect.height() < 450.0, "without it: capped to the larger side: {without:?}");
}
#[test]
fn an_overlay_at_least_as_wide_as_its_anchor_takes_that_width_below_it_but_not_beside_it() {
let field = rect(100.0, 100.0, 300.0, 24.0);
@@ -298,7 +327,8 @@ mod tests {
.side(side)
.align(align)
.rtl(i % 2 == 0)
.gap(if i == 1 { 4.0 } else { 0.0 });
.gap(if i == 1 { 4.0 } else { 0.0 })
.covers_anchor(align != Align::Start);
let placed = place(&request).rect;
assert!(placed.min.is_finite() && placed.max.is_finite(), "{request:?}");
assert!(BOUNDS.contains_rect(placed), "{request:?} gave {placed:?}");
@@ -3,7 +3,7 @@
use super::dismissal::{dismissal, popup_dismissal};
use super::opener::{Opening, Trigger};
use super::place::{OverlayFit, Place};
use super::placement::{anchor_rect, opened_overlay, placement};
use super::placement::{anchor_rect, layer_covers_anchor, opened_overlay, placement};
use super::props::{Outside, Overlay};
use crate::runtime::access::{self};
use crate::runtime::egui::areas::{self, LayerSpec};
@@ -134,6 +134,7 @@ pub(crate) fn host(ctx: &Context, layer: &Layer<'_>, body: &mut dyn FnMut(&mut U
if let Some((_, opening)) = &opened {
memory.opening.get_or_insert(*opening);
}
let covers = layer_covers_anchor(ctx, layer.overlay, memory.opening);
let layer = &Layer { overlay: opened.as_ref().map_or(layer.overlay, |(overlay, _)| overlay), ..*layer };
let overlay = layer.overlay;
let bounds = layer.region.map_or_else(|| areas::safe_rect(ctx), |region| region.intersect(areas::safe_rect(ctx)));
@@ -155,7 +156,7 @@ pub(crate) fn host(ctx: &Context, layer: &Layer<'_>, body: &mut dyn FnMut(&mut U
Some(size) => Some(egui::vec2(size.x.max(min_width), size.y)),
None => areas::layer_rect(ctx, area).map(|rect| rect.size()),
};
let mut placed = placement(ctx, layer, size, bounds, anchor);
let mut placed = placement(ctx, layer, covers, size, bounds, anchor);
let nudge = Nudge::of(ctx, layer, appears);
if let Some(nudge) = &nudge {
placed.at += nudge.offset();
@@ -275,6 +276,7 @@ fn host_in_popup(ctx: &Context, layer: &Layer<'_>, popup: PopupRoom, body: &mut
if let Some((_, opening)) = &opened {
memory.opening.get_or_insert(*opening);
}
let covers = layer_covers_anchor(ctx, layer.overlay, memory.opening);
// The popup window lies outside any region of the opener: the work area is its room.
let layer = &Layer { overlay: opened.as_ref().map_or(layer.overlay, |(overlay, _)| overlay), region: None, ..*layer };
let overlay = layer.overlay;
@@ -282,7 +284,7 @@ fn host_in_popup(ctx: &Context, layer: &Layer<'_>, popup: PopupRoom, body: &mut
let anchor = anchor_rect(ctx, layer, bounds, &mut memory);
let min_width = overlay.least_width(anchor.width());
let measured = layer.size.map(|size| egui::vec2(size.x.max(min_width), size.y));
let unplaced = placement(ctx, layer, measured, bounds, anchor);
let unplaced = placement(ctx, layer, covers, measured, bounds, anchor);
let parent = overlay::drawing_parent(ctx);
let tier = overlay::layer_tier(ctx, overlay.rank);
let scope = (overlay.focus != FocusPolicy::NONE).then_some(overlay.focus);
@@ -302,7 +304,7 @@ fn host_in_popup(ctx: &Context, layer: &Layer<'_>, popup: PopupRoom, body: &mut
shown = Some(show_in_popup(ui, layer, &unplaced, min_width, scope, &mut memory, &mut *body));
});
let (size, dismissed) = shown.unwrap_or((measured.unwrap_or_default(), None));
let placed = if known.is_some() { unplaced } else { placement(ctx, layer, Some(size), bounds, anchor) };
let placed = if known.is_some() { unplaced } else { placement(ctx, layer, covers, Some(size), bounds, anchor) };
let rect = Rect::from_min_size(placed.at, size);
if known.is_none() {
windows::place_popup(ctx, layer.id, rect.translate(popup.origin.to_vec2()));
@@ -178,6 +178,16 @@ impl crate::runtime::env::EnvValue for LayerFloor {
}
}
/// Whether a layer opened by `opener` may cover the anchor [`opener_anchor`] gives it: the pointer, or the rect a context
/// request named. Both are a context menu's anchor, a point or a row.
pub(super) fn covers_anchor(opener: &Opener, opening: Opening, place: &Place) -> bool {
match opener_anchor(opener, opening, place) {
Anchor::Pointer => true,
Anchor::Rect(_) => opener.anchor.is_none() && matches!(opening, Opening::At(_)),
_ => false,
}
}
/// The anchor a layer opened by `opener` hangs from: the opener's own, else the layer's `Pointer` or `Corner`
/// place, else the pointer for a context request a secondary release made, else the rect a key's context request
/// named, else the opener's rect.
@@ -1,7 +1,7 @@
//! Where a shown layer goes: its anchor, the region and screen it stays in, and the room its body has.
use super::host::{Layer, LayerMemory, memory_slot};
use super::opener::{Opening, opener_anchor};
use super::opener::{Opening, covers_anchor, opener_anchor};
use super::place::{Anchor, OverlayFit, Place, ViewportShare};
use super::props::Overlay;
use crate::runtime::egui::areas::{self};
@@ -51,6 +51,18 @@ pub(super) fn opened_overlay(ctx: &Context, overlay: &Overlay, stored: Option<Op
Some((resolved, opening))
}
/// Whether a layer of `overlay` may cover its anchor when it fits on neither side of it: it hangs from the pointer, or
/// from the rect a context request named (`covers_anchor`). `stored` is the opening the layer decided.
pub(super) fn layer_covers_anchor(ctx: &Context, overlay: &Overlay, stored: Option<Opening>) -> bool {
match &overlay.opener {
Some(first) => {
let (opener, opening) = first.active(ctx, stored);
covers_anchor(opener, opening, &overlay.place)
}
None => matches!(overlay.place, Place::Anchored { anchor: Anchor::Pointer, .. }),
}
}
/// The opening the layer `id` decided when it appeared, `None` before the host has run for it.
fn stored_opening(ctx: &Context, id: Id) -> Option<Opening> {
MemoryStage::peek::<LayerMemory>(ctx, memory_slot(id)).opening
@@ -172,7 +184,9 @@ pub(crate) fn measuring_room(
inset: f32,
rtl: bool,
) -> Option<egui::Vec2> {
let opened = opened_overlay(ctx, overlay, stored_opening(ctx, id));
let stored = stored_opening(ctx, id);
let covers = layer_covers_anchor(ctx, overlay, stored);
let opened = opened_overlay(ctx, overlay, stored);
let overlay = opened.as_ref().map_or(overlay, |(overlay, _)| overlay);
if !matches!(overlay.place, Place::Anchored { .. }) {
return None;
@@ -183,7 +197,7 @@ pub(crate) fn measuring_room(
let bounds = screen_for(ctx, env, overlay);
let resolved = resolve_anchor(ctx, id, overlay, own, own_to_screen);
let anchor = resolved_anchor(ctx, overlay, resolved, bounds, inset, &mut MemoryStage::peek::<LayerMemory>(ctx, memory_slot(id)).pointer_at);
Some(content_room(overlay, bounds, kept_in(overlay, resolved, bounds, inset), anchor, rtl))
Some(content_room(overlay, covers, bounds, kept_in(overlay, resolved, bounds, inset), anchor, rtl))
}
/// The most a layer's body may take: the fit's share of the safe rect `bounds`, at least as wide as the layer's
@@ -198,13 +212,13 @@ fn cap(overlay: &Overlay, bounds: Rect, anchor: Rect) -> egui::Vec2 {
}
/// The room a layer offers its content before it is placed: the cap (a share of the safe rect `bounds`), and for an
/// anchored layer the cap within the larger side of its anchor along the side's axis, inside `kept` (`kept_in`).
/// Content measured in it fits wherever `placement` puts it: on its preferred side when it fits there, else on the
/// larger side.
pub(crate) fn content_room(overlay: &Overlay, bounds: Rect, kept: Rect, anchor: Rect, rtl: bool) -> egui::Vec2 {
/// anchored layer the cap within the larger side of its anchor along the side's axis, inside `kept` (`kept_in`), or
/// within `kept` for a layer that `covers` its anchor. Content measured in it fits wherever `placement` puts it: on its
/// preferred side when it fits there, else on the larger side, or over the anchor.
pub(crate) fn content_room(overlay: &Overlay, covers: bool, bounds: Rect, kept: Rect, anchor: Rect, rtl: bool) -> egui::Vec2 {
let cap = cap(overlay, bounds, anchor);
let Place::Anchored { side, .. } = &overlay.place else { return cap.min(bounds.size()) };
let request = PlaceRequest::new(anchor, cap, kept).side(*side).rtl(rtl).min_width(overlay.min_width);
let request = PlaceRequest::new(anchor, cap, kept).side(*side).rtl(rtl).min_width(overlay.min_width).covers_anchor(covers);
let on = |side: Side| craft_behavior::overlay::place(&request.kept(Some(side))).rect.size();
let (vertical, other) = match side {
Side::Below => (true, Side::Above),
@@ -228,17 +242,18 @@ 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. 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.
pub(super) fn placement(ctx: &Context, layer: &Layer<'_>, size: Option<egui::Vec2>, bounds: Rect, anchor: Rect) -> Placed {
/// while it stays open 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.
pub(super) fn placement(ctx: &Context, layer: &Layer<'_>, covers: bool, size: Option<egui::Vec2>, bounds: Rect, anchor: Rect) -> Placed {
let rtl = layer.env.direction.is_rtl();
let cap = cap(layer.overlay, bounds, anchor);
match &layer.overlay.place {
Place::Anchored { side, align, flips_across, .. } => {
let kept = layer_bounds(layer, bounds);
let Some(size) = size else {
let room = content_room(layer.overlay, bounds, kept, anchor, rtl);
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 };
};
// The side taken in an earlier pass is the preferred one while the layer stays open: it stays while the
@@ -249,9 +264,11 @@ pub(super) fn placement(ctx: &Context, layer: &Layer<'_>, size: Option<egui::Vec
.align(*align)
.rtl(rtl)
.min_width(layer.overlay.min_width)
.flips_across(*flips_across);
.flips_across(*flips_across)
.covers_anchor(covers);
let placed = craft_behavior::overlay::place(&request);
// The room is what the side the layer took leaves for a body as large as the cap.
// 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 }
}
+40 -1
View File
@@ -1,6 +1,7 @@
//! A menu taller than its room: it stays inside the room at the width of its rows, scrolls by the strips above and
//! below its rows and by the wheel, and reopens at its first row; one that fits shows no strips. A tall submenu of a
//! low row opens upward.
//! low row opens upward. A context menu that fits on neither side of the pointer but fits the window covers the
//! pointer instead of scrolling.
use craft_host::target::Os;
use craft_ui::buttons::Button;
@@ -108,6 +109,44 @@ fn a_menu_that_fits_shows_no_strips_and_hugs_its_rows() {
assert!(((first.top() - rect.top()) - (rect.bottom() - last.bottom())).abs() < 0.5, "as tall as its rows: {rect:?}, rows {first:?} to {last:?}");
}
/// A context menu of `rows` rows on a target `top` down the fixture's 800 x 600 window, right-clicked at the target's
/// centre: the fixture, the menu's id and the pointer.
fn context_menu(top: f32, rows: usize) -> (Fixture, Id, egui::Pos2) {
let menu = Rc::new(Cell::new(None));
let seen = Rc::clone(&menu);
let mut fixture = Fixture::new(move |ui| {
ui.add_space(top);
let target = Button::new("Target").show(ui);
let shown = Menu::context("Target menu", &target).show(ui, |ui, _| {
for at in 0..rows {
MenuItem::new(row(at)).show(ui);
}
});
seen.set(Some(shown.output.response.id));
});
fixture.settle();
let at = fixture.by_label("Target").expect("the target").center();
fixture.frame(vec![pointer::moved(at)]);
fixture.frame(vec![pointer::secondary(at, true)]);
fixture.frame(vec![pointer::secondary(at, false)]);
fixture.settle();
(fixture, menu.get().expect("the menu's id"), at)
}
#[trial]
fn a_context_menu_too_tall_for_either_side_of_the_pointer_covers_it_and_shows_every_row_without_strips() {
let rows = 18;
let (fixture, menu, at) = context_menu(290.0, rows);
let room = fixture.ctx().content_rect();
let rect = menu_rect(&fixture);
assert!(strips(&fixture, menu).is_none(), "the menu {rect:?} fits the window {room:?}: no strips");
let (first, last) = (fixture.by_label(&row(0)).expect("the first row").rect, fixture.by_label(&row(rows - 1)).expect("the last row").rect);
let tall = last.bottom() - first.top();
assert!(tall > at.y - room.top() && tall > room.bottom() - at.y, "the rows ({tall}) fit on neither side of the pointer at {at:?}");
assert!(room.contains_rect(rect), "the menu {rect:?} stays inside the window {room:?}");
assert!(rect.contains_rect(first) && rect.contains_rect(last), "every row shows: {first:?} to {last:?} in {rect:?}");
}
/// A menu of `above` rows over a submenu row whose submenu has `rows` rows, in the fixture's 800 x 600 window.
fn low_submenu(above: usize, rows: usize) -> Fixture {
let mut fixture = Fixture::new(move |ui| {