mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 13:02:50 -05:00
Merge pull request #10450 from dotCooCoo/fix/swatch-library-freeze
Library panels: a large library scrolls without rebuilding its rows every frame
This commit is contained in:
6 files changed
+109
-18
No files matched your search
@@ -884,6 +884,9 @@ impl LibraryKind for BrushLibraries {
|
||||
fn path(app: &VectorcraftApp, id: &str) -> Option<String> {
|
||||
app.session.brush_libraries.path(id).map(str::to_string)
|
||||
}
|
||||
fn same(a: &Self::Lib, b: &Self::Lib) -> bool {
|
||||
Rc::ptr_eq(a, b)
|
||||
}
|
||||
fn rows<'a>(lib: &'a Self::Lib, _id: &str) -> Vec<Row<'a, LibBrush>> {
|
||||
lib.iter().map(|b| Row { name: b.name(), item: Some(b) }).collect()
|
||||
}
|
||||
|
||||
@@ -1004,6 +1004,9 @@ impl LibraryKind for GraphicStyleLibraries {
|
||||
fn path(app: &VectorcraftApp, id: &str) -> Option<String> {
|
||||
app.session.style_libraries.path(id).map(str::to_string)
|
||||
}
|
||||
fn same(a: &Self::Lib, b: &Self::Lib) -> bool {
|
||||
Arc::ptr_eq(a, b)
|
||||
}
|
||||
fn rows<'a>(lib: &'a Self::Lib, _id: &str) -> Vec<Row<'a, LibStyle>> {
|
||||
lib.iter().map(|s| Row { name: &s.style().name, item: Some(s) }).collect()
|
||||
}
|
||||
|
||||
@@ -15,9 +15,11 @@
|
||||
//! Library… (`ui.libraryPanel.locate`). The find text is the browser's; the selection is panel state
|
||||
//! (`UiState::library_selection`).
|
||||
|
||||
use std::any::Any;
|
||||
use std::borrow::Cow;
|
||||
use std::cell::RefCell;
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
use craft::behavior::{IconId, Selection, SelectionModel};
|
||||
use craft::host::PickRequest;
|
||||
@@ -25,9 +27,7 @@ use craft::theme::variant::Tone;
|
||||
use craft::ui::contract::{Bind, MemoKey, MemoProps, SelectMode, Show as _, ShowContainer as _, Trigger};
|
||||
use craft::ui::menu::{Menu, MenuItem, PanelRow};
|
||||
use craft::ui::notices::{Notice, NoticeAction, NoticeLayout};
|
||||
use craft::ui::patterns::presets::{
|
||||
PresetBrowser, PresetEvent, PresetFns, PresetKind, PresetRow, PresetRowMenu, PresetSource, PresetState, PresetView,
|
||||
};
|
||||
use craft::ui::patterns::presets::{PresetBrowser, PresetEvent, PresetFns, PresetRow, PresetRowMenu, PresetSource, PresetState, PresetView};
|
||||
use craft::ui::primitives::{ContentPainterCx, IconSpec};
|
||||
use craft::ui::runtime::access::PopupKind;
|
||||
use craft::ui::runtime::commands::{CommandRef, CommandState, CommandStates};
|
||||
@@ -122,7 +122,7 @@ pub(crate) trait LibraryKind {
|
||||
/// The view a panel shows until another is picked.
|
||||
const VIEW: View = View::MediumThumb;
|
||||
/// A library's contents, cheap to clone.
|
||||
type Lib;
|
||||
type Lib: Clone + 'static;
|
||||
type Item: Sync;
|
||||
/// What the thumbnails paint with besides each item (a panel setting, the documents a library's patterns come
|
||||
/// from); `()` for nothing.
|
||||
@@ -135,6 +135,9 @@ pub(crate) trait LibraryKind {
|
||||
fn find(app: &VectorcraftApp, key: &str) -> Found;
|
||||
/// The file library `id` was read from, if any.
|
||||
fn path(app: &VectorcraftApp, id: &str) -> Option<String>;
|
||||
/// Whether `a` and `b` are the same contents, not only equal ones: the panel reuses the browser rows it built for
|
||||
/// `a` while [`get`](Self::get) returns `b`.
|
||||
fn same(a: &Self::Lib, b: &Self::Lib) -> bool;
|
||||
/// The rows of library `id`; the browser's search filters what it shows (by the shown name and
|
||||
/// [`PresetRow::search_name`]).
|
||||
fn rows<'a>(lib: &'a Self::Lib, id: &str) -> Vec<Row<'a, Self::Item>>;
|
||||
@@ -608,6 +611,7 @@ fn saved_view(view: PresetView, zoom: f64) -> View {
|
||||
/// The browser rows of library `id`'s `rows`, flat: a folder is a row of its own before its items, which a click
|
||||
/// selects as it selects an item (production's folder tiles neither opened nor closed).
|
||||
fn browser_rows<K: LibraryKind>(panel: &str, id: &str, rows: &[Row<K::Item>]) -> Vec<PresetRow<LibraryKey>> {
|
||||
BUILDS.with(|n| n.set(n.get() + 1));
|
||||
rows.iter()
|
||||
.map(|r| {
|
||||
let label = K::label(id, r.name);
|
||||
@@ -624,22 +628,73 @@ fn browser_rows<K: LibraryKind>(panel: &str, id: &str, rows: &[Row<K::Item>]) ->
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A library panel's browser rows, built for one library's contents in one UI language.
|
||||
struct BuiltRows<L> {
|
||||
/// The contents the rows were built from, held so that [`LibraryKind::same`] does not match other contents
|
||||
/// later stored at the same address.
|
||||
lib: L,
|
||||
lang: crate::i18n::Lang,
|
||||
/// Different for every build, so the browser's memo key changes with the rows.
|
||||
build: u64,
|
||||
rows: Rc<Vec<PresetRow<LibraryKey>>>,
|
||||
/// Each item row's position in [`LibraryKind::rows`], by stored name.
|
||||
items: Rc<HashMap<String, usize>>,
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
/// Each library panel's [`BuiltRows`], by panel id.
|
||||
static BUILT_ROWS: RefCell<HashMap<String, Box<dyn Any>>> = RefCell::new(HashMap::new());
|
||||
/// How many times [`browser_rows`] has run, for tests.
|
||||
static BUILDS: Cell<u64> = const { Cell::new(0) };
|
||||
}
|
||||
|
||||
/// How many times the panels have built browser rows on this thread.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn row_builds() -> u64 {
|
||||
BUILDS.with(Cell::get)
|
||||
}
|
||||
|
||||
/// [`BuiltRows::build`], [`BuiltRows::rows`] and [`BuiltRows::items`].
|
||||
type Built = (u64, Rc<Vec<PresetRow<LibraryKey>>>, Rc<HashMap<String, usize>>);
|
||||
|
||||
/// Panel `panel`'s browser rows of `lib`: the ones built before while the panel shows the same contents in the same
|
||||
/// language, otherwise built now. Building drops the rows of the panels that are no longer open in `ui`.
|
||||
fn built_rows<K: LibraryKind>(ui: &UiState, panel: &str, id: &str, lib: &K::Lib) -> Built {
|
||||
let lang = crate::i18n::current();
|
||||
BUILT_ROWS.with(|cache| {
|
||||
let mut cache = cache.borrow_mut();
|
||||
if let Some(b) = cache.get(panel).and_then(|b| b.downcast_ref::<BuiltRows<K::Lib>>())
|
||||
&& b.lang == lang
|
||||
&& K::same(&b.lib, lib)
|
||||
{
|
||||
return (b.build, b.rows.clone(), b.items.clone());
|
||||
}
|
||||
let flat = K::rows(lib, id);
|
||||
let rows = Rc::new(browser_rows::<K>(panel, id, &flat));
|
||||
let items = Rc::new(flat.iter().enumerate().filter(|(_, r)| r.item.is_some()).map(|(i, r)| (r.name.to_string(), i)).collect());
|
||||
let build = BUILDS.with(Cell::get);
|
||||
let built = BuiltRows { lib: lib.clone(), lang, build, rows: rows.clone(), items: Rc::clone(&items) };
|
||||
cache.retain(|p, _| is_open(ui, p));
|
||||
cache.insert(panel.to_string(), Box::new(built));
|
||||
(build, rows, items)
|
||||
})
|
||||
}
|
||||
|
||||
/// What a library's browser shows: its rows, the items their thumbnails paint and what those paint with.
|
||||
struct Shown<'a, K: LibraryKind> {
|
||||
/// [`BuiltRows::build`] of `rows`.
|
||||
build: u64,
|
||||
rows: &'a [PresetRow<LibraryKey>],
|
||||
items: HashMap<&'a str, &'a K::Item>,
|
||||
flat: &'a [Row<'a, K::Item>],
|
||||
items: &'a HashMap<String, usize>,
|
||||
thumbs: K::Thumbs,
|
||||
}
|
||||
|
||||
/// By the rows (each names its library and item, whose look a library keeps) and what the thumbnails paint with.
|
||||
/// By the build of the rows (each names its library and item, whose look a library keeps) and what the thumbnails
|
||||
/// paint with.
|
||||
impl<K: LibraryKind> MemoProps for Shown<'_, K> {
|
||||
fn key(&self, key: &mut MemoKey) {
|
||||
for row in self.rows {
|
||||
row.key.key(key);
|
||||
row.label.key(key);
|
||||
row.depth.key(key);
|
||||
(row.kind == PresetKind::Item).key(key);
|
||||
}
|
||||
self.build.key(key);
|
||||
self.rows.len().key(key);
|
||||
self.thumbs.key(key);
|
||||
}
|
||||
@@ -658,15 +713,14 @@ fn body<K: LibraryKind>(app: &mut VectorcraftApp, ui: &mut Ui, id: &str) {
|
||||
};
|
||||
let panel = panel_id(K::KIND, id);
|
||||
let (view, hide_find) = settings(&app.ui, K::KIND, id).map_or((K::VIEW, false), |s| (s.view.unwrap_or(K::VIEW), s.hide_find));
|
||||
let (build, rows, items) = built_rows::<K>(&app.ui, &panel, id, &lib);
|
||||
let flat = K::rows(&lib, id);
|
||||
let rows = browser_rows::<K>(&panel, id, &flat);
|
||||
let items = flat.iter().filter_map(|r| r.item.map(|i| (r.name, i))).collect();
|
||||
let fns = PresetFns::new(|shown: &Shown<'_, K>, _| shown.rows.to_vec()).thumb(|shown, cx, rect, info| {
|
||||
if let Some(item) = shown.items.get(info.key.name.as_str()) {
|
||||
if let Some(item) = shown.items.get(info.key.name.as_str()).and_then(|&i| shown.flat.get(i)).and_then(|r| r.item) {
|
||||
K::thumb(&shown.thumbs, cx, rect, item);
|
||||
}
|
||||
});
|
||||
let source = PresetSource::new(Shown::<K> { rows: &rows, items, thumbs: K::thumbs(app, &lib) }, fns);
|
||||
let source = PresetSource::new(Shown::<K> { build, rows: &rows, flat: &flat, items: &items, thumbs: K::thumbs(app, &lib) }, fns);
|
||||
let selection = selected(app, &panel);
|
||||
// The search text and the open groups are the browser's; the view is the panel's setting.
|
||||
let state_id = egui::Id::unique(("library-panel-state", &panel));
|
||||
|
||||
@@ -786,3 +786,27 @@ fn built_in_library_swatches_are_listed_in_the_ui_language() {
|
||||
assert!(app.session.doc().unwrap().doc.swatch("Clay 1").is_some(), "added under its own name");
|
||||
crate::i18n::set_current(crate::i18n::Lang::EN);
|
||||
}
|
||||
|
||||
/// A library panel builds its browser rows once for the library it shows, not on every frame, and builds them again
|
||||
/// when the UI language changes the names it lists.
|
||||
#[test]
|
||||
fn library_panel_builds_its_rows_once_per_library_and_language() {
|
||||
let mut app = app();
|
||||
let ctx = context();
|
||||
app.run("window.swatchLibrary", json!({"library": "earth-tones"})).unwrap();
|
||||
let panel = shown(&app);
|
||||
app.run("ui.libraryPanel", json!({"panel": panel, "view": "smallList"})).unwrap();
|
||||
let draw = |app: &mut VectorcraftApp, ui: &mut Ui| show(app, ui, &panel);
|
||||
let before = row_builds();
|
||||
for _ in 0..4 {
|
||||
draw_frame(&mut app, &ctx, &draw, vec![]);
|
||||
}
|
||||
assert_eq!(row_builds() - before, 1, "four frames of an unchanged library build its rows once");
|
||||
crate::i18n::set_current(crate::i18n::Lang::from_code("fr").expect("fr registered"));
|
||||
draw_frame(&mut app, &ctx, &draw, vec![]);
|
||||
let out = draw_frame(&mut app, &ctx, &draw, vec![]);
|
||||
crate::i18n::set_current(crate::i18n::Lang::EN);
|
||||
assert_eq!(row_builds() - before, 2, "a new UI language builds the rows again, once");
|
||||
let painted = crate::tests_labels::shapes_text(&out);
|
||||
assert!(painted.lines().any(|l| l == "Argile 1"), "the rebuilt rows are in the new language: {painted}");
|
||||
}
|
||||
@@ -106,6 +106,9 @@ impl LibraryKind for SwatchLibraries {
|
||||
fn path(app: &VectorcraftApp, id: &str) -> Option<String> {
|
||||
app.session.swatch_libraries.path(id).map(str::to_string)
|
||||
}
|
||||
fn same(a: &Self::Lib, b: &Self::Lib) -> bool {
|
||||
Arc::ptr_eq(a, b)
|
||||
}
|
||||
/// Ungrouped swatches, then each colour group's folder and swatches, as in the Swatches panel; the browser's Find
|
||||
/// narrows them.
|
||||
fn rows<'a>(lib: &'a Self::Lib, _id: &str) -> Vec<Row<'a, Swatch>> {
|
||||
@@ -135,7 +138,7 @@ impl LibraryKind for SwatchLibraries {
|
||||
row
|
||||
}
|
||||
fn thumbs(app: &VectorcraftApp, lib: &Self::Lib) -> PatternSources {
|
||||
PatternSources(lib.iter().filter_map(|w| Some((w.name.clone(), swatchlib::pattern_source(&app.session, w)?))).collect())
|
||||
PatternSources(lib.iter().filter_map(|w| swatchlib::pattern_source(&app.session, w).map(|d| (w.name.clone(), d))).collect())
|
||||
}
|
||||
/// A pattern library's pattern, or a loaded library's, drawn from the library.
|
||||
fn thumb(sources: &PatternSources, cx: &ContentPainterCx<'_>, rect: Rect, w: &Swatch) {
|
||||
|
||||
@@ -981,6 +981,10 @@ impl LibraryKind for SymbolLibraries {
|
||||
fn path(app: &VectorcraftApp, id: &str) -> Option<String> {
|
||||
app.session.symbol_libraries.path(id).map(str::to_string)
|
||||
}
|
||||
/// [`get`](LibraryKind::get) wraps the library anew each time: by the library the symbols are in.
|
||||
fn same(a: &Self::Lib, b: &Self::Lib) -> bool {
|
||||
a.len() == b.len() && a.first().zip(b.first()).is_none_or(|(a, b)| Arc::ptr_eq(&a.lib, &b.lib))
|
||||
}
|
||||
fn rows<'a>(lib: &'a Self::Lib, _id: &str) -> Vec<Row<'a, LibSymbol>> {
|
||||
lib.iter().map(|s| Row { name: s.name(), item: Some(s) }).collect()
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user