mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
craft-ui-gallery: the viewer's panels are memoized views over tracked browser state; tab names are owned and resolved through the catalog
The tree, stories, properties, inspector, Overview, component tabs and the Variants view each read only their props. The browser's panel state is Tracked and written only on a change; tab opens and Variants requests leave through outboxes. The shell places the Variants view from the dock layout. PanelId::Component holds an owned TabName; a saved layout drops the tabs the catalog does not list, which replaces the interned static names.
This commit is contained in:
@@ -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<usize> = 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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<InspectLayers>,
|
||||
/// 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<VariantsEvent>,
|
||||
}
|
||||
|
||||
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<PanelId> for GalleryDock<'_> {
|
||||
@@ -48,7 +87,12 @@ impl DockPanels<PanelId> 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),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<PanelId>,
|
||||
/// 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<PanelId>, selected: bool) -> Option<Inspected> {
|
||||
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<Inspected> {
|
||||
(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<usize>,
|
||||
inspect: &'a Inspect,
|
||||
layers: &'a mut Tracked<InspectLayers>,
|
||||
}
|
||||
|
||||
/// 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<InspectLayers>) {
|
||||
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::<Vec<_>>()
|
||||
})
|
||||
.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::<Vec<_>>())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// One row per element of `recorded` with a node or a focus stop, indented by the shown elements it sits in; an element
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<usize> {
|
||||
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<Selection<TreeKey>>,
|
||||
/// The story each component's card shows.
|
||||
pub story_of: &'a mut Tracked<StoryOf>,
|
||||
/// 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<Inspected>,
|
||||
/// The Variants view open in this tab.
|
||||
pub variants: Option<VariantsIn>,
|
||||
/// 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<PanelId>,
|
||||
inspected: Option<&Inspected>,
|
||||
cards: &[Spec],
|
||||
p: &mut Panels<'_>,
|
||||
cells: &mut Cells,
|
||||
generation: u32,
|
||||
one_column: bool,
|
||||
reveal: Option<&'static str>,
|
||||
) -> Vec<Reported> {
|
||||
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<Reported>) {
|
||||
/// 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<Reported>) {
|
||||
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<Option<&'static str>> {
|
||||
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<String>,
|
||||
/// The level shown, by its title.
|
||||
level: &'a mut Tracked<String>,
|
||||
/// The selection the cards last followed.
|
||||
followed: &'a mut Tracked<Option<Option<&'static str>>>,
|
||||
}
|
||||
|
||||
/// 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<String>,
|
||||
level: &mut Tracked<String>,
|
||||
followed: &mut Tracked<Option<Option<&'static str>>>,
|
||||
) {
|
||||
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<Entry<craft_ui::contract::Level>> = groups.iter().map(|(level, _)| Entry::new(*level, level.title())).collect();
|
||||
let selected = selected_type(p);
|
||||
let cards: Vec<Spec> = groups[shown].1.iter().filter_map(|component| component_card(p, component, selected)).collect();
|
||||
let cards: Vec<Spec> = 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<Spec> {
|
||||
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<Spec> {
|
||||
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<Spec> = 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);
|
||||
}
|
||||
|
||||
@@ -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<TreeKey>,
|
||||
/// The open level groups.
|
||||
pub open: &'a mut OpenSet<TreeKey>,
|
||||
/// 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<Option<&'static str>>,
|
||||
/// The selected component the tree last opened the level of.
|
||||
pub tree_seen: &'a mut Option<Option<&'static str>>,
|
||||
/// 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<crate::viewer::VariantAxis>,
|
||||
/// The centre tab the Variants view is open in.
|
||||
pub variants_tab: &'a mut Option<PanelId>,
|
||||
/// 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<craft_theme_editor::Pick>,
|
||||
/// 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<TreeKey>) -> 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<usize> {
|
||||
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<Label>) {
|
||||
});
|
||||
}
|
||||
|
||||
/// The values of the story at catalog index `index`: the one place the panels reach into the shown cells.
|
||||
fn state_of(cells: &mut Cells, index: usize) -> &mut CardState {
|
||||
cells.entry(index).or_default()
|
||||
}
|
||||
|
||||
/// The declared facts of the component registered as `type_name`, the Inspect panel's last section: its decision
|
||||
/// record, then where it is declared and what it is for.
|
||||
pub(super) fn declared(ui: &mut Ui, catalog: &Catalog, type_name: Option<&'static str>) {
|
||||
@@ -121,156 +102,201 @@ pub(super) fn declared(ui: &mut Ui, catalog: &Catalog, type_name: Option<&'stati
|
||||
});
|
||||
}
|
||||
|
||||
impl Panels<'_> {
|
||||
/// Shows the body of `panel`.
|
||||
pub fn show(&mut self, ui: &mut Ui, panel: PanelId) {
|
||||
/// What the component tree reads and edits.
|
||||
#[derive(MemoProps)]
|
||||
struct ComponentsProps<'a> {
|
||||
catalog: &'a Catalog,
|
||||
filter: &'a mut Tracked<String>,
|
||||
selection: &'a mut Tracked<Selection<TreeKey>>,
|
||||
open: &'a mut Tracked<OpenSet<TreeKey>>,
|
||||
/// The selected component the tree last opened the level of.
|
||||
seen: &'a mut Tracked<Option<Option<&'static str>>>,
|
||||
/// The components whose tab a row asked to open.
|
||||
opened: Outbox<'a, &'static str>,
|
||||
}
|
||||
|
||||
/// The component tree: the filter over the levels and their components. Opening a row asks for the component's tab
|
||||
/// through `opened`.
|
||||
pub(super) fn components(ui: &mut Ui, browser: &mut Browser, opened: &mut Vec<&'static str>) {
|
||||
let Browser { catalog, filter, selection, open, tree_seen, .. } = browser;
|
||||
let props = ComponentsProps { catalog, filter, selection, open, seen: tree_seen, opened: Outbox::new(opened) };
|
||||
let _ = MemoView::new(view_id(&PanelId::Components), props, components_view).show(ui);
|
||||
}
|
||||
|
||||
fn components_view(props: &mut ComponentsProps<'_>, ui: &mut Ui) {
|
||||
let ComponentsProps { catalog, filter, selection, open, seen, opened } = props;
|
||||
let before = selected_of(selection);
|
||||
viewer::open_level_of_selection(open, &catalog.components, before, seen);
|
||||
let shown = viewer::rows(&catalog.components, filter.as_str(), open);
|
||||
let groups = viewer::groups(&shown);
|
||||
let by_key: HashMap<&TreeKey, &TreeRow> = shown.iter().map(|row| (&row.key, row)).collect();
|
||||
let mut events = Vec::new();
|
||||
Stack::new().fill().show(ui, |ui, _| {
|
||||
TextField::search("Filter components", bind_tracked!(filter)).placeholder("Filter components").show(ui);
|
||||
let listed = List::<TreeKey>::new("Components")
|
||||
.select(SelectMode::Single)
|
||||
.type_ahead(true)
|
||||
.tree(true)
|
||||
.selection(bind_tracked!(selection))
|
||||
.empty("No component matches the filter")
|
||||
.show_items(ui, &TreeRows::new(&shown), &mut |key, cx| {
|
||||
let Some(row) = by_key.get(key) else { return ListRow::spacer() };
|
||||
let item = ListRow::new(row.label.clone()).depth(row.depth);
|
||||
if row.group {
|
||||
return item.group(row.open);
|
||||
}
|
||||
// The Open icon shows on the selected row and the row under the pointer.
|
||||
if cx.row.selected || cx.row.hovered {
|
||||
item.trailing_part(OPEN, TrailingFace::Icon(IconId::new("open").into()), format!("Open {}", row.label))
|
||||
} else {
|
||||
item
|
||||
}
|
||||
});
|
||||
events = listed.events;
|
||||
});
|
||||
for event in events {
|
||||
match event {
|
||||
CollectionEvent::Open(change) => open.apply(&change.key, change.open, change.all, groups.iter()),
|
||||
CollectionEvent::Activate(TreeKey::Component(type_name)) | CollectionEvent::Item(TreeKey::Component(type_name), RowEvent::Trailing { .. }) => {
|
||||
opened.send(type_name);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
// Selecting a component opens its level.
|
||||
let now = selected_of(selection);
|
||||
if now != before
|
||||
&& let Some(component) = now.and_then(|type_name| catalog.component(type_name))
|
||||
{
|
||||
viewer::open_level(open, component.level);
|
||||
}
|
||||
}
|
||||
|
||||
/// What the story list reads and edits.
|
||||
#[derive(MemoProps)]
|
||||
struct StoriesProps<'a> {
|
||||
catalog: &'a Catalog,
|
||||
selection: &'a Tracked<Selection<TreeKey>>,
|
||||
story_of: &'a mut Tracked<StoryOf>,
|
||||
}
|
||||
|
||||
/// The story groups of the selected component; picking one shows it on the component's Overview card.
|
||||
pub(super) fn stories(ui: &mut Ui, browser: &mut Browser) {
|
||||
let Browser { catalog, selection, story_of, .. } = browser;
|
||||
let props = StoriesProps { catalog, selection, story_of };
|
||||
let _ = MemoView::new(view_id(&PanelId::Stories), props, stories_view).show(ui);
|
||||
}
|
||||
|
||||
fn stories_view(props: &mut StoriesProps<'_>, ui: &mut Ui) {
|
||||
let StoriesProps { catalog, selection, story_of } = props;
|
||||
let Some(component) = selected_of(selection).and_then(|type_name| catalog.component(type_name)) else {
|
||||
return scrolled(ui, |ui| {
|
||||
hint(ui, "Select a component");
|
||||
});
|
||||
};
|
||||
let (type_name, names) = (component.type_name, component.groups.iter().map(|group| group.name.clone()).collect::<Vec<_>>());
|
||||
let listed: Vec<usize> = (0..names.len()).collect();
|
||||
let shown = target_story(catalog, story_of, type_name).and_then(|at| catalog.group_at(type_name, at));
|
||||
let mut picked = Selection::default();
|
||||
picked.items.extend(shown);
|
||||
let before = picked.items.clone();
|
||||
Stack::new().fill().show(ui, |ui, _| {
|
||||
let source = keyed(&listed, |at| *at);
|
||||
List::<usize>::new("Stories")
|
||||
.select(SelectMode::Single)
|
||||
.selection(Bind::bound(&mut picked))
|
||||
.show_items(ui, &source, &mut |at, _| ListRow::new(names.get(*at).cloned().unwrap_or_default()));
|
||||
});
|
||||
if picked.items != before
|
||||
&& let Some(&group) = picked.items.first()
|
||||
&& let Some(&first) = catalog.group_stories(type_name, group).first()
|
||||
{
|
||||
let story = catalog.entries[first].registration.story.name;
|
||||
if story_of.get(type_name) != Some(&story) {
|
||||
story_of.insert(type_name, story);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What the Properties panel reads and edits: the target story and its state.
|
||||
#[derive(MemoProps)]
|
||||
struct PropertiesProps<'a> {
|
||||
catalog: &'a Catalog,
|
||||
/// The selected component and the catalog index of the story it shows.
|
||||
target: Option<(&'static str, usize)>,
|
||||
/// That story's state, keyed by its data's revision.
|
||||
state: Option<&'a mut CardState>,
|
||||
}
|
||||
|
||||
/// The target story's name and its values' editors, with Reset args once a value changed.
|
||||
pub(super) fn properties(ui: &mut Ui, browser: &mut Browser) {
|
||||
let target = browser.selected_type().zip(browser.target_story());
|
||||
let Browser { catalog, cells, .. } = browser;
|
||||
let state = target.map(|(_, index)| cells.state_of(index));
|
||||
let props = PropertiesProps { catalog, target, state };
|
||||
let _ = MemoView::new(view_id(&PanelId::Properties), props, properties_view).show(ui);
|
||||
}
|
||||
|
||||
/// The display title of the story at catalog index `at` of `component`.
|
||||
fn title(catalog: &Catalog, component: &ComponentInfo, at: usize) -> String {
|
||||
let place = catalog.stories_of(component.type_name).iter().position(|&story| story == at);
|
||||
place.and_then(|place| component.titles.get(place)).cloned().unwrap_or_default()
|
||||
}
|
||||
|
||||
fn properties_view(props: &mut PropertiesProps<'_>, ui: &mut Ui) {
|
||||
let PropertiesProps { catalog, target, state } = props;
|
||||
let component = target.and_then(|(type_name, _)| catalog.component(type_name));
|
||||
let (Some(component), Some((_, index)), Some(state)) = (component, *target, state.as_deref_mut()) else {
|
||||
return scrolled(ui, |ui| {
|
||||
hint(ui, "Select a component");
|
||||
});
|
||||
};
|
||||
let title = title(catalog, component, index);
|
||||
// The values exist once the story has run; the first pass that finds them records the start.
|
||||
if !state.data.changed_since(&StoryData::default().snapshot()).is_empty() {
|
||||
state.note_start();
|
||||
}
|
||||
let changed = state.changed();
|
||||
let mut reset = false;
|
||||
scrolled(ui, |ui| {
|
||||
Row::new().fill_width().align(RowAlign::SpaceBetween).show(ui, |ui, _| {
|
||||
Row::new().show(ui, |ui, _| {
|
||||
Text::new(component.name.clone()).role(TypeRole::Heading).show(ui);
|
||||
Text::new(title.clone()).shade(Shade::Muted).show(ui);
|
||||
});
|
||||
if changed {
|
||||
reset = IconButton::new(IconId::new("undo")).label("Reset args").show(ui).clicks > 0;
|
||||
}
|
||||
});
|
||||
story_props(ui, &mut state.data);
|
||||
});
|
||||
// An edit in the editors moves the revision the story's view keys on.
|
||||
state.data.settle();
|
||||
if reset {
|
||||
state.reset();
|
||||
}
|
||||
}
|
||||
|
||||
/// The theme workspace's panels: what they read and edit in one pass. They run every pass.
|
||||
#[non_exhaustive]
|
||||
pub struct ThemePanels<'a> {
|
||||
/// What the theme panels share.
|
||||
pub themes: &'a mut ThemeWorkspace,
|
||||
/// The theme and appearance the theme panels explain.
|
||||
pub subject: ThemeSubject<'a>,
|
||||
/// The row the Problems view picked in this pass, which the token browser brings into view.
|
||||
pub revealed: Option<craft_theme_editor::Pick>,
|
||||
}
|
||||
|
||||
impl ThemePanels<'_> {
|
||||
/// Shows the body of the theme panel `panel`; other panels show nothing.
|
||||
pub fn show(&mut self, ui: &mut Ui, panel: &PanelId) {
|
||||
match panel {
|
||||
PanelId::Components => self.components(ui),
|
||||
PanelId::Stories => self.stories(ui),
|
||||
PanelId::Properties => self.properties(ui),
|
||||
PanelId::Inspect => inspect::panel(ui, self),
|
||||
PanelId::Overview => overview::overview(ui, self),
|
||||
PanelId::Component(type_name) => overview::component_tab(ui, self, type_name),
|
||||
PanelId::Preview => {}
|
||||
PanelId::Problems => self.problems(ui),
|
||||
PanelId::Tokens => self.tokens(ui),
|
||||
PanelId::Detail => self.detail(ui),
|
||||
PanelId::AppSizes => self.overrides(ui),
|
||||
}
|
||||
}
|
||||
|
||||
/// The selected component.
|
||||
fn selected(&self) -> Option<&ComponentInfo> {
|
||||
let type_name = self.selection.items.iter().rev().find_map(|key| match key {
|
||||
TreeKey::Component(type_name) => Some(*type_name),
|
||||
_ => None,
|
||||
})?;
|
||||
self.catalog.component(type_name)
|
||||
}
|
||||
|
||||
fn components(&mut self, ui: &mut Ui) {
|
||||
let selected = self.selected().map(|component| component.type_name);
|
||||
viewer::open_level_of_selection(self.open, &self.catalog.components, selected, &mut *self.tree_seen);
|
||||
let shown = viewer::rows(&self.catalog.components, self.filter.as_str(), &*self.open);
|
||||
let groups = viewer::groups(&shown);
|
||||
let by_key: HashMap<&TreeKey, &TreeRow> = shown.iter().map(|row| (&row.key, row)).collect();
|
||||
let before = self.selected().map(|component| component.type_name);
|
||||
let mut opened = None;
|
||||
let (filter, selection, open) = (&mut *self.filter, &mut *self.selection, &mut *self.open);
|
||||
Stack::new().fill().show(ui, |ui, _| {
|
||||
TextField::search("Filter components", Bind::bound(&mut *filter)).placeholder("Filter components").show(ui);
|
||||
let listed = List::<TreeKey>::new("Components")
|
||||
.select(SelectMode::Single)
|
||||
.type_ahead(true)
|
||||
.tree(true)
|
||||
.selection(Bind::bound(&mut *selection))
|
||||
.empty("No component matches the filter")
|
||||
.show_items(ui, &TreeRows::new(&shown), &mut |key, cx| {
|
||||
let Some(row) = by_key.get(key) else { return ListRow::spacer() };
|
||||
let item = ListRow::new(row.label.clone()).depth(row.depth);
|
||||
if row.group {
|
||||
return item.group(row.open);
|
||||
}
|
||||
// The Open icon shows on the selected row and the row under the pointer.
|
||||
if cx.row.selected || cx.row.hovered {
|
||||
item.trailing_part(OPEN, TrailingFace::Icon(IconId::new("open").into()), format!("Open {}", row.label))
|
||||
} else {
|
||||
item
|
||||
}
|
||||
});
|
||||
for event in listed.events {
|
||||
match event {
|
||||
CollectionEvent::Open(change) => open.apply(&change.key, change.open, change.all, groups.iter()),
|
||||
CollectionEvent::Activate(TreeKey::Component(type_name)) => opened = Some(type_name),
|
||||
CollectionEvent::Item(TreeKey::Component(type_name), RowEvent::Trailing { .. }) => opened = Some(type_name),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
});
|
||||
if let Some(type_name) = opened {
|
||||
*self.open_request = Some(type_name);
|
||||
}
|
||||
// Selecting a component opens its level.
|
||||
let now = self.selected().map(|component| (component.type_name, component.level));
|
||||
if let Some((type_name, level)) = now
|
||||
&& Some(type_name) != before
|
||||
{
|
||||
self.open.set(&TreeKey::Level(level), true);
|
||||
}
|
||||
}
|
||||
|
||||
/// The story groups of the selected component; picking one shows it on the component's Overview card.
|
||||
fn stories(&mut self, ui: &mut Ui) {
|
||||
let catalog = self.catalog;
|
||||
let Some(component) = self.selected() else {
|
||||
return scrolled(ui, |ui| {
|
||||
hint(ui, "Select a component");
|
||||
});
|
||||
};
|
||||
let (type_name, names) = (component.type_name, component.groups.iter().map(|group| group.name.clone()).collect::<Vec<_>>());
|
||||
let listed: Vec<usize> = (0..names.len()).collect();
|
||||
let shown = self.target(type_name).and_then(|at| catalog.group_at(type_name, at));
|
||||
let mut selection = Selection::default();
|
||||
selection.items.extend(shown);
|
||||
let before = selection.items.clone();
|
||||
Stack::new().fill().show(ui, |ui, _| {
|
||||
let source = keyed(&listed, |at| *at);
|
||||
List::<usize>::new("Stories")
|
||||
.select(SelectMode::Single)
|
||||
.selection(Bind::bound(&mut selection))
|
||||
.show_items(ui, &source, &mut |at, _| ListRow::new(names.get(*at).cloned().unwrap_or_default()));
|
||||
});
|
||||
if selection.items != before
|
||||
&& let Some(&picked) = selection.items.first()
|
||||
&& let Some(&first) = catalog.group_stories(type_name, picked).first()
|
||||
{
|
||||
self.story_of.insert(type_name, catalog.entries[first].registration.story.name);
|
||||
}
|
||||
}
|
||||
|
||||
/// The index of the story the component registered as `type_name` shows: the one picked for it, else its first.
|
||||
fn target(&self, type_name: &'static str) -> Option<usize> {
|
||||
self.catalog.pick(type_name, self.story_of.get(type_name).copied())
|
||||
}
|
||||
|
||||
/// The display title of the story at catalog index `at` of `component`.
|
||||
fn title(&self, component: &ComponentInfo, at: usize) -> String {
|
||||
let place = self.catalog.stories_of(component.type_name).iter().position(|&story| story == at);
|
||||
place.and_then(|place| component.titles.get(place)).cloned().unwrap_or_default()
|
||||
}
|
||||
|
||||
fn properties(&mut self, ui: &mut Ui) {
|
||||
let component = self.selected().cloned();
|
||||
let target = component.as_ref().and_then(|component| self.target(component.type_name));
|
||||
let (Some(component), Some(index)) = (component, target) else {
|
||||
return scrolled(ui, |ui| {
|
||||
hint(ui, "Select a component");
|
||||
});
|
||||
};
|
||||
let title = self.title(&component, index);
|
||||
let state = state_of(self.cells, index);
|
||||
// The values exist once the story has run; the first pass that finds them records the start.
|
||||
if !state.data.changed_since(&StoryData::default().snapshot()).is_empty() {
|
||||
state.note_start();
|
||||
}
|
||||
let changed = state.changed();
|
||||
let mut reset = false;
|
||||
scrolled(ui, |ui| {
|
||||
Row::new().fill_width().align(RowAlign::SpaceBetween).show(ui, |ui, _| {
|
||||
Row::new().show(ui, |ui, _| {
|
||||
Text::new(component.name.clone()).role(TypeRole::Heading).show(ui);
|
||||
Text::new(title.clone()).shade(Shade::Muted).show(ui);
|
||||
});
|
||||
if changed {
|
||||
reset = IconButton::new(IconId::new("undo")).label("Reset args").show(ui).clicks > 0;
|
||||
}
|
||||
});
|
||||
story_props(ui, &mut state.data);
|
||||
});
|
||||
// An edit in the editors moves the revision the story's view keys on.
|
||||
state.data.settle();
|
||||
if reset {
|
||||
state.reset();
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
//! The viewer's window: the shell with the options bar over the dock of the shown workspace, and the settings it keeps.
|
||||
|
||||
use super::dock::GalleryDock;
|
||||
use super::card::Look;
|
||||
use super::inspect::Inspect;
|
||||
use super::options::options_bar;
|
||||
use super::panels::Panels;
|
||||
use super::panels::ThemePanels;
|
||||
use super::persist::Persisted;
|
||||
use crate::viewer::{Browser, Catalog, GalleryCommands, GallerySettings, PanelId, TreeKey, Viewer, Workspace, theme_of};
|
||||
use super::variants::{VariantsEvent, VariantsIn};
|
||||
use crate::viewer::{Catalog, GalleryCommands, GallerySettings, PanelId, Viewer, Workspace, theme_of};
|
||||
use craft_behavior::dock::{PanelState, Workspace as Dock};
|
||||
use craft_theme::AppSizes;
|
||||
use craft_theme_editor::{ThemeSubject, parse_sizes};
|
||||
@@ -77,65 +79,74 @@ fn read_dropped(file: &OsFile) -> Result<AppSizes, String> {
|
||||
|
||||
/// The registered name of the component whose tab is in front and selected in the shown workspace's dock.
|
||||
fn front_component(viewer: &Viewer) -> Option<&'static str> {
|
||||
let dock = if viewer.workspace == Workspace::Theme { &viewer.theme_dock } else { &viewer.browser.dock };
|
||||
viewer
|
||||
.browser
|
||||
.selection
|
||||
.items
|
||||
.iter()
|
||||
.rev()
|
||||
.find_map(|key| match key {
|
||||
TreeKey::Component(type_name) => Some(*type_name),
|
||||
_ => None,
|
||||
})
|
||||
.filter(|type_name| dock.layout.panel_state(&PanelId::Component(type_name)) == PanelState::Front)
|
||||
let dock = if viewer.workspace == Workspace::Theme { &viewer.theme_dock } else { &viewer.dock };
|
||||
viewer.browser.selected_type().filter(|type_name| dock.layout.panel_state(&PanelId::component(type_name)) == PanelState::Front)
|
||||
}
|
||||
|
||||
/// Gives an open Variants view its centre tab: the front centre tab of the components workspace when the view opens,
|
||||
/// and the view closes when its tab is no longer in front. The dock layout says which tab is in front, before any body
|
||||
/// runs.
|
||||
fn place_variants(viewer: &mut Viewer) {
|
||||
let front = |panel: &PanelId| matches!(panel, PanelId::Overview | PanelId::Component(_)) && viewer.dock.layout.panel_state(panel) == PanelState::Front;
|
||||
let owner = match (&viewer.variants, &viewer.variants_tab) {
|
||||
(None, _) => None,
|
||||
(Some(_), Some(tab)) if front(tab) => Some(tab.clone()),
|
||||
(Some(_), Some(_)) => None,
|
||||
(Some(_), None) => viewer.dock.layout.panels().into_iter().find(|panel| front(panel)).cloned(),
|
||||
};
|
||||
if owner.is_none() {
|
||||
viewer.variants = None;
|
||||
}
|
||||
viewer.variants_tab = owner;
|
||||
}
|
||||
|
||||
/// The dock of the shown workspace over the viewer's state.
|
||||
fn body(ui: &mut Ui, _: &mut BodyCx<'_>, viewer: &mut Viewer) {
|
||||
let selected = front_component(viewer).map(PanelId::Component);
|
||||
let Viewer { env, workspace, preview, browser, themes, theme_dock, mock, variants, variants_tab, inspect_layers, .. } = viewer;
|
||||
let subject = ThemeSubject::new(theme_of(env.theme_id()), env.appearance(ui.ctx()));
|
||||
place_variants(viewer);
|
||||
let selected = front_component(viewer).map(PanelId::component);
|
||||
let Viewer { env, workspace, preview, browser, dock, themes, theme_dock, mock, variants, variants_tab, inspect_layers, .. } = viewer;
|
||||
let appearance = env.appearance(ui.ctx());
|
||||
let subject = ThemeSubject::new(theme_of(env.theme_id()), appearance);
|
||||
let in_theme = *workspace == Workspace::Theme;
|
||||
let Browser { catalog, filter, selection, open, dock, story_of, generation, cells, open_request, followed, tree_seen, level, .. } = browser;
|
||||
let dock: &mut Dock<_> = if in_theme { theme_dock } else { dock };
|
||||
let front = |panel: PanelId| dock.layout.panel_state(&panel) == PanelState::Front;
|
||||
let front = |panel: &PanelId| dock.layout.panel_state(panel) == PanelState::Front;
|
||||
let inspect = Inspect {
|
||||
on: !in_theme && variants.is_none() && front(PanelId::Inspect),
|
||||
front: [Some(PanelId::Overview), selected].into_iter().flatten().filter(|panel| front(*panel)).collect(),
|
||||
layers: inspect_layers,
|
||||
on: !in_theme && variants.is_none() && front(&PanelId::Inspect),
|
||||
front: [Some(PanelId::Overview), selected].into_iter().flatten().filter(|panel| front(panel)).collect(),
|
||||
};
|
||||
let panels = Panels {
|
||||
catalog,
|
||||
filter,
|
||||
selection,
|
||||
open,
|
||||
story_of,
|
||||
cells,
|
||||
open_request,
|
||||
followed,
|
||||
tree_seen,
|
||||
generation: *generation,
|
||||
themes,
|
||||
subject,
|
||||
level,
|
||||
variants,
|
||||
variants_tab,
|
||||
env,
|
||||
revealed: None,
|
||||
let window = Look { mode: Some(appearance.mode), direction: Some(env.direction), ..Look::default() };
|
||||
let variants_in = (*variants).zip(variants_tab.clone()).map(|(axis, tab)| (tab, VariantsIn::new(axis, window)));
|
||||
let mut content = GalleryDock {
|
||||
browser,
|
||||
inspect,
|
||||
layers: inspect_layers,
|
||||
variants: variants_in,
|
||||
theme: ThemePanels { themes, subject, revealed: None },
|
||||
preview: *preview,
|
||||
app: mock,
|
||||
opened: Vec::new(),
|
||||
variants_out: Vec::new(),
|
||||
};
|
||||
let mut content = GalleryDock { panels, preview: *preview, app: mock };
|
||||
// Component tabs are documents: they close from their ×.
|
||||
let config = DockConfig::default().tab_close_buttons(true);
|
||||
DockArea::new(if in_theme { "Theme dock" } else { "Components dock" }, Bind::bound(&mut *dock), &mut content, config).show(ui);
|
||||
if let Some(type_name) = content.panels.open_request.take() {
|
||||
let _ = dock.set_visible(crate::viewer::PanelId::Component(type_name), true, Some(crate::viewer::PanelId::Overview), false, |panel| {
|
||||
panel.permissions()
|
||||
});
|
||||
let GalleryDock { opened, variants_out, .. } = content;
|
||||
for type_name in opened {
|
||||
let _ = dock.set_visible(PanelId::component(type_name), true, Some(PanelId::Overview), false, |panel| panel.permissions());
|
||||
}
|
||||
for event in variants_out {
|
||||
if let VariantsEvent::Use(look) = event {
|
||||
if let Some(mode) = look.mode {
|
||||
env.mode = mode.into();
|
||||
}
|
||||
if let Some(direction) = look.direction {
|
||||
env.direction = direction;
|
||||
}
|
||||
}
|
||||
*variants = None;
|
||||
}
|
||||
// A closed component tab does not come back from the layout's record of closed panels.
|
||||
dock.hidden.retain(|(panel, _)| !matches!(panel, crate::viewer::PanelId::Component(_)));
|
||||
dock.hidden.retain(|(panel, _)| !matches!(panel, PanelId::Component(_)));
|
||||
}
|
||||
|
||||
impl ShellApp for Gallery {
|
||||
|
||||
@@ -2,20 +2,47 @@
|
||||
//! front centre tab's content.
|
||||
|
||||
use super::card::Look;
|
||||
use super::overview::{Spec, SpecStory, apply, grid, on_background, selected_type, target_story};
|
||||
use super::panels::Panels;
|
||||
use crate::viewer::{DIRECTIONS, MODES, PanelId, VariantAxis, entry_of};
|
||||
use super::overview::{Centre, Spec, SpecStory, apply, grid, on_background};
|
||||
use super::panels::{selected_of, target_story};
|
||||
use crate::viewer::{DIRECTIONS, MODES, VariantAxis, entry_of};
|
||||
use craft_i18n::Direction;
|
||||
use craft_theme::variant::Tone;
|
||||
use craft_theme::{ControlSize, Mode, ModeChoice};
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::contract::{Show as _, ShowContainer as _, StoryLayout};
|
||||
use craft_ui::contract::{MemoProps, Show as _, ShowContainer as _, StoryLayout};
|
||||
use craft_ui::display::{Shade, Text};
|
||||
use craft_ui::layout_components::Row;
|
||||
use craft_ui::notices::Notice;
|
||||
use craft_ui::style::TypeRole;
|
||||
use egui::Ui;
|
||||
|
||||
/// The Variants view open in a centre tab: its axis, and the window's look, which the card that shows it now notes.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, MemoProps)]
|
||||
#[non_exhaustive]
|
||||
pub struct VariantsIn {
|
||||
/// What the view repeats the story over.
|
||||
pub axis: VariantAxis,
|
||||
/// The window's mode and direction.
|
||||
pub shown: Look,
|
||||
}
|
||||
|
||||
impl VariantsIn {
|
||||
/// The view of `axis` under the window's `shown` look.
|
||||
pub fn new(axis: VariantAxis, shown: Look) -> Self {
|
||||
VariantsIn { axis, shown }
|
||||
}
|
||||
}
|
||||
|
||||
/// What the Variants view asks of the viewer.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
#[non_exhaustive]
|
||||
pub enum VariantsEvent {
|
||||
/// Close the view.
|
||||
Close,
|
||||
/// Make a card's look the window's, and close the view.
|
||||
Use(Look),
|
||||
}
|
||||
|
||||
/// One value of an axis: its name, the look a card shows it with, and what picking it sets.
|
||||
struct Value {
|
||||
label: &'static str,
|
||||
@@ -90,42 +117,18 @@ fn values(axis: VariantAxis) -> Option<Vec<Value>> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the Variants view is open in `tab`: it opens in the tab that is in front when it is asked for and closes
|
||||
/// when another one comes to the front.
|
||||
fn open_in(p: &mut Panels<'_>, tab: PanelId) -> bool {
|
||||
if p.variants.is_none() {
|
||||
*p.variants_tab = None;
|
||||
return false;
|
||||
}
|
||||
match *p.variants_tab {
|
||||
None => {
|
||||
*p.variants_tab = Some(tab);
|
||||
true
|
||||
}
|
||||
Some(owner) if owner == tab => true,
|
||||
Some(_) => {
|
||||
*p.variants = None;
|
||||
*p.variants_tab = None;
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Shows the Variants view in `tab` when one is open there. Returns whether it did.
|
||||
pub(super) fn show(ui: &mut Ui, p: &mut Panels<'_>, tab: PanelId) -> bool {
|
||||
if !open_in(p, tab) {
|
||||
return false;
|
||||
}
|
||||
let Some(axis) = *p.variants else { return false };
|
||||
/// Shows the Variants view when one is open in the tab `c` shows. Returns whether it did. Close and Use leave through
|
||||
/// `c.variants_out`.
|
||||
pub(super) fn show(ui: &mut Ui, c: &mut Centre<'_>) -> bool {
|
||||
let Some(VariantsIn { axis, shown: window }) = c.variants else { return false };
|
||||
let mut closed = false;
|
||||
let target = selected_type(p).and_then(|type_name| target_story(p, type_name).map(|index| (type_name, index)));
|
||||
let target = selected_of(c.selection).and_then(|type_name| target_story(c.catalog, c.story_of, type_name).map(|index| (type_name, index)));
|
||||
let cards: Vec<Spec> = match (target, values(axis)) {
|
||||
(Some((type_name, index)), Some(values)) => {
|
||||
let Some(entry) = entry_of(type_name) else { return false };
|
||||
let (mode, direction) = (p.env.appearance(ui.ctx()).mode, p.env.direction);
|
||||
let shown = |look: &Look| match axis {
|
||||
VariantAxis::Mode => look.mode == Some(mode),
|
||||
VariantAxis::Direction => look.direction == Some(direction),
|
||||
VariantAxis::Mode => look.mode == window.mode,
|
||||
VariantAxis::Direction => look.direction == window.direction,
|
||||
_ => false,
|
||||
};
|
||||
values
|
||||
@@ -135,7 +138,7 @@ pub(super) fn show(ui: &mut Ui, p: &mut Panels<'_>, tab: PanelId) -> bool {
|
||||
type_name,
|
||||
title: value.label.to_owned(),
|
||||
note: shown(&value.look).then(|| "shown now".to_owned()),
|
||||
stories: vec![SpecStory::of(p.catalog, index, &entry.name)],
|
||||
stories: vec![SpecStory::of(c.catalog, index, &entry.name)],
|
||||
layout: StoryLayout::Stacked,
|
||||
surface: entry.surface,
|
||||
open: false,
|
||||
@@ -150,9 +153,10 @@ pub(super) fn show(ui: &mut Ui, p: &mut Panels<'_>, tab: PanelId) -> bool {
|
||||
};
|
||||
let heading = target.and_then(|(type_name, index)| {
|
||||
let entry = entry_of(type_name)?;
|
||||
Some(format!("{} · {} in every {}", entry.name, p.catalog.entries[index].registration.story.caption(&entry.name), noun(axis)))
|
||||
Some(format!("{} · {} in every {}", entry.name, c.catalog.entries[index].registration.story.caption(&entry.name), noun(axis)))
|
||||
});
|
||||
let mut reported = Vec::new();
|
||||
let generation = c.generation;
|
||||
on_background(ui, "variants", |ui, cx| {
|
||||
Row::new().show(ui, |ui, _| {
|
||||
Text::new(heading.clone().unwrap_or_else(|| "Variants".to_owned())).role(TypeRole::Heading).show(ui);
|
||||
@@ -167,23 +171,16 @@ pub(super) fn show(ui: &mut Ui, p: &mut Panels<'_>, tab: PanelId) -> bool {
|
||||
};
|
||||
let _ = Notice::new(Tone::Info, text).show(ui);
|
||||
} else {
|
||||
reported = grid(ui, "variants", None, &cards, p, false, None);
|
||||
reported = grid(ui, "variants", None, &cards, c.cells, generation, false, None);
|
||||
}
|
||||
closed |= cx.take_cancel();
|
||||
});
|
||||
let used = reported.iter().find(|reported| reported.events.used).map(|reported| reported.spec);
|
||||
apply(p, &cards, reported);
|
||||
apply(c, &cards, reported);
|
||||
if let Some(card) = used.and_then(|spec| values(axis).and_then(|values| values.into_iter().nth(spec))) {
|
||||
if let Some(mode) = card.look.mode {
|
||||
p.env.mode = mode.into();
|
||||
}
|
||||
if let Some(direction) = card.look.direction {
|
||||
p.env.direction = direction;
|
||||
}
|
||||
closed = true;
|
||||
}
|
||||
if closed {
|
||||
*p.variants = None;
|
||||
c.variants_out.send(VariantsEvent::Use(card.look));
|
||||
} else if closed {
|
||||
c.variants_out.send(VariantsEvent::Close);
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ choices!(SCALES: Scale = [
|
||||
choices!(DIRECTIONS: Direction = [(Direction::Ltr, "ltr", "LTR"), (Direction::Rtl, "rtl", "RTL")]);
|
||||
|
||||
/// What a Variants view repeats the story over.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, craft_ui::contract::MemoProps)]
|
||||
#[non_exhaustive]
|
||||
pub enum VariantAxis {
|
||||
/// Every built-in theme.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! What the viewer lists: every registered story, grouped by the component it shows.
|
||||
|
||||
use craft_ui::contract::{self, Level, Probe, Registration};
|
||||
use craft_ui::contract::{self, Level, MemoKey, MemoProps, Probe, Registration, Tracked};
|
||||
use craft_ui::testing::{StoryRegistration, stories};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::OnceLock;
|
||||
@@ -169,7 +169,8 @@ pub fn components(entries: &[Entry]) -> Vec<ComponentInfo> {
|
||||
out
|
||||
}
|
||||
|
||||
/// Every registered story, the components they show and what the registrations say about each type.
|
||||
/// Every registered story, the components they show and what the registrations say about each type. Fixed once
|
||||
/// built: a memoized view that reads it keys on its source, never on its contents.
|
||||
#[non_exhaustive]
|
||||
pub struct Catalog {
|
||||
/// The stories, ordered by type then story name.
|
||||
@@ -180,6 +181,15 @@ pub struct Catalog {
|
||||
pub declared: HashMap<&'static str, Declared>,
|
||||
/// Each component's place in `components` and the catalog indices of its stories, in the order of its `stories`.
|
||||
by_type: HashMap<&'static str, (usize, Vec<usize>)>,
|
||||
/// A revision source unique to this catalog: what its readers' keys hold of it.
|
||||
source: Tracked<()>,
|
||||
}
|
||||
|
||||
/// By the catalog's source: a catalog does not change once built, so two catalogs with one source are one catalog.
|
||||
impl MemoProps for Catalog {
|
||||
fn key(&self, key: &mut MemoKey) {
|
||||
self.source.key(key);
|
||||
}
|
||||
}
|
||||
|
||||
impl Catalog {
|
||||
@@ -204,7 +214,13 @@ impl Catalog {
|
||||
stories.push(at);
|
||||
}
|
||||
}
|
||||
Catalog { entries, components, declared, by_type }
|
||||
Catalog { entries, components, declared, by_type, source: Tracked::new(()) }
|
||||
}
|
||||
|
||||
/// The catalog's own copy of the registered name `name`, when it lists that component: the name a saved layout or
|
||||
/// setting resolves to.
|
||||
pub fn component_named(&self, name: &str) -> Option<&'static str> {
|
||||
self.by_type.get_key_value(name).map(|(type_name, _)| *type_name)
|
||||
}
|
||||
|
||||
/// The component registered as `type_name`.
|
||||
|
||||
@@ -1,19 +1,56 @@
|
||||
//! The viewer's dock layouts: the panels of each workspace, the default arrangements, what the user may do with a
|
||||
//! panel, and the saved form of a layout.
|
||||
//!
|
||||
//! A saved layout names a component tab by its component's registered name. A name the catalog no longer has loads as a
|
||||
//! panel the reconciliation drops, so a layout saved by another build costs at most its component tabs.
|
||||
//! A saved layout names a component tab by its component's registered name. Loading resolves each name through the
|
||||
//! catalog and drops the tabs it does not list, so a layout saved by another build costs at most its component tabs.
|
||||
|
||||
use super::catalog::display_name;
|
||||
use super::catalog::{Catalog, display_name};
|
||||
use super::state::Workspace;
|
||||
use craft_behavior::Axis;
|
||||
use craft_behavior::dock::{Column, DockEdge, Group, Layout, Node, Permissions, SplitSize, Workspace as Dock};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::BTreeSet;
|
||||
use std::sync::{Mutex, PoisonError};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// The name of a component tab: its component's registered type name, owned by the layout that holds the tab.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, craft_ui::contract::MemoProps)]
|
||||
pub struct TabName(Arc<str>);
|
||||
|
||||
/// Saved as the name's text.
|
||||
impl Serialize for TabName {
|
||||
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serializer.serialize_str(&self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for TabName {
|
||||
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
Ok(TabName(String::deserialize(deserializer)?.into()))
|
||||
}
|
||||
}
|
||||
|
||||
impl TabName {
|
||||
/// The registered type name.
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for TabName {
|
||||
fn from(name: &str) -> Self {
|
||||
TabName(name.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Deref for TabName {
|
||||
type Target = str;
|
||||
|
||||
fn deref(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// A panel of the viewer's dock layouts.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, craft_ui::contract::MemoProps)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, craft_ui::contract::MemoProps)]
|
||||
#[non_exhaustive]
|
||||
pub enum PanelId {
|
||||
/// The component tree.
|
||||
@@ -29,7 +66,7 @@ pub enum PanelId {
|
||||
/// The centre's first tab: every component of one level.
|
||||
Overview,
|
||||
/// A centre tab with every story of one component, by the component's registered type name.
|
||||
Component(&'static str),
|
||||
Component(TabName),
|
||||
/// The theme workspace's centre: the mock app or the Controls level.
|
||||
Preview,
|
||||
/// The picked token's detail.
|
||||
@@ -41,8 +78,13 @@ pub enum PanelId {
|
||||
}
|
||||
|
||||
impl PanelId {
|
||||
/// The tab of the component registered as `type_name`.
|
||||
pub fn component(type_name: &str) -> Self {
|
||||
PanelId::Component(type_name.into())
|
||||
}
|
||||
|
||||
/// The panel's title.
|
||||
pub fn title(self) -> String {
|
||||
pub fn title(&self) -> String {
|
||||
match self {
|
||||
PanelId::Components => "Components".to_owned(),
|
||||
PanelId::Stories => "Stories".to_owned(),
|
||||
@@ -59,7 +101,7 @@ impl PanelId {
|
||||
}
|
||||
|
||||
/// Whether the layout of `workspace` holds the panel. The token browser is in both.
|
||||
pub fn in_workspace(self, workspace: Workspace) -> bool {
|
||||
pub fn in_workspace(&self, workspace: Workspace) -> bool {
|
||||
match self {
|
||||
PanelId::Components | PanelId::Stories | PanelId::Properties | PanelId::Inspect | PanelId::Overview | PanelId::Component(_) => {
|
||||
workspace == Workspace::Components
|
||||
@@ -80,72 +122,6 @@ impl PanelId {
|
||||
}
|
||||
}
|
||||
|
||||
/// [`PanelId`] as saved: the same variants, with a component tab named by an owned string.
|
||||
#[derive(Serialize, Deserialize)]
|
||||
enum PanelRepr {
|
||||
Components,
|
||||
Stories,
|
||||
Properties,
|
||||
Inspect,
|
||||
Tokens,
|
||||
Overview,
|
||||
Component(String),
|
||||
Preview,
|
||||
Detail,
|
||||
Problems,
|
||||
AppSizes,
|
||||
}
|
||||
|
||||
/// The names of the component tabs read from saved layouts. A tab name is a `&'static str`, so each distinct name is
|
||||
/// kept once for the process.
|
||||
fn interned(name: String) -> &'static str {
|
||||
static NAMES: Mutex<BTreeSet<&'static str>> = Mutex::new(BTreeSet::new());
|
||||
let mut names = NAMES.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
if let Some(known) = names.get(name.as_str()) {
|
||||
return known;
|
||||
}
|
||||
let leaked: &'static str = Box::leak(name.into_boxed_str());
|
||||
names.insert(leaked);
|
||||
leaked
|
||||
}
|
||||
|
||||
impl Serialize for PanelId {
|
||||
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
let repr = match *self {
|
||||
PanelId::Components => PanelRepr::Components,
|
||||
PanelId::Stories => PanelRepr::Stories,
|
||||
PanelId::Properties => PanelRepr::Properties,
|
||||
PanelId::Inspect => PanelRepr::Inspect,
|
||||
PanelId::Tokens => PanelRepr::Tokens,
|
||||
PanelId::Overview => PanelRepr::Overview,
|
||||
PanelId::Component(name) => PanelRepr::Component(name.to_owned()),
|
||||
PanelId::Preview => PanelRepr::Preview,
|
||||
PanelId::Detail => PanelRepr::Detail,
|
||||
PanelId::Problems => PanelRepr::Problems,
|
||||
PanelId::AppSizes => PanelRepr::AppSizes,
|
||||
};
|
||||
repr.serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for PanelId {
|
||||
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
Ok(match PanelRepr::deserialize(deserializer)? {
|
||||
PanelRepr::Components => PanelId::Components,
|
||||
PanelRepr::Stories => PanelId::Stories,
|
||||
PanelRepr::Properties => PanelId::Properties,
|
||||
PanelRepr::Inspect => PanelId::Inspect,
|
||||
PanelRepr::Tokens => PanelId::Tokens,
|
||||
PanelRepr::Overview => PanelId::Overview,
|
||||
PanelRepr::Component(name) => PanelId::Component(interned(name)),
|
||||
PanelRepr::Preview => PanelId::Preview,
|
||||
PanelRepr::Detail => PanelId::Detail,
|
||||
PanelRepr::Problems => PanelId::Problems,
|
||||
PanelRepr::AppSizes => PanelId::AppSizes,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// The share a split beside a dock records. The dock's width comes from its columns, so the split never reads it.
|
||||
const BESIDE_DOCK: SplitSize = SplitSize::Ratio(0.5);
|
||||
|
||||
@@ -180,12 +156,13 @@ pub fn default_for(workspace: Workspace) -> Dock<PanelId> {
|
||||
}
|
||||
|
||||
/// `saved` made valid for `workspace`: a layout that fails validation is replaced by the default, panels of the other
|
||||
/// workspace and component tabs `known` rejects are dropped, and missing panels come back where the default has them.
|
||||
pub fn reconciled(mut saved: Dock<PanelId>, workspace: Workspace, known: &dyn Fn(&str) -> bool) -> Dock<PanelId> {
|
||||
/// workspace and component tabs `catalog` does not list are dropped, and missing panels come back where the default has
|
||||
/// them.
|
||||
pub fn reconciled(mut saved: Dock<PanelId>, workspace: Workspace, catalog: &Catalog) -> Dock<PanelId> {
|
||||
let registered = |panel: &PanelId| {
|
||||
panel.in_workspace(workspace)
|
||||
&& match panel {
|
||||
PanelId::Component(name) => known(name),
|
||||
PanelId::Component(name) => catalog.component_named(name).is_some(),
|
||||
_ => true,
|
||||
}
|
||||
};
|
||||
@@ -199,19 +176,20 @@ pub fn save(layout: &Dock<PanelId>) -> String {
|
||||
serde_json::to_string(layout).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The layout saved as `text`, made valid for `workspace`; the default when the text is empty or unreadable.
|
||||
pub fn load(text: &str, workspace: Workspace, known: &dyn Fn(&str) -> bool) -> Dock<PanelId> {
|
||||
serde_json::from_str::<Dock<PanelId>>(text).ok().map_or_else(|| default_for(workspace), |saved| reconciled(saved, workspace, known))
|
||||
/// The layout saved as `text`, made valid for `workspace` with the component tabs `catalog` lists; the default when the
|
||||
/// text is empty or unreadable.
|
||||
pub fn load(text: &str, workspace: Workspace, catalog: &Catalog) -> Dock<PanelId> {
|
||||
serde_json::from_str::<Dock<PanelId>>(text).ok().map_or_else(|| default_for(workspace), |saved| reconciled(saved, workspace, catalog))
|
||||
}
|
||||
|
||||
/// The component tabs `layout` holds, in layout order.
|
||||
pub fn open_tabs(layout: &Dock<PanelId>) -> Vec<&'static str> {
|
||||
pub fn open_tabs(layout: &Dock<PanelId>) -> Vec<TabName> {
|
||||
layout
|
||||
.layout
|
||||
.panels()
|
||||
.into_iter()
|
||||
.filter_map(|panel| match panel {
|
||||
PanelId::Component(name) => Some(*name),
|
||||
PanelId::Component(name) => Some(name.clone()),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
|
||||
@@ -14,8 +14,8 @@ pub use appearance::{
|
||||
};
|
||||
pub use catalog::{Catalog, ComponentInfo, Declared, Entry, StoryGroup, display_name};
|
||||
pub use commands::GalleryCommands;
|
||||
pub use layout::{PanelId, default_components, default_for, default_theme, load as load_layout, open_tabs, reconciled, save as save_layout};
|
||||
pub use picks::{CardState, DialogState, by_level, entry_of, levels};
|
||||
pub use layout::{PanelId, TabName, default_components, default_for, default_theme, load as load_layout, open_tabs, reconciled, save as save_layout};
|
||||
pub use picks::{CardState, Cells, DialogState, by_level, entry_of, levels};
|
||||
pub use settings::GallerySettings;
|
||||
pub use state::{Browser, Cells, Preview, Viewer, Workspace};
|
||||
pub use tree::{TreeKey, TreeRow, TreeRows, groups, level_named, open_level_of_selection, rows, slug};
|
||||
pub use state::{Browser, Preview, Viewer, Workspace};
|
||||
pub use tree::{TreeKey, TreeRow, TreeRows, groups, level_named, open_level, open_level_of_selection, rows, slug};
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
use super::catalog::{Catalog, ComponentInfo, fold_filter};
|
||||
use craft_ui::contract::{Catalog as Levels, CatalogEntry, Level};
|
||||
use craft_ui::testing::{StoryData, StorySnapshot};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
/// Whether a story's dialog is closed behind its button.
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
@@ -38,12 +39,55 @@ pub struct CardState {
|
||||
/// write the card's own run makes moves the key, so the card runs once more and then replays.
|
||||
impl craft_ui::contract::MemoProps for &mut CardState {
|
||||
fn key(&self, key: &mut craft_ui::contract::MemoKey) {
|
||||
let DialogState { known, hidden, shown, painted } = self.dialog;
|
||||
(self.data.revision(), self.start.is_some(), (known, hidden, shown, painted), self.resets).key(key);
|
||||
self.feed(key);
|
||||
}
|
||||
}
|
||||
|
||||
/// What each shown story keeps between frames, by story index, in index order.
|
||||
#[derive(Default)]
|
||||
pub struct Cells(BTreeMap<usize, CardState>);
|
||||
|
||||
impl Cells {
|
||||
/// The state of the story at catalog index `index`, made fresh the first time it is asked for.
|
||||
pub fn state_of(&mut self, index: usize) -> &mut CardState {
|
||||
self.0.entry(index).or_default()
|
||||
}
|
||||
|
||||
/// The states of the stories at `indices`, in index order, each made fresh the first time it is asked for.
|
||||
pub fn states_of(&mut self, indices: &[usize]) -> Vec<(usize, &mut CardState)> {
|
||||
for &index in indices {
|
||||
self.0.entry(index).or_default();
|
||||
}
|
||||
self.0.iter_mut().filter(|(index, _)| indices.contains(index)).map(|(index, state)| (*index, state)).collect()
|
||||
}
|
||||
|
||||
/// Forgets every story's state.
|
||||
pub fn clear(&mut self) {
|
||||
self.0.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// A view's view of every shown story's state, as one digest of each state's key (`&mut CardState`) in index order: the
|
||||
/// key stays a fixed size at any number of cells. Computing it walks the cells once per pass of the view that holds it.
|
||||
impl craft_ui::contract::MemoProps for &mut Cells {
|
||||
fn key(&self, key: &mut craft_ui::contract::MemoKey) {
|
||||
let mut cells = craft_ui::contract::MemoKey::new();
|
||||
for (index, state) in &self.0 {
|
||||
index.key(&mut cells);
|
||||
state.feed(&mut cells);
|
||||
}
|
||||
cells.finish().key(key);
|
||||
}
|
||||
}
|
||||
|
||||
impl CardState {
|
||||
/// Feeds what a view shows from the state into `key`: the `&mut CardState` key.
|
||||
fn feed(&self, key: &mut craft_ui::contract::MemoKey) {
|
||||
use craft_ui::contract::MemoProps as _;
|
||||
let DialogState { known, hidden, shown, painted } = self.dialog;
|
||||
(self.data.revision(), self.start.is_some(), (known, hidden, shown, painted), self.resets).key(key);
|
||||
}
|
||||
|
||||
/// Whether the story's values differ from the ones it started with.
|
||||
pub fn changed(&self) -> bool {
|
||||
self.start.as_ref().is_some_and(|start| !self.data.changed_since(start).is_empty())
|
||||
|
||||
@@ -73,12 +73,12 @@ impl GallerySettings {
|
||||
direction: DIRECTIONS.name(env.direction).to_owned(),
|
||||
workspace: workspace_name(viewer.workspace).to_owned(),
|
||||
preview: preview_name(viewer.preview).to_owned(),
|
||||
level: browser.level.clone(),
|
||||
level: (*browser.level).clone(),
|
||||
stories: browser.story_of.iter().map(|(component, story)| ((*component).to_owned(), (*story).to_owned())).collect(),
|
||||
filter: browser.filter.clone(),
|
||||
filter: (*browser.filter).clone(),
|
||||
selected: viewer.selected_type().unwrap_or_default().to_owned(),
|
||||
open_levels: viewer.open_levels(),
|
||||
dock: layout::save(&browser.dock),
|
||||
dock: layout::save(&viewer.dock),
|
||||
theme_dock: layout::save(&viewer.theme_dock),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,53 +5,47 @@ use super::appearance::VariantAxis;
|
||||
use super::catalog::Catalog;
|
||||
use super::commands::GalleryCommands;
|
||||
use super::layout::{self, PanelId};
|
||||
use super::picks::CardState;
|
||||
use super::picks::Cells;
|
||||
use super::settings::GallerySettings;
|
||||
use super::tree::{self, TreeKey};
|
||||
use crate::mock::MockState;
|
||||
use craft_behavior::Selection;
|
||||
use craft_behavior::dock::Workspace as Dock;
|
||||
use craft_theme_editor::ThemeWorkspace;
|
||||
use craft_ui::contract::Level;
|
||||
use craft_ui::contract::{Level, Tracked};
|
||||
use craft_ui::display::InspectLayers;
|
||||
use craft_ui::list::OpenSet;
|
||||
use craft_ui::runtime::env::RootEnv;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// What each shown story keeps between frames, by story index.
|
||||
pub type Cells = HashMap<usize, CardState>;
|
||||
|
||||
/// What the panels browse: the catalog, the tree's state and the shown cells.
|
||||
/// What the panels browse: the catalog, the tree's state and the shown cells. The values a panel reads or edits are
|
||||
/// `Tracked`, so a panel's memoized view keys on their revisions.
|
||||
#[non_exhaustive]
|
||||
pub struct Browser {
|
||||
/// What is listed.
|
||||
pub catalog: Catalog,
|
||||
/// The text that narrows the tree.
|
||||
pub filter: String,
|
||||
pub filter: Tracked<String>,
|
||||
/// The selected row of the tree.
|
||||
pub selection: Selection<TreeKey>,
|
||||
pub selection: Tracked<Selection<TreeKey>>,
|
||||
/// The open level groups.
|
||||
pub open: OpenSet<TreeKey>,
|
||||
/// The components workspace's dock layout.
|
||||
pub dock: Dock<PanelId>,
|
||||
pub open: Tracked<OpenSet<TreeKey>>,
|
||||
/// The story each component's card shows, by the component's registered name and the story's name. A component
|
||||
/// without an entry shows its first story.
|
||||
pub story_of: HashMap<&'static str, &'static str>,
|
||||
pub story_of: Tracked<HashMap<&'static str, &'static str>>,
|
||||
/// The level the Overview shows, as its slug; empty until one is chosen.
|
||||
pub level: String,
|
||||
pub level: Tracked<String>,
|
||||
/// Counts the resets of the stories; a card's id scope names it, so a reset mounts the card anew.
|
||||
pub generation: u32,
|
||||
/// The shown stories' values.
|
||||
pub cells: Cells,
|
||||
/// The component whose tab a panel asked to open in this pass.
|
||||
pub open_request: Option<&'static str>,
|
||||
/// The selection the Overview's cards last followed: its level shown and its card brought into view; the outer `None`
|
||||
/// before they first ran.
|
||||
pub followed: Option<Option<&'static str>>,
|
||||
pub followed: Tracked<Option<Option<&'static str>>>,
|
||||
/// The selected component the tree last opened the level of; the outer `None` before the first pass. Applying settings
|
||||
/// sets it to the restored selection, so a level closed before a restart stays closed.
|
||||
pub tree_seen: Option<Option<&'static str>>,
|
||||
pub tree_seen: Tracked<Option<Option<&'static str>>>,
|
||||
}
|
||||
|
||||
/// What the dock shows.
|
||||
@@ -95,12 +89,14 @@ pub struct Viewer {
|
||||
pub browser: Browser,
|
||||
/// What the theme panels share.
|
||||
pub themes: ThemeWorkspace,
|
||||
/// The components workspace's dock layout.
|
||||
pub dock: Dock<PanelId>,
|
||||
/// The theme workspace's dock layout.
|
||||
pub theme_dock: Dock<PanelId>,
|
||||
/// The mock application the theme workspace's App preview shows.
|
||||
pub mock: MockState,
|
||||
/// The drawings the inspector shows over the selected card's stories.
|
||||
pub inspect_layers: InspectLayers,
|
||||
pub inspect_layers: Tracked<InspectLayers>,
|
||||
/// The revision of the theme workspace's app layer that `env.sizes` holds; none before the first sync.
|
||||
sizes_synced: Option<u64>,
|
||||
}
|
||||
@@ -110,17 +106,15 @@ impl Viewer {
|
||||
pub fn new(catalog: Catalog, settings: &GallerySettings) -> Self {
|
||||
let browser = Browser {
|
||||
catalog,
|
||||
filter: String::new(),
|
||||
selection: Selection::default(),
|
||||
open: OpenSet::new(),
|
||||
dock: layout::default_components(),
|
||||
story_of: HashMap::new(),
|
||||
level: String::new(),
|
||||
filter: Tracked::default(),
|
||||
selection: Tracked::default(),
|
||||
open: Tracked::new(OpenSet::new()),
|
||||
story_of: Tracked::default(),
|
||||
level: Tracked::default(),
|
||||
generation: 0,
|
||||
cells: Cells::new(),
|
||||
open_request: None,
|
||||
followed: None,
|
||||
tree_seen: None,
|
||||
cells: Cells::default(),
|
||||
followed: Tracked::default(),
|
||||
tree_seen: Tracked::default(),
|
||||
};
|
||||
let mut viewer = Viewer {
|
||||
env: RootEnv::default(),
|
||||
@@ -131,9 +125,10 @@ impl Viewer {
|
||||
more_open: false,
|
||||
browser,
|
||||
themes: ThemeWorkspace::new(),
|
||||
dock: layout::default_components(),
|
||||
theme_dock: layout::default_theme(),
|
||||
mock: MockState::default(),
|
||||
inspect_layers: InspectLayers::default(),
|
||||
inspect_layers: Tracked::new(InspectLayers::default()),
|
||||
sizes_synced: None,
|
||||
};
|
||||
viewer.restore(settings);
|
||||
@@ -146,21 +141,21 @@ impl Viewer {
|
||||
self.workspace = settings.workspace();
|
||||
self.preview = settings.preview();
|
||||
let browser = &mut self.browser;
|
||||
browser.filter.clone_from(&settings.filter);
|
||||
browser.open = OpenSet::new();
|
||||
browser.filter.edit(|filter| filter.clone_from(&settings.filter));
|
||||
let mut open = OpenSet::new();
|
||||
for level in settings.open_levels.iter().filter_map(|saved| tree::level_named(saved)) {
|
||||
browser.open.set(&TreeKey::Level(level), true);
|
||||
open.set(&TreeKey::Level(level), true);
|
||||
}
|
||||
browser.selection = browser
|
||||
*browser.open = open;
|
||||
*browser.selection = browser
|
||||
.catalog
|
||||
.component(&settings.selected)
|
||||
.map_or_else(Selection::default, |component| Selection::single(TreeKey::Component(component.type_name)));
|
||||
browser.tree_seen = Some(browser.selected_type());
|
||||
browser.level.clone_from(&settings.level);
|
||||
browser.story_of = settings.story_of(&browser.catalog);
|
||||
let known = |name: &str| browser.catalog.component(name).is_some();
|
||||
browser.dock = layout::load(&settings.dock, Workspace::Components, &known);
|
||||
self.theme_dock = layout::load(&settings.theme_dock, Workspace::Theme, &known);
|
||||
*browser.tree_seen = Some(browser.selected_type());
|
||||
browser.level.edit(|level| level.clone_from(&settings.level));
|
||||
*browser.story_of = settings.story_of(&browser.catalog);
|
||||
self.dock = layout::load(&settings.dock, Workspace::Components, &browser.catalog);
|
||||
self.theme_dock = layout::load(&settings.theme_dock, Workspace::Theme, &browser.catalog);
|
||||
}
|
||||
|
||||
/// Makes the root's app sizes the theme workspace's app layer, when the layer was edited since the last sync.
|
||||
@@ -186,26 +181,26 @@ impl Viewer {
|
||||
/// The dock layout of the shown workspace.
|
||||
pub fn dock_mut(&mut self) -> &mut Dock<PanelId> {
|
||||
match self.workspace {
|
||||
Workspace::Components => &mut self.browser.dock,
|
||||
Workspace::Components => &mut self.dock,
|
||||
Workspace::Theme => &mut self.theme_dock,
|
||||
}
|
||||
}
|
||||
|
||||
/// Opens the tab of the component registered as `type_name` beside the Overview and shows it.
|
||||
pub fn open_component(&mut self, type_name: &'static str) {
|
||||
let _ = self.browser.dock.set_visible(PanelId::Component(type_name), true, Some(PanelId::Overview), false, |panel| panel.permissions());
|
||||
pub fn open_component(&mut self, type_name: &str) {
|
||||
let _ = self.dock.set_visible(PanelId::component(type_name), true, Some(PanelId::Overview), false, |panel| panel.permissions());
|
||||
}
|
||||
|
||||
/// Puts the shown workspace's panels back at their default places. The open component tabs are documents and stay.
|
||||
pub fn reset_layout(&mut self) {
|
||||
let workspace = self.workspace;
|
||||
let tabs = layout::open_tabs(&self.browser.dock);
|
||||
let tabs = layout::open_tabs(&self.dock);
|
||||
*self.dock_mut() = layout::default_for(workspace);
|
||||
if workspace == Workspace::Components {
|
||||
for tab in tabs {
|
||||
self.open_component(tab);
|
||||
self.open_component(&tab);
|
||||
}
|
||||
let _ = self.browser.dock.set_visible(PanelId::Overview, true, None, false, |panel| panel.permissions());
|
||||
let _ = self.dock.set_visible(PanelId::Overview, true, None, false, |panel| panel.permissions());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
|
||||
use super::catalog::{ComponentInfo, fold_filter};
|
||||
use craft_behavior::{DisplayOrder, ItemSource};
|
||||
use craft_ui::contract::Level;
|
||||
use craft_ui::contract::{Level, Tracked};
|
||||
use craft_ui::list::OpenSet;
|
||||
use std::borrow::Cow;
|
||||
|
||||
/// The key of a tree row.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, craft_ui::contract::PlainData)]
|
||||
#[non_exhaustive]
|
||||
pub enum TreeKey {
|
||||
/// A level group.
|
||||
@@ -43,19 +43,28 @@ pub fn level_named(slug_text: &str) -> Option<Level> {
|
||||
}
|
||||
|
||||
/// Opens the level of `selected` when the selection is not the one `seen` records, then records it, so a selection
|
||||
/// that moves into a closed level shows its row. The levels the user opened or closed otherwise stay as they are.
|
||||
/// that moves into a closed level shows its row. The levels the user opened or closed otherwise stay as they are. Each
|
||||
/// value is written only when it changes, so its readers' revisions move only then.
|
||||
pub fn open_level_of_selection(
|
||||
open: &mut OpenSet<TreeKey>,
|
||||
open: &mut Tracked<OpenSet<TreeKey>>,
|
||||
components: &[ComponentInfo],
|
||||
selected: Option<&'static str>,
|
||||
seen: &mut Option<Option<&'static str>>,
|
||||
seen: &mut Tracked<Option<Option<&'static str>>>,
|
||||
) {
|
||||
if *seen == Some(selected) {
|
||||
if **seen == Some(selected) {
|
||||
return;
|
||||
}
|
||||
*seen = Some(selected);
|
||||
**seen = Some(selected);
|
||||
if let Some(component) = selected.and_then(|type_name| components.iter().find(|component| component.type_name == type_name)) {
|
||||
open.set(&TreeKey::Level(component.level), true);
|
||||
open_level(open, component.level);
|
||||
}
|
||||
}
|
||||
|
||||
/// Opens the group of `level`, writing `open` only when the group was closed.
|
||||
pub fn open_level(open: &mut Tracked<OpenSet<TreeKey>>, level: Level) {
|
||||
let key = TreeKey::Level(level);
|
||||
if !open.is_open(&key) {
|
||||
open.set(&key, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -462,7 +462,7 @@ fn the_inspected_card_replays_with_the_record_of_its_run() {
|
||||
catalog
|
||||
.stories_of(CHECKBOX)
|
||||
.iter()
|
||||
.filter_map(|story| craft_ui::runtime::inspect::recorded(fixture.ctx(), region_key(PanelId::Overview, *story)))
|
||||
.filter_map(|story| craft_ui::runtime::inspect::recorded(fixture.ctx(), region_key(&PanelId::Overview, *story)))
|
||||
.collect()
|
||||
};
|
||||
let ran = records(&fixture);
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! The viewer's own logic: default and saved layouts, and the crates that register components.
|
||||
|
||||
use craft_ui::testing::trial;
|
||||
use craft_ui_gallery::viewer::{PanelId, Workspace, default_for, load_layout, open_tabs, save_layout};
|
||||
use craft_ui::contract::Level;
|
||||
use craft_ui_gallery::viewer::{Catalog, ComponentInfo, PanelId, TabName, Workspace, default_for, load_layout, open_tabs, save_layout};
|
||||
|
||||
const COMPONENTS: [PanelId; 6] = [PanelId::Components, PanelId::Stories, PanelId::Properties, PanelId::Inspect, PanelId::Overview, PanelId::Tokens];
|
||||
const THEME: [PanelId; 5] = [PanelId::Preview, PanelId::Detail, PanelId::Problems, PanelId::AppSizes, PanelId::Tokens];
|
||||
@@ -19,22 +20,25 @@ fn each_default_layout_is_valid_and_holds_exactly_its_workspaces_panels() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Regression: a saved layout naming a component the catalog lacks, or lacking a default panel, must still load.
|
||||
/// A saved layout keeps its component tabs by name across a save and a load; a tab the catalog does not list, and a
|
||||
/// default panel the save lacks, come back as the catalog and the default have them.
|
||||
#[trial]
|
||||
fn a_saved_layout_loads_without_the_components_the_catalog_lacks_and_with_every_default_panel() {
|
||||
let mut saved = default_for(Workspace::Components);
|
||||
let _ = saved.set_visible(PanelId::Component("craft_ui::buttons::link::Link"), true, Some(PanelId::Overview), false, |panel| panel.permissions());
|
||||
let _ = saved.set_visible(PanelId::Component("gone::Removed"), true, Some(PanelId::Overview), false, |panel| panel.permissions());
|
||||
fn a_saved_layout_round_trips_its_component_tabs_by_name() {
|
||||
const LINK: &str = "craft_ui::buttons::link::Link";
|
||||
let catalog = Catalog::listing(vec![ComponentInfo::new(LINK, Level::Controls, "Link", &[])]);
|
||||
let open = |names: &[&str]| {
|
||||
let mut dock = default_for(Workspace::Components);
|
||||
for name in names {
|
||||
let _ = dock.set_visible(PanelId::component(name), true, Some(PanelId::Overview), false, |panel| panel.permissions());
|
||||
}
|
||||
dock
|
||||
};
|
||||
let mut saved = open(&[LINK, "gone::Removed"]);
|
||||
let _ = saved.set_visible(PanelId::Stories, false, None, false, |_| craft_behavior::dock::Permissions::ALL);
|
||||
assert!(!saved.layout.contains(&PanelId::Stories));
|
||||
let loaded = load_layout(&save_layout(&saved), Workspace::Components, &|name| name == "craft_ui::buttons::link::Link");
|
||||
assert_eq!(loaded.validate(), Ok(()));
|
||||
assert_eq!(open_tabs(&loaded), ["craft_ui::buttons::link::Link"]);
|
||||
assert!(loaded.hidden.iter().all(|(panel, _)| !matches!(panel, PanelId::Component(_))));
|
||||
for panel in COMPONENTS {
|
||||
assert!(loaded.layout.contains(&panel), "{panel:?}");
|
||||
}
|
||||
let unreadable = load_layout("{\"layout\":", Workspace::Theme, &|_| true);
|
||||
let loaded = load_layout(&save_layout(&saved), Workspace::Components, &catalog);
|
||||
assert_eq!(loaded, open(&[LINK]));
|
||||
assert_eq!(open_tabs(&loaded), [TabName::from(LINK)]);
|
||||
let unreadable = load_layout("{\"layout\":", Workspace::Theme, &catalog);
|
||||
assert_eq!(unreadable, default_for(Workspace::Theme));
|
||||
}
|
||||
|
||||
@@ -52,9 +56,9 @@ fn every_linked_crate_registers() {
|
||||
fn the_overview_is_pinned_and_a_component_tab_is_not() {
|
||||
use craft_behavior::dock::{Action, DockPlacement};
|
||||
let rect = egui::Rect::from_min_size(egui::pos2(40.0, 40.0), egui::vec2(300.0, 200.0));
|
||||
let tab = PanelId::Component("craft_ui::buttons::link::Link");
|
||||
let tab = PanelId::component("craft_ui::buttons::link::Link");
|
||||
let mut dock = default_for(Workspace::Components);
|
||||
let _ = dock.set_visible(tab, true, Some(PanelId::Overview), false, |panel| panel.permissions());
|
||||
let _ = dock.set_visible(tab.clone(), true, Some(PanelId::Overview), false, |panel| panel.permissions());
|
||||
assert!(dock.layout.contains(&tab), "the component tab opens beside the Overview");
|
||||
let refused = [
|
||||
Action::float(PanelId::Overview, rect),
|
||||
@@ -66,6 +70,6 @@ fn the_overview_is_pinned_and_a_component_tab_is_not() {
|
||||
assert!(tried.apply(action.clone(), false, |panel| panel.permissions()).is_err(), "refused: {action:?}");
|
||||
}
|
||||
let mut floated = dock.clone();
|
||||
assert!(floated.apply(Action::float(tab, rect), false, |panel| panel.permissions()).is_ok(), "the component tab floats");
|
||||
assert!(dock.apply(Action::close(tab), false, |panel| panel.permissions()).is_ok(), "the component tab closes");
|
||||
assert!(floated.apply(Action::float(tab.clone(), rect), false, |panel| panel.permissions()).is_ok(), "the component tab floats");
|
||||
assert!(dock.apply(Action::close(tab.clone()), false, |panel| panel.permissions()).is_ok(), "the component tab closes");
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ use std::sync::Arc;
|
||||
mod stories;
|
||||
|
||||
/// Which drawings an [`InspectOverlay`] shows.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, crate::contract::MemoProps)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, crate::contract::MemoProps, crate::contract::PlainData)]
|
||||
#[non_exhaustive]
|
||||
pub struct InspectLayers {
|
||||
/// Each element's box, its padding filled.
|
||||
|
||||
Reference in New Issue
Block a user