Merge pull request #10062 from dotCooCoo/fix/export-screens-artboard-list

Export for Screens: the items sit in a framed view, and checked items show selected
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-29 09:07:20 -08:00
commit 033a829995
2 files changed
+123 -12

No files matched your search

@@ -30,13 +30,13 @@
use std::borrow::Cow;
use std::sync::Arc;
use craft::behavior::{ColumnKey, ColumnWidth, DisplayOrder, GesturePhase, IconId, ItemSource, SelectionChange};
use craft::behavior::{ColumnKey, ColumnWidth, DisplayOrder, GesturePhase, IconId, ItemSource, Selection, SelectionChange};
use craft::host::PickRequest;
use craft::ui::buttons::Button;
use craft::ui::choice::{Checkbox, Radio, RadioGroup, Segmented};
use craft::ui::contract::{
Bind, CollectionEvent, ColumnSpec, Entry, Props as _, Show as _, ShowCollection as _, ShowContainer as _, SpaceAction, SweepApply, SweepStep,
ToggleColumn,
Bind, CollectionEvent, ColumnSpec, Entry, Pad, Props as _, Show as _, ShowCollection as _, ShowContainer as _, SpaceAction, SweepApply,
SweepStep, ToggleColumn,
};
use craft::ui::dialog::{DialogId, DialogResult, DialogStack, FieldRefusal, HeldAnswer, HeldOk, StackDialog, dialog_id};
use craft::ui::display::{Shade, Text};
@@ -46,6 +46,7 @@ use craft::ui::patterns::form_dialogs::{FormDialog, PathRow};
use craft::ui::primitives::{ContentPainterCx, fit_rect};
use craft::ui::runtime::env::Label;
use craft::ui::runtime::focus::Initial;
use craft::ui::style::SurfaceKind;
use craft::ui::tabs::{Tab, TabKind, TabStrip};
use craft::ui::text::TextField;
use craft::ui::tile_grid::{Tile, TileGrid, TilePartEvent};
@@ -93,10 +94,11 @@ const SETTABLE: [&str; 20] = [
/// The check column of the list view and its name column.
const CHECK: ColumnKey = ColumnKey::new("check");
const NAME: ColumnKey = ColumnKey::new("name");
/// Rows of the list view and of the grid view the items pane shows before it scrolls, a pane as tall as the options
/// column's first sections.
/// How many items the list view and the grid view hold before they scroll, and how tall they are with fewer: a
/// missing item counts a control row, so the pane stays about as tall as the options column's first sections with
/// one artboard. The framework counts a grid's items, not its rows of tiles.
const LIST_ROWS: u16 = 9;
const GRID_ROWS: u16 = 3;
const GRID_ITEMS: u16 = 4;
/// The longest side of an artboard thumbnail, in pixels.
const THUMB_PX: u32 = 92;
@@ -219,6 +221,7 @@ pub fn open(app: &mut VectorcraftApp) {
pending: None,
renaming_in: None,
rename: None,
leads: Default::default(),
held: HeldAnswer::new(),
};
if let Some(dialogs) = app.dialogs() {
@@ -541,6 +544,8 @@ pub(crate) struct ExportForScreensDialog {
renaming_in: Option<(u64, u64)>,
/// A committed artboard name: ((document uid, reverts), artboard id, name), for the pump to run.
rename: Option<((u64, u64), u32, String)>,
/// The lead and anchor of the Artboards and the Assets view, by tab.
leads: [Selection<u64>; 2],
held: HeldAnswer<Map<String, Value>>,
}
@@ -616,7 +621,7 @@ impl ExportForScreensDialog {
let checked = |at: usize| on.get(at).copied().unwrap_or(false);
let shelf =
Shelf { items: &items, enabled: text(fields, "select") != "full", reveal: None, caption: &caption, checked: &checked, paint: &paint };
let picks = shelf.show(ui, text(fields, "view") == "list");
let picks = shelf.show(ui, text(fields, "view") == "list", &mut self.leads[0]);
for key in picks.toggled {
if let Some(b) = items.index_of(&key).and_then(|at| on.get_mut(at)) {
*b ^= true;
@@ -654,7 +659,7 @@ impl ExportForScreensDialog {
let checked = |at: usize| items.0.get(at).is_some_and(|(id, _)| on.contains(id));
let reveal = fields.get("__scrollTo").and_then(Value::as_u64).filter(|id| items.index_of(id).is_some());
let shelf = Shelf { items: &items, enabled: true, reveal, caption: &caption, checked: &checked, paint: &paint };
let picks = shelf.show(ui, text(fields, "view") == "list");
let picks = shelf.show(ui, text(fields, "view") == "list", &mut self.leads[1]);
for id in picks.toggled {
if on.contains(&id) {
on.retain(|a| *a != id);
@@ -724,23 +729,30 @@ struct Shelf<'a> {
}
impl Shelf<'_> {
/// Shows the items as a list of rows (`list`) or a grid of tiles → what the user did to them.
fn show(self, ui: &mut Ui, list: bool) -> Picks {
/// Shows the items as a list of rows (`list`) or a grid of tiles in a sunken frame, with the checked items
/// selected → what the user did to them. `lead` holds the selection's lead and anchor from pass to pass; its
/// items are the checked ones.
fn show(self, ui: &mut Ui, list: bool, lead: &mut Selection<u64>) -> Picks {
let Shelf { items, enabled, reveal, caption, checked, paint } = self;
let mut picks = Picks::default();
let rename = reveal.is_none();
let at = |key: &u64| items.index_of(key).unwrap_or(usize::MAX);
let state = |at: usize| if checked(at) { ToggleState::On } else { ToggleState::Off };
let mut selection = Selection::new((0..items.len()).filter(|at| checked(*at)).map(|at| items.key(at)).collect(), lead.lead, lead.anchor);
if list {
let columns =
vec![ColumnSpec::toggle(CHECK, Label::Empty, ToggleColumn::default()), ColumnSpec::new(NAME, Label::Empty, ColumnWidth::fill(1.0))];
let mut view = List::<u64>::new(tl!("Export for Screens"))
.surface(SurfaceKind::Sunken)
.pad(Pad::Control)
.header(false)
.space(SpaceAction::ToggleCell)
.columns(columns)
.rename(rename)
.enabled(enabled)
.max_rows(LIST_ROWS);
.min_rows(LIST_ROWS)
.max_rows(LIST_ROWS)
.selection(Bind::bound(&mut selection));
if rename {
view = view.rename_column(NAME);
}
@@ -763,7 +775,15 @@ impl Shelf<'_> {
}
}
} else {
let mut grid = TileGrid::<u64>::new(tl!("Export for Screens")).checks(true).rename(rename).enabled(enabled).max_rows(GRID_ROWS);
let mut grid = TileGrid::<u64>::new(tl!("Export for Screens"))
.surface(SurfaceKind::Sunken)
.pad(Pad::Control)
.checks(true)
.rename(rename)
.enabled(enabled)
.min_rows(GRID_ITEMS)
.max_rows(GRID_ITEMS)
.selection(Bind::bound(&mut selection));
if let Some(key) = reveal {
grid = grid.reveal(key);
}
@@ -778,6 +798,13 @@ impl Shelf<'_> {
}
}
}
// A click picks the item it lands on as the lead; the checks alone say which items are selected.
(lead.lead, lead.anchor) = (selection.lead, selection.anchor);
// Space on a selected item flips that item first and then the other selected items to the same value. The
// selection here is every checked item, so only the first item's flips are kept.
if let Some(&first) = picks.toggled.first() {
picks.toggled.retain(|key| *key == first);
}
picks
}
}
@@ -1664,3 +1664,87 @@ fn the_format_rows_fit_the_right_column_at_the_default_and_least_window_sizes()
}
}
}
/// The items pane, in the grid and in the list, with one artboard and with many: the artboards sit in a sunken frame
/// at least one and a half tiles or five rows tall, the view buttons and the prefix sit under the frame, and the checked
/// artboards are the selected ones.
#[test]
fn the_artboards_sit_in_a_framed_view_as_tall_as_the_pane_and_the_checked_ones_are_selected() {
for (boards, view) in [(1, "grid"), (1, "list"), (12, "grid"), (12, "list")] {
let (mut app, _) = app(boards, false);
let mut w = Window::default();
w.run(&mut app, vec![]);
open(&mut app);
set(&mut app, "view", json!(view));
let shown = |w: &mut Window, app: &mut VectorcraftApp| {
w.run(app, vec![]);
let out = w.run(app, vec![]);
let nodes = out.platform_output.accesskit_update.map(|u| u.nodes).unwrap_or_default();
(out.shapes, nodes)
};
let rect = |n: &egui::accesskit::Node| {
n.bounds().map(|b| egui::Rect::from_min_max(egui::pos2(b.x0 as f32, b.y0 as f32), egui::pos2(b.x1 as f32, b.y1 as f32)))
};
let (shapes, nodes) = shown(&mut w, &mut app);
let find = |label: &str, role: Role| {
nodes
.iter()
.find(|(_, n)| n.label() == Some(label) && n.role() == role)
.and_then(|(_, n)| rect(n))
.unwrap_or_else(|| panic!("{view}: no {role:?} {label:?}"))
};
let pane = nodes
.iter()
.find(|(_, n)| n.label() == Some("Export for Screens") && matches!(n.role(), Role::ListBox | Role::List))
.and_then(|(_, n)| rect(n))
.unwrap_or_else(|| panic!("{boards} {view}: no items view"));
let clear = find("Clear Selection", Role::Button);
let tile = find("1 Artboard 1", Role::ListItem);
// The frame: a filled or outlined rectangle that holds the items view.
fn rects(shape: &egui::Shape, out: &mut Vec<egui::epaint::RectShape>) {
match shape {
egui::Shape::Rect(r) => out.push(r.clone()),
egui::Shape::Vec(v) => v.iter().for_each(|s| rects(s, out)),
_ => {}
}
}
let mut painted = Vec::new();
shapes.iter().for_each(|c| rects(&c.shape, &mut painted));
let near: Vec<_> = painted.iter().filter(|r| r.rect.expand(4.0).contains_rect(pane) && pane.expand(4.0).contains_rect(r.rect)).collect();
let framed = near.iter().any(|r| r.stroke.width > 0.0 || r.fill.a() > 0);
assert!(framed, "{boards} {view}: the items view at {pane:?} has a frame: {near:?}");
assert!(clear.top() >= pane.bottom(), "{boards} {view}: Clear Selection {clear:?} is under the items {pane:?}");
// One and a half tiles tall in the grid, five rows in the list, whatever the count of artboards.
let least = if view == "grid" { 1.5 } else { 5.0 } * tile.height();
assert!(pane.height() >= least, "{boards} {view}: the items view {pane:?} is at least {least} tall (an item is {tile:?})");
let selected = |nodes: &[(egui::accesskit::NodeId, egui::accesskit::Node)]| {
nodes.iter().find(|(_, n)| n.label() == Some("1 Artboard 1") && n.role() == Role::ListItem).and_then(|(_, n)| n.is_selected())
};
assert_eq!(selected(&nodes), Some(true), "{boards} {view}: the checked artboard is selected");
let mut off = vec![true; boards];
off[0] = false;
set(&mut app, "boards", json!(off));
let (_, nodes) = shown(&mut w, &mut app);
assert_ne!(selected(&nodes), Some(true), "{boards} {view}: an artboard that is not checked is not selected");
}
}
/// With two artboards checked, the arrow keys move to a checked one and, a frame later, Space unchecks only that one,
/// in the grid and in the list.
#[test]
fn space_on_a_checked_artboard_flips_only_its_own_check() {
for (view, back) in [("grid", egui::Key::ArrowLeft), ("list", egui::Key::ArrowUp)] {
let (mut app, _) = app(3, false);
let mut w = Window::default();
w.names(&mut app);
open(&mut app);
set(&mut app, "view", json!(view));
set(&mut app, "boards", json!([true, true, false]));
w.press(&mut app, "3 Artboard 3", Role::ListItem);
assert_eq!(field(&app, "boards"), json!([true, true, false]), "{view}: a click on the third artboard leaves the checks");
w.key(&mut app, back);
w.run(&mut app, vec![]);
w.key(&mut app, egui::Key::Space);
assert_eq!(field(&app, "boards"), json!([true, false, false]), "{view}: Space unchecks only the second artboard");
}
}