From 8dbe58929e1ba43ebc0395545edf90a94d52cc23 Mon Sep 17 00:00:00 2001 From: ".CooCoo" Date: Tue, 24 Nov 2026 23:04:46 -0800 Subject: [PATCH] Control bar: Style is a swatch of the linked graphic style, as production's Style chip was (#9254) --- crates/ui-egui/src/chrome/tests.rs | 107 ++++++++++++++++++++ crates/ui-egui/src/panels/graphic_styles.rs | 28 ++++- 2 files changed, 131 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/src/chrome/tests.rs b/crates/ui-egui/src/chrome/tests.rs index 04111e3104..e6ac73031a 100644 --- a/crates/ui-egui/src/chrome/tests.rs +++ b/crates/ui-egui/src/chrome/tests.rs @@ -2740,6 +2740,113 @@ fn the_control_bar_align_to_menu_chooses_what_align_aligns_to() { assert_eq!(menu(&mut bar).1.as_deref(), Some("Align to Selection")); } +/// What the latest frame paints in the Control bar's Style field. +struct StyleFace { + /// The field's rect. + field: egui::Rect, + /// The rects of the textured meshes inside the field, each with the texel at its middle when the texture is known. + pictures: Vec<(egui::Rect, Option)>, + /// The rects the field fills, each with its fill, in paint order. + fills: Vec<(egui::Rect, egui::Color32)>, +} + +/// The Style field of the settled bar. `textures` holds the textures the frames uploaded since the fixture began +/// recording; the uploads of these frames are applied to it. +fn style_face(bar: &mut Shown, textures: &mut std::collections::HashMap) -> StyleFace { + fn flat(shape: egui::Shape, out: &mut Vec) { + if let egui::Shape::Vec(shapes) = shape { + shapes.into_iter().for_each(|s| flat(s, out)); + } else { + out.push(shape); + } + } + let field = bar.settle().named("Graphic Styles").rect; + let mut paint = bar.fixture.take_paint().expect("the fixture records what it paints"); + for (id, delta) in paint.textures_delta.set.iter().flat_map(|(id, deltas)| deltas.iter().map(move |delta| (id, delta))) { + let egui::ImageData::Color(image) = &delta.image; + match delta.pos { + None => { + textures.insert(*id, egui::ColorImage::clone(image.as_ref())); + } + Some([x0, y0]) => { + let Some(page) = textures.get_mut(id) else { continue }; + for (i, texel) in image.pixels.iter().enumerate() { + let (x, y) = (x0 + i % image.size[0], y0 + i / image.size[0]); + if x < page.size[0] && y < page.size[1] { + page.pixels[y * page.size[0] + x] = *texel; + } + } + } + } + } + // The uploads are applied to `textures`, not to a renderer. + paint.textures_delta.clear(); + let mut shapes = Vec::new(); + for clipped in std::mem::take(&mut paint.shapes) { + flat(clipped.shape, &mut shapes); + } + let mut face = StyleFace { field, pictures: Vec::new(), fills: Vec::new() }; + for shape in shapes { + match shape { + egui::Shape::Mesh(mesh) if mesh.texture_id != egui::TextureId::default() => { + let rect = egui::Rect::from_points(&mesh.vertices.iter().map(|v| v.pos).collect::>()); + if !field.contains_rect(rect) { + continue; + } + let uv = egui::Rect::from_points(&mesh.vertices.iter().map(|v| v.uv).collect::>()).center(); + let texel = textures.get(&mesh.texture_id).map(|image| { + let x = ((uv.x * image.size[0] as f32) as usize).min(image.size[0] - 1); + let y = ((uv.y * image.size[1] as f32) as usize).min(image.size[1] - 1); + image.pixels[y * image.size[0] + x] + }); + face.pictures.push((rect, texel)); + } + egui::Shape::Rect(r) if field.contains_rect(r.rect) && r.fill != egui::Color32::TRANSPARENT => face.fills.push((r.rect, r.fill)), + _ => {} + } + } + face +} + +/// The Control bar's Style picker is a swatch of the style the selection is linked to: the field shows that style's +/// thumbnail, and white once the selection is linked to no style. It paints no style's name, and it keeps its width +/// whether the selection is linked or not and when a style with a long name is added. +#[test] +fn the_control_bar_style_picker_is_a_swatch_of_the_linked_style() { + crate::i18n::set_current(crate::i18n::Lang::EN); + let mut app = crate::VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.run("shape.rectangle", json!({"x": 20, "y": 20, "width": 40, "height": 40})).unwrap(); + let mut bar = control_shown(app); + bar.fixture.record_paint(); + let mut textures = std::collections::HashMap::new(); + bar.room(egui::vec2(2400.0, 300.0)); + let unlinked = style_face(&mut bar, &mut textures).field.width(); + let long = "A graphic style whose name is longer than any field on the bar"; + bar.app().run("paint.setFill", json!({"color": "#ff0000"})).unwrap(); + bar.app().run("graphicStyle.new", json!({"name": long})).unwrap(); + bar.app().run("graphicStyle.apply", json!({"name": long})).unwrap(); + let linked = bar.app().session.selection_graphic_style().is_some_and(|(g, linked)| linked && g.name == long); + assert!(linked, "the rectangle is linked to the new style"); + let face = style_face(&mut bar, &mut textures); + let shown = face.field.width(); + assert!((shown - unlinked).abs() < 0.5, "the swatch keeps its width: {unlinked} unlinked, {shown} linked to {long:?}"); + assert!(!bar.texts().iter().any(|t| t.text.contains(long)), "the swatch paints no style name: {:?}", bar.texts()); + let largest = |face: &StyleFace| face.pictures.iter().copied().max_by(|a, b| a.0.area().total_cmp(&b.0.area())); + let Some((thumb, Some(texel))) = largest(&face) else { panic!("the field shows the style's thumbnail: {:?}", face.pictures) }; + assert!(texel.r() > 180 && texel.g() < 70 && texel.b() < 70, "the thumbnail shows the style's red fill: {texel:?}"); + + // Break Link to Graphic Style: the field turns white where the thumbnail was. + bar.app().run("graphicStyle.breakLink", json!({})).unwrap(); + assert!(bar.app().session.selection_graphic_style().is_none_or(|(_, linked)| !linked), "the rectangle is linked to no style"); + let face = style_face(&mut bar, &mut textures); + assert!((face.field.width() - unlinked).abs() < 0.5, "the swatch keeps its width once unlinked"); + let middle = thumb.center(); + assert!(!face.pictures.iter().any(|(r, _)| r.contains(middle)), "no thumbnail shows once unlinked: {:?}", face.pictures); + let top = face.fills.iter().rev().find(|(r, _)| r.contains(middle)).map(|(_, fill)| *fill); + assert_eq!(top, Some(egui::Color32::WHITE), "the unlinked swatch is white: {:?}", face.fills); +} + /// The Control bar shows the Global Edit Options dropdown while Start Global Edit can run. Its /// options are the session's, which the Properties panel's dropdown also sets: Match Size from /// the Control bar narrows the Results and the objects Start Global Edit takes. The dropdown is off diff --git a/crates/ui-egui/src/panels/graphic_styles.rs b/crates/ui-egui/src/panels/graphic_styles.rs index 0633f3fbcd..c59d8734e1 100644 --- a/crates/ui-egui/src/panels/graphic_styles.rs +++ b/crates/ui-egui/src/panels/graphic_styles.rs @@ -27,7 +27,8 @@ use craft::ui::patterns::presets::{ PickerField, PresetBrowser, PresetDrop, PresetEvent, PresetFns, PresetPicker, PresetReorder, PresetRow, PresetSource, PresetState, PresetView, }; use craft::ui::primitives::{ - ContentPainterCx, IconSpec, PaintShape, Priority, Revision, ThumbPaint, ThumbRender, ThumbRequest, ThumbSource, ThumbnailOptions, Thumbnails, + ContentPainterCx, FacePaint, IconSpec, PaintShape, Priority, Revision, ThumbPaint, ThumbRender, ThumbRequest, ThumbSource, ThumbnailOptions, + Thumbnails, }; use craft::ui::runtime::commands::{CommandRef, CommandState, CommandStates}; use craft::ui::runtime::env::Env; @@ -686,16 +687,20 @@ fn footer_drop(app: &mut VectorcraftApp, index: usize, grabbed: &str, alt: bool) } } -/// The Control bar's Style picker: the styles in a popover, the field showing the style the selection is -/// linked to (blank when it is not); picking one applies it to the selection, with Alt on top of its look. +/// The Control bar's Style picker: the styles in a popover, the field a swatch of the style the selection is +/// linked to (white when it is not, [`linked_face`]); picking one applies it to the selection, with Alt on top of its +/// look. pub(crate) fn style_picker(app: &mut VectorcraftApp, ui: &mut Ui) { let Some(doc) = app.session.active().map(|d| d.doc.clone()) else { return }; let mut panel = std::mem::take(&mut app.ui.graphic_styles_panel); let looks = look_hashes(&mut panel, app); let mut linked: Option = app.session.selection_graphic_style().filter(|(_, linked)| *linked).map(|(g, _)| g.name.clone()); - let source = style_source(style_thumbs(app, &doc, &looks, panel.preview)); + let thumbs = style_thumbs(app, &doc, &looks, panel.preview); + let face = linked_face(thumbs, &doc, &looks, linked.as_deref()); + let source = style_source(thumbs); let out = PresetPicker::new(tl!("Graphic Styles").to_string(), Bind::bound(&mut linked), &source, Bind::bound(&mut panel.picker_state)) .field(PickerField::Thumbnail) + .face_thumb(face) .show(ui); app.ui.graphic_styles_panel = panel; if let Some(name) = out.picked { @@ -703,6 +708,21 @@ pub(crate) fn style_picker(app: &mut VectorcraftApp, ui: &mut Ui) { } } +/// The Style picker's field: the thumbnail of the style `linked` from the panel's thumbnails (`thumbs`, over the +/// shared `doc` and `looks`), white while the selection is linked to no style, as production's Style chip showed it. +fn linked_face(thumbs: StyleThumbs<'_>, doc: &Arc, looks: &Arc>, linked: Option<&str>) -> FacePaint { + use std::hash::{Hash, Hasher}; + let StyleThumbs { uid, revision, look, .. } = thumbs; + let linked = linked.unwrap_or_default().to_owned(); + let mut hasher = std::collections::hash_map::DefaultHasher::new(); + (uid, revision, &linked, look.preview, look.override_chars).hash(&mut hasher); + let (doc, looks) = (Arc::clone(doc), Arc::clone(looks)); + FacePaint::new(hasher.finish(), move |cx, rect| { + let thumbs = StyleThumbs { uid, revision, d: &doc, looks: &looks, look }; + paint_thumb(cx, rect, &thumbnails(cx.painter.ctx(), "vc-style-thumbs"), &thumbs, &linked); + }) +} + /// A new style from the selection, first asking for its name (`ask`) in Graphic Style Options. fn new_style(app: &mut VectorcraftApp, ask: bool) { if ask {