diff --git a/crates/engine/src/cmd/wordart.rs b/crates/engine/src/cmd/wordart.rs index 2f92679311..de9dcb61ea 100644 --- a/crates/engine/src/cmd/wordart.rs +++ b/crates/engine/src/cmd/wordart.rs @@ -47,8 +47,8 @@ fn theme(s: &Session) -> Vec { } /// The text box story the selection formats as a whole: the selected shape's, or the one the -/// caret is in. -fn box_story(s: &Session) -> Option { +/// caret is in. The ribbon reads the current effects from the same place. +pub fn box_story(s: &Session) -> Option { if let Some((_, InlineObject::Shape { story: Some(id), .. })) = super::objects::object_selection(s) { return Some(*id); } diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index ebdcd26cde..813d239e4e 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -35,6 +35,7 @@ use charts::{chart_design, smart_art_design}; pub(crate) use common::mi; #[cfg(test)] pub(crate) use common::size_menu; +pub(crate) use common::weight_menu; use common::{custom_groups, edge_cue}; use design::design; #[cfg(test)] diff --git a/crates/ui-egui/src/ribbon/common.rs b/crates/ui-egui/src/ribbon/common.rs index da7c640bdd..84c092c4d6 100644 --- a/crates/ui-egui/src/ribbon/common.rs +++ b/crates/ui-egui/src/ribbon/common.rs @@ -1,5 +1,5 @@ use super::language::mi_check; -use super::{Align2, MeasureExt, Rect, Sense, Tokens, Ui, Value, WordApp, big, group, json, small, vec2}; +use super::{Align2, MeasureExt, Rect, Sense, Stroke, Tokens, Ui, Value, WordApp, big, group, json, pos2, small, vec2}; /// Custom groups (Customize Ribbon, #379): up to three commands as large buttons, more as small /// labelled buttons in columns of three. Each shows the command's own icon and label. @@ -145,6 +145,38 @@ pub(crate) fn size_menu(ui: &mut Ui, app: &mut WordApp) { mi(ui, app, crate::dialogs::PAPER_LABELS.more, "ui.dialog", json!({"name": "pageSetup"})); } +/// The outline weights Word offers, in points: Text Outline, Shape Outline and the border dialogs. +const OUTLINE_WEIGHTS: [f32; 9] = [0.25, 0.5, 0.75, 1.0, 1.5, 2.25, 3.0, 4.5, 6.0]; + +/// An outline's Weight submenu (#3917): each weight as its label and a line sample drawn at that +/// weight, the `current` one highlighted. Choosing one runs `id` with `params(weight)`. +pub(crate) fn weight_menu(ui: &mut Ui, app: &mut WordApp, id: &str, current: Option, params: impl Fn(f32) -> Value) { + let enabled = crate::widgets::enabled(app, id); + let t = Tokens::get(ui.ctx()); + let color = if enabled { t.text } else { t.text_disabled }; + // A hairline under a device pixel would vanish, so the thinnest samples get one. + let pixel = 1.0 / ui.ctx().pixels_per_point(); + let labels = OUTLINE_WEIGHTS.map(|w| crate::i18n::fmt(tl!("{n} pt"), &[("n", &w.to_string())])); + // The samples start a short gap after the widest label and end as far from the right edge as + // the labels start from the left one. + let font = egui::TextStyle::Button.resolve(ui.style()); + let widest = labels.iter().map(|l| ui.painter().layout_no_wrap(l.clone(), font.clone(), color).size().x).fold(0.0, f32::max); + let pad = ui.spacing().button_padding.x; + for (w, label) in OUTLINE_WEIGHTS.into_iter().zip(&labels) { + let checked = current.is_some_and(|c| (c - w).abs() < 0.01); + let resp = ui.add_enabled(enabled, egui::Button::new(label.as_str()).selected(checked).min_size(vec2(200.0, 22.0))); + resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::Button, enabled, checked, label)); + let r = resp.rect; + let y = r.center().y; + let (x0, x1) = (r.left() + pad + widest + 12.0, r.right() - pad); + ui.painter().line_segment([pos2(x0, y), pos2(x1, y)], Stroke::new(w.max(pixel), color)); + if resp.clicked() { + crate::widgets::run_from_ribbon(app, id, params(w)); + ui.close(); + } + } +} + pub(crate) fn mi(ui: &mut Ui, app: &mut WordApp, label: &str, id: &str, params: Value) { mi_text(ui, app, tl!(label), id, params); } diff --git a/crates/ui-egui/src/ribbon/shape.rs b/crates/ui-egui/src/ribbon/shape.rs index c5c971ac60..c08644f773 100644 --- a/crates/ui-egui/src/ribbon/shape.rs +++ b/crates/ui-egui/src/ribbon/shape.rs @@ -1,5 +1,5 @@ use super::arrange::{align_menu, rotate_menu, rotation_field, wrap_menu}; -use super::common::{mi, size_field, stack}; +use super::common::{mi, size_field, stack, weight_menu}; use super::language::mi_check; use super::{Target, Ui, WordApp, big, group, json, menu_button, small, vec2}; @@ -43,12 +43,28 @@ pub(crate) fn gradient_menu(ui: &mut Ui, app: &mut WordApp, fill: Option<&wordcr } } +/// The outline (colour, width) Shape Outline changes: the selected shape's, or the selected +/// drawing canvas's border. +pub(super) fn selected_outline(s: &wordcraft_engine::Session) -> Option<(wordcraft_doc::Rgb, f32)> { + match wordcraft_engine::cmd::objects::selected(s)?.1 { + wordcraft_doc::para::InlineObject::Shape { stroke, stroke_width, .. } => stroke.map(|c| (c, stroke_width)), + o => o.canvas_style().and_then(|c| c.stroke.map(|stroke| (stroke, c.stroke_width))), + } +} + +/// Shape Outline › Weight: a new weight keeps the outline's colour (black when it has none). +pub(super) fn shape_weight_menu(ui: &mut Ui, app: &mut WordApp) { + let outline = selected_outline(&app.session); + let color = outline.map_or(wordcraft_doc::Rgb::BLACK, |o| o.0).hex(); + weight_menu(ui, app, "shape.outline", outline.map(|o| o.1), |w| json!({"color": color, "width": w})); +} + /// Shape Format: fill, outline and effects of the selected shape, and its arrangement. pub(super) fn shape_format(app: &mut WordApp, ui: &mut Ui) { - let (stroke, glow_size, fill) = match wordcraft_engine::cmd::objects::selected(&app.session) { - Some((_, wordcraft_doc::para::InlineObject::Shape { stroke, effects, fill, .. })) => (stroke, effects.glow.map(|g| g.size), fill), - Some((_, o)) => (None, None, o.canvas_style().and_then(|c| c.fill.clone())), - _ => (None, None, None), + let (glow_size, fill) = match wordcraft_engine::cmd::objects::selected(&app.session) { + Some((_, wordcraft_doc::para::InlineObject::Shape { effects, fill, .. })) => (effects.glow.map(|g| g.size), fill), + Some((_, o)) => (None, o.canvas_style().and_then(|c| c.fill.clone())), + _ => (None, None), }; let theme = app.session.doc.settings.theme_colors.clone(); group(ui, "Shape Styles", None, app, |ui, app| { @@ -72,12 +88,7 @@ pub(super) fn shape_format(app: &mut WordApp, ui: &mut Ui) { mi(ui, app, "No Color", "shape.outline", json!({"color": null})); crate::colors::menu(ui, app, &theme, Target::cmd("shape.outline")); ui.separator(); - ui.menu_button(tl!("Weight"), |ui| { - let color = stroke.unwrap_or(wordcraft_doc::Rgb::BLACK).hex(); - for w in [0.25, 0.5, 0.75, 1.0, 1.5, 2.25, 3.0, 4.5, 6.0] { - mi(ui, app, &format!("{w} pt"), "shape.outline", json!({"color": color, "width": w})); - } - }); + ui.menu_button(tl!("Weight"), |ui| shape_weight_menu(ui, app)); }, ); menu_button( diff --git a/crates/ui-egui/src/ribbon/tests.rs b/crates/ui-egui/src/ribbon/tests.rs index e5e4f5b6b1..256db700c3 100644 --- a/crates/ui-egui/src/ribbon/tests.rs +++ b/crates/ui-egui/src/ribbon/tests.rs @@ -199,6 +199,68 @@ fn design_style_set_gallery_reads_from_the_right_in_rtl() { } } +/// #3917: an outline's Weight submenu offers Word's nine weights, each a named entry with a +/// line sample drawn at its weight; the current weight is highlighted, and choosing one sets it. +#[test] +fn weight_menu_draws_each_weight_and_applies_it() { + use egui::accesskit::Toggled; + use egui_kittest::kittest::{NodeT, Queryable}; + let mut a = WordApp::new(Session::new(wordcraft_doc::Document::from_text("Lanterns")), crate::Services::default()); + a.run("select.all", json!({})).unwrap(); + let mut h = egui_kittest::Harness::builder().with_size(vec2(400.0, 400.0)).build_ui_state( + |ui, app: &mut WordApp| { + let current = crate::wordart_menus::current_outline(app).map(|c| c.1); + weight_menu(ui, app, "format.textEffects", current, |w| json!({"outline": {"color": "1F4E79", "width": w}})); + }, + a, + ); + h.run_steps(2); + let labels = ["0.25 pt", "0.5 pt", "0.75 pt", "1 pt", "1.5 pt", "2.25 pt", "3 pt", "4.5 pt", "6 pt"]; + for label in labels { + let node = h.get_by_label(label); + assert_eq!(node.accesskit_node().toggled(), Some(Toggled::False), "{label} not highlighted with no outline set"); + } + // One line sample per weight, at that weight. + let widths: Vec = h + .output() + .shapes + .iter() + .filter_map(|s| match &s.shape { + egui::Shape::LineSegment { stroke, .. } => Some(stroke.width), + _ => None, + }) + .collect(); + assert_eq!(widths, [0.25, 0.5, 0.75, 1.0, 1.5, 2.25, 3.0, 4.5, 6.0].map(|w: f32| w.max(1.0)), "samples, a pixel at least"); + let outline = |h: &egui_kittest::Harness<'_, WordApp>| crate::wordart_menus::current_outline(h.state()); + for (label, width) in [("6 pt", 6.0), ("0.25 pt", 0.25)] { + h.get_by_label(label).click(); + h.run_steps(2); + assert_eq!(outline(&h), Some((wordcraft_doc::Rgb::parse("1F4E79").unwrap(), width)), "{label}"); + assert_eq!(h.get_by_label(label).accesskit_node().toggled(), Some(Toggled::True), "{label} is highlighted once chosen"); + assert_eq!(h.get_by_label("1 pt").accesskit_node().toggled(), Some(Toggled::False)); + } +} + +/// #3917 review: Shape Outline › Weight on a drawing canvas reads the canvas's border, so its +/// weight is highlighted and a new weight keeps the border's colour instead of turning it black. +#[test] +fn shape_outline_weight_keeps_a_canvas_borders_colour() { + use super::shape::{selected_outline, shape_weight_menu}; + use egui::accesskit::Toggled; + use egui_kittest::kittest::{NodeT, Queryable}; + let mut a = WordApp::new(Session::new(wordcraft_doc::Document::new()), crate::Services::default()); + a.run("insert.canvas", json!({})).unwrap(); + a.run("shape.outline", json!({"color": "1F4E79", "width": 1.5})).unwrap(); + let blue = wordcraft_doc::Rgb::parse("1F4E79"); + assert_eq!(selected_outline(&a.session), blue.map(|c| (c, 1.5)), "the canvas's border"); + let mut h = egui_kittest::Harness::builder().with_size(vec2(400.0, 400.0)).build_ui_state(|ui, app: &mut WordApp| shape_weight_menu(ui, app), a); + h.run_steps(2); + assert_eq!(h.get_by_label("1.5 pt").accesskit_node().toggled(), Some(Toggled::True), "the border's weight"); + h.get_by_label("3 pt").click(); + h.run_steps(2); + assert_eq!(selected_outline(&h.state().session), blue.map(|c| (c, 3.0)), "a new weight keeps the border blue"); +} + /// #2738: Home › Underline's drop-down: every line sample is a named entry (assistive /// technology, tooltips) that applies its own style; Words only, None and Underline Color › /// Automatic do what they say. diff --git a/crates/ui-egui/src/wordart_menus.rs b/crates/ui-egui/src/wordart_menus.rs index 78f51761d9..25ef63f3ea 100644 --- a/crates/ui-egui/src/wordart_menus.rs +++ b/crates/ui-egui/src/wordart_menus.rs @@ -132,9 +132,7 @@ pub fn outline_menu(ui: &mut Ui, app: &mut WordApp, theme: &[Rgb], current: Opti ui.separator(); ui.menu_button(crate::tl!("Weight"), |ui| { let color = current.map_or(Rgb::BLACK, |c| c.0).hex(); - for w in [0.25, 0.5, 0.75, 1.0, 1.5, 2.25, 3.0] { - mi(ui, app, &format!("{w} pt"), "format.textEffects", json!({"outline": {"color": color, "width": w}})); - } + crate::ribbon::weight_menu(ui, app, "format.textEffects", current.map(|c| c.1), |w| json!({"outline": {"color": color, "width": w}})); }); } @@ -209,14 +207,75 @@ pub fn transform_menu(ui: &mut Ui, app: &mut WordApp) { }); } -/// The selected shape's text outline (colour, width), for the Weight menu. +/// The text outline (colour, width) Text Outline would change, for its Weight menu: read where +/// `format.textEffects` applies, so choosing a weight keeps that text's colour. A selected shape, +/// or a caret in a text box, formats the box's whole text, read from its start; otherwise the +/// selection, read as typing reads it (the first selected character, or the caret's). pub fn current_outline(app: &WordApp) -> Option<(Rgb, f32)> { - let story = match wordcraft_engine::cmd::objects::object_selection(&app.session) { - Some((_, wordcraft_doc::InlineObject::Shape { story: Some(id), .. })) => wordcraft_doc::StoryRef::Part(*id), - _ => app.session.sel.focus.story, + let s = &app.session; + let props = match wordcraft_engine::cmd::wordart::box_story(s) { + Some(id) => s.doc.para_at(&s.doc.start_of(wordcraft_doc::StoryRef::Part(id)))?.props_at(0).clone(), + None => s.typing_props(), }; - let start = app.session.doc.start_of(story); - let para = app.session.doc.para_at(&start)?; - let fx = para.props_at(0).text_effects.as_deref()?; + let fx = props.text_effects.as_deref()?; fx.outline.and_then(|o| o.color.map(|c| (c, o.width))) } + +#[cfg(test)] +mod tests { + use super::*; + use wordcraft_engine::Session; + + fn outline_at(app: &WordApp, para: usize) -> Option<(Rgb, f32)> { + let p = app.session.doc.para_at(&wordcraft_doc::Pos::body(para, 0))?; + let fx = p.props_of_char(0).text_effects.as_deref()?; + fx.outline.and_then(|o| o.color.map(|c| (c, o.width))) + } + + /// #3917 review: the Weight menu reads the outline where Text Outline applies, the selection, + /// not the first character of the story. Its highlight is the selected text's weight, and a + /// new weight keeps that text's colour and leaves other paragraphs alone. + #[test] + fn the_weight_menu_reads_and_keeps_the_selected_texts_outline() { + use egui::accesskit::Toggled; + use egui_kittest::kittest::{NodeT, Queryable}; + let rgb = |h: &str| Rgb::parse(h); + let mut a = WordApp::new(Session::new(wordcraft_doc::Document::from_text("Red line\nBlue line")), crate::Services::default()); + a.run("select.text", json!({"text": "Red line"})).unwrap(); + a.run("format.textEffects", json!({"outline": {"color": "C00000", "width": 1.0}})).unwrap(); + a.run("select.text", json!({"text": "Blue line"})).unwrap(); + a.run("format.textEffects", json!({"outline": {"color": "1F4E79", "width": 3.0}})).unwrap(); + assert_eq!(current_outline(&a), rgb("1F4E79").map(|c| (c, 3.0)), "the selection's outline, not the first paragraph's"); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(500.0, 700.0)).build_ui_state( + |ui, app: &mut WordApp| { + let theme = app.session.doc.settings.theme_colors.clone(); + let current = current_outline(app); + outline_menu(ui, app, &theme, current); + }, + a, + ); + h.run_steps(2); + h.get_by_label("Weight").hover(); + h.run(); + h.get_by_label("Weight").click(); + h.run(); + assert_eq!(h.get_by_label("3 pt").accesskit_node().toggled(), Some(Toggled::True), "the selected text's weight"); + assert_eq!(h.get_by_label("1 pt").accesskit_node().toggled(), Some(Toggled::False)); + h.get_by_label("6 pt").click(); + h.run_steps(2); + assert_eq!(outline_at(h.state(), 1), rgb("1F4E79").map(|c| (c, 6.0)), "the new weight keeps the selected text's colour"); + assert_eq!(outline_at(h.state(), 0), rgb("C00000").map(|c| (c, 1.0)), "the other paragraph is untouched"); + } + + /// A caret in a text box formats the box's whole text, so its outline is read from the box's + /// start; back in the body, the body text's (none here). + #[test] + fn a_caret_in_a_text_box_reads_the_boxs_outline() { + let mut a = WordApp::new(Session::new(wordcraft_doc::Document::from_text("Body")), crate::Services::default()); + a.run("insert.textBox", json!({"text": "Boxed words"})).unwrap(); + a.run("format.textEffects", json!({"outline": {"color": "00B050", "width": 2.25}})).unwrap(); + assert_eq!(current_outline(&a), Rgb::parse("00B050").map(|c| (c, 2.25))); + a.run("caret.set", json!({"pos": wordcraft_doc::Pos::body(0, 2)})).unwrap(); + assert_eq!(current_outline(&a), None, "the body text has no outline"); + } +}