diff --git a/crates/color-ui/src/ui/picker_dialog.rs b/crates/color-ui/src/ui/picker_dialog.rs index 98e6125e2c..e646baf396 100644 --- a/crates/color-ui/src/ui/picker_dialog.rs +++ b/crates/color-ui/src/ui/picker_dialog.rs @@ -24,7 +24,7 @@ use craft_ui::dialog::{ ButtonPlace, Dialog, DialogId, DialogResult, DialogStack, FieldRefusal, StackDialog, dialog_id, serde_fields, set_serde_field, }; use craft_ui::layout::{CellAlign, GridFit}; -use craft_ui::layout_components::{Grid, Stack}; +use craft_ui::layout_components::{Grid, Row, Stack}; use craft_ui::notices::{Notice, NoticeLayout}; use craft_ui::patterns::form_dialogs::{HostRequests, ResetFooter}; use craft_ui::pickers::Select; @@ -41,7 +41,7 @@ use std::borrow::Cow; use std::sync::Arc; use super::picker::TWO_COLUMNS; -use super::{ColorPicker, ColorPickerLayout, ColorWarning, Gamut, PickedColor, ScreenPickGesture, SwatchPalette}; +use super::{ColorPicker, ColorPickerLayout, ColorPickerWords, ColorWarning, CompareSwatch, Gamut, PickedColor, ScreenPickGesture, SwatchPalette}; use crate::{AreaMode, CmykModel, not_web_safe, web_safe}; type StandardColors = (Vec<(Label, Srgba)>, Vec<(Label, Srgba)>); @@ -318,27 +318,47 @@ impl TabbedBody<'_> { let (cells, greys) = self.standard.map_or((&[][..], &[][..]), |(cells, greys)| (cells.as_slice(), greys.as_slice())); let current = held.same().map(|picked| picked.color); let mut pick = None; - Stack::new().show(ui, |ui, _| { - let honeycomb = SwatchTable::new(self.words.standard_colors.clone(), cells) - .arrangement(SwatchArrangement::Honeycomb) - .tips(true) - .selection(Bind::given(chosen(cells, current))) - .show_all(ui); - if let Some(at) = honeycomb.selected { - pick = cells.get(at).map(|(_, color)| *color); - } - let row = SwatchTable::new(self.words.greys.clone(), greys) - .per_row(greys.len().max(1)) - .tips(true) - .selection(Bind::given(chosen(greys, current))) - .show_all(ui); - if let Some(at) = row.selected { - pick = greys.get(at).map(|(_, color)| *color); + let mut restore = None; + // The compare block stands beside the honeycomb, as it does beside the Custom page's area. + Row::new().show(ui, |ui, _| { + Stack::new().show(ui, |ui, _| { + let honeycomb = SwatchTable::new(self.words.standard_colors.clone(), cells) + .arrangement(SwatchArrangement::Honeycomb) + .tips(true) + .selection(Bind::given(chosen(cells, current))) + .show_all(ui); + if let Some(at) = honeycomb.selected { + pick = cells.get(at).map(|(_, color)| *color); + } + let row = SwatchTable::new(self.words.greys.clone(), greys) + .per_row(greys.len().max(1)) + .tips(true) + .selection(Bind::given(chosen(greys, current))) + .show_all(ui); + if let Some(at) = row.selected { + pick = greys.get(at).map(|(_, color)| *color); + } + }); + if let Some(compare) = self.compare { + let new = held.same().map_or(Mixed::Mixed, |picked| Mixed::Same(picked.color)); + let current = match compare { + Mixed::Same(current) => Mixed::Same(current.color), + Mixed::Mixed => Mixed::Mixed, + }; + let label = Env::of(ui).words::().new_color.clone(); + if CompareSwatch::new(label, new, current).show(ui).restore + && let Mixed::Same(current) = compare + { + restore = Some(current); + } } }); if let Some(color) = pick { *held = Mixed::Same(PickedColor::rgb(color)); } + if let Some(current) = restore { + *held = Mixed::Same(current); + } } /// The compact picker over the colour, the compare swatch beside its area; the model switch is the memory's. diff --git a/crates/color-ui/tests/it/picker.rs b/crates/color-ui/tests/it/picker.rs index 6e6ab0d54a..d6dc9368a8 100644 --- a/crates/color-ui/tests/it/picker.rs +++ b/crates/color-ui/tests/it/picker.rs @@ -597,6 +597,15 @@ fn the_tabbed_colors_dialog_opens_on_its_tab_and_answers_from_either_page() { fixture.click(&cell); fixture.idle(4); assert_eq!(dialog_color(&dialogs).as_deref(), Some("#0000ff"), "a honeycomb pick is the colour"); + // #7984: the Standard page shows the compare block too, and its current half restores the opening colour. + assert!(fixture.by_label("New color").is_some(), "the Standard page shows the compare block"); + let restore = fixture.by_label("Restore current color").expect("the compare block's current half"); + fixture.click(&restore); + fixture.idle(4); + assert_eq!(dialog_color(&dialogs).as_deref(), Some("#123456"), "the current half restores the opening colour"); + let cell = fixture.by_label("Standard 2").expect("the honeycomb shows its cells"); + fixture.click(&cell); + fixture.idle(4); assert!(dialogs.borrow_mut().confirm::(&ctx), "OK answers"); let answered = dialogs.borrow_mut().take::().and_then(DialogResult::ok).map(|result| result.picked.color.to_hex()); diff --git a/crates/menu/src/ribbon/gallery.rs b/crates/menu/src/ribbon/gallery.rs index 683fb76fb2..08f9683a10 100644 --- a/crates/menu/src/ribbon/gallery.rs +++ b/crates/menu/src/ribbon/gallery.rs @@ -226,6 +226,8 @@ pub enum GalleryFace<'a> { thumbnails: &'a Thumbnails, /// What renders the thumbnail. source: &'a dyn ThumbSource, + /// Whether the transparency grid shows behind transparent pixels. + checker: bool, }, /// A tile painted directly. Paint(&'a dyn Fn(&ContentPainterCx, Rect)), @@ -250,7 +252,17 @@ impl<'a> GalleryFace<'a> { /// A thumbnail under `key`, rendered by `source` into `thumbnails`. pub fn rendered(key: u64, thumbnails: &'a Thumbnails, source: &'a dyn ThumbSource) -> Self { - GalleryFace::Rendered { key, thumbnails, source } + GalleryFace::Rendered { key, thumbnails, source, checker: true } + } + + /// Whether a rendered thumbnail shows the transparency grid behind its transparent pixels (default on). Off for a + /// thumbnail that is a mask on the surface it sits on, such as an equation symbol. Other faces ignore it. + #[must_use] + pub fn checker(mut self, on: bool) -> Self { + if let GalleryFace::Rendered { checker, .. } = &mut self { + *checker = on; + } + self } /// The picture this face shows, `size` high and `aspect` times as wide. It is decoration: the tile that holds it @@ -258,7 +270,9 @@ impl<'a> GalleryFace<'a> { fn picture(&self, aspect: ThumbAspect, size: ThumbSize) -> Picture<'a> { let picture = match self { GalleryFace::Image { key, revision, image } => Picture::new(Label::default(), *key, *image).revision(*revision), - GalleryFace::Rendered { key, thumbnails, source } => Picture::rendered(Label::default(), *key, thumbnails, *source), + GalleryFace::Rendered { key, thumbnails, source, checker } => { + Picture::rendered(Label::default(), *key, thumbnails, *source).checker(*checker) + } GalleryFace::Paint(paint) => { let paint = *paint; Picture::painter(Label::default(), aspect, move |cx: &ContentPainterCx<'_>, rect: Rect| paint(cx, rect)) diff --git a/crates/menu/src/ribbon/gallery/stories.rs b/crates/menu/src/ribbon/gallery/stories.rs index 786f074e2b..2a80c79e78 100644 --- a/crates/menu/src/ribbon/gallery/stories.rs +++ b/crates/menu/src/ribbon/gallery/stories.rs @@ -172,8 +172,7 @@ fn gallery_rendered(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { let options = ThumbnailOptions::new(); let thumbnails = Thumbnails::::in_context(ui.ctx(), "craft_menu.gallery.story", options); let source = FakeThumbSource::::new().later(true); - let items: Vec> = (0..COUNT) - .map(|at| GalleryItem { key: at, label: name(at), face: GalleryFace::Rendered { key: at as u64, thumbnails: &thumbnails, source: &source } }) - .collect(); + let items: Vec> = + (0..COUNT).map(|at| GalleryItem { key: at, label: name(at), face: GalleryFace::rendered(at as u64, &thumbnails, &source) }).collect(); show(ui, &items) } diff --git a/crates/menu/tests/it/ribbon_gallery.rs b/crates/menu/tests/it/ribbon_gallery.rs index 44abf3d767..93e83d0e78 100644 --- a/crates/menu/tests/it/ribbon_gallery.rs +++ b/crates/menu/tests/it/ribbon_gallery.rs @@ -263,3 +263,26 @@ fn the_current_tile_paints_an_accent_fill_and_edge_even_when_focused() { assert!(shapes.iter().any(|p| p.fill == fill && near(p, current)), "{who}: the focused current tile keeps its fill"); } } + +/// How many shapes a gallery of one rendered tile of a transparent image paints, as built or with `checker(false)`. +fn shapes_of_rendered(opt_out: bool) -> usize { + use craft_ui::primitives::textures::{FakeThumbSource, ThumbnailOptions, Thumbnails}; + let mut fixture = Fixture::new(move |ui| { + let options = ThumbnailOptions::new().jobs(std::sync::Arc::new(craft_host::InlineJobs)); + let thumbnails = Thumbnails::::in_context(ui.ctx(), "gallery_checker_test", options); + let source = FakeThumbSource::::new(); + // The default case sets nothing, so a default that turned the grid off fails here. + let face = GalleryFace::rendered(1, &thumbnails, &source); + let face = if opt_out { face.checker(false) } else { face }; + let items = [GalleryItem { key: 0_usize, label: Label::from("Mask"), face }]; + Gallery::new("Symbols", &items, None).tiles(1, 1).show(ui); + }); + fixture.settle(); + fixture.frame_shapes().len() +} + +#[trial] +fn a_rendered_gallery_tile_paints_a_checker_unless_it_opts_out() { + let (with, without) = (shapes_of_rendered(false), shapes_of_rendered(true)); + assert!(with > without, "the default paints {with} shapes, the opt-out {without}: the grid is the difference"); +} diff --git a/crates/text/src/ui/columns.rs b/crates/text/src/ui/columns.rs index a20e11d767..9cc1d0f9c0 100644 --- a/crates/text/src/ui/columns.rs +++ b/crates/text/src/ui/columns.rs @@ -4,11 +4,10 @@ use super::length::{DocUnit, length_field, shown}; use craft_behavior::{EditEvent, EditEvents, Metrics, Mixed, NumberCodec}; -use craft_ui::buttons::Button; use craft_ui::choice::Checkbox; use craft_ui::contract::{ - Bind, Common, Component, Cx, Entry, FieldLayout, MeasureCx, PaintUi, Props as _, Role, Show as _, ShowContainer as _, SideLabel, Sizing, - Validity, clicked, + Bind, Common, Component, Cx, Entry, FieldLayout, MeasureCx, PaintUi, Pointer as _, Props as _, Role, Show as _, ShowContainer as _, SideLabel, + Sizing, Validity, }; use craft_ui::dialog::{Dialog, DialogId, DialogResult, DialogStack, HeldAnswer, HeldOk, StackDialog, dialog_id}; use craft_ui::form::{Field, FieldGroup}; @@ -18,7 +17,7 @@ use craft_ui::primitives::{PaintShape, StrokeWidth}; use craft_ui::runtime::env::{Env, Label, Message, Units}; use craft_ui::runtime::words::CommonWords; use craft_ui::sections::{DialogRole, FooterButton, GroupBox}; -use craft_ui::style::{ComponentStyle, StyleColor, StyleCx, SurfaceKind, TypeRole}; +use craft_ui::style::{ComponentStyle, FocusLook, StyleColor, StyleCx, SurfaceKind, TypeRole}; use craft_ui::text::{NumberField, NumberOutput}; use egui::{Pos2, Rect, Ui, Vec2, pos2, vec2}; use std::cell::Cell; @@ -129,6 +128,19 @@ pub enum ColumnsPreset { Right, } +impl ColumnsPreset { + /// The number of columns, and for Left and Right whether the narrow one is first. + fn shape(self) -> (u32, Option) { + match self { + ColumnsPreset::One => (1, None), + ColumnsPreset::Two => (2, None), + ColumnsPreset::Three => (3, None), + ColumnsPreset::Left => (2, Some(true)), + ColumnsPreset::Right => (2, Some(false)), + } + } +} + /// The Columns dialog: only what changed comes back. /// /// - [`word`](Self::word) off (PowerPoint): Number (1 to [`MAX_COLUMNS`]) and Spacing (0 to [`MAX_SPACING`] points); @@ -287,13 +299,7 @@ impl TextColumnsDialog { let Some(width) = self.word else { return }; let spacing = DEFAULT_SPACING; self.edited.spacing = Some(spacing); - let (count, narrow_first) = match preset { - ColumnsPreset::One => (1, None), - ColumnsPreset::Two => (2, None), - ColumnsPreset::Three => (3, None), - ColumnsPreset::Left => (2, Some(true)), - ColumnsPreset::Right => (2, Some(false)), - }; + let (count, narrow_first) = preset.shape(); self.edited.count = Some(count); match narrow_first { None => { @@ -533,7 +539,11 @@ impl TextColumnsDialog { let mut chosen = None; Row::new().show(ui, |ui, _| { for (preset, label) in every { - if clicked(&Button::new(label.clone()).show(ui)) { + let tile = Field::new(label.clone()) + .layout(FieldLayout::Above) + .sizing(Sizing::Content) + .show(ui, |ui, _| ColumnsPresetTile::new(label.clone(), Self::preset_rows(preset)).show(ui).clicked); + if tile.inner == Some(true) { chosen = Some(preset); } } @@ -543,6 +553,25 @@ impl TextColumnsDialog { } } + /// The columns a preset draws as, over a text width of 1 and a spacing of a twelfth. + pub(crate) fn preset_rows(preset: ColumnsPreset) -> Vec<(f32, f32)> { + let (count, narrow_first) = preset.shape(); + let spacing = 1.0 / 12.0; + match narrow_first { + // Not `even_rows`: its minimum column width is in points and would close the gaps over a width of 1. + None => { + let each = (1.0 - spacing * (count - 1) as f32) / count as f32; + (0..count).map(|i| (each, if i + 1 < count { spacing } else { 0.0 })).collect() + } + Some(left) => { + let narrow = (1.0 - spacing) * NARROW_SHARE; + let wide = 1.0 - spacing - narrow; + let (first, second) = if left { (narrow, wide) } else { (wide, narrow) }; + vec![(first, spacing), (second, 0.0)] + } + } + } + /// One row per column: its number, then its width and the spacing after it, each behind its caption. fn grid(&mut self, ui: &mut Ui, words: &TextColumnsWords, units: &Units) { let rows = self.rows(); @@ -723,6 +752,25 @@ pub struct ColumnsPreviewStyle { pane: f32, /// The distance between text lines. pitch: f32, + #[style(focus)] + focus: FocusLook, +} + +impl ColumnsPreviewStyle { + /// The preview's look; a preset thumbnail takes the same with a smaller pane. + fn at(cx: &StyleCx<'_>) -> Self { + let colors = &cx.tokens.color; + ColumnsPreviewStyle { + metrics: Metrics::new(), + page: cx.color(colors.surface.raised), + edge: cx.color(colors.line.border), + line: cx.color(colors.text.faint), + separator: cx.color(colors.text.normal), + pane: cx.tokens.size.thumb.sample.lg, + pitch: cx.tokens.typography.get(TypeRole::Body).line_height * LINE_PITCH, + focus: cx.focus_look(), + } + } } /// The page preview: the columns and the line between them on a page, as Word's dialog draws it. @@ -750,16 +798,7 @@ impl Component for ColumnsPreview { type Output = egui::Response; fn style(cx: &StyleCx<'_>, _: ColumnsPreviewStyleKey) -> ColumnsPreviewStyle { - let colors = &cx.tokens.color; - ColumnsPreviewStyle { - metrics: Metrics::new(), - page: cx.color(colors.surface.raised), - edge: cx.color(colors.line.border), - line: cx.color(colors.text.faint), - separator: cx.color(colors.text.normal), - pane: cx.tokens.size.thumb.md, - pitch: cx.tokens.typography.get(TypeRole::Body).line_height * LINE_PITCH, - } + ColumnsPreviewStyle::at(cx) } fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 { @@ -767,26 +806,15 @@ impl Component for ColumnsPreview { } fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) { - let style = cx.style; - let pane = cx.rect; - let height = pane.height().min(pane.width() / PAGE_ASPECT); - let page = Rect::from_center_size(pane.center(), vec2(height * PAGE_ASPECT, height)); - let draw = ui.draw(); - draw.surface(pane, SurfaceKind::Sunken, craft_ui::style::Corners::None); - draw.fill(&PaintShape::Rect(page, egui::CornerRadius::ZERO), style.page); - draw.stroke(&PaintShape::Rect(page, egui::CornerRadius::ZERO), StrokeWidth::Thin, style.edge); - let plan = preview_plan(&self.rows, self.text_width, self.line_between, page); - let line = |a: Pos2, b: Pos2, color: StyleColor| draw.stroke(&PaintShape::Path(vec![a, b]), StrokeWidth::Thin, color); - for &(left, right) in &plan.columns { - let mut y = plan.text.top(); - while y + style.pitch / 2.0 <= plan.text.bottom() { - line(pos2(left, y), pos2(right, y), style.line); - y += style.pitch; - } - } - for &x in &plan.separators { - line(pos2(x, plan.text.top()), pos2(x, plan.text.bottom()), style.separator); - } + paint_columns( + ui, + [cx.style.page, cx.style.edge, cx.style.line, cx.style.separator], + cx.style.pitch, + cx.rect, + &self.rows, + self.text_width, + self.line_between, + ); } fn finish(self, cx: Cx<'_, Self>) -> egui::Response { @@ -794,6 +822,98 @@ impl Component for ColumnsPreview { } } +/// Draws the page of `rows` over `text_width` in `pane`. +fn paint_columns(ui: &mut PaintUi<'_>, inks: [StyleColor; 4], pitch: f32, pane: Rect, rows: &[(f32, f32)], text_width: f32, line_between: bool) { + let height = pane.height().min(pane.width() / PAGE_ASPECT); + let page = Rect::from_center_size(pane.center(), vec2(height * PAGE_ASPECT, height)); + let draw = ui.draw(); + draw.surface(pane, SurfaceKind::Sunken, craft_ui::style::Corners::None); + let [page_ink, edge, text, separator] = inks; + draw.fill(&PaintShape::Rect(page, egui::CornerRadius::ZERO), page_ink); + draw.stroke(&PaintShape::Rect(page, egui::CornerRadius::ZERO), StrokeWidth::Thin, edge); + let plan = preview_plan(rows, text_width, line_between, page); + let line = |a: Pos2, b: Pos2, color: StyleColor| draw.stroke(&PaintShape::Path(vec![a, b]), StrokeWidth::Thin, color); + for &(left, right) in &plan.columns { + let mut y = plan.text.top(); + while y + pitch / 2.0 <= plan.text.bottom() { + line(pos2(left, y), pos2(right, y), text); + y += pitch; + } + } + for &x in &plan.separators { + line(pos2(x, plan.text.top()), pos2(x, plan.text.bottom()), separator); + } +} + +/// A preset's thumbnail: a small page with the preset's columns, a button named by the label. +#[derive(craft_ui::contract::Component, craft_ui::contract::Props)] +pub struct ColumnsPresetTile { + #[common] + common: Common, + rows: Vec<(f32, f32)>, + clicked: bool, +} + +/// What a preset tile reports. +#[derive(Clone, Debug)] +#[non_exhaustive] +pub struct ColumnsPresetTileOutput { + /// The tile's response. + pub response: egui::Response, + /// A click, Space or Enter reached the enabled tile. + pub clicked: bool, +} + +impl craft_ui::contract::Outcome for ColumnsPresetTileOutput { + type Value = craft_ui::contract::NoValue; + + fn response(&self) -> &egui::Response { + &self.response + } +} + +impl ColumnsPresetTile { + /// A tile named `label` drawing `rows` (each column's width and the spacing after it). + pub fn new(label: impl Into