From a555f84b715e873ca15fc87c5ea76c7c9702daaf Mon Sep 17 00:00:00 2001 From: aiongg Date: Fri, 13 Nov 2026 17:02:26 +0000 Subject: [PATCH] PresetPicker as a pattern over a Popover container: open at a point without the field (open_at), resizable with a remembered size; PopoverOutput reports its open state from its binding (ColorPopover and PresetPicker read it, not the exit fade); no hover proposals --- crates/color/src/ui/popover.rs | 2 +- crates/imaging/src/ui/layer_style/page.rs | 8 +- .../src/patterns/presets/browser.rs | 33 +- .../ui-patterns/src/patterns/presets/event.rs | 7 +- .../src/patterns/presets/picker.rs | 474 ++++++++++++------ .../src/patterns/presets/picker/stories.rs | 2 +- crates/ui-patterns/tests/it/presets/picker.rs | 53 +- crates/ui-widgets/src/popover/anchored.rs | 16 +- crates/ui-widgets/src/popover/popup_list.rs | 2 +- 9 files changed, 408 insertions(+), 189 deletions(-) diff --git a/crates/color/src/ui/popover.rs b/crates/color/src/ui/popover.rs index db3e847c9..30e4d1160 100644 --- a/crates/color/src/ui/popover.rs +++ b/crates/color/src/ui/popover.rs @@ -132,7 +132,7 @@ fn popover( } picker.show(ui) }); - let open = shown.inner.is_some() && shown.output.closed.is_none(); + let open = shown.output.open; match shown.inner { Some(ColorPickerOutput { events, screen_pick, .. }) => (events, screen_pick, open), None => (EditEvents::new(), None, open), diff --git a/crates/imaging/src/ui/layer_style/page.rs b/crates/imaging/src/ui/layer_style/page.rs index baf3a7d66..9aea544d1 100644 --- a/crates/imaging/src/ui/layer_style/page.rs +++ b/crates/imaging/src/ui/layer_style/page.rs @@ -278,13 +278,13 @@ fn field_row(ui: &mut Ui, input: &PageInput<'_>, field: &EffectField, shown: &St let current = stored.map(str::to_owned).or(first.clone()); let picker = Field::new(label.clone()) .id_salt(key) - .show(ui, |ui, _| PresetPicker::new(label.clone(), Bind::given(current.clone()), input.patterns, Bind::kept()).show(ui)) + .show(ui, |ui, _| PresetPicker::new(label.clone(), Bind::given(current.clone()), input.patterns, Bind::kept()).show(ui).edit) .inner; - if let Some(picker) = picker { - if picker.edit.events.is_empty() && stored.is_none() && first.is_some() { + if let Some(edit) = picker { + if edit.events.is_empty() && stored.is_none() && first.is_some() { out.fill_default = current.map(|text| (key.to_owned(), StyleValue::Text(text))); } - let edit = picker.edit.map(Option::unwrap_or_default); + let edit = edit.map(Option::unwrap_or_default); push_edit(&mut out.edit, style_edit(&edit, input.style, |v| param(StyleValue::Text(v.clone())))); } } diff --git a/crates/ui-patterns/src/patterns/presets/browser.rs b/crates/ui-patterns/src/patterns/presets/browser.rs index c7596a8e3..42d43a484 100644 --- a/crates/ui-patterns/src/patterns/presets/browser.rs +++ b/crates/ui-patterns/src/patterns/presets/browser.rs @@ -20,8 +20,8 @@ use super::words::PresetWords; use crate::bars::{Bar, BarKind}; use crate::choice::Segmented; use crate::contract::{ - Bind, BodyCx, CollectionEvent, DropMark, DropTarget, DropZone, Entry, LeadSource, ListDrag, OpenChange, Pattern, Props, Rename, ReorderMove, - ReorderRule, SelectMode, Show as _, ShowCollection as _, ShowContainer as _, request_rename, + Bind, BodyCx, CollectionEvent, DropMark, DropTarget, DropZone, Entry, ListDrag, OpenChange, Pattern, Props, Rename, ReorderMove, ReorderRule, + SelectMode, Show as _, ShowCollection as _, ShowContainer as _, request_rename, }; use crate::display::Divider; use crate::layout_components::Stack; @@ -206,6 +206,10 @@ pub struct PresetBrowser<'a, K: Clone + Eq + Hash + Send + Sync + 'static> { /// A row above the presets with the search field and the grid and list buttons. #[prop] toolbar: bool, + /// The toolbar's search field; off, the toolbar keeps the view buttons and the host's tools, and the search is + /// cleared. + #[prop] + search: bool, /// The host's controls at the reading end of the toolbar. #[prop(func = "FnMut(&mut Ui)")] tools: Option>, @@ -266,6 +270,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> PresetBrowser<'a, K> { thumb_range: TileZoom::Ladder, default_open: DefaultOpen::All, toolbar: false, + search: true, tools: None, row_menu: true, view_lock: None, @@ -473,7 +478,6 @@ enum Seen { Activate(K), Open(OpenChange), Menu(K), - Pointer(K), Reorder(EditEvent>), Rename(EditEvent>), } @@ -487,7 +491,6 @@ fn seen(events: Vec>) -> Vec> { CollectionEvent::Activate(key) => Some(Seen::Activate(key)), CollectionEvent::Open(change) => Some(Seen::Open(change)), CollectionEvent::ContextMenu(request) => Some(Seen::Menu(request.key)), - CollectionEvent::LeadMoved { key, by: LeadSource::Pointer } => Some(Seen::Pointer(key)), CollectionEvent::Reorder(edit) => Some(Seen::Reorder(edit)), CollectionEvent::Rename(edit) => Some(Seen::Rename(edit)), _ => None, @@ -546,6 +549,7 @@ impl PresetBrowser<'_, K> { thumb_range, default_open, toolbar, + search, mut tools, row_menu, view_lock, @@ -595,14 +599,21 @@ impl PresetBrowser<'_, K> { bar = bar.trailing(|ui, _| tools.show(ui)); } let _ = bar.show(ui, |ui, _| { - let field = TextField::search(words.search.clone(), Bind::bound(&mut local.filter)).placeholder(words.search.clone()); - let field = field.drives(list_id); - field.show(ui); + if search { + let field = TextField::search(words.search.clone(), Bind::bound(&mut local.filter)).placeholder(words.search.clone()); + let field = field.drives(list_id); + field.show(ui); + } if !locked { Segmented::new(words.view.clone(), Bind::bound(&mut local.view), &modes).show(ui); } }); } + // A hidden search field filters nothing: a search typed before hiding it would otherwise keep presets + // out of the list with no field to show why. + if toolbar && !search { + local.filter.clear(); + } let view = view_lock.unwrap_or(local.view); let query = local.filter.trim().to_owned(); @@ -645,7 +656,6 @@ impl PresetBrowser<'_, K> { } }; - let mut hovered: Option = None; let (response, displayed, pass) = if shown.is_empty() && actions.is_empty() { let message = if query.is_empty() { words.empty.clone() } else { words.no_match.clone() }; let response = Notice::new(Tone::Neutral, message).layout(NoticeLayout::Centred).show(ui).response; @@ -852,7 +862,6 @@ impl PresetBrowser<'_, K> { menu_target = Some(key); } } - Seen::Pointer(key) => hovered = key.preset().filter(|key| row_of(key).is_some_and(|at| !rows[at].is_group())).cloned(), Seen::Reorder(edit) => { let moved = |mv: ReorderMove>| { let mv = app_move(mv)?; @@ -932,7 +941,7 @@ impl PresetBrowser<'_, K> { if !has_footer { commit(cx, &state, rows, &name); } - (response, hovered, events) + (response, events) }; let footer_region = |ui: &mut Ui, cx: &mut BodyCx<'_>| { @@ -948,10 +957,10 @@ impl PresetBrowser<'_, K> { let stack = room_height.map_or_else(|| Stack::new().fill(), |height| Stack::new().fill_width().room_height(height)); let stack = if has_footer { stack.footer(footer_region) } else { stack }; let shown = stack.show(ui, body); - let (response, hovered, mut events) = shown.inner.unwrap_or_else(|| (shown.output.clone(), None, Vec::new())); + let (response, mut events) = shown.inner.unwrap_or_else(|| (shown.output.clone(), Vec::new())); events.extend(footer_events.take().into_iter().map(PresetEvent::Footer)); let drag = crate::runtime::drag::active::>(ui.ctx()).filter(|drag| drag.list == list_id).map(|drag| (*drag).clone()); - PresetOutput::new(response, events, drag, hovered) + PresetOutput::new(response, events, drag) } } diff --git a/crates/ui-patterns/src/patterns/presets/event.rs b/crates/ui-patterns/src/patterns/presets/event.rs index 6543be8c3..b37257504 100644 --- a/crates/ui-patterns/src/patterns/presets/event.rs +++ b/crates/ui-patterns/src/patterns/presets/event.rs @@ -115,15 +115,12 @@ pub struct PresetOutput { /// While a preset or group is dragged, its payload, so the host can draw its own drop highlight; `None` /// otherwise. pub drag: Option>, - /// The preset under the pointer this pass, for a hover preview; `None` off the list, over a header, or while a - /// drag is under way. - pub hovered: Option, } impl PresetOutput { /// The output of a browser pass. - pub fn new(response: Response, events: Vec>, drag: Option>, hovered: Option) -> Self { - PresetOutput { response, events, drag, hovered } + pub fn new(response: Response, events: Vec>, drag: Option>) -> Self { + PresetOutput { response, events, drag } } } diff --git a/crates/ui-patterns/src/patterns/presets/picker.rs b/crates/ui-patterns/src/patterns/presets/picker.rs index 9119eab45..5ccfc79da 100644 --- a/crates/ui-patterns/src/patterns/presets/picker.rs +++ b/crates/ui-patterns/src/patterns/presets/picker.rs @@ -1,33 +1,37 @@ //! `PresetPicker`: a closed field that shows the current preset and opens a `PresetBrowser` in a popover. //! -//! The closed field is a `SelectFace` over the field well, as `Select` shows it. Hovering a preset in the popover -//! proposes it, a pick commits and closes, and closing without a pick restores the value. A host can put content -//! above the browser (`head`), keep the popover open on a click (`keep_open`), give the browser a footer and set -//! how far the thumbnails zoom. +//! The closed field is a `SelectFace` over the field well, as `Select` shows it; the popover is a `Popover` container +//! with the browser. A pick sets the value and closes. A host can put content above the browser (`head`), keep the popover open on a click +//! (`keep_open`), give the browser a footer, set how far the thumbnails zoom, open the popover at a point without the +//! field (`open_at`) and let the user resize it (`resizable`). #[cfg(feature = "testing")] mod stories; +use std::sync::Arc; + use super::browser::{BrowserTools, PresetBrowser, ThumbPainter}; -use super::event::PresetEvent; +use super::event::{PresetEvent, PresetOutput}; use super::footer::PresetFooterOptions; use super::model::{CellShape, PresetRow, ThumbInfo}; use super::state::{DefaultOpen, PresetState, PresetView}; use super::words::PresetWords; use crate::contract::{ - Access, Bind, BindMut, BodyCx, Common, Component, Cx, Edit as _, EditResponse, MeasureCx, Outcome, Overlay, OverlayUi, Overlays as _, PaintUi, - PartsCx, Pointer as _, Props as _, Role, ShowContainer as _, WordSetCx as _, + Access, Bind, BindMut, BodyCx, Common, Component, Cx, Edit as _, EditResponse, Focus as _, MeasureCx, Outcome, PaintUi, PartsCx, Pointer as _, + Props as _, Remember as _, Role, SaveKey, Show as _, ShowContainer as _, Tooltip, WordSetCx as _, }; use crate::layout_components::Stack; use crate::list::RowTest; -use crate::pickers::{SelectFace, toggle_on_key}; +use crate::pickers::SelectFace; +use crate::popover::Popover; use crate::primitives::ContentPainterCx; use crate::runtime::env::{EnvView, Label}; use crate::runtime::memory::StageAccess; +use crate::runtime::overlay::CloseReason; use crate::style::{ComponentStyle, FocusLook, StyleCx, SurfaceLook}; use crate::tile_grid::TileZoom; -use craft_behavior::{CancelReason, GesturePhase, HitLevel, HitPriority, Metrics, PartKey, PressPhase, Sense, Value}; -use egui::{Id, Rect, Response, Ui, Vec2}; +use craft_behavior::{EditEvent, EditEvents, HitLevel, HitPriority, Intent, Metrics, Mixed, PartKey, PressPhase, Sense, Value}; +use egui::{Pos2, Rect, Response, Ui, Vec2}; use std::hash::Hash; /// The face under the whole field. @@ -61,7 +65,7 @@ pub enum PickerField { ThumbnailAndName, } -/// The style of a preset picker: the field well and the focus edge; the face carries the padding. +/// The style of a preset picker's field: the field well and the focus edge; the face carries the padding. #[derive(Clone, Debug, PartialEq, ComponentStyle)] #[non_exhaustive] pub struct PresetPickerStyle { @@ -80,7 +84,7 @@ pub struct PresetPickerStyle { #[non_exhaustive] #[must_use = "the host applies the picker's events"] pub struct PresetPickerOutput { - /// The field's response and the edit of its value. + /// The field's response and the edit of its value, with the popover's picks. pub edit: EditResponse>, /// The preset picked in this pass, which the binding now holds. pub picked: Option, @@ -89,6 +93,11 @@ pub struct PresetPickerOutput { pub events: Vec>, /// The pick closed the popover (a double click or Enter); false for a click while `keep_open` holds it open. pub closed: bool, + /// How the popover closed in this pass (a pick is `Activated`, Escape is `Escape`); `None` while it stays open + /// or stays closed. + pub closed_as: Option, + /// Whether the popover is open after this pass. + pub open: bool, /// The thumbnail of the face was pressed (with `edit_face`): the host opens its editor. pub edit_requested: bool, /// The row `PresetPicker::create_row` added was chosen in this pass; the popover has closed. @@ -111,22 +120,22 @@ impl Outcome for PresetPickerOutput { } /// A field that shows the current preset and opens its presets. A click, Enter, Space or Alt+Down opens the popover -/// with the browser; hovering a preset proposes it, a click or Enter picks it and closes, and Escape closes and -/// restores the value. +/// with the browser; a click or Enter picks a preset and closes, and Escape closes and keeps the value. /// /// - **Value.** The binding holds the picked preset's key, or none while the value is no preset of `rows`; the field /// then shows `placeholder`, "Custom" by default. /// - **State.** `state` is the browser's view settings; bind it with `Bind::bound`. /// - **Thumbnails.** Each row's `preview` paints its thumbnail, in the field and in the browser. +/// - **Popover.** A `Popover` container: it hangs from the field, or from a point with [`open_at`](Self::open_at), +/// and with [`resizable`](Self::resizable) its edge grips resize it and the size is saved. /// /// ```ignore /// let out = PresetPicker::new("Contour", Bind::bound(&mut shape), &rows, Bind::bound(&mut state)).field(PickerField::ThumbnailAndName).show(ui); /// ``` -#[derive(crate::contract::Component, crate::contract::Props)] -#[component(overlay, probe(PresetPicker<'static, u32>))] +#[derive(crate::contract::Pattern, crate::contract::Props)] +#[pattern(probe(PresetPicker<'static, u32>))] pub struct PresetPicker<'a, K: Value + Eq + Hash + 'static> { - #[common] - common: Common, + label: Label, value: Bind<'a, Option>, rows: &'a [PresetRow], state: Bind<'a, PresetState>, @@ -145,13 +154,16 @@ pub struct PresetPicker<'a, K: Value + Eq + Hash + 'static> { /// A row above the presets with the search field. #[prop] toolbar: bool, + /// The search field in the toolbar; off, the toolbar keeps the host's tools and the search is cleared. + #[prop] + search: bool, /// The host's controls at the reading end of the browser's toolbar. #[prop(func = "FnMut(&mut Ui)")] tools: Option>, /// The id of the browser in the popover, which `PresetBrowser::request_rename` names: a host that starts a rename /// from its own control (a menu's Rename) gives one. #[prop] - browser_id: Option, + browser_id: Option, /// Which presets and groups the browser may rename. #[prop(func = "Fn(&K) -> bool")] can_rename: Option>, @@ -173,11 +185,13 @@ pub struct PresetPicker<'a, K: Value + Eq + Hash + 'static> { /// A short line under the field's thumbnail (a brush's current size). #[prop(into)] caption: Option