diff --git a/packages/frontend/desktop/src/views_extra/right_sidebar/properties/custom_rows.rs b/packages/frontend/desktop/src/views_extra/right_sidebar/properties/custom_rows.rs index d0d2496818..b81f405336 100644 --- a/packages/frontend/desktop/src/views_extra/right_sidebar/properties/custom_rows.rs +++ b/packages/frontend/desktop/src/views_extra/right_sidebar/properties/custom_rows.rs @@ -12,7 +12,7 @@ use ui_tokens::icons::{self, IconDef}; use crate::DesktopFlexExt; use super::{ - Properties, PropertyFocusStop, button, + Properties, PropertyFocusStop, property_input::{Commit, PropertyInput}, row_order::{Builtin, OrderedRow, RenderedRow}, }; @@ -24,12 +24,24 @@ const KINDS: [(&str, &str, IconDef); 4] = [ ("date", "Date", icons::DATE_TIME), ]; -const VISIBILITY: [(&str, &str); 3] = [ - ("always-show", "Always show"), - ("hide-when-empty", "Hide when empty"), - ("always-hide", "Always hide"), +const VISIBILITY: [(&str, &str, IconDef); 3] = [ + ("always-show", "Always show", icons::VIEW), + ("hide-when-empty", "Hide when empty", icons::INVISIBLE), + ("always-hide", "Always hide", icons::INVISIBLE), ]; +pub(super) fn builtin_icon(id: &str) -> IconDef { + match id { + "tags" => icons::TAG, + "docPrimaryMode" => icons::FILE, + "journal" => icons::TODAY, + "createdAt" => icons::HISTORY, + "updatedAt" => icons::DATE_TIME, + "pageWidth" => icons::PAGE_WIDTH, + _ => icons::TEXT, + } +} + #[derive(Clone, PartialEq)] enum Menu { Add, @@ -275,9 +287,16 @@ impl Panel<'_> { .flex() .items_center() .gap(px(6.0)) + .min_h(px(super::ROW_HEIGHT)) .p(px(super::CELL_PADDING)) - .child(icon(row.icon, palette.text_muted)) - .child(row.label.to_owned()) + .child(icon(row.icon, super::icon_secondary(palette))) + .child( + div() + .flex_1() + .min_w_0() + .truncate() + .child(row.label.to_owned()), + ) }; let name = name.text_color(palette.text_muted); let mut value = div().flex().items_start().min_w_0(); @@ -496,7 +515,7 @@ impl Panel<'_> { for row in rows.iter().copied().filter(|row| row.hidden()) { let (id, label, icon) = match row { OrderedRow::Custom(row) => (row.info.id.as_str(), row.label, row.icon), - OrderedRow::Builtin(row) => (row.id, row.label, icons::INVISIBLE), + OrderedRow::Builtin(row) => (row.id, row.label, builtin_icon(row.id)), }; let (button, stop) = self.control(&format!("show.{id}"), label, Some(icon)); let controls = self.controls.clone(); @@ -525,14 +544,20 @@ impl Panel<'_> { .child(field) .child(self.menu_label(&format!("Type {}", row.kind))) .child(self.menu_label("Visibility")); - for (mode, label) in VISIBILITY { + for (mode, label, glyph) in VISIBILITY { let selected = row.info.show.as_deref().unwrap_or("always-show") == mode; - let glyph = if selected { - icons::DONE + let color = if selected { + self.properties.palette.accent } else { - icons::INVISIBLE + super::icon_secondary(self.properties.palette) + }; + let (button, stop) = + self.control_with_icon(&format!("visibility.{mode}"), label, Some((glyph, color))); + let button = if selected { + button.child(div().flex_1()).child(icon(icons::DONE, color)) + } else { + button }; - let (button, stop) = self.control(&format!("visibility.{mode}"), label, Some(glyph)); let controls = self.controls.clone(); let edit = self.edit(&row.info.id); menu = menu.child(activate(button, stop, move |window, app| { @@ -667,6 +692,16 @@ impl Panel<'_> { id: &str, label: &str, glyph: Option, + ) -> (Stateful
, PropertyFocusStop) { + let color = super::icon_secondary(self.properties.palette); + self.control_with_icon(id, label, glyph.map(|glyph| (glyph, color))) + } + + fn control_with_icon( + &mut self, + id: &str, + label: &str, + glyph: Option<(IconDef, ui::Hsla)>, ) -> (Stateful
, PropertyFocusStop) { let focus = self .window @@ -679,7 +714,19 @@ impl Panel<'_> { let focused = focus.handle.is_focused(self.window); let palette = self.properties.palette; let snapshot = ui::snap_id!("{}.custom.{id}", self.properties.location.snapshot_prefix()); - let mut control = button(self.properties.custom_key(id), "", palette, true) + let mut control = div() + .id(self.properties.custom_key(id)) + .flex() + .flex_shrink(0.0) + .items_center() + .min_h(px(28.0)) + .px(px(8.0)) + .rounded(px(6.0)) + .text_size(px(13.0)) + .text_color(palette.text_primary) + .tab_stop(true) + .cursor_pointer() + .hover(move |style| style.bg(palette.hover_color)) .snapshot(snapshot, None) .snapshot_focused(focused) .track_focus(&focus.handle) @@ -692,8 +739,8 @@ impl Panel<'_> { }) .gap(px(6.0)) .child(focus.measure()) - .children(glyph.map(|glyph| icon(glyph, palette.text_muted))) - .child(div().truncate().child(label.to_owned())); + .children(glyph.map(|(glyph, color)| icon(glyph, color))) + .children((!label.is_empty()).then(|| div().truncate().child(label.to_owned()))); control.style().justify_content = Some(ui::JustifyContent::FlexStart); (control, focus) } diff --git a/packages/frontend/desktop/src/views_extra/right_sidebar/properties/mod.rs b/packages/frontend/desktop/src/views_extra/right_sidebar/properties/mod.rs index d2ba24f596..6746925a3a 100644 --- a/packages/frontend/desktop/src/views_extra/right_sidebar/properties/mod.rs +++ b/packages/frontend/desktop/src/views_extra/right_sidebar/properties/mod.rs @@ -457,7 +457,7 @@ impl RenderOnce for Properties { _ => continue, }; properties = properties.child( - property(builtin.label, value, palette) + property(custom_rows::builtin_icon(id), builtin.label, value, palette) .snapshot(ui::snap_id!("{prefix}.builtin.{id}"), parent_snapshot), ); if id == "tags" { @@ -754,13 +754,32 @@ fn property_row(name: Div, value: Div) -> Div { .child(value.flex_1()) } -fn property(label: &str, value: Div, palette: DesktopPalette) -> Div { +pub(super) fn icon_secondary(palette: DesktopPalette) -> ui::Hsla { + let mode = if palette.bg_canvas.l < 0.5 { + ui_tokens::ThemeMode::Dark + } else { + ui_tokens::ThemeMode::Light + }; + let tokens = crate::theme_tokens_for(mode); + crate::theme_color(tokens, "--affine-v2-icon-secondary", tokens.text_secondary) +} + +fn property( + glyph: ui_tokens::icons::IconDef, + label: &str, + value: Div, + palette: DesktopPalette, +) -> Div { property_row( div() + .flex() + .items_center() + .gap(px(6.0)) + .min_h(px(ROW_HEIGHT)) .p(px(CELL_PADDING)) - .whitespace_nowrap() .text_color(palette.text_muted) - .child(label.to_owned()), + .child(custom_rows::icon(glyph, icon_secondary(palette))) + .child(div().flex_1().min_w_0().truncate().child(label.to_owned())), value.p(px(CELL_PADDING)).text_color(palette.text_primary), ) } diff --git a/packages/frontend/desktop/tests/properties_panel_cache_parity.rs b/packages/frontend/desktop/tests/properties_panel_cache_parity.rs index 0e7b97efa7..f1ab5a44ad 100644 --- a/packages/frontend/desktop/tests/properties_panel_cache_parity.rs +++ b/packages/frontend/desktop/tests/properties_panel_cache_parity.rs @@ -2,14 +2,16 @@ //! inspector rows that follow property, trash, and link edits. use affine_desktop::{ - DesktopAppState, DesktopRoute, DesktopShellView, DocInspectorTab, register_desktop_fonts, + DesktopAppState, DesktopRoute, DesktopShellView, DocInspectorTab, palette_for, + register_desktop_fonts, }; +use affine_store::workspace_db::WorkspaceDbTableName; use anyhow::{Context as _, Result}; use ui::{ - AppContext, Application, ApplicationHandle, Bounds, HeadlessPlatform, HeadlessWindow, - SnapshotRect, WindowBounds, WindowHandle, WindowOptions, point, px, size, + AppContext, Application, ApplicationHandle, Bounds, HeadlessPlatform, HeadlessWindow, Hsla, + Rgba, SnapshotRect, WindowBounds, WindowHandle, WindowOptions, point, px, size, }; -use ui_tokens::ThemeMode; +use ui_tokens::{DARK_THEME, LIGHT_THEME, ThemeMode}; const DOC: &str = "doc-sync"; /// Web `propertyNameContainer` width and `propertyRoot` gap. @@ -75,6 +77,82 @@ impl Shell { }) }) } + + fn click(&self, id: &str) -> Result<()> { + let bounds = self + .bounds(id)? + .with_context(|| format!("missing element {id}"))?; + self.window.simulate_click(point( + px(bounds.x + bounds.width * 0.5), + px(bounds.y + bounds.height * 0.5), + )); + self.window.request_frame(); + Ok(()) + } + + fn icon_sprites(&self) -> Result> { + self.collect_icon_sprites(0) + } + + fn menu_icon_sprites(&self, menu: SnapshotRect) -> Result> { + let min_order = self.app.update(|cx| { + self.handle.update(cx, |_, window, _| { + window + .rendered_scene() + .quads + .iter() + .filter(|quad| { + (quad.bounds.origin.x.0 - menu.x).abs() <= 1.0 + && (quad.bounds.origin.y.0 - menu.y).abs() <= 1.0 + && (quad.bounds.size.width.0 - menu.width).abs() <= 1.0 + && (quad.bounds.size.height.0 - menu.height).abs() <= 1.0 + }) + .map(|quad| quad.order) + .min() + .context("menu quad") + }) + })??; + self.collect_icon_sprites(min_order) + } + + fn collect_icon_sprites(&self, min_order: u32) -> Result> { + self.app.update(|cx| { + self.handle.update(cx, |_, window, _| { + window + .rendered_scene() + .monochrome_sprites + .iter() + .filter(|sprite| { + let w = sprite.bounds.size.width.0; + let h = sprite.bounds.size.height.0; + sprite.order >= min_order + && (w - 16.0).abs() <= 0.1 + && (h - 16.0).abs() <= 0.1 + }) + .map(|sprite| { + ( + SnapshotRect::new( + sprite.bounds.origin.x.0, + sprite.bounds.origin.y.0, + sprite.bounds.size.width.0, + sprite.bounds.size.height.0, + ), + sprite.color, + ) + }) + .collect() + }) + }) + } + + fn render_image(&self) -> Result> { + self.app.update(|cx| { + self.handle.update(cx, |_, window, _| { + let img = window.render_to_image()?; + Ok::<_, anyhow::Error>(img.into_raw()) + }) + })? + } } fn state(theme: ThemeMode) -> (DesktopAppState, [String; 2]) { @@ -157,3 +235,275 @@ fn inspector_rows_follow_property_trash_and_link_edits() -> Result<()> { ); Ok(()) } + +fn expected_icon_secondary(theme: ThemeMode) -> Hsla { + let tokens = match theme { + ThemeMode::Light => &LIGHT_THEME, + ThemeMode::Dark => &DARK_THEME, + }; + let color = tokens + .color("--affine-v2-icon-secondary") + .unwrap_or(tokens.text_secondary); + Rgba { + r: f32::from(color.r) / 255.0, + g: f32::from(color.g) / 255.0, + b: f32::from(color.b) / 255.0, + a: f32::from(color.a) / 255.0, + } + .into() +} + +fn assert_hsla_close(actual: Hsla, expected: Hsla, label: &str) { + let a = Rgba::from(actual); + let e = Rgba::from(expected); + for (ch, (av, ev)) in [ + ("r", (a.r, e.r)), + ("g", (a.g, e.g)), + ("b", (a.b, e.b)), + ("a", (a.a, e.a)), + ] { + assert!( + (av - ev).abs() <= 0.01, + "{label} channel {ch}: got {av}, expected {ev}" + ); + } +} + +fn sprites_in( + sprites: &[(SnapshotRect, Hsla)], + container: SnapshotRect, +) -> Vec<(SnapshotRect, Hsla)> { + let mut found: Vec<_> = sprites + .iter() + .copied() + .filter(|(rect, _)| { + let cx = rect.x + rect.width * 0.5; + let cy = rect.y + rect.height * 0.5; + cx >= container.x + && cx <= container.right() + && cy >= container.y + && cy <= container.bottom() + }) + .collect(); + found.sort_by(|(a, _), (b, _)| a.x.total_cmp(&b.x)); + found +} + +fn icon_patch(raw: &[u8], rect: SnapshotRect) -> Vec<[u8; 4]> { + let x0 = rect.x.round() as u32; + let y0 = rect.y.round() as u32; + let mut pixels = Vec::with_capacity(16 * 16); + for dy in 0..16 { + for dx in 0..16 { + let idx = (((y0 + dy) * 1280 + (x0 + dx)) * 4) as usize; + pixels.push([raw[idx], raw[idx + 1], raw[idx + 2], raw[idx + 3]]); + } + } + pixels +} + +#[test] +fn builtin_rows_render_web_icons_aligned_with_custom_rows() -> Result<()> { + let builtins = [ + "tags", + "docPrimaryMode", + "journal", + "createdAt", + "updatedAt", + "pageWidth", + ]; + for theme in [ThemeMode::Light, ThemeMode::Dark] { + let (mut state, ids) = state(theme); + for (id, kind, index) in [ + ("docPrimaryMode", "docPrimaryMode", "a0000002"), + ("journal", "journal", "a0000003"), + ] { + let row = serde_json::json!({ + "id": id, + "type": kind, + "index": index, + "show": "always-show" + }); + state + .store + .directory + .db_mut() + .upsert_db_row(WorkspaceDbTableName::DocCustomPropertyInfo, &row) + .expect("seed builtin definition"); + } + let due_id = state + .store + .create_custom_property(DOC, "Due", "date") + .expect("date property"); + assert!( + state + .store + .set_custom_property_value(DOC, &due_id, "2026-12-08") + ); + + let shell = Shell::new(state)?; + let sprites = shell.icon_sprites()?; + let image = shell.render_image()?; + let expected_color = expected_icon_secondary(theme); + + let custom_name = shell + .bounds(&format!("desktop.inspector.custom.{}.name", ids[0]))? + .context("custom name")?; + let custom_icon = sprites_in(&sprites, custom_name) + .into_iter() + .next() + .context("custom row icon")?; + assert_hsla_close(custom_icon.1, expected_color, "custom row icon color"); + + let due_name = shell + .bounds(&format!("desktop.inspector.custom.{due_id}.name"))? + .context("due name")?; + let due_icon = sprites_in(&sprites, due_name) + .into_iter() + .next() + .context("due row icon")?; + let due_patch = icon_patch(&image, due_icon.0); + + let mut patches = Vec::new(); + for id in builtins { + let row = shell + .bounds(&format!("desktop.inspector.builtin.{id}"))? + .with_context(|| format!("missing builtin row {id}"))?; + let name_cell = SnapshotRect::new(row.x, row.y, NAME_WIDTH, 30.0); + let row_icons = sprites_in(&sprites, name_cell); + assert_eq!(row_icons.len(), 1, "{theme:?} {id}: expected 1 icon"); + let (rect, color) = row_icons[0]; + assert!( + (rect.x - custom_icon.0.x).abs() <= 0.1, + "{theme:?} {id}: icon x {} != custom {}", + rect.x, + custom_icon.0.x + ); + let dy = rect.y - row.y; + let custom_dy = custom_icon.0.y - custom_name.y; + assert!( + (dy - custom_dy).abs() <= 0.5, + "{theme:?} {id}: icon dy {dy} != custom {custom_dy}" + ); + assert_hsla_close(color, expected_color, &format!("{theme:?} {id}")); + let patch = icon_patch(&image, rect); + assert!( + patch.iter().any(|px| *px != patch[0]), + "{theme:?} {id}: icon patch is flat" + ); + if id == "updatedAt" { + assert_eq!( + patch, due_patch, + "{theme:?}: updatedAt and date share DateTimeIcon" + ); + } + patches.push((id, patch)); + } + + for i in 0..patches.len() { + for j in (i + 1)..patches.len() { + assert_ne!( + patches[i].1, patches[j].1, + "{theme:?}: {} and {} should render distinct icons", + patches[i].0, patches[j].0 + ); + } + } + } + Ok(()) +} + +#[test] +fn visibility_menu_renders_view_and_invisible_icons_and_selected_mark() -> Result<()> { + for theme in [ThemeMode::Light, ThemeMode::Dark] { + let (state, ids) = state(theme); + let shell = Shell::new(state)?; + let trigger = format!("desktop.inspector.custom.{}.name", ids[0]); + let accent = palette_for(theme).accent; + let muted_icon = expected_icon_secondary(theme); + + shell.click(&trigger)?; + let menu = shell + .bounds("desktop.inspector.custom.menu.edit")? + .context("edit menu")?; + let sprites = shell.menu_icon_sprites(menu)?; + let always_show = shell + .bounds("desktop.inspector.custom.visibility.always-show")? + .context("always-show")?; + let hide_empty = shell + .bounds("desktop.inspector.custom.visibility.hide-when-empty")? + .context("hide-when-empty")?; + let always_hide = shell + .bounds("desktop.inspector.custom.visibility.always-hide")? + .context("always-hide")?; + + let show_icons = sprites_in(&sprites, always_show); + let empty_icons = sprites_in(&sprites, hide_empty); + let hide_icons = sprites_in(&sprites, always_hide); + assert_eq!( + show_icons.len(), + 2, + "{theme:?}: selected always-show has prefix + done" + ); + assert_eq!( + empty_icons.len(), + 1, + "{theme:?}: unselected hide-when-empty has 1 icon" + ); + assert_eq!( + hide_icons.len(), + 1, + "{theme:?}: unselected always-hide has 1 icon" + ); + assert_hsla_close(show_icons[0].1, accent, "selected prefix"); + assert_hsla_close(show_icons[1].1, accent, "selected done"); + assert_hsla_close(empty_icons[0].1, muted_icon, "hide-when-empty prefix"); + assert_hsla_close(hide_icons[0].1, muted_icon, "always-hide prefix"); + + shell.click("desktop.inspector.custom.visibility.hide-when-empty")?; + shell.click(&trigger)?; + let menu = shell + .bounds("desktop.inspector.custom.menu.edit")? + .context("edit menu")?; + let sprites = shell.menu_icon_sprites(menu)?; + let image = shell.render_image()?; + let always_show = shell + .bounds("desktop.inspector.custom.visibility.always-show")? + .context("always-show")?; + let hide_empty = shell + .bounds("desktop.inspector.custom.visibility.hide-when-empty")? + .context("hide-when-empty")?; + let always_hide = shell + .bounds("desktop.inspector.custom.visibility.always-hide")? + .context("always-hide")?; + let show_icons = sprites_in(&sprites, always_show); + let empty_icons = sprites_in(&sprites, hide_empty); + let hide_icons = sprites_in(&sprites, always_hide); + assert_eq!( + show_icons.len(), + 1, + "{theme:?}: unselected always-show has 1 icon" + ); + assert_eq!( + empty_icons.len(), + 2, + "{theme:?}: selected hide-when-empty has prefix + done" + ); + assert_eq!( + hide_icons.len(), + 1, + "{theme:?}: unselected always-hide has 1 icon" + ); + assert_hsla_close(show_icons[0].1, muted_icon, "unselected always-show prefix"); + assert_hsla_close(empty_icons[0].1, accent, "selected hide-when-empty prefix"); + assert_hsla_close(empty_icons[1].1, accent, "selected hide-when-empty done"); + + let view_patch = icon_patch(&image, show_icons[0].0); + let invisible_patch = icon_patch(&image, hide_icons[0].0); + assert_ne!( + view_patch, invisible_patch, + "{theme:?}: always-show (ViewIcon) must differ from always-hide (InvisibleIcon)" + ); + } + Ok(()) +} diff --git a/packages/ui/tokens/extract-tokens.mjs b/packages/ui/tokens/extract-tokens.mjs index e9b83d4545..2867b64aa3 100644 --- a/packages/ui/tokens/extract-tokens.mjs +++ b/packages/ui/tokens/extract-tokens.mjs @@ -131,6 +131,9 @@ const CORE_ICONS = [ // Document properties ['NUMBER', 'NumberIcon'], ['DATE_TIME', 'DateTimeIcon'], + ['FILE', 'FileIcon'], + ['HISTORY', 'HistoryIcon'], + ['VIEW', 'ViewIcon'], ]; /** @@ -1135,6 +1138,9 @@ mod tests { assert_eq!(icon_by_name("PresentationIcon"), Some(&PRESENTATION)); assert_eq!(icon_by_name("NumberIcon"), Some(&NUMBER)); assert_eq!(icon_by_name("DateTimeIcon"), Some(&DATE_TIME)); + assert_eq!(icon_by_name("FileIcon"), Some(&FILE)); + assert_eq!(icon_by_name("HistoryIcon"), Some(&HISTORY)); + assert_eq!(icon_by_name("ViewIcon"), Some(&VIEW)); } #[test] diff --git a/packages/ui/tokens/icons.rs b/packages/ui/tokens/icons.rs index 33e61c1b13..2118faadd2 100644 --- a/packages/ui/tokens/icons.rs +++ b/packages/ui/tokens/icons.rs @@ -573,6 +573,21 @@ pub const DATE_TIME: IconDef = IconDef { svg: "", }; +pub const FILE: IconDef = IconDef { + name: "FileIcon", + svg: "", +}; + +pub const HISTORY: IconDef = IconDef { + name: "HistoryIcon", + svg: "", +}; + +pub const VIEW: IconDef = IconDef { + name: "ViewIcon", + svg: "", +}; + pub const ALL_ICONS: &[IconDef] = &[ ADD_TEXT, ALL_DOCS, @@ -618,6 +633,7 @@ pub const ALL_ICONS: &[IconDef] = &[ EXPAND, FAVORITE, FAVORITED, + FILE, FOLDER, FRAME, GROUPING, @@ -628,6 +644,7 @@ pub const ALL_ICONS: &[IconDef] = &[ HEADING_4, HEADING_5, HEADING_6, + HISTORY, HOME, IMAGE, INFORMATION, @@ -681,6 +698,7 @@ pub const ALL_ICONS: &[IconDef] = &[ UNDERLINE, UNDO, UNLOCK, + VIEW, VIEW_LAYERS, WARNING, WRAP, @@ -740,6 +758,9 @@ mod tests { assert_eq!(icon_by_name("PresentationIcon"), Some(&PRESENTATION)); assert_eq!(icon_by_name("NumberIcon"), Some(&NUMBER)); assert_eq!(icon_by_name("DateTimeIcon"), Some(&DATE_TIME)); + assert_eq!(icon_by_name("FileIcon"), Some(&FILE)); + assert_eq!(icon_by_name("HistoryIcon"), Some(&HISTORY)); + assert_eq!(icon_by_name("ViewIcon"), Some(&VIEW)); } #[test]