From 5fecd6dbd6df24347aa0996ebb5be8f629acb58c Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sat, 21 Nov 2026 21:26:28 +0700 Subject: [PATCH] canvas-w8: net change of 1 commits - canvas-w8: written from its sheet --- crates/behavior/src/overlay.rs | 29 ++- .../ui-canvas/src/canvas_controls/task_bar.rs | 217 ++++++++++++------ .../src/canvas_controls/task_bar/stories.rs | 69 +++++- .../tests/it/canvas_controls/task_bar.rs | 143 ++++++++++-- .../ui-contract/src/contract/overlay/place.rs | 16 +- .../src/contract/overlay/placement.rs | 6 +- .../ui-contract/src/contract/overlay/props.rs | 10 +- crates/ui-widgets/src/bars/bar.rs | 8 +- crates/ui-widgets/src/bars/stories.rs | 45 +++- crates/ui-widgets/tests/it/bars/bar.rs | 84 +++++++ 10 files changed, 516 insertions(+), 111 deletions(-) diff --git a/crates/behavior/src/overlay.rs b/crates/behavior/src/overlay.rs index 621de8fdd..3866e836a 100644 --- a/crates/behavior/src/overlay.rs +++ b/crates/behavior/src/overlay.rs @@ -220,7 +220,8 @@ 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. +/// other. The rect is then shifted by the request's `offset` and clamped again, unless the shifted rect would cover +/// `clear`, in which case the unshifted one stays. 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); @@ -265,7 +266,11 @@ pub fn place(request: &PlaceRequest) -> Placement { (Side::Start, true) | (Side::End, false) => Pos2::new(anchor.right() + gap, across_y), }; let max_min = Pos2::new((bounds.right() - size.x).max(bounds.left()), (bounds.bottom() - size.y).max(bounds.top())); - let min = Pos2::new(min.x.clamp(bounds.left(), max_min.x), min.y.clamp(bounds.top(), max_min.y)); + let keep = |min: Pos2| Pos2::new(min.x.clamp(bounds.left(), max_min.x), min.y.clamp(bounds.top(), max_min.y)); + let min = keep(min); + let shifted = keep(min + Vec2::new(finite(request.offset.x), finite(request.offset.y))); + let covers = request.clear.map(clean).is_some_and(|clear| clear.intersects(Rect::from_min_size(shifted, size))); + let min = if covers { min } else { shifted }; Placement { rect: Rect::from_min_size(min, size), side } } @@ -301,6 +306,22 @@ mod tests { assert_eq!(place(&request.side(Side::End)).rect.width(), 120.0, "beside it: its own width"); } + #[test] + fn an_offset_overlay_moves_unless_it_would_cover_its_clear_rect() { + let request = PlaceRequest::new(rect(100.0, 100.0, 200.0, 24.0), Vec2::new(100.0, 40.0), BOUNDS); + let home = place(&request).rect; + assert_eq!(home.min, Pos2::new(100.0, 124.0), "below the anchor, aligned to its start"); + let by = Vec2::new(50.0, 30.0); + let moved = request.offset(by); + assert_eq!(place(&moved).rect.min, home.min + by, "shifted by the offset"); + let point = Rect::from_min_max(Pos2::new(160.0, 170.0), Pos2::new(160.0, 170.0)); + assert_eq!(place(&moved.clear(Some(point))).rect, home, "the shifted rect would cover the point: the unshifted one stays"); + let aside = rect(600.0, 500.0, 10.0, 10.0); + assert_eq!(place(&moved.clear(Some(aside))).rect.min, home.min + by, "a clear rect off to the side changes nothing"); + let far = place(&request.offset(Vec2::new(10_000.0, 0.0))).rect; + assert_eq!(far.right(), BOUNDS.right(), "the shifted rect is clamped inside the bounds"); + } + #[test] fn rect_is_finite_and_inside_the_bounds_for_any_request() { let sides = [Side::Below, Side::Above, Side::Start, Side::End]; @@ -316,7 +337,9 @@ 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 }) + .offset(Vec2::new(w, y)) + .clear(Some(rect(h, x, w.abs(), y.abs()))); let placed = place(&request).rect; assert!(placed.min.is_finite() && placed.max.is_finite(), "{request:?}"); assert!(BOUNDS.contains_rect(placed), "{request:?} gave {placed:?}"); diff --git a/crates/ui-canvas/src/canvas_controls/task_bar.rs b/crates/ui-canvas/src/canvas_controls/task_bar.rs index ac9c242e2..79d4f2984 100644 --- a/crates/ui-canvas/src/canvas_controls/task_bar.rs +++ b/crates/ui-canvas/src/canvas_controls/task_bar.rs @@ -1,4 +1,4 @@ -//! `TaskBar`: the contextual task bar over a canvas, hung below its anchor until the user moves it. +//! `TaskBar`: the contextual task bar over a canvas, hung beside its anchor until the user moves it. #[cfg(feature = "testing")] mod stories; @@ -12,26 +12,28 @@ use crate::contract::{ Bind, BindMut, Common, Component, Container, ContainerProps, Cx, DragBand, HeaderRegion, Inner, Mount, NoValue, Outcome, Overlay, Pad, PopupKind, Props as _, Role, Show as _, ShowContainer as _, Sizing, Trigger, }; -use crate::display::Icon; -use crate::menu::{Menu, MenuItem, PanelRow}; +use crate::display::{Divider, Icon}; +use crate::menu::{Menu, MenuItem, PanelMenu, PanelRow}; use crate::primitives::IconSpec; use crate::runtime::env::{Env, Label}; use crate::style::{ComponentStyle, StyleCx, SurfaceKind}; use craft_behavior::kurbo::Point; -use craft_behavior::overlay::{Align, Side}; +use craft_behavior::overlay::{Align, PlaceRequest, Side}; use craft_behavior::{EditEvent, IconId, Metrics, Mixed, Sense}; -use egui::{Pos2, Rect, Response, Ui}; +use egui::{Pos2, Rect, Response, Ui, Vec2}; use std::sync::Arc; /// Where the task bar sits: pinned or following its anchor, and where the user moved it. The one owner of the bar's -/// place; the serde shape is PhotoCraft's, so saved preferences load unchanged, and a non-finite `at` counts as unset. +/// place; the serde shape is PhotoCraft's, so saved preferences load unchanged, and a non-finite `at` or `offset` +/// counts as unset. #[derive(Clone, Copy, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)] #[non_exhaustive] pub struct TaskBarPlace { /// The bar keeps its place instead of following the anchor. #[serde(default)] pub pinned: bool, - /// The bar's top left corner relative to the area's, once moved; `None` while it follows the anchor. + /// The bar's top left corner relative to the area's while it is pinned; an unpinned place that holds it turns it + /// into `offset` at once. #[serde(default)] pub at: Option<[f32; 2]>, /// Where an unpinned bar sits relative to its anchor, once moved; the bar follows the anchor with this offset. @@ -51,19 +53,28 @@ impl TaskBarPlace { let [x, y] = self.at.filter(|at| at.iter().all(|v| v.is_finite()))?; Some(Pos2::new(area.min.x + x, area.min.y + y)) } + + /// How far the bar sits from its unmoved place; `None` while it sits there. A non-finite `offset` counts as unset. + fn offset_in(&self) -> Option { + let [x, y] = self.offset.filter(|offset| offset.iter().all(|v| v.is_finite()))?; + Some(Vec2::new(x, y)) + } } -/// The contextual task bar: a movable modeless layer hung below the screen bounds of `anchor` while its place is -/// unset, with a body of members and a More menu (Hide, Reset, Pin). One place, pin and Hide setting for Crop and +/// The contextual task bar: a movable modeless layer hung on a side of the screen bounds of `anchor`, with a body of +/// members and a More menu (Hide, Pin, Reset, then the host's rows). One place, pin and Hide setting for Crop and /// Frame. /// -/// - Place: centred under the anchor's bounds on screen, a section gap below them, following the anchor each pass. -/// Dragged by its grip (any free point of the bar), it stays where it is dropped and keeps its left edge while the -/// anchor moves or the bar grows; a press with no drag changes nothing. Pinned, it stays where it shows. +/// - Place: on `side` of the anchor's bounds on screen (below, centred, by default), a section gap away, following the +/// anchor each pass; it flips to the other side when there is no room. Dragged by its grip (any free point of the +/// bar), it keeps its offset from that place while the anchor moves or the bar grows; a press with no drag changes +/// nothing. A moved bar whose offset place would cover the `keep_clear` point shows at its unmoved place and keeps +/// its offset. Pinned, it stays where it shows. /// - Area: the bar shows inside `area` (the canvas rect): a move stays inside it, and a stored place outside it comes -/// back inside, its left and top edges winning where the bar does not fit. +/// back inside, its left and top edges winning where the bar does not fit. Its members continue on a new line when +/// the area is too narrow for them. /// - Menu: Hide bar reports `hide`; Reset bar position clears the place, so the bar follows its anchor again; Pin bar -/// position keeps it where it shows. +/// position keeps it where it shows and, picked again, lets it follow its anchor from where it shows. /// - Nothing shows outside a canvas, or while the anchor's bounds or `area` is not finite or `area` has no size. /// - `place` is the caller's value (`Bind::bound`): the bar reads it before it shows and writes the pass's change /// after. @@ -73,6 +84,10 @@ pub struct TaskBar<'a> { label: Label, anchor: &'a [Point], place: Bind<'a, TaskBarPlace>, + rows: Vec, + side: Side, + align: Align, + keep_clear: Option, } /// What the task bar reports. @@ -119,6 +134,10 @@ enum MenuPick { /// The More button and its menu, placed once per run of the bar's body. struct MoreMenu { words: Arc, + /// Whether the bar is pinned: the Pin row shows a check mark. + pinned: bool, + /// The host's rows, after the bar's own. + rows: Vec, placed: bool, pick: Option, } @@ -133,74 +152,97 @@ impl MoreMenu { let more = IconButton::new(IconId::new("more")).popup(PopupKind::Menu).label(words.task_bar_more.clone()).show(ui); let mut pick = None; let rows = [ - (MenuPick::Hide, "eye_off", &words.task_bar_hide), - (MenuPick::Reset, "reset", &words.task_bar_reset), - (MenuPick::Pin, "pin", &words.task_bar_pin), + (MenuPick::Hide, "eye_off", &words.task_bar_hide, false), + (MenuPick::Pin, "pin", &words.task_bar_pin, self.pinned), + (MenuPick::Reset, "reset", &words.task_bar_reset, false), ]; + let host = &self.rows; let _ = Menu::new(words.task_bar_more.clone()).opened_by(&more, Trigger::Click).show(ui, |ui, _| { - for (row, icon, name) in rows { - if MenuItem::new(name.clone()).icon(IconSpec::svg(IconId::new(icon))).show(ui).ran { + for (row, icon, name, checked) in rows { + if MenuItem::new(name.clone()).icon(IconSpec::svg(IconId::new(icon))).check(checked).show(ui).ran { pick = Some(row); } } + if !host.is_empty() { + Divider::horizontal().show(ui); + let _ = PanelMenu::new(host.clone(), Vec::new()).show(ui); + } }); self.pick = pick.or(self.pick); } } +/// Whether the More menu offers `row`: a dock row (`PanelRow::Item`), which only a dock reads, does not belong in it. +fn offered(row: &PanelRow) -> bool { + match row { + PanelRow::Item(_) => false, + PanelRow::Submenu(_, rows) => rows.iter().all(offered), + _ => true, + } +} + impl<'a> TaskBar<'a> { /// A bar named `label` hung below the screen bounds of `anchor` (document points) until `place` moves it. pub fn new(label: impl Into