mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 04:27:49 +00:00
Add document-aligned popup list variant
This commit is contained in:
@@ -7,4 +7,4 @@ mod words;
|
||||
|
||||
pub use anchored::{Popover, PopoverOutput, PopoverStyle};
|
||||
pub use drop_veil::{DropVeil, DropVeilOutput, DropVeilStyle, WholeWindow};
|
||||
pub use popup_list::{PopupList, PopupListStyle};
|
||||
pub use popup_list::{PopupList, PopupListKind, PopupListStyle};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! `PopupList` (15, 17): a dropdown under a field, holding the caller's `List`.
|
||||
|
||||
use super::anchored::PopoverOutput;
|
||||
use crate::contract::{Bind, BindMut, Common, Component, Container, ContainerProps, Cx, Mount, Overlay, POPUP_PAD, Props as _, Role, Trigger};
|
||||
use crate::contract::{Bind, BindMut, Common, Component, Container, ContainerProps, Cx, Mount, Overlay, POPUP_PAD, Pad, Props as _, Role, Trigger};
|
||||
use crate::runtime::env::Label;
|
||||
use crate::runtime::overlay::CloseReason;
|
||||
use crate::style::{ComponentStyle, StyleCx, SurfaceKind};
|
||||
@@ -19,6 +19,16 @@ pub struct PopupListStyle {
|
||||
metrics: Metrics,
|
||||
}
|
||||
|
||||
/// How a popup list relates its rows to its opener.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub enum PopupListKind {
|
||||
/// A UI list with padding around its rows.
|
||||
#[default]
|
||||
Standard,
|
||||
/// Document-field options whose rows extend to the field's edges.
|
||||
Document,
|
||||
}
|
||||
|
||||
/// A list popup under a field the app owns (a combo, a history picker). The body is the caller's `List::options`.
|
||||
///
|
||||
/// - The popup is at least as wide as its opener and capped to a share of the viewport.
|
||||
@@ -47,6 +57,16 @@ impl<'a> PopupList<'a> {
|
||||
PopupList { common: Common::new(), props, open, closed: None }.label(label)
|
||||
}
|
||||
|
||||
/// Selects the list's relationship to its opener.
|
||||
#[must_use]
|
||||
pub fn kind(mut self, kind: PopupListKind) -> Self {
|
||||
self.props = self.props.pad(match kind {
|
||||
PopupListKind::Standard => POPUP_PAD,
|
||||
PopupListKind::Document => Pad::None,
|
||||
});
|
||||
self
|
||||
}
|
||||
|
||||
/// Changes the popup's overlay as `Popover::overlay` does; callers hang the list from a part with
|
||||
/// `Overlay::opener_anchor(Anchor::Rect(..))`.
|
||||
#[must_use]
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//! Stories of `PopupList`.
|
||||
|
||||
use super::PopupList;
|
||||
use super::{PopupList, PopupListKind};
|
||||
use crate::buttons::Button;
|
||||
use crate::contract::overlay::Anchor;
|
||||
use crate::contract::{Bind, Show as _, ShowCollection as _, ShowContainer as _};
|
||||
use crate::contract::{Bind, Show as _, ShowCollection as _, ShowContainer as _, clicked};
|
||||
use crate::fonts::FontRef;
|
||||
use crate::layout_components::Stack;
|
||||
use crate::list::{DocumentRows, List, ListRow};
|
||||
@@ -65,9 +65,16 @@ const DOCUMENT_SIZE: f32 = 12.0;
|
||||
/// anchor, as a form field drawn on a document is.
|
||||
fn document_popup(ui: &mut Ui, data: &mut StoryData, rows: DocumentRows) -> StoryOutput {
|
||||
let names = ["Round", "Square", "Chisel", "Fan"];
|
||||
let page = Stack::new().fill().show(ui, |ui, _| Button::new("Brush tip").show(ui).rect);
|
||||
let page = Stack::new().fill().show(ui, |ui, _| {
|
||||
let opener = Button::new("Brush tip").show(ui);
|
||||
if clicked(&opener) {
|
||||
*data.value("open", false) = true;
|
||||
}
|
||||
opener.rect
|
||||
});
|
||||
let field = page.inner.unwrap_or(page.output.rect);
|
||||
PopupList::new("Brush tips", Bind::bound(data.value("open", true)))
|
||||
PopupList::new("Brush tips", Bind::bound(data.value("open", false)))
|
||||
.kind(PopupListKind::Document)
|
||||
.opened_by(&page.output)
|
||||
.overlay(|overlay| overlay.opener_anchor(Anchor::Rect(field)))
|
||||
.show(ui, |ui, _| {
|
||||
|
||||
@@ -6,7 +6,7 @@ use craft_ui::buttons::Button;
|
||||
use craft_ui::contract::{Bind, CollectionEvent, Overlay, Props as _, Scroll, Show as _, ShowCollection as _, ShowContainer as _, Trigger, clicked};
|
||||
use craft_ui::layout_components::Stack;
|
||||
use craft_ui::list::{List, ListRow};
|
||||
use craft_ui::popover::PopupList;
|
||||
use craft_ui::popover::{PopupList, PopupListKind};
|
||||
use craft_ui::testing::pointer::{self, click, moved, press, release};
|
||||
use craft_ui::testing::{Fixture, Frames, trial};
|
||||
use egui::{Key, Modifiers, Pos2, Rect, Ui, Vec2};
|
||||
@@ -124,6 +124,23 @@ fn a_popup_list_with_short_rows_fits_its_wide_opener() {
|
||||
assert!(row.left() >= popup.left() && row.right() <= popup.right(), "the row stays inside the popup: {row:?} {popup:?}");
|
||||
}
|
||||
|
||||
/// A document field's options start at its edge, so the selection fills the field's width.
|
||||
#[trial]
|
||||
fn a_document_popup_list_aligns_its_rows_with_the_opener() {
|
||||
let mut open = true;
|
||||
let anchor = Rect::from_min_size(Pos2::new(100.0, 100.0), Vec2::new(240.0, 30.0));
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let opener = Button::new("Open").show_at(ui, anchor).response;
|
||||
PopupList::new("Popup", Bind::bound(&mut open)).kind(PopupListKind::Document).opened_by(&opener).show(ui, |ui, _| {
|
||||
List::<usize>::options("Choices").show_items(ui, &Rows, &mut |at, _| ListRow::new(row(*at)).option());
|
||||
});
|
||||
});
|
||||
fixture.room(Vec2::new(600.0, 400.0)).settle();
|
||||
let opener = fixture.by_label("Open").expect("the opener").rect;
|
||||
let row = fixture.by_label(&row(0)).expect("a row").rect;
|
||||
assert!((row.left() - opener.left()).abs() <= 1.0 && (row.right() - opener.right()).abs() <= 1.0, "row {row:?}, opener {opener:?}");
|
||||
}
|
||||
|
||||
/// A scrolling page taller than the window: an "Open" button that opens a popup list of `ROWS` rows while `open`, then
|
||||
/// `PAGE` buttons. The rows the list activates go to `activated`.
|
||||
fn page(open: Rc<Cell<bool>>, activated: Rc<RefCell<Vec<usize>>>) -> Fixture {
|
||||
|
||||
Reference in New Issue
Block a user