diff --git a/crates/ui-egui/src/panels/properties/appearance_section.rs b/crates/ui-egui/src/panels/properties/appearance_section.rs new file mode 100644 index 0000000000..e52851e55d --- /dev/null +++ b/crates/ui-egui/src/panels/properties/appearance_section.rs @@ -0,0 +1,221 @@ +//! The Properties panel's Appearance section (Fill, Stroke, Variable Width Profile, Opacity and +//! the effect controls) and Global Edit, whose options the Control bar shows too. + +use craft::behavior::IconId; +use craft::keymap::CommandRef; +use craft::ui::buttons::{Button, IconButton}; +use craft::ui::choice::Checkbox; +use craft::ui::contract::{Bind, Props as _, Show as _, ShowContainer as _, Trigger, clicked}; +use craft::ui::display::{Elide, Shade, Text}; +use craft::ui::form::Field; +use craft::ui::layout_components::Row; +use craft::ui::menu::Menu; +use craft::ui::popover::Popover; +use craft::ui::sections::Section; +use craft::ui::style::TypeRole; +use craft::ui::text::{NumberField, TextField}; +use egui::{Response, Ui}; +use serde_json::{Value, json}; +use vectorcraft_doc::{Node, NodeId, WidthProfile}; +use vectorcraft_engine::cmd::globaledit; + +use crate::VectorcraftApp; +use crate::edits::{EditPath, apply_edits}; +use crate::panels::{self, appearance, first_selected, stroke}; + +/// The Appearance section for the first selected object: its Fill and Stroke, the stroke's width +/// profile, Opacity and the effect controls. +pub(super) fn appearance_section(app: &mut VectorcraftApp, ui: &mut Ui) { + let Some(n) = first_selected(app) else { return }; + Section::fixed(tl!("Appearance")).show(ui, |ui, _| { + fill_stroke_rows(app, ui); + width_profile_row(app, ui); + opacity_row(app, ui, &n); + effects_row(app, ui, &n); + }); +} + +/// The command button that shows `panel` in the dock, for the More Options icons. +fn panel_button<'a>(icon: &'static str, panel: &str, label: &'static str) -> IconButton<'a> { + IconButton::command(IconId::new(icon), CommandRef::with_params("window.panel.activate", json!({ "panel": panel }))).label(label) +} + +/// The Opacity field, and the icon that shows the Transparency panel. +fn opacity_row(app: &mut VectorcraftApp, ui: &mut Ui, n: &Node) { + Row::new().show(ui, |ui, _| { + let shown = Some(n.opacity as f64 * 100.0); + let out = Field::new(tl!("Opacity")) + .show(ui, |ui, _| NumberField::new(tl!("Opacity"), Bind::given(shown)).range(0.0..=100.0).unit("%").fixed_decimals(0).show(ui)) + .inner; + if let Some(out) = out { + apply_edits(app, &out.edit, EditPath::Scope, |app, value, _| { + if let Some(opacity) = value { + app.run("object.setProps", json!({ "opacity": opacity })).ok(); + } + }); + } + panel_button("ellipsis", "transparency", tl!("Transparency")).show(ui); + }); +} + +/// The fx button that opens the effect menu, as the Appearance panel's does, the Delete icon and +/// the icon that shows the Appearance panel. +fn effects_row(app: &mut VectorcraftApp, ui: &mut Ui, n: &Node) { + // The effects of one selected object: the fill's and stroke's as well as its own. + let effects: Vec<(Option, usize, &str)> = if panels::selection_len(app) == 1 { + let own = n.appearance.effects.iter().enumerate().map(|(k, e)| (None, k, e.id.as_str())); + let items = + n.appearance.items.iter().enumerate().flat_map(|(i, it)| it.effects().iter().enumerate().map(move |(k, e)| (Some(i), k, e.id.as_str()))); + own.chain(items).collect() + } else { + vec![] + }; + Row::new().show(ui, |ui, _| { + let fx = Button::new("fx").tooltip(tl!("Add New Effect")).show(ui); + Menu::new(tl!("Add New Effect")).opened_by(&fx.response, Trigger::Click).show(ui, |ui, _| appearance::fx_menu(app, ui)); + effect_delete(app, ui, n.id, &effects); + panel_button("ellipsis", "appearance", tl!("Appearance panel")).show(ui); + }); +} + +/// The Delete icon beside the fx button, as the Properties panel overview has it +/// (): +/// with one effect applied, its name and an icon that removes it (`effect.remove`); with several, +/// the icon shows the Appearance panel to remove them there. Nothing without effects. +fn effect_delete(app: &mut VectorcraftApp, ui: &mut Ui, id: NodeId, effects: &[(Option, usize, &str)]) { + match effects { + [] => {} + [(item, k, effect)] => { + Text::new(appearance::shown_effect_label(effect)).elide(Elide::End).show(ui); + if IconButton::new(IconId::new("trash-2")).label(tl!("Remove Effect")).show(ui).clicked() { + app.run("effect.remove", json!({"ids": [id.0], "item": item, "index": k})).ok(); + } + } + _ => { + panel_button("trash-2", "appearance", tl!("Remove Effect")).show(ui); + } + } +} + +/// The stroke's Variable Width Profile (the Control bar's list), shown while the Width tool is in +/// use or the stroke has a variable width, as the Properties panel overview places it +/// (). +fn width_profile_row(app: &mut VectorcraftApp, ui: &mut Ui) { + let stroke = panels::current_stroke(app); + let profile = stroke.as_ref().and_then(|s| s.profile.as_ref()); + if app.session.tool_id() != "width" && WidthProfile::id_of(profile) == "uniform" { + return; + } + Field::new(tl!("Profile:")).show(ui, |ui, _| stroke::profile_select(app, ui)); +} + +/// The Fill and Stroke rows, with the Control bar's widgets: the chips (a click opens the +/// swatches, Shift-click the mixer), the Stroke link that opens the Stroke panel as a popover +/// and the weight field with its presets. With nothing selected they set up the next object. +pub(super) fn fill_stroke_rows(app: &mut VectorcraftApp, ui: &mut Ui) { + let weight = stroke::shown_weight(app, panels::current_stroke(app).as_ref(), &panels::stroke_mixed(app, ui.ctx())); + Row::new().show(ui, |ui, _| { + panels::paint_chip(app, ui, false); + Text::new(tl!("Fill")).show(ui); + }); + Row::new().show(ui, |ui, _| { + panels::paint_chip(app, ui, true); + stroke::link_field(app, ui, tl!("Stroke"), weight); + }); +} + +/// Start Global Edit (Stop Global Edit while it is on) with the Global Edit Options beside it, in +/// Quick Actions as the help page places them +/// (). +pub(super) fn global_edit_row(app: &mut VectorcraftApp, ui: &mut Ui) { + if !crate::menus::enabled(app, "select.globalEdit") { + return; + } + let on = app.session.active().is_some_and(|d| d.global_edit.is_some()); + // A copy, so the popover's flag does not borrow `app` while its body runs commands. + let mut open = app.ui.properties_panel.global_edit_open; + Row::new().show(ui, |ui, _| { + // The command's own state gives its title: Start or Stop Global Edit. + Button::command("select.globalEdit").show(ui); + let options = IconButton::new(IconId::new("chevron-down")).label(tl!("Global Edit Options")).enabled(!on).show(ui); + global_edit_popover(app, ui, &options.response, &mut open); + }); + app.ui.properties_panel.global_edit_open = open; +} + +/// The Global Edit Options popover hanging from `opener`, a click on which toggles `open`. It is +/// closed while Global Edit is on, including when Global Edit starts with it open. The Control bar +/// shows the same popover with its own flag. +pub(crate) fn global_edit_popover(app: &mut VectorcraftApp, ui: &mut Ui, opener: &Response, open: &mut bool) { + if app.session.active().is_some_and(|d| d.global_edit.is_some()) { + *open = false; + } else if clicked(opener) { + *open = !*open; + } + Popover::new(tl!("Global Edit Options"), Bind::bound(open)).opened_by(opener).show(ui, |ui, _| global_edit_options(app, ui)); +} + +/// A checkbox of the Global Edit Options: the value a click gave it. +fn option_check(ui: &mut Ui, label: &'static str, on: bool) -> Option { + Checkbox::new(Bind::given(on)).label(label).show(ui).events.committed().copied() +} + +/// The Global Edit Options: what else similar objects must share (Match), where they may lie (On +/// Artboards) and how many objects that takes now. The Control bar's Global Edit Options dropdown +/// shows them too. +pub(crate) fn global_edit_options(app: &mut VectorcraftApp, ui: &mut Ui) { + let o = app.session.global_edit_options.clone(); + let set = |app: &mut VectorcraftApp, p: Value| crate::menus::invoke(app, "select.globalEditOptions", p); + let caption = |ui: &mut Ui, text: &'static str| { + Text::new(text).role(TypeRole::Caption).shade(Shade::Muted).show(ui); + }; + caption(ui, tl!("Match")); + if let Some(on) = option_check(ui, tl!("Appearance"), o.appearance) { + set(app, json!({ "appearance": on })); + } + if let Some(on) = option_check(ui, tl!("Size"), o.size) { + set(app, json!({ "size": on })); + } + caption(ui, tl!("On Artboards")); + if let Some(all) = option_check(ui, tl!("All Artboards"), o.artboards.is_none()) { + set(app, json!({ "artboards": if all { "all" } else { "1" } })); + } + if let Some(range) = &o.artboards { + let typed = Field::new(tl!("Range:")).show(ui, |ui, _| TextField::new(tl!("Range:"), Bind::given(range.clone())).show(ui)).inner; + if let Some(range) = typed.and_then(|t| t.events.committed().cloned()) { + set(app, json!({ "artboards": range })); + } + } + if let Some(on) = option_check(ui, tl!("Include Objects On Canvas"), o.include_canvas) { + set(app, json!({ "includeCanvas": on })); + } + Text::new(global_edit_results(global_edit_count(app))).shade(Shade::Muted).show(ui); +} + +/// The options' line with how many objects Start Global Edit would take, in the interface language. +pub(crate) fn global_edit_results(n: usize) -> String { + crate::i18n::tn(n as u64, "Result: {n}", "Results: {n}") +} + +/// What the Results count was found for: the document, its revision, the options and the one +/// selected object. +pub(crate) type GlobalEditKey = (u64, u64, globaledit::Options, Option); + +/// How many objects Start Global Edit would take now (the options' Results line), kept in +/// `app.canvas.global_edit_count` per [`GlobalEditKey`], as finding them walks the whole document. +pub(crate) fn global_edit_count(app: &mut VectorcraftApp) -> usize { + let Some(st) = app.session.active() else { return 0 }; + let selected = match st.selection.objects.as_slice() { + [id] => Some(id.0), + _ => None, + }; + let key: GlobalEditKey = (st.uid, st.revision, app.session.global_edit_options.clone(), selected); + if let Some((k, count)) = &app.canvas.global_edit_count + && *k == key + { + return *count; + } + let count = globaledit::match_count(&app.session); + app.canvas.global_edit_count = Some((key, count)); + count +} diff --git a/crates/ui-egui/src/panels/properties/transform_section.rs b/crates/ui-egui/src/panels/properties/transform_section.rs new file mode 100644 index 0000000000..83a6bd6c8c --- /dev/null +++ b/crates/ui-egui/src/panels/properties/transform_section.rs @@ -0,0 +1,118 @@ +//! The Properties panel's Transform section (X, Y, W, H, Rotate, the flips, More Options and a live +//! shape's rows), the Corner rows of Direct-Selected corners and the Transform section of ruler +//! guides selected on their own. + +use craft::behavior::{IconId, Mixed}; +use craft::draw::ui::TransformGroups; +use craft::ui::buttons::IconButton; +use craft::ui::choice::Checkbox; +use craft::ui::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _, clicked}; +use craft::ui::display::{Shade, Text}; +use craft::ui::form::Field; +use craft::ui::layout_components::Row; +use craft::ui::pickers::Select; +use craft::ui::popover::Popover; +use craft::ui::sections::Section; +use egui::Ui; +use serde_json::json; +use vectorcraft_doc::{LiveCorners, LiveShape, NodeKind}; +use vectorcraft_geom::shapes::CornerKind; + +use crate::VectorcraftApp; +use crate::panels::{self, first_selected, transform}; + +/// The Transform section for the selection: its position and size about the reference point, the +/// rotation, the two flips and the More Options popover, then the rows of a selected live shape. +/// "No Selection" in place of the section when nothing is selected. +pub fn transform_section(app: &mut VectorcraftApp, ui: &mut Ui) { + if app.session.active().is_none() { + return; + } + if app.selection_box().is_none() { + Text::new(tl!("No Selection")).shade(Shade::Muted).show(ui); + return; + } + Section::fixed(tl!("Transform")).show(ui, |ui, _| { + let mut groups = TransformGroups::NONE; + groups.reference = true; + groups.position = true; + groups.size = true; + transform::object_fields(app, ui, groups); + Row::new().show(ui, |ui, _| { + transform::rotate_field(app, ui); + // The icons are named for their mirror line: `flip-vertical-2` draws a vertical line, the + // left-right flip, which the Properties panel calls Flip Along Horizontal Axis (#1910). + IconButton::command(IconId::new("flip-vertical-2"), "ui.transform.flipHorizontal").label(tl!("Flip Along Horizontal Axis")).show(ui); + IconButton::command(IconId::new("flip-horizontal-2"), "ui.transform.flipVertical").label(tl!("Flip Along Vertical Axis")).show(ui); + more_options(app, ui); + }); + if let Some(n) = first_selected(app) + && let NodeKind::Path { live: Some(live), .. } = &n.kind + { + transform::live_shape_rows(app, ui, &n, live, false); + } + }); +} + +/// The More Options icon and its popover: the Transform panel's Scale Corners and Scale Strokes & +/// Effects, which stay open while they are toggled. +fn more_options(app: &mut VectorcraftApp, ui: &mut Ui) { + // A copy, so the popover's binding does not borrow `app` while its body runs commands. + let mut open = app.ui.properties_panel.more_options_open; + let more = IconButton::new(IconId::new("ellipsis")).label(tl!("More Options")).expanded(open).show(ui); + if clicked(&more.response) { + open = !open; + } + let prefs = &app.session.prefs; + let rows = [(tl!("Scale Corners"), "scaleCorners", prefs.scale_corners), (tl!("Scale Strokes & Effects"), "scaleStrokes", prefs.scale_strokes)]; + Popover::new(tl!("More Options"), Bind::bound(&mut open)).opened_by(&more.response).show(ui, |ui, _| { + for (label, key, on) in rows { + if let Some(on) = Checkbox::new(Bind::given(on)).label(label).show(ui).events.committed().copied() { + transform::set_pref(app, key, on); + } + } + }); + app.ui.properties_panel.more_options_open = open; +} + +/// The Corners of the one path whose corners Direct Selection has selected: their kind and radius, +/// which `object.setLiveShape` sets on those corners. The Properties panel overview shows the +/// control while the Direct Selection tool is chosen and a corner of an object is selected +/// (). +/// A live rectangle's and a live polygon's own rows already set the selected corners. +pub(super) fn corner_rows(app: &mut VectorcraftApp, ui: &mut Ui) { + if app.session.tool_id() != "directSelection" { + return; + } + let Some(st) = app.session.active() else { return }; + let [id] = st.selection.objects[..] else { return }; + let Some(n) = st.doc.node(id) else { return }; + if matches!(n.live_shape(), Some(LiveShape::Rectangle { .. } | LiveShape::Polygon { .. })) { + return; + } + let Some(lc) = LiveCorners::of(n) else { return }; + if st.selection.partial(id).is_none_or(|a| lc.corners_of(a).is_empty()) { + return; + } + let n = n.clone(); + // The corners differ in kind: no entry is the value. + let kind = panels::corner_style(app, &n).1.map_or(Mixed::Mixed, Mixed::Same); + let entries = corner_entries(); + let picked = Field::new(tl!("Corner:")).show(ui, |ui, _| Select::new(tl!("Corner"), Bind::given(kind), &entries).show(ui).value().copied()).inner; + if let Some(Some(kind)) = picked { + app.run("object.setLiveShape", json!({"kind": kind})).ok(); + } + Field::new(tl!("Corner Radius:")).show(ui, |ui, _| panels::corner_radius_field(app, ui, &n)); +} + +// PLAN: `panels::corner_entries()` (hub-w2's request) is not in the tree; this is the one copy until it lands, then it is deleted. +/// One entry per corner kind, labelled as the Corners dialog labels them. +fn corner_entries() -> Vec> { + CornerKind::ALL.iter().map(|k| Entry::new(*k, crate::dialogs::corners::kind_label(*k))).collect() +} + +/// Ruler guides selected on their own: a Transform section with the X of the vertical ones and +/// the Y of the horizontal ones, which move them, as the Transform panel shows them (#1597). +pub(super) fn guide_sections(app: &mut VectorcraftApp, ui: &mut Ui, guides: (Option, Option)) { + Section::fixed(tl!("Transform")).show(ui, |ui, _| transform::guide_fields(app, ui, guides)); +}