From 3a7e15649b9c25e6f95856fd8d1522117e00f01f Mon Sep 17 00:00:00 2001 From: ruchirguitar <113586074+ruchirguitar@users.noreply.github.com> Date: Sat, 5 Dec 2026 18:32:50 +0530 Subject: [PATCH] fix(ui): anchor control bar Stroke, Opacity, and Style popovers (#10233) --- crates/ui-egui/src/chrome/control_bar.rs | 40 ++++++++++++----- crates/ui-egui/src/chrome/tests.rs | 57 ++++++++++++++++++++++++ crates/ui-egui/src/panels/stroke.rs | 32 ++++++------- crates/ui-egui/src/tests/taskbar.rs | 28 ++++++++++++ 4 files changed, 130 insertions(+), 27 deletions(-) diff --git a/crates/ui-egui/src/chrome/control_bar.rs b/crates/ui-egui/src/chrome/control_bar.rs index 266726909a..c1fed7f9fa 100644 --- a/crates/ui-egui/src/chrome/control_bar.rs +++ b/crates/ui-egui/src/chrome/control_bar.rs @@ -6,10 +6,11 @@ use craft::ui::display::{Divider, Elide, Shade, Text}; use craft::ui::form::Field; use craft::ui::layout_components::Row; use craft::ui::menu::{Menu, MenuItem}; +use craft::ui::popover::Popover; use craft::ui::shell::{BarGrip, OptionsBarPlace}; use craft::ui::style::SurfaceKind; use craft::ui::text::NumberField; -use egui::Ui; +use egui::{Ui, vec2}; use serde_json::{Value, json}; use vectorcraft_doc::NodeKind; @@ -281,14 +282,17 @@ fn fill_and_stroke(app: &mut VectorcraftApp, ui: &mut Ui) { ranked(ui, rank::DETAIL, |ui| stroke_panel::profile_select(app, ui)); } -/// The Control bar's Opacity group: its link, which opens the Transparency panel and scrubs the field, and the -/// field, showing `opacity`. Only a selection takes the edit. +const PANEL_POPOVER_W: f32 = 260.0; +const PANEL_POPOVER_MIN_TALL: f32 = 200.0; + +/// The Control bar's Opacity group: its link, which opens the Transparency panel in a popover anchored to it +/// and scrubs the field, and the field, showing `opacity`. Only a selection takes the edit. fn opacity_field(app: &mut VectorcraftApp, ui: &mut Ui, opacity: f32, selected: bool) { let label = tl!("Opacity:"); let (mut link, mut typed) = (false, None); let target = crate::panels::transparency_target(app); group(ui, rank::OPACITY, |ui| { - Field::new(label).side(SideLabel::new().link().scrub(true)).show(ui, |ui, _| { + let field = Field::new(label).side(SideLabel::new().link().scrub(true)).show(ui, |ui, _| { let number = NumberField::new(label, Bind::given(Some(crate::panels::shown_percent(opacity)))) .range(0.0..=100.0) .unit("%") @@ -298,10 +302,19 @@ fn opacity_field(app: &mut VectorcraftApp, ui: &mut Ui, opacity: f32, selected: link |= number.link; typed = number.edit.events.committed().copied().flatten().or(typed); }); + let open_id = field.output.id.with("transparency_popover_open"); + let mut open = ui.data(|d| d.get_temp::(open_id).unwrap_or(false)); + if link { + open = !open; + } + Popover::new(tl!("Transparency"), Bind::bound(&mut open)).opened_by(&field.output).show(ui, |ui, _| { + ui.scope(|ui| { + ui.set_max_width(PANEL_POPOVER_W); + crate::panels::transparency::show(app, ui); + }); + }); + ui.data_mut(|d| d.insert_temp(open_id, open)); }); - if link { - app.ui.open_panel = Some("transparency".into()); - } if let Some(o) = typed.filter(|_| selected) { app.run("transparency.set", json!({"opacity": o.clamp(0.0, 100.0)})).ok(); } @@ -311,14 +324,17 @@ fn opacity_field(app: &mut VectorcraftApp, ui: &mut Ui, opacity: f32, selected: /// Global Edit Options. A narrow bar leaves out Global Edit Options first, then Recolor Artwork, then the Style /// link with its picker. fn style_group(app: &mut VectorcraftApp, ui: &mut Ui) { - let mut open = false; ranked(ui, rank::GROUP, |ui| { - open |= crate::panels::option_link(tl!("Style:")).show(ui).clicked(); + let link = crate::panels::option_link(tl!("Style:")).show(ui); + let tall = (ui.ctx().content_rect().height() * 0.6).max(PANEL_POPOVER_MIN_TALL); + Popover::new(tl!("Graphic Styles"), Bind::kept()).collection_body().overlay(|overlay| overlay.opened_by(&link, Trigger::Click)).show( + ui, + |ui, _| { + ui.allocate_ui(vec2(PANEL_POPOVER_W, tall), |ui| crate::panels::graphic_styles::show(app, ui)); + }, + ); crate::panels::graphic_styles::style_picker(app, ui); }); - if open { - app.ui.open_panel = Some("graphicStyles".into()); - } // Recolor Artwork, when the selection has colours to recolour. let colored = app.session.active().is_some_and(|st| crate::canvas::has_colors(&st.doc, &st.selection.objects)); if colored { diff --git a/crates/ui-egui/src/chrome/tests.rs b/crates/ui-egui/src/chrome/tests.rs index 24df04ceba..cb72280797 100644 --- a/crates/ui-egui/src/chrome/tests.rs +++ b/crates/ui-egui/src/chrome/tests.rs @@ -3629,3 +3629,60 @@ fn the_control_bar_selects_similar_objects() { bar.app().run("window.control.options", json!({"group": "objectOptions"})).unwrap(); assert!(bar.settle().find("Select Similar Objects").is_some(), "Object Options on again"); } + +/// Clicking Stroke:, Opacity: or Style: on the Control bar opens its panel in a popover anchored to that link rather +/// than at the window's left edge or in the dock flyout; Stroke: in the Control bar stays independent of Properties' +/// Stroke row. +#[test] +fn control_bar_panel_links_open_anchored_popovers() { + 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": 200})).unwrap(); + let id = app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 30, "height": 20})).unwrap()["id"].clone(); + app.run("select.set", json!({ "ids": [id] })).unwrap(); + let mut bar = Shown::new(app, egui::vec2(2400.0, 700.0), |app, ui| { + super::control_bar(app, ui); + crate::panels::properties::show(app, ui); + }); + bar.room(egui::vec2(2400.0, 700.0)).settle(); + + let assert_anchored = |bar: &Shown, link: &Target, content: &Target, label: &str| { + assert!(link.rect.left() > 80.0, "{label}: link {:?} sits past the left edge", link.rect); + assert!(content.rect.top() >= link.rect.bottom() - 2.0, "{label}: popover {:?} opens below {:?}", content.rect, link.rect); + assert!( + (content.rect.center().x - link.rect.center().x).abs() < 220.0 && content.rect.left() > 40.0, + "{label}: popover {:?} anchors horizontally near {:?}, not on the left side", + content.rect, + link.rect + ); + assert!(bar.app().ui.open_panel.is_none(), "{label}: opens as a popover rather than a dock flyout"); + }; + + // Stroke: opens its popover under the Control bar's link and leaves Properties' Stroke row closed. + let stroke_link = caption(&bar, "Stroke:").expect("Stroke: link"); + assert!(bar.find("Dashed line").is_none(), "Stroke popover starts closed"); + bar.click_on(&stroke_link).settle(); + let dashed: Vec<_> = bar.fixture.by_role(Role::CHECK_BOX).into_iter().filter(|t| bar.tree().name_of(t.node) == Some("Dashed line")).collect(); + assert_eq!(dashed.len(), 1, "Control bar and Properties keep separate Stroke popovers: {dashed:?}, names: {:?}", bar.names()); + assert_anchored(&bar, &stroke_link, &dashed[0], "Stroke:"); + bar.click_on(&stroke_link).settle(); + assert!(bar.find("Dashed line").is_none(), "a second click closes the Stroke popover"); + + // Opacity: opens Transparency in an anchored popover. + let opacity_link = caption(&bar, "Opacity:").expect("Opacity: link"); + assert!(bar.find("Isolate Blending").is_none(), "Transparency popover starts closed"); + bar.click_on(&opacity_link).settle(); + let isolate = bar.find("Isolate Blending").unwrap_or_else(|| panic!("Transparency popover opens: {:?}", bar.names())); + assert_anchored(&bar, &opacity_link, &isolate, "Opacity:"); + bar.click_on(&opacity_link).settle(); + assert!(bar.find("Isolate Blending").is_none(), "a second click closes Transparency"); + + // Style: opens Graphic Styles in an anchored popover. + let style_link = caption(&bar, "Style:").expect("Style: link"); + assert!(bar.find("Break Link to Graphic Style").is_none(), "Graphic Styles popover starts closed"); + bar.click_on(&style_link).settle(); + let break_link = bar.find("Break Link to Graphic Style").unwrap_or_else(|| panic!("Graphic Styles popover opens: {:?}", bar.names())); + assert_anchored(&bar, &style_link, &break_link, "Style:"); + bar.click_on(&style_link).settle(); + assert!(bar.find("Break Link to Graphic Style").is_none(), "a second click closes Graphic Styles"); +} diff --git a/crates/ui-egui/src/panels/stroke.rs b/crates/ui-egui/src/panels/stroke.rs index 8ea894b54a..007eeccb01 100644 --- a/crates/ui-egui/src/panels/stroke.rs +++ b/crates/ui-egui/src/panels/stroke.rs @@ -10,7 +10,9 @@ use craft::draw::ui::stroke::{self as draw, StrokeEditor, StrokeField, StrokeSec use craft::keymap::CommandRef; use craft::ui::buttons::IconButton; use craft::ui::choice::Segmented; -use craft::ui::contract::{Bind, EditResponse, Entry, FieldLabelRole, Props as _, Show as _, ShowContainer as _, SideLabel, Sizing, TooltipSpec}; +use craft::ui::contract::{ + Bind, EditResponse, Entry, FieldLabelRole, Inner, Props as _, Show as _, ShowContainer as _, SideLabel, Sizing, TooltipSpec, +}; use craft::ui::display::Divider; use craft::ui::form::{Field, FieldGroup}; use craft::ui::layout_components::{Grid, Row}; @@ -24,7 +26,7 @@ use craft::ui::runtime::env::{LengthUnits, UnitKind}; use craft::ui::style::DataColor; use craft::ui::text::{LinkMode, LinkedFields}; use craft::ui::text::{NumberField, NumberOutput, NumberPick, StepArrows}; -use egui::{Rect, Ui, pos2}; +use egui::{Rect, Response, Ui, pos2}; use serde_json::{Value, json}; use vectorcraft_color::{Color, Paint}; use vectorcraft_doc::{Appearance, ArrowAlign, Arrowhead, Dash, Document, LineCap, LineJoin, Node, StrokeAlign, StrokeLayer, Unit, WidthProfile}; @@ -56,8 +58,6 @@ pub(crate) struct StrokePanel { pub scale_linked: bool, /// The cache of [`brush_keeps_size`], keyed by (document uid, revision, active Appearance row). pub brushed: Option<(BrushedKey, Option)>, - /// The Stroke popover of the weight's link label is open. - pub link_open: bool, /// The running gesture of a Stroke panel select. pub select_gesture: Option, } @@ -447,24 +447,26 @@ pub(crate) fn apply_weight(app: &mut VectorcraftApp, out: &NumberOutput, target: }); } -/// The weight field in a row labelled `label` with the side label `side`: the output of the number field, none when -/// the row's body did not run. -fn weight_row(app: &VectorcraftApp, ui: &mut Ui, label: &'static str, side: SideLabel, weight: Option) -> Option { - Field::new(label).side(side).id_salt("weight").sizing(Sizing::Content).show(ui, |ui, _| weight_number(app, weight).show(ui)).inner +/// The weight field in a row labelled `label` with the side label `side`: the field response and the output of +/// the number field when the row's body ran. +fn weight_row(app: &VectorcraftApp, ui: &mut Ui, label: &'static str, side: SideLabel, weight: Option) -> Inner { + Field::new(label).side(side).id_salt("weight").sizing(Sizing::Content).show(ui, |ui, _| weight_number(app, weight).show(ui)) } /// The weight field whose link side label (`label`: the Control bar's "Stroke:", the Properties panel's "Stroke") -/// opens the Stroke panel in a popover under it, and whose drag scrubs the weight. The field keeps its name, Weight: -/// the label beside the Stroke chip says which paint it is the weight of. +/// opens the Stroke panel in a popover anchored to it, and whose drag scrubs the weight. The field keeps its name, +/// Weight: the label beside the Stroke chip says which paint it is the weight of. pub(crate) fn link_field(app: &mut VectorcraftApp, ui: &mut Ui, label: &'static str, weight: Option) { let side = SideLabel::new().scrub(true).link().role(FieldLabelRole::Describes).tooltip(TooltipSpec::new(tl!("Stroke options"))); - let mut open = app.ui.stroke_panel.link_open; - if let Some(out) = weight_row(app, ui, label, side, weight) { + let field = weight_row(app, ui, label, side, weight); + let open_id = field.output.id.with("stroke_popover_open"); + let mut open = ui.data(|d| d.get_temp::(open_id).unwrap_or(false)); + if let Some(out) = field.inner { apply_weight(app, &out, WeightTarget::Shown); open ^= out.link; } - Popover::new(tl!("Stroke"), Bind::bound(&mut open)).show(ui, |ui, _| show(app, ui)); - app.ui.stroke_panel.link_open = open; + Popover::new(tl!("Stroke"), Bind::bound(&mut open)).opened_by(&field.output).show(ui, |ui, _| show(app, ui)); + ui.data_mut(|d| d.insert_temp(open_id, open)); } /// The Stroke panel: the weight, the stroke editor (cap, corner, align, dashes), arrowheads, scale, align and the @@ -492,7 +494,7 @@ fn rows(app: &mut VectorcraftApp, ui: &mut Ui) { let st = current_stroke(app); let mixed = stroke_mixed(app, ui.ctx()); let weight = shown_weight(app, st.as_ref(), &mixed); - if let Some(out) = weight_row(app, ui, tl!("Weight"), SideLabel::new().scrub(true), weight) { + if let Some(out) = weight_row(app, ui, tl!("Weight"), SideLabel::new().scrub(true), weight).inner { apply_weight(app, &out, WeightTarget::Shown); } if app.ui.stroke_panel.hide_options { diff --git a/crates/ui-egui/src/tests/taskbar.rs b/crates/ui-egui/src/tests/taskbar.rs index 07f42b9c6c..f8b8ea9678 100644 --- a/crates/ui-egui/src/tests/taskbar.rs +++ b/crates/ui-egui/src/tests/taskbar.rs @@ -2245,3 +2245,31 @@ fn the_bar_is_outlined_in_the_accent_on_its_surface() { let surface = outlines.iter().find(|o| o.1 != accent).unwrap_or_else(|| panic!("the surface paints its own border: {outlines:?}")); assert_eq!(edged.0, surface.0, "the accent outline lies on the surface's own border"); } + +/// The Contextual Task Bar's Shape Properties popover anchors to its Shape Properties button rather than opening +/// on the left side of the window. +#[test] +fn the_task_bar_shape_properties_popover_anchors_to_its_button() { + let mut app = app(); + app.run("tool.select", json!({"tool": "selection"})).unwrap(); + let rect = id(app.run("shape.rectangle", json!({"x": 240, "y": 120, "width": 120, "height": 80, "radius": 10})).unwrap()); + select(&mut app, &[rect]); + on_canvas(&mut app, |rig| { + let button = rig.target("Shape Properties").expect("Shape Properties button"); + assert!(button.rect.left() > 100.0, "taskbar button {:?} sits past the left edge", button.rect); + rig.click("Shape Properties"); + let headings = rig.painted("Rectangle Properties:"); + assert_eq!(headings.len(), 1, "Rectangle Properties heading appears once: {headings:?}"); + let heading = headings[0]; + assert!( + heading.bottom() <= button.rect.top() + 2.0 || heading.top() >= button.rect.bottom() - 2.0, + "popover {heading:?} opens above or below {:?}", + button.rect + ); + assert!( + (heading.center().x - button.rect.center().x).abs() < 200.0 && heading.left() > 40.0, + "popover {heading:?} anchors horizontally near {:?}, not on the left side", + button.rect + ); + }); +}