mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-28 14:19:57 -08:00
Merge pull request #9969 from dotCooCoo/fix/issue-8307
Swatches: the footer stays inside the panel when the list fills it (#8307)
This commit is contained in:
2 files changed
+140
-30
No files matched your search
@@ -17,6 +17,7 @@ struct Harness {
|
||||
ctx: egui::Context,
|
||||
time: f64,
|
||||
nodes: Vec<(egui::accesskit::NodeId, egui::accesskit::Node)>,
|
||||
shapes: Vec<egui::epaint::ClippedShape>,
|
||||
}
|
||||
|
||||
impl Harness {
|
||||
@@ -26,7 +27,7 @@ impl Harness {
|
||||
app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
|
||||
let ctx = crate::testing::ui_context();
|
||||
ctx.enable_accesskit();
|
||||
let mut h = Self { app, ctx, time: 0.0, nodes: Vec::new() };
|
||||
let mut h = Self { app, ctx, time: 0.0, nodes: Vec::new(), shapes: Vec::new() };
|
||||
h.frames(3);
|
||||
h
|
||||
}
|
||||
@@ -43,10 +44,11 @@ impl Harness {
|
||||
}
|
||||
}
|
||||
|
||||
/// Draw one application frame and retain its accessibility output for assertions.
|
||||
/// Draw one application frame and retain its accessibility output and shapes for assertions.
|
||||
fn run(&mut self, input: egui::RawInput) {
|
||||
let out = crate::testing::app_frame(&mut self.app, &self.ctx, input);
|
||||
self.nodes = out.platform_output.accesskit_update.map_or_else(Vec::new, |update| update.nodes);
|
||||
self.shapes = out.shapes;
|
||||
}
|
||||
|
||||
/// The rect of the node of `role` named like `panel` (its title, ignoring case and spaces) in the last frame.
|
||||
@@ -1011,6 +1013,85 @@ fn a_floating_panel_resized_past_the_panel_minimum_stops_at_it() {
|
||||
assert!(after >= PANEL_MIN_WIDTH - 0.5, "dragged in past the minimum: {after}");
|
||||
}
|
||||
|
||||
/// #8307: Swatches with more swatches than fit, floating or docked above Transparency and sized
|
||||
/// short, keeps its footer buttons inside the panel, and scrolled to its end shows nothing from its
|
||||
/// list in the strip between those buttons and the panel's bottom edge.
|
||||
#[test]
|
||||
fn a_short_swatches_panel_keeps_its_footer_inside_and_its_rows_out_of_the_strip_under_it() {
|
||||
for (docked, below, height) in
|
||||
[(false, false, None), (false, true, None), (true, false, None), (true, true, None), (true, true, Some(200.0)), (true, true, Some(150.0))]
|
||||
{
|
||||
let case = format!("docked {docked}, above Transparency {below}, height {height:?}");
|
||||
let mut h = Harness::new();
|
||||
h.app.run("window.panel.float", json!({"panel": "swatches"})).unwrap();
|
||||
if below {
|
||||
h.app.run("window.panel.float", json!({"panel": "transparency", "below": "swatches"})).unwrap();
|
||||
}
|
||||
if docked {
|
||||
h.app.run("window.panel.dock", json!({"panel": "swatches", "column": 1})).unwrap();
|
||||
}
|
||||
if height.is_some() {
|
||||
let mut layout = h.layout();
|
||||
layout.apply(Action::resize_group("swatches".into(), height)).unwrap();
|
||||
h.set_layout(layout);
|
||||
}
|
||||
h.frames(3);
|
||||
footer_strip_shows_no_rows(&mut h, &case);
|
||||
}
|
||||
}
|
||||
|
||||
/// Scrolls the Swatches list to its end and checks its footer and the strip under the footer.
|
||||
fn footer_strip_shows_no_rows(h: &mut Harness, case: &str) {
|
||||
let rect = |b: egui::accesskit::Rect| Rect::from_min_max(pos2(b.x0 as f32, b.y0 as f32), pos2(b.x1 as f32, b.y1 as f32));
|
||||
// The wheel turns over the list, or over the panel when it is too short to show any row.
|
||||
let named =
|
||||
|h: &Harness, role| h.nodes.iter().find(|(_, n)| n.role() == role && n.label() == Some("Swatches")).and_then(|(_, n)| n.bounds()).map(rect);
|
||||
let over = named(h, egui::accesskit::Role::ListBox)
|
||||
.or_else(|| named(h, egui::accesskit::Role::TabPanel))
|
||||
.unwrap_or_else(|| panic!("{case}: the panel is drawn"));
|
||||
let wheel = Event::MouseWheel {
|
||||
unit: egui::MouseWheelUnit::Point,
|
||||
delta: vec2(0.0, -400.0),
|
||||
modifiers: Default::default(),
|
||||
phase: egui::TouchPhase::Move,
|
||||
source: egui::MouseWheelSource::Unknown,
|
||||
};
|
||||
for _ in 0..10 {
|
||||
h.frame(vec![Event::PointerMoved(over.center()), wheel.clone()]);
|
||||
}
|
||||
h.frames(10);
|
||||
let footer = ["Swatch Libraries Menu", "Show Swatch Kinds", "Swatch Options", "Delete Swatch", "New Swatch", "New Color Group"]
|
||||
.iter()
|
||||
.map(|label| {
|
||||
let found = h.nodes.iter().find(|(_, n)| n.label() == Some(*label)).and_then(|(_, n)| n.bounds()).map(rect);
|
||||
found.unwrap_or_else(|| panic!("{case}: the footer's {label} button is drawn"))
|
||||
})
|
||||
.reduce(|a, b| a.union(b))
|
||||
.unwrap_or(Rect::NOTHING);
|
||||
let body = named(h, egui::accesskit::Role::TabPanel).unwrap_or_else(|| panic!("{case}: the panel body is drawn"));
|
||||
assert!(body.contains_rect(footer), "{case}: the footer buttons {footer:?} lie outside the panel {body:?}");
|
||||
// A shape of the list starts above the footer; any part of it seen in the strip is a row showing through.
|
||||
let strip = Rect::from_min_max(pos2(body.left(), footer.bottom()), body.right_bottom());
|
||||
let transparent = |shape: &egui::Shape| match shape {
|
||||
egui::Shape::Noop => true,
|
||||
egui::Shape::Rect(r) => r.fill.a() == 0,
|
||||
egui::Shape::Mesh(m) => m.vertices.iter().all(|v| v.color.a() == 0),
|
||||
_ => false,
|
||||
};
|
||||
let showing: Vec<Rect> = h
|
||||
.shapes
|
||||
.iter()
|
||||
.filter(|c| !transparent(&c.shape))
|
||||
.map(|c| (c.shape.visual_bounding_rect(), c.clip_rect))
|
||||
.filter(|(bounds, clip)| {
|
||||
let seen = clip.intersect(*bounds).intersect(strip);
|
||||
bounds.height() < 60.0 && bounds.top() < footer.top() && seen.is_positive() && seen.height() > 0.5
|
||||
})
|
||||
.map(|(bounds, _)| bounds)
|
||||
.collect();
|
||||
assert!(showing.is_empty(), "{case}: list shapes show in the strip {strip:?} under the footer: {showing:?}");
|
||||
}
|
||||
|
||||
/// A docked column saved narrower than `PANEL_MIN_WIDTH` is drawn at the minimum.
|
||||
#[test]
|
||||
fn a_docked_column_is_never_narrower_than_the_panel_minimum() {
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
//! proxy's paint command with the object's id) and onto the Gradient panel's ramp; a Fill/Stroke
|
||||
//! proxy or the Gradient panel's thumbnail dropped on the panel becomes a swatch (`swatch.new`).
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::sync::Arc;
|
||||
|
||||
use craft::behavior::{EditEvent, IconId, Selection};
|
||||
@@ -16,11 +17,12 @@ use craft::theme::Srgba;
|
||||
use craft::ui::choice::SwatchFace;
|
||||
use craft::ui::contract::{Bind, DropPlace, ListDrag, MemoProps, PopupKind, SelectMode};
|
||||
use craft::ui::contract::{Entry, Props as _, Show as _, ShowContainer as _, Trigger};
|
||||
use craft::ui::layout_components::Stack;
|
||||
use craft::ui::menu::{Menu, MenuItem};
|
||||
use craft::ui::patterns::presets::{PresetEvent, PresetView, ThumbInfo};
|
||||
use craft::ui::primitives::{ContentPainterCx, Glyph, IconSpec, PaintShape, StrokeWidth};
|
||||
use craft::ui::runtime::env::Label;
|
||||
use craft::ui::sections::{FooterAction, FooterBar};
|
||||
use craft::ui::sections::{FooterAction, FooterBar, FooterOutput};
|
||||
use craft::ui::style::{DataColor, UiTokens};
|
||||
use egui::{Color32, Painter, Rect, Response, Shape, Stroke, StrokeKind, Ui, pos2, vec2};
|
||||
use serde_json::{Value, json};
|
||||
@@ -382,6 +384,7 @@ fn body(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
super::color_guide::choose_base(app, c);
|
||||
apply_click(app, ui, json!({"color": color_json(&c)}));
|
||||
}
|
||||
let (footer_state, footer_actions) = footer_actions(app);
|
||||
// The model leaves the app while the browser borrows the document, and comes back before the events run.
|
||||
let mut model = std::mem::take(&mut app.ui.swatches_panel);
|
||||
let view = app.ui.swatches_view;
|
||||
@@ -410,33 +413,45 @@ fn body(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
let document = st.uid;
|
||||
let reverts = st.reverts;
|
||||
let shown_app: &VectorcraftApp = app;
|
||||
let browser = SwatchesPanel::new(&data, Bind::bound(&mut model.state), Srgba::rgb(0.0, 0.0, 0.0))
|
||||
.drag_payload(move |drag: &ListDrag<SwatchKey>| Some(dnd::drag_of(&dragged_doc, document, &drag.grabbed, dragged_rows(drag))))
|
||||
.thumb(thumbs, thumb)
|
||||
.row_icons(|key| match key {
|
||||
SwatchKey::Swatch(name) => row_icons(shown_app, name),
|
||||
_ => vec![],
|
||||
})
|
||||
.row_items(|key| row_items(shown_app, key))
|
||||
.takes_drop::<PanelDrag>(DropPlace::Between, |drag, _| dnd::accepts(drag, document, reverts))
|
||||
.takes_drop::<PanelDrag>(DropPlace::Onto, |drag, _| dnd::accepts(drag, document, reverts))
|
||||
.select(SelectMode::Multi)
|
||||
.search(model.show_find)
|
||||
.reveal(focus.map(SwatchKey::Swatch))
|
||||
.footer(None);
|
||||
// A list drag is keyed by its browser ID. Each document has its own browser.
|
||||
let out = ui.push_id(("swatch-rows", document), |ui| browser.show(ui)).inner;
|
||||
// The footer stays under the browser, which fills the room left above it.
|
||||
let footer_out: RefCell<Option<FooterOutput>> = RefCell::new(None);
|
||||
let shown = Stack::new()
|
||||
.fill()
|
||||
.footer(|ui, _| *footer_out.borrow_mut() = Some(FooterBar::actions(footer_actions.clone()).at_start(EDIT + 1).show_footer(ui)))
|
||||
.show(ui, |ui, _| {
|
||||
let dragged_doc = Arc::clone(&dragged_doc);
|
||||
let browser = SwatchesPanel::new(&data, Bind::bound(&mut model.state), Srgba::rgb(0.0, 0.0, 0.0))
|
||||
.drag_payload(move |drag: &ListDrag<SwatchKey>| Some(dnd::drag_of(&dragged_doc, document, &drag.grabbed, dragged_rows(drag))))
|
||||
.thumb(thumbs.clone(), thumb)
|
||||
.row_icons(|key| match key {
|
||||
SwatchKey::Swatch(name) => row_icons(shown_app, name),
|
||||
_ => vec![],
|
||||
})
|
||||
.row_items(|key| row_items(shown_app, key))
|
||||
.takes_drop::<PanelDrag>(DropPlace::Between, |drag, _| dnd::accepts(drag, document, reverts))
|
||||
.takes_drop::<PanelDrag>(DropPlace::Onto, |drag, _| dnd::accepts(drag, document, reverts))
|
||||
.select(SelectMode::Multi)
|
||||
.search(model.show_find)
|
||||
.reveal(focus.clone().map(SwatchKey::Swatch))
|
||||
.footer(None);
|
||||
// A list drag is keyed by its browser ID. Each document has its own browser.
|
||||
ui.push_id(("swatch-rows", document), |ui| browser.show(ui)).inner
|
||||
});
|
||||
app.ui.swatches_panel = model;
|
||||
let shown = shown_keys(&data);
|
||||
if let Some((at, _, alt)) = out.used
|
||||
&& let Some(entry) = data.get(at.group).and_then(|g| g.entries.get(at.index))
|
||||
{
|
||||
apply(app, &entry.name, alt);
|
||||
let shown_rows = shown_keys(&data);
|
||||
if let Some(out) = shown.inner {
|
||||
if let Some((at, _, alt)) = out.used
|
||||
&& let Some(entry) = data.get(at.group).and_then(|g| g.entries.get(at.index))
|
||||
{
|
||||
apply(app, &entry.name, alt);
|
||||
}
|
||||
for event in out.events {
|
||||
on_event(app, &shown_rows, event);
|
||||
}
|
||||
}
|
||||
for event in out.events {
|
||||
on_event(app, &shown, event);
|
||||
if let Some(out) = footer_out.into_inner() {
|
||||
on_footer(app, ui, footer_state, out);
|
||||
}
|
||||
footer(app, ui);
|
||||
}
|
||||
|
||||
/// The rows `drag` moves: the dragged rows of the grabbed row's kind (colour groups or swatches), in panel order.
|
||||
@@ -508,7 +523,7 @@ fn menu_command(app: &mut VectorcraftApp, key: &SwatchKey, command: &str) {
|
||||
}
|
||||
|
||||
/// What the swatch thumbnails read: the active document at its revision and the names drawn selected.
|
||||
#[derive(MemoProps)]
|
||||
#[derive(Clone, MemoProps)]
|
||||
struct Thumbs {
|
||||
/// The document's uid and revision.
|
||||
revision: (u64, u64),
|
||||
@@ -757,11 +772,20 @@ const DELETE: usize = 3;
|
||||
const NEW: usize = 4;
|
||||
const NEW_GROUP: usize = 5;
|
||||
|
||||
/// What the footer's actions act on, read before the panel is drawn.
|
||||
struct FooterState {
|
||||
sel: Vec<String>,
|
||||
kind: Kind,
|
||||
del: Vec<String>,
|
||||
opts: Option<String>,
|
||||
group: Option<String>,
|
||||
}
|
||||
|
||||
/// The panel's footer: Swatch Libraries, Show Swatch Kinds, Edit Color Group or Swatch Options, New Color Group, New
|
||||
/// Swatch and Delete. Alt-click on Delete deletes without asking; Cmd/Ctrl-click on New Swatch makes a spot color, and
|
||||
/// Cmd/Ctrl+Shift-click a global process color. Swatches dragged from the list and dropped on New Swatch are
|
||||
/// duplicated, and rows dropped on Delete are deleted, also with nothing selected.
|
||||
fn footer(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
fn footer_actions(app: &VectorcraftApp) -> (FooterState, [FooterAction; 6]) {
|
||||
let sel = selected(app);
|
||||
let kind = app.ui.swatches_panel.kind;
|
||||
let del = actions::deletable(app, &sel);
|
||||
@@ -790,7 +814,12 @@ fn footer(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
FooterAction::new(icon("dc-new-item"), tl!("New Swatch")).accepts_drops::<PanelDrag>(rows_drag),
|
||||
FooterAction::new(icon("dc-folder"), tl!("New Color Group")),
|
||||
];
|
||||
let out = FooterBar::actions(actions).at_start(EDIT + 1).show_footer(ui);
|
||||
(FooterState { sel, kind, del, opts, group }, actions)
|
||||
}
|
||||
|
||||
/// Runs what the footer's `out` reports and shows the menus its buttons open.
|
||||
fn on_footer(app: &mut VectorcraftApp, ui: &mut Ui, state: FooterState, out: FooterOutput) {
|
||||
let FooterState { sel, kind, del, opts, group } = state;
|
||||
match out.picked {
|
||||
Some(DELETE) if !del.is_empty() => actions::delete(app, del, out.mods.alt),
|
||||
Some(NEW) => actions::new_swatch(app, &sel, out.mods),
|
||||
|
||||
Reference in new issue
Block a user