mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 02:54:25 +00:00
Slicer: height_for measures the card at a width; the story reserves that room
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use craft_behavior::{DisplayOrder, IconId, ItemSource, Metrics, Selection, Sense, TileLayout};
|
||||
use craft_behavior::{DisplayOrder, IconId, ItemSource, Metrics, Proposal, Selection, Sense, TileLayout};
|
||||
use craft_theme::Variant;
|
||||
use craft_theme::variant::{Emphasis, Tone};
|
||||
use craft_ui::buttons::IconButton;
|
||||
@@ -101,20 +101,31 @@ impl<'a> Slicer<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
/// The slicer's height at `width`: its header over every row of tiles, none scrolled. An app sizes a new slicer
|
||||
/// with it.
|
||||
pub fn height_for(&self, ui: &Ui, width: f32) -> f32 {
|
||||
let mut card = SlicerCard::new(self.caption.clone(), self.filtered);
|
||||
card.measure_in(ui, Proposal::Width(width), |ui, _| self.tiles(ui)).y
|
||||
}
|
||||
|
||||
/// Shows the slicer in `rect` and reports what the user did.
|
||||
pub fn show(self, ui: &mut Ui, rect: Rect) -> SlicerOutput {
|
||||
let Slicer { caption, items, columns, filtered, zoom } = self;
|
||||
let source = Values(items);
|
||||
let card = SlicerCard::new(caption.clone(), filtered);
|
||||
let shown = card.show_at(ui, rect, |ui, _| {
|
||||
let tiles = SlicerTiles::new(columns, zoom).label(caption.clone());
|
||||
tiles.show_items(ui, &source, &mut |key, _| SlicerTile::new(items[*key].0.clone()).on(items[*key].1))
|
||||
});
|
||||
let card = SlicerCard::new(self.caption.clone(), self.filtered);
|
||||
let shown = card.show_at(ui, rect, |ui, _| self.tiles(ui));
|
||||
let items = self.items;
|
||||
let toggled = shown.inner.and_then(|tiles| toggled(&tiles, items));
|
||||
let SlicerCardOutput { response, cleared } = shown.output;
|
||||
let action = if cleared { Some(SlicerAction::Clear) } else { toggled.map(SlicerAction::Toggle) };
|
||||
SlicerOutput { response, action }
|
||||
}
|
||||
|
||||
/// The card's body: the value tiles.
|
||||
fn tiles(&self, ui: &mut Ui) -> CollectionResponse<usize, ()> {
|
||||
let items = self.items;
|
||||
let source = Values(items);
|
||||
let tiles = SlicerTiles::new(self.columns, self.zoom).label(self.caption.clone());
|
||||
tiles.show_items(ui, &source, &mut |key, _| SlicerTile::new(items[*key].0.clone()).on(items[*key].1))
|
||||
}
|
||||
}
|
||||
|
||||
/// The value a tile press or key reported.
|
||||
|
||||
@@ -2,18 +2,26 @@
|
||||
|
||||
use super::{Slicer, SlicerCard, SlicerTile, SlicerTiles, Values};
|
||||
use craft_ui::contract::{Props as _, ShowCollection as _, ShowContainer as _};
|
||||
use craft_ui::testing::{Drive, StoryData, StoryOutput, show_as_only_item, story};
|
||||
use craft_ui::testing::{Drive, StoryData, StoryOutput, show_as_only_item, stage_fitting, stage_width, story};
|
||||
use egui::Ui;
|
||||
|
||||
fn colours() -> Vec<(String, bool)> {
|
||||
["Red", "Green", "Blue", "Cyan", "Magenta"].iter().map(|name| ((*name).to_owned(), *name == "Green")).collect()
|
||||
}
|
||||
|
||||
/// Six regions, every other one on.
|
||||
fn regions() -> Vec<(String, bool)> {
|
||||
["North", "South", "East", "West", "Central", "Export"].iter().enumerate().map(|(at, name)| ((*name).to_owned(), at % 2 == 0)).collect()
|
||||
}
|
||||
|
||||
#[story(Slicer<'static>, covers = [Default], drive = Drive::Never)]
|
||||
fn slicer(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
let items = colours();
|
||||
let rect = ui.available_rect_before_wrap();
|
||||
Slicer::new("Colour", &items).columns(2).filtered(true).show(ui, rect).into()
|
||||
let items = regions();
|
||||
let filtered = items.iter().any(|(_, on)| !on);
|
||||
let slicer = Slicer::new("Region", &items).columns(2).filtered(filtered);
|
||||
let height = slicer.height_for(ui, stage_width(ui));
|
||||
let rect = stage_fitting(ui, |_| height);
|
||||
slicer.show(ui, rect).into()
|
||||
}
|
||||
|
||||
fn card(ui: &mut Ui, filtered: bool) -> StoryOutput {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
mod grid;
|
||||
mod outline;
|
||||
mod refpick;
|
||||
mod slicer;
|
||||
|
||||
use craft_table::ui::testing::registered;
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
//! `Slicer`: its own height holds every tile.
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
|
||||
use craft_table::ui::Slicer;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::{Rect, vec2};
|
||||
|
||||
const VALUES: [&str; 7] = ["North", "South", "East", "West", "Central", "Export", "Import"];
|
||||
|
||||
/// Shown at the height it measures at a width, a slicer shows every tile inside its card, none scrolled out.
|
||||
#[trial]
|
||||
fn a_slicer_at_its_own_height_shows_every_tile() {
|
||||
let items: Vec<(String, bool)> = VALUES.iter().enumerate().map(|(at, name)| ((*name).to_owned(), at % 2 == 0)).collect();
|
||||
let card = Rc::new(Cell::new(Rect::NOTHING));
|
||||
let seen = card.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let slicer = Slicer::new("Region", &items).columns(2);
|
||||
let width = ui.available_width() / 2.0;
|
||||
let rect = Rect::from_min_size(ui.max_rect().min, vec2(width, slicer.height_for(ui, width)));
|
||||
seen.set(slicer.show(ui, rect).response.rect);
|
||||
});
|
||||
fixture.settle();
|
||||
let card = card.get();
|
||||
for value in VALUES {
|
||||
let tile = fixture.by_label(value).unwrap_or_else(|| panic!("the tile {value} has a node"));
|
||||
assert!(card.contains_rect(tile.rect), "{value} at {:?} lies inside the card {card:?}", tile.rect);
|
||||
}
|
||||
}
|
||||
@@ -44,7 +44,7 @@ pub use crate::runtime::measure::{
|
||||
pub use craft_ui_macros::{story, trial};
|
||||
pub use fixture::{Fixture, Impure, Room, Target};
|
||||
pub use frames::Frames;
|
||||
pub use stage::{room_height, stage, stage_fitting};
|
||||
pub use stage::{room_height, stage, stage_fitting, stage_width};
|
||||
#[doc(hidden)]
|
||||
pub use story::{
|
||||
CollectionKind, Drive, EditKind, Grip, Required, Story, StoryData, StoryOutput, StoryRegistration, StorySnapshot, show_as_only_item, stories,
|
||||
|
||||
@@ -22,9 +22,15 @@ pub fn room_height(ui: &Ui) -> f32 {
|
||||
/// Allocates a stage as wide as [`stage`]'s and as tall as the story's content at that width, which `height` gives,
|
||||
/// and returns its rect: the stage of a canvas whose document is only as tall as its content (a text being edited).
|
||||
pub fn stage_fitting(ui: &mut Ui, height: impl FnOnce(f32) -> f32) -> Rect {
|
||||
let width = stage_width(ui);
|
||||
crate::runtime::egui::alloc::space(ui, egui::vec2(width, height(width)))
|
||||
}
|
||||
|
||||
/// The width of the stage [`stage_fitting`] allocates: the width left in `ui` between the least and greatest stage
|
||||
/// width. A story whose content measures itself at a width (through `ui`) reads it before allocating.
|
||||
pub fn stage_width(ui: &Ui) -> f32 {
|
||||
let story = Env::of(ui).tokens.tokens().size.story;
|
||||
let room = flow_room(ui).x;
|
||||
let room = if room.is_finite() { room } else { ui.available_width() };
|
||||
let width = room.clamp(story.width_min, story.width_max);
|
||||
crate::runtime::egui::alloc::space(ui, egui::vec2(width, height(width)))
|
||||
room.clamp(story.width_min, story.width_max)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user