diff --git a/crates/ui-gallery/src/chrome/card.rs b/crates/ui-gallery/src/chrome/card.rs index 853baadce..cd2e9dbf3 100644 --- a/crates/ui-gallery/src/chrome/card.rs +++ b/crates/ui-gallery/src/chrome/card.rs @@ -179,15 +179,8 @@ impl Card<'_> { /// Shows the card in the room `ui` offers, with the states of its stories: a memoized view, which replays its last /// run until the card, a state of its stories or what it shows around it changes. A replay reports no events. pub fn show(&self, ui: &mut Ui, cells: &mut Cells) -> CardEvents { - for story in self.stories { - cells.entry(story.index).or_default(); - } - let mut states: Vec<(usize, &mut CardState)> = cells - .iter_mut() - .filter(|(index, _)| self.stories.iter().any(|story| story.index == **index)) - .map(|(index, state)| (*index, state)) - .collect(); - states.sort_unstable_by_key(|(index, _)| *index); + let indices: Vec = self.stories.iter().map(|story| story.index).collect(); + let states = cells.states_of(&indices); let mut sent = Vec::new(); let props = CardProps { card: self, states, events: Outbox::new(&mut sent) }; let rect = MemoView::new(("gallery.card", &self.key), props, card_view).show(ui).flatten(); @@ -294,7 +287,7 @@ impl Card<'_> { look: self.look, gated: self.gated, caption: caption.map(str::to_owned), - inspect: self.inspect.map(|inspected| (region_key(inspected.tab, story.index), inspected.layers)), + inspect: self.inspect.as_ref().map(|inspected| (region_key(&inspected.tab, story.index), inspected.layers)), ..StoryBody::new(key, story.story, self.surface, self.room, Pad::None) } } diff --git a/crates/ui-gallery/src/chrome/dock.rs b/crates/ui-gallery/src/chrome/dock.rs index f75054afb..dac9db2fc 100644 --- a/crates/ui-gallery/src/chrome/dock.rs +++ b/crates/ui-gallery/src/chrome/dock.rs @@ -1,26 +1,65 @@ //! The dock: what the dock asks of the viewer for each panel of the shown workspace. -use super::panels::Panels; -use crate::viewer::{GalleryCommands, PanelId, Preview}; +use super::inspect::{self, Inspect}; +use super::overview::{self, Centre}; +use super::panels::{self, ThemePanels}; +use super::variants::{VariantsEvent, VariantsIn}; +use crate::viewer::{Browser, GalleryCommands, PanelId, Preview}; use craft_behavior::dock::Permissions; use craft_theme::variant::Tone; use craft_ui::contract::Show as _; -use craft_ui::contract::{BodyCx, SizeLimits}; +use craft_ui::contract::{BodyCx, Outbox, SizeLimits, Tracked}; +use craft_ui::display::InspectLayers; use craft_ui::docking::{DockHeights, DockPanels, PanelSpec}; use craft_ui::menu::PanelRow; use craft_ui::notices::Notice; use craft_window::policy::WindowKind; use egui::Ui; -/// The viewer's panels as the dock's contents. +/// The viewer's panels as the dock's contents. The panels' requests that change the viewer beyond the browser's state +/// collect in `opened` and `variants_out` for the shell to apply after the dock. #[non_exhaustive] pub struct GalleryDock<'a> { - /// What the panel bodies read and edit. - pub panels: Panels<'a>, + /// What the panels browse. + pub browser: &'a mut Browser, + /// Where the inspector is on. + pub inspect: Inspect, + /// The drawings the inspector shows; the Inspect panel edits them. + pub layers: &'a mut Tracked, + /// The centre tab an open Variants view is in, and the view. + pub variants: Option<(PanelId, VariantsIn)>, + /// The theme workspace's panels. + pub theme: ThemePanels<'a>, /// What the theme workspace's preview shows. pub preview: Preview, /// The mock application the App preview shows. pub app: &'a mut crate::mock::MockState, + /// The components whose tab a panel asked to open in this pass. + pub opened: Vec<&'static str>, + /// What the Variants view asked for in this pass. + pub variants_out: Vec, +} + +impl GalleryDock<'_> { + /// The centre tab `tab`'s view of the browser, with the rest of the browser's values the Overview reads. + fn centre_body(&mut self, ui: &mut Ui, tab: &PanelId) { + let Browser { catalog, filter, selection, story_of, cells, generation, level, followed, .. } = &mut *self.browser; + let centre = Centre { + catalog, + selection, + story_of, + cells, + generation: *generation, + inspected: self.inspect.of(tab, **self.layers), + variants: self.variants.as_ref().filter(|(owner, _)| owner == tab).map(|(_, view)| *view), + opened: Outbox::new(&mut self.opened), + variants_out: Outbox::new(&mut self.variants_out), + }; + match tab { + PanelId::Overview => overview::overview(ui, centre, filter, level, followed), + _ => overview::component_tab(ui, centre, tab), + } + } } impl DockPanels for GalleryDock<'_> { @@ -48,7 +87,12 @@ impl DockPanels for GalleryDock<'_> { (PanelId::Preview, Preview::Components) => { Notice::new(Tone::Info, "The Controls level comes with the overview.").show(ui); } - _ => self.panels.show(ui, *panel), + (PanelId::Components, _) => panels::components(ui, self.browser, &mut self.opened), + (PanelId::Stories, _) => panels::stories(ui, self.browser), + (PanelId::Properties, _) => panels::properties(ui, self.browser), + (PanelId::Inspect, _) => inspect::panel(ui, self.browser, &self.inspect, self.layers), + (PanelId::Overview | PanelId::Component(_), _) => self.centre_body(ui, panel), + _ => self.theme.show(ui, panel), } } diff --git a/crates/ui-gallery/src/chrome/inspect.rs b/crates/ui-gallery/src/chrome/inspect.rs index 5935c4626..6691ce0b2 100644 --- a/crates/ui-gallery/src/chrome/inspect.rs +++ b/crates/ui-gallery/src/chrome/inspect.rs @@ -1,12 +1,13 @@ //! The inspector: while the Inspect panel is in front, the selected card's stories are shown as inspected regions //! with the inspector's drawing over each, and the panel lists what each region showed. -use super::overview::{SpecStory, selected_type, target_story}; -use super::panels::Panels; -use crate::viewer::PanelId; +use super::overview::SpecStory; +use super::panels::view_id; +use crate::viewer::{Browser, Catalog, PanelId}; use craft_theme::variant::Tone; + use craft_ui::choice::Checkbox; -use craft_ui::contract::{Bind, Props as _, Scroll, Show as _, ShowContainer as _}; +use craft_ui::contract::{MemoProps, MemoView, Props as _, Scroll, Show as _, ShowContainer as _, Tracked, bind_tracked}; use craft_ui::display::{InspectLayers, Shade, Text}; use craft_ui::layout_components::{Row, Stack}; use craft_ui::runtime::env::Env; @@ -15,28 +16,27 @@ use craft_ui::sections::Section; use craft_ui::style::TypeRole; use egui::{Id, Ui}; -/// What the inspector shows in a pass. +/// Where the inspector is on in a pass, as the dock layout says. +#[derive(Clone, Debug, Default, PartialEq, MemoProps)] #[non_exhaustive] -pub struct Inspect<'a> { +pub struct Inspect { /// The Inspect panel is in front in the components workspace, with no Variants view open: the selected card's /// stories are recorded and drawn over. pub on: bool, /// The centre tabs in front whose selected card is inspected: the Overview, the selected component's tab. pub front: Vec, - /// The drawings shown. - pub layers: &'a mut InspectLayers, } -impl Inspect<'_> { - /// What the selected card of `tab` inspects, when the inspector is on and the tab is in front. - pub fn of(&self, tab: Option, selected: bool) -> Option { - let tab = tab.filter(|tab| self.on && selected && self.front.contains(tab))?; - Some(Inspected { tab, layers: *self.layers }) +impl Inspect { + /// How the selected card of `tab` is inspected with the drawings `layers`, when the inspector is on and the tab is + /// in front. + pub fn of(&self, tab: &PanelId, layers: InspectLayers) -> Option { + (self.on && self.front.contains(tab)).then(|| Inspected { tab: tab.clone(), layers }) } } /// How a card's stories are inspected: the tab the card is in, which keys their regions, and the drawings shown. -#[derive(Clone, Copy, Debug, PartialEq, craft_ui::contract::MemoProps)] +#[derive(Clone, Debug, PartialEq, craft_ui::contract::MemoProps)] #[non_exhaustive] pub struct Inspected { /// The centre tab the card is in. @@ -46,21 +46,40 @@ pub struct Inspected { } /// The inspected region of the story at catalog index `story` on the selected card of `tab`. -pub fn region_key(tab: PanelId, story: usize) -> Id { +pub fn region_key(tab: &PanelId, story: usize) -> Id { Id::NULL.with("gallery.inspect").with(tab).with(story) } +/// What the Inspect panel reads and edits. The records it lists are read through `inspect::recorded`, which the +/// framework tracks: a changed record runs the view. +#[derive(MemoProps)] +struct InspectProps<'a> { + catalog: &'a Catalog, + /// The selected component. + selected: Option<&'static str>, + /// The catalog index of the story it shows. + target: Option, + inspect: &'a Inspect, + layers: &'a mut Tracked, +} + /// The Inspect panel: the drawing switches, then for each front tab one section per story of the selected card with the /// elements its region showed, then the selected component's declared facts. -pub(super) fn panel(ui: &mut Ui, p: &mut Panels<'_>) { - let sections = regions(p); +pub(super) fn panel(ui: &mut Ui, browser: &mut Browser, inspect: &Inspect, layers: &mut Tracked) { + let (selected, target) = (browser.selected_type(), browser.target_story()); + let props = InspectProps { catalog: &browser.catalog, selected, target, inspect, layers }; + let _ = MemoView::new(view_id(&PanelId::Inspect), props, panel_view).show(ui); +} + +fn panel_view(props: &mut InspectProps<'_>, ui: &mut Ui) { + let sections = regions(props); let least = Env::of(ui).tokens.size.control.sm; - let (catalog, selected) = (p.catalog, selected_type(p)); - let layers = &mut *p.inspect.layers; + let InspectProps { catalog, selected, layers, .. } = props; Stack::new().fill().scroll(Scroll::vertical()).show(ui, |ui, _| { - Checkbox::new(Bind::bound(&mut layers.padding)).label("Padding").show(ui); - Checkbox::new(Bind::bound(&mut layers.hits)).label("Hit areas").show(ui); - Checkbox::new(Bind::bound(&mut layers.focus)).label("Focus order").show(ui); + let (padding, hits, focus) = bind_tracked!(layers => padding, hits, focus); + Checkbox::new(padding).label("Padding").show(ui); + Checkbox::new(hits).label("Hit areas").show(ui); + Checkbox::new(focus).label("Focus order").show(ui); Text::new(format!("Hit areas under {least} pt (size.control.sm) are flagged.")).role(TypeRole::Caption).shade(Shade::Muted).show(ui); for (at, (key, caption)) in sections.iter().enumerate() { Section::new(caption.clone()).id_salt(*key).initially_open(at == 0).show(ui, |ui, _| { @@ -72,21 +91,21 @@ pub(super) fn panel(ui: &mut Ui, p: &mut Panels<'_>) { }); }); } - super::panels::declared(ui, catalog, selected); + super::panels::declared(ui, catalog, *selected); }); } /// The regions the panel lists, with their captions: the stories of the selected card of each front tab. -fn regions(p: &Panels<'_>) -> Vec<(Id, String)> { - let Some(type_name) = selected_type(p).filter(|_| p.inspect.on) else { return Vec::new() }; - let (Some(component), Some(target)) = (p.catalog.component(type_name), target_story(p, type_name)) else { return Vec::new() }; - let Some(group) = p.catalog.group_at(type_name, target) else { return Vec::new() }; - let stories = p.catalog.group_stories(type_name, group); - let tabs = p.inspect.front.iter().filter(|tab| matches!(tab, PanelId::Overview) || **tab == PanelId::Component(type_name)); - tabs.flat_map(|tab| { - stories.iter().map(|&index| (region_key(*tab, index), SpecStory::of(p.catalog, index, &component.name).caption)).collect::>() - }) - .collect() +fn regions(props: &InspectProps<'_>) -> Vec<(Id, String)> { + let InspectProps { catalog, selected, target, inspect, .. } = props; + let Some(type_name) = selected.filter(|_| inspect.on) else { return Vec::new() }; + let (Some(component), Some(target)) = (catalog.component(type_name), *target) else { return Vec::new() }; + let Some(group) = catalog.group_at(type_name, target) else { return Vec::new() }; + let stories = catalog.group_stories(type_name, group); + let tab = PanelId::component(type_name); + let tabs = inspect.front.iter().filter(|front| matches!(front, PanelId::Overview) || **front == tab); + tabs.flat_map(|tab| stories.iter().map(|&index| (region_key(tab, index), SpecStory::of(catalog, index, &component.name).caption)).collect::>()) + .collect() } /// One row per element of `recorded` with a node or a focus stop, indented by the shown elements it sits in; an element diff --git a/crates/ui-gallery/src/chrome/mod.rs b/crates/ui-gallery/src/chrome/mod.rs index df44c1e37..acb1e7e0a 100644 --- a/crates/ui-gallery/src/chrome/mod.rs +++ b/crates/ui-gallery/src/chrome/mod.rs @@ -15,6 +15,6 @@ pub use card::{BodyShown, Card, CardEvents, CardStory, Look, StoryBody}; pub use inspect::{Inspect, Inspected, region_key}; pub use options::options_bar; pub use overview::{Spec, SpecStory, overview_card}; -pub use panels::Panels; +pub use panels::ThemePanels; pub use persist::Persisted; pub use shell::Gallery; diff --git a/crates/ui-gallery/src/chrome/overview.rs b/crates/ui-gallery/src/chrome/overview.rs index 1a093ad3b..f2769af7c 100644 --- a/crates/ui-gallery/src/chrome/overview.rs +++ b/crates/ui-gallery/src/chrome/overview.rs @@ -1,14 +1,19 @@ //! The centre tabs: the Overview (the cards of one level), a component's tab (a card per story group) and the Variants view -//! that replaces the front one. +//! that replaces the front one. Each tab's body is a memoized view around its cards, which are memoized views of their +//! own. use super::card::{Card, CardEvents, CardStory, Look}; -use super::panels::Panels; -use super::variants; -use crate::viewer::{Catalog, ComponentInfo, PanelId, TreeKey, by_level, entry_of}; +use super::inspect::Inspected; +use super::panels::{StoryOf, selected_of, target_story, view_id}; +use super::variants::{self, VariantsEvent, VariantsIn}; +use crate::viewer::{Catalog, Cells, ComponentInfo, PanelId, TreeKey, by_level, entry_of}; use craft_behavior::{ColumnWidth, Selection}; use craft_theme::variant::Tone; use craft_theme::{Tokens, TypeRole}; -use craft_ui::contract::{Bind, BodyCx, Entry, Gap, Lazy, Pad, Props as _, Scroll, Show as _, ShowContainer as _, Sizing, StoryLayout}; +use craft_ui::contract::{ + Bind, BodyCx, Entry, Gap, Lazy, MemoProps, MemoView, Outbox, Pad, Props as _, Scroll, Show as _, ShowContainer as _, Sizing, StoryLayout, + Tracked, +}; use craft_ui::display::{Shade, Text}; use craft_ui::layout_components::{Grid, Row, Stack}; use craft_ui::notices::Notice; @@ -125,18 +130,29 @@ pub(super) struct Reported { pub events: CardEvents, } -/// The index in `catalog` of the story the component registered as `type_name` shows: the one picked for it, else its -/// first. -pub(super) fn target_story(p: &Panels<'_>, type_name: &'static str) -> Option { - p.catalog.pick(type_name, p.story_of.get(type_name).copied()) -} - -/// The selected component's registered name. -pub(super) fn selected_type(p: &Panels<'_>) -> Option<&'static str> { - p.selection.items.iter().rev().find_map(|key| match key { - TreeKey::Component(type_name) => Some(*type_name), - _ => None, - }) +/// What a centre tab's view reads and edits besides its own values: the browser's state its cards show and change, and +/// where its requests go. +#[derive(MemoProps)] +#[non_exhaustive] +pub struct Centre<'a> { + /// What is listed. + pub catalog: &'a Catalog, + /// The selected row of the tree; a card press selects its component. + pub selection: &'a mut Tracked>, + /// The story each component's card shows. + pub story_of: &'a mut Tracked, + /// The shown stories' states. + pub cells: &'a mut Cells, + /// The reset count the cards' id scopes carry. + pub generation: u32, + /// How the tab's selected card is inspected, while the inspector is on and the tab is in front. + pub inspected: Option, + /// The Variants view open in this tab. + pub variants: Option, + /// The components whose tab a card asked to open. + pub opened: Outbox<'a, &'static str>, + /// What the Variants view asked for. + pub variants_out: Outbox<'a, VariantsEvent>, } /// The window's background (an embedded shell, which paints it and keeps its cards' dialogs in the tab) with the @@ -151,21 +167,23 @@ pub(super) fn on_background(ui: &mut Ui, key: &str, mut add: impl FnMut(&mut Ui, /// The cards in one or two columns, scrolling vertically; each row is as tall as its tallest card. A row out of view /// does not run (`Lazy`). Each card and each story in it is a memoized view that replays its last run until its inputs -/// change (the story's data by its revision, the card's look and state, the room and place). The card of the component `reveal` names glides to the top of the view. The selected card of `tab` is -/// inspected while the inspector is on. The cards report into `reported`. +/// change (the story's data by its revision, the card's look and state, the room and place). The card of the component +/// `reveal` names glides to the top of the view. The selected card is inspected as `inspected` says. The cards report +/// into `reported`. +#[expect(clippy::too_many_arguments, reason = "the grid's own values beside the shared cells; a struct would only rename them")] pub(super) fn grid( ui: &mut Ui, scroll_id: impl std::hash::Hash + std::fmt::Debug, - tab: Option, + inspected: Option<&Inspected>, cards: &[Spec], - p: &mut Panels<'_>, + cells: &mut Cells, + generation: u32, one_column: bool, reveal: Option<&'static str>, ) -> Vec { let mut reported = Vec::new(); let tokens = Env::of(ui).tokens.clone(); let columns = if one_column || ui.available_width() < 2.0 * card_min_width(&tokens) { 1 } else { 2 }; - let generation = p.generation; // A story that fills takes its content height, at most the view's, as on the old gallery's tiles. let room = ui.available_height(); Stack::new().fill().scroll(Scroll::vertical()).gap(Gap::CardRow).id_salt(scroll_id).show(ui, |ui, _| { @@ -183,8 +201,8 @@ pub(super) fn grid( Lazy::new((generation, spec.key.as_str()), spec.estimate(&tokens)).show(ui, |ui| { let stories = spec.card_stories(); let mut card = spec.card(&stories, format!("{}-{generation}", spec.key), room); - card.inspect = p.inspect.of(tab, spec.selected); - let events = card.show(ui, p.cells); + card.inspect = inspected.filter(|_| spec.selected).cloned(); + let events = card.show(ui, cells); reported.push(Reported { spec: row * columns + at, events }); }); } @@ -196,52 +214,76 @@ pub(super) fn grid( } /// Applies what the cards reported: a press selects the card's component and picks the card's first story unless the -/// picked one is on the card, Open opens its tab, Reset gives the card's stories their start values. -pub(super) fn apply(p: &mut Panels<'_>, cards: &[Spec], reported: Vec) { +/// picked one is on the card, Open asks for its tab, Reset gives the card's stories their start values. A value is +/// written only when it changes. +pub(super) fn apply(c: &mut Centre<'_>, cards: &[Spec], reported: Vec) { for Reported { spec, events } in reported { let Some(spec) = cards.get(spec) else { continue }; if events.reset { for story in &spec.stories { - p.cells.entry(story.index).or_default().reset(); + c.cells.state_of(story.index).reset(); } } if events.opened { - *p.open_request = Some(spec.type_name); + c.opened.send(spec.type_name); } if events.pressed || events.opened { - *p.selection = Selection::single(TreeKey::Component(spec.type_name)); + let single = Selection::single(TreeKey::Component(spec.type_name)); + if **c.selection != single { + **c.selection = single; + } // The picked story stays while it is one of the card's; else the card's first story is picked. - let picked = target_story(p, spec.type_name); + let picked = target_story(c.catalog, c.story_of, spec.type_name); if let Some(first) = spec.stories.first() && !spec.stories.iter().any(|story| Some(story.index) == picked) { - p.story_of.insert(spec.type_name, first.story.story.name); + c.story_of.insert(spec.type_name, first.story.story.name); } } } } -/// The selection, when it changed since the Overview's cards last followed it, and its component's level as the shown -/// one. -fn follow_selection(p: &mut Panels<'_>) -> Option> { - let selected = selected_type(p); - if *p.followed == Some(selected) { - return None; - } - if let Some(entry) = selected.and_then(entry_of) { - *p.level = entry.level.title().to_owned(); - } - Some(selected) +/// What the Overview reads and edits. +#[derive(MemoProps)] +struct OverviewProps<'a> { + centre: Centre<'a>, + /// The text that narrows the levels' components. + filter: &'a Tracked, + /// The level shown, by its title. + level: &'a mut Tracked, + /// The selection the cards last followed. + followed: &'a mut Tracked>>, } /// The Overview: the level select over the cards of the level's components. -pub(super) fn overview(ui: &mut Ui, p: &mut Panels<'_>) { - if variants::show(ui, p, PanelId::Overview) { +pub(super) fn overview( + ui: &mut Ui, + centre: Centre<'_>, + filter: &Tracked, + level: &mut Tracked, + followed: &mut Tracked>>, +) { + let props = OverviewProps { centre, filter, level, followed }; + let _ = MemoView::new(view_id(&PanelId::Overview), props, overview_view).show(ui); +} + +fn overview_view(p: &mut OverviewProps<'_>, ui: &mut Ui) { + let OverviewProps { centre, filter, level: shown_level, followed } = p; + let (shown_level, followed) = (&mut **shown_level, &mut **followed); + if variants::show(ui, centre) { return; } - let follow = follow_selection(p); - let groups = by_level(p.catalog, p.filter); - let Some(shown) = groups.iter().position(|(level, _)| level.title() == p.level.as_str()).or(if groups.is_empty() { None } else { Some(0) }) + // The selection, when it changed since the cards last followed it; its component's level becomes the shown one. + let selected = selected_of(centre.selection); + let follow = (**followed != Some(selected)).then_some(selected); + if follow.is_some() + && let Some(entry) = selected.and_then(entry_of) + && shown_level.as_str() != entry.level.title() + { + **shown_level = entry.level.title().to_owned(); + } + let groups = by_level(centre.catalog, filter.as_str()); + let Some(shown) = groups.iter().position(|(level, _)| level.title() == shown_level.as_str()).or(if groups.is_empty() { None } else { Some(0) }) else { on_background(ui, "overview", |ui, _| { Notice::new(Tone::Info, "No components match the filter.").show(ui); @@ -250,27 +292,27 @@ pub(super) fn overview(ui: &mut Ui, p: &mut Panels<'_>) { }; let level = groups[shown].0; let entries: Vec> = groups.iter().map(|(level, _)| Entry::new(*level, level.title())).collect(); - let selected = selected_type(p); - let cards: Vec = groups[shown].1.iter().filter_map(|component| component_card(p, component, selected)).collect(); + let cards: Vec = groups[shown].1.iter().filter_map(|component| component_card(centre.catalog, centre.story_of, component, selected)).collect(); let mut chosen = level; let mut reported = Vec::new(); // The cards follow the selection in the pass they run: its card glides into view, and only then is it followed. let mut ran = false; + let (inspected, generation) = (centre.inspected.clone(), centre.generation); on_background(ui, "overview", |ui, _| { ran = true; Row::new().show(ui, |ui, _| { Text::new("Level:").shade(Shade::Muted).show(ui); Select::new("Level", Bind::bound(&mut chosen), &entries).sizing(Sizing::Content).show(ui); }); - reported = grid(ui, level.title(), Some(PanelId::Overview), &cards, p, false, follow.flatten()); + reported = grid(ui, level.title(), inspected.as_ref(), &cards, centre.cells, generation, false, follow.flatten()); }); if ran && let Some(selected) = follow { - *p.followed = Some(selected); + **followed = Some(selected); } if chosen != level { - *p.level = chosen.title().to_owned(); + **shown_level = chosen.title().to_owned(); } - apply(p, &cards, reported); + apply(centre, &cards, reported); } /// The stories of group `group` of the component registered as `type_name`, under their captions. @@ -279,14 +321,14 @@ fn group_stories(catalog: &Catalog, type_name: &'static str, group: usize, name: } /// The group the component registered as `type_name` shows: the group of its picked story, else its first. -fn target_group(p: &Panels<'_>, type_name: &'static str) -> usize { - target_story(p, type_name).and_then(|at| p.catalog.group_at(type_name, at)).unwrap_or(0) +fn target_group(catalog: &Catalog, story_of: &StoryOf, type_name: &'static str) -> usize { + target_story(catalog, story_of, type_name).and_then(|at| catalog.group_at(type_name, at)).unwrap_or(0) } /// The Overview's card of `component`: the cases of the group of its picked story; selected when it is the selected /// component. -fn component_card(p: &Panels<'_>, component: &ComponentInfo, selected: Option<&'static str>) -> Option { - let mut spec = overview_card(p.catalog, component, target_group(p, component.type_name))?; +fn component_card(catalog: &Catalog, story_of: &StoryOf, component: &ComponentInfo, selected: Option<&'static str>) -> Option { + let mut spec = overview_card(catalog, component, target_group(catalog, story_of, component.type_name))?; spec.selected = selected == Some(component.type_name); Some(spec) } @@ -316,19 +358,40 @@ pub fn overview_card(catalog: &Catalog, component: &ComponentInfo, group: usize) }) } +/// What a component's tab reads and edits. +#[derive(MemoProps)] +struct ComponentTabProps<'a> { + centre: Centre<'a>, + /// The tab. + tab: &'a PanelId, + /// The registered name of the tab's component, when the catalog lists it. + type_name: Option<&'static str>, +} + /// A component's tab: each story group of the component as a card in one column, titled with the group's name. -pub(super) fn component_tab(ui: &mut Ui, p: &mut Panels<'_>, type_name: &'static str) { - if variants::show(ui, p, PanelId::Component(type_name)) { +pub(super) fn component_tab(ui: &mut Ui, centre: Centre<'_>, tab: &PanelId) { + let type_name = match tab { + PanelId::Component(name) => centre.catalog.component_named(name), + _ => None, + }; + let props = ComponentTabProps { centre, tab, type_name }; + let _ = MemoView::new(view_id(tab), props, component_tab_view).show(ui); +} + +fn component_tab_view(p: &mut ComponentTabProps<'_>, ui: &mut Ui) { + let ComponentTabProps { centre, type_name, .. } = p; + if variants::show(ui, centre) { return; } - let (Some(entry), Some(component)) = (entry_of(type_name), p.catalog.component(type_name)) else { + let found = type_name.and_then(|type_name| Some((type_name, entry_of(type_name)?, centre.catalog.component(type_name)?))); + let Some((type_name, entry, component)) = found else { on_background(ui, "component", |ui, _| { Notice::new(Tone::Info, "The component has no stories.").show(ui); }); return; }; - let target = target_group(p, type_name); - let chosen = selected_type(p) == Some(type_name); + let target = target_group(centre.catalog, centre.story_of, type_name); + let chosen = selected_of(centre.selection) == Some(type_name); let cards: Vec = component .groups .iter() @@ -338,7 +401,7 @@ pub(super) fn component_tab(ui: &mut Ui, p: &mut Panels<'_>, type_name: &'static type_name, title: group.name.clone(), note: None, - stories: group_stories(p.catalog, type_name, at, &entry.name), + stories: group_stories(centre.catalog, type_name, at, &entry.name), layout: entry.story_layout(&group.name), surface: entry.surface, open: false, @@ -349,6 +412,9 @@ pub(super) fn component_tab(ui: &mut Ui, p: &mut Panels<'_>, type_name: &'static }) .collect(); let mut reported = Vec::new(); - on_background(ui, "component", |ui, _| reported = grid(ui, ("component", type_name), Some(PanelId::Component(type_name)), &cards, p, true, None)); - apply(p, &cards, reported); + let (inspected, generation) = (centre.inspected.clone(), centre.generation); + on_background(ui, "component", |ui, _| { + reported = grid(ui, ("component", type_name), inspected.as_ref(), &cards, centre.cells, generation, true, None); + }); + apply(centre, &cards, reported); } diff --git a/crates/ui-gallery/src/chrome/panels.rs b/crates/ui-gallery/src/chrome/panels.rs index 744ddd924..e94377ca5 100644 --- a/crates/ui-gallery/src/chrome/panels.rs +++ b/crates/ui-gallery/src/chrome/panels.rs @@ -1,17 +1,22 @@ //! The bodies of the viewer's panels: the component tree, the target component's stories and facts, the inspector, //! and the theme workspace's tokens, problems, detail and app sizes. The dock draws each panel's tab and frame. +//! +//! The tree, the stories and the properties are memoized views: each reads only its props, so it runs when a value it +//! reads changes and otherwise adds its last run again. The theme panels run every pass. use super::describe; -use super::inspect::{self, Inspect}; -use super::overview; -use crate::viewer::{self, CardState, Catalog, Cells, ComponentInfo, PanelId, TreeKey, TreeRow, TreeRows}; +use crate::viewer::{self, Browser, CardState, Catalog, ComponentInfo, PanelId, TreeKey, TreeRow, TreeRows}; use craft_behavior::IconId; use craft_behavior::{Selection, keyed}; use craft_theme::Explained; use craft_theme::report::ContrastReport; use craft_theme_editor::{AppLayerEditor, Problems, ThemeSubject, ThemeWorkspace, TokenBrowser, TokenDetail}; + use craft_ui::buttons::IconButton; -use craft_ui::contract::{Bind, CollectionEvent, Props as _, RowAlign, Scroll, SelectMode, Show as _, ShowCollection as _, ShowContainer as _}; +use craft_ui::contract::{ + Bind, CollectionEvent, MemoProps, MemoView, Outbox, Props as _, RowAlign, Scroll, SelectMode, Show as _, ShowCollection as _, ShowContainer as _, + Tracked, bind_tracked, +}; use craft_ui::display::{Shade, Text}; use craft_ui::layout_components::{Row, Stack}; use craft_ui::list::{List, ListRow, OpenSet, RowEvent, TrailingFace, TrailingPart}; @@ -24,45 +29,26 @@ use egui::Ui; use std::collections::HashMap; use std::sync::Arc; -/// What the panels read and edit in one pass. -#[non_exhaustive] -pub struct Panels<'a> { - /// What is listed. - pub catalog: &'a Catalog, - /// The text that narrows the tree. - pub filter: &'a mut String, - /// The selected row of the tree. - pub selection: &'a mut Selection, - /// The open level groups. - pub open: &'a mut OpenSet, - /// The story each component's card shows. - pub story_of: &'a mut HashMap<&'static str, &'static str>, - /// The shown stories' values. - pub cells: &'a mut Cells, - /// The component whose tab the tree asked to open in this pass. - pub open_request: &'a mut Option<&'static str>, - /// The selection the Overview's cards last followed. - pub followed: &'a mut Option>, - /// The selected component the tree last opened the level of. - pub tree_seen: &'a mut Option>, - /// The reset count the cards' id scopes carry. - pub generation: u32, - /// What the theme panels share. - pub themes: &'a mut ThemeWorkspace, - /// The theme and appearance the theme panels explain. - pub subject: ThemeSubject<'a>, - /// The level the Overview shows, by its title. - pub level: &'a mut String, - /// The Variants view of the target story, while one is open. - pub variants: &'a mut Option, - /// The centre tab the Variants view is open in. - pub variants_tab: &'a mut Option, - /// The root values; a Variants card that is used sets its value. - pub env: &'a mut craft_ui::runtime::env::RootEnv, - /// The row the Problems view picked in this pass, which the token browser brings into view. - pub revealed: Option, - /// What the inspector shows. - pub inspect: Inspect<'a>, +/// The story each component's card shows, by the component's registered name and the story's name. +pub(super) type StoryOf = HashMap<&'static str, &'static str>; + +/// The registered name of the component `selection` selects. +pub(super) fn selected_of(selection: &Selection) -> Option<&'static str> { + selection.items.iter().rev().find_map(|key| match key { + TreeKey::Component(type_name) => Some(*type_name), + _ => None, + }) +} + +/// The index in `catalog` of the story the component registered as `type_name` shows: the one picked for it, else its +/// first. +pub(super) fn target_story(catalog: &Catalog, story_of: &StoryOf, type_name: &str) -> Option { + catalog.pick(type_name, story_of.get(type_name).copied()) +} + +/// The id of the memoized view of `panel`'s body. +pub(super) fn view_id(panel: &PanelId) -> (&'static str, &PanelId) { + ("gallery.panel", panel) } /// `add` filling the panel's body and scrolling vertically; the dock group insets the body. The body may run more than @@ -88,11 +74,6 @@ fn fact(ui: &mut Ui, name: &'static str, value: impl Into