diff --git a/packages/frontend/desktop/src/settings_modal.rs b/packages/frontend/desktop/src/settings_modal.rs index fcb2cfc0ad..0c76e9f2ac 100644 --- a/packages/frontend/desktop/src/settings_modal.rs +++ b/packages/frontend/desktop/src/settings_modal.rs @@ -1,290 +1,1036 @@ -//! Centered `Settings` modal adhering to iOS 27 `pane_corner = 38pt` geometry -//! (`16pt` edge inset, `22pt` concentric inner cards, `36×36pt` close button), -//! three-state `System / Light / Dark` theme switcher, and independent wheel scrolling. +//! Desktop settings navigation and its responsive modal. +use affine_store::directory::DocMode; +use blocksuite_editor::scroll::{ScrollAxis, wheel_delta}; +use std::{cell::Cell, rc::Rc}; use ui::{ - Context, FocusHandle, FontWeight, InteractiveElement, IntoElement, ParentElement, - StatefulInteractiveElement, Styled, div, px, svg, + App, BoxShadow, ClickEvent, Context, Div, Entity, FocusHandle, FontWeight, InteractiveElement, + IntoElement, ParentElement, RenderOnce, Stateful, StatefulInteractiveElement, Styled, + WeakEntity, Window, div, px, svg, }; -use ui_tokens::FONT_SANS; +use ui_tokens::{DARK_THEME, FONT_SANS, LIGHT_THEME, ThemeMode, icons}; use crate::{ - CLOSE_ICON_SVG, DesktopFlexExt, DesktopPalette, + CLOSE_ICON_SVG, DesktopFlexExt, DesktopPalette, WorkspaceKind, doc_store::DesktopDocStore, draft_store::{ComposerDraftStore, DesktopDraftBackend}, + rgba_to_hsla, state::ThemePreference, workbench::{DesktopShellView, backdrop}, }; -fn info_row(label: &'static str, value: String, palette: DesktopPalette) -> ui::Div { - div() - .flex() - .flex_row() - .items_center() - .justify_between() - .flex_wrap() - .gap(px(4.0)) - .w_full() - .py(px(4.0)) - .child( - div() - .text_size(px(12.5)) - .text_color(palette.text_muted) - .child(label), - ) - .child( - div() - .text_size(px(12.5)) - .font_weight(FontWeight::MEDIUM) - .text_color(palette.text_primary) - .min_w_0() - .text_ellipsis() - .child(value), - ) +const SIDEBAR_WIDTH: f32 = 216.0; +const MODAL_ID: &str = "desktop.settings.modal"; + +struct NewDocMode(DocMode); +impl ui::Global for NewDocMode {} + +pub(crate) fn new_doc_default_mode(app: &App) -> DocMode { + if app.has_global::() { + app.global::().0 + } else { + DocMode::Page + } } -fn theme_pill( - id: &'static str, - label: &'static str, - active: bool, - pref: ThemePreference, +#[derive(Clone, Copy, Default, PartialEq, Eq)] +enum Page { + #[default] + Appearance, + Editor, + Shortcuts, + Language, + Preference, + Storage, + About, +} + +impl Page { + const GENERAL: [Self; 4] = [ + Self::Appearance, + Self::Editor, + Self::Shortcuts, + Self::Language, + ]; + const WORKSPACE: [Self; 2] = [Self::Preference, Self::Storage]; + + fn labels(self) -> (&'static str, &'static str) { + match self { + Self::Appearance => ("Appearance", "desktop.settings.appearance"), + Self::Editor => ("Editor", "desktop.settings.editor"), + Self::Shortcuts => ("Shortcuts", "desktop.settings.shortcuts"), + Self::Language => ("Language", "desktop.settings.language"), + Self::Preference => ("Preference", "desktop.settings.preference"), + Self::Storage => ("Storage", "desktop.settings.storage"), + Self::About => ("About", "desktop.settings.about"), + } + } + + fn id(self) -> &'static str { + self.labels().1 + } + + fn title_id(self) -> String { + format!("{}.title", self.id()) + } + + fn icon(self) -> &'static icons::IconDef { + match self { + Self::Appearance => &icons::BRUSH, + Self::Editor => &icons::PEN, + Self::Shortcuts => &icons::KEYBOARD, + Self::Language => &icons::TEXT, + Self::Preference => &icons::PAGE, + Self::Storage => &icons::ATTACHMENT, + Self::About => &icons::INFORMATION, + } + } +} + +struct SettingsModal { + shell: Entity, + focus: FocusHandle, palette: DesktopPalette, - cx: &mut Context, -) -> ui::Stateful { - div() - .id(id) - .h(px(32.0)) - .px(px(14.0)) - .rounded(px(12.0)) - .bg(if active { - palette.accent - } else { - palette.bg_pill - }) - .flex() - .items_center() - .justify_center() - .text_size(px(12.5)) - .font_weight(FontWeight::MEDIUM) - .text_color(if active { - ui::hsla(0.0, 0.0, 1.0, 1.0) - } else { - palette.text_primary - }) - .on_click(cx.listener(move |this, _, _, cx| { - this.state.set_theme_preference(pref); - cx.notify(); - })) - .child(label) + pref: ThemePreference, + workspace: WorkspaceSummary, } -/// Render the centered `Settings` modal (Appearance `System/Light/Dark`, Workspace & Local Drafts). pub(crate) fn render_settings_modal( pref: ThemePreference, store: &DesktopDocStore, - draft_store: &ComposerDraftStore, + _draft_store: &ComposerDraftStore, focus: &FocusHandle, - (width, height): (f32, f32), + _size: (f32, f32), palette: DesktopPalette, cx: &mut Context, -) -> impl IntoElement { - let ws = store.active_workspace(); - let content = div() +) -> impl IntoElement + use<> { + ui::ViewElement::new(SettingsModal { + shell: cx.entity(), + focus: focus.clone(), + palette, + pref, + workspace: WorkspaceSummary::from_store(store), + }) +} + +fn dismiss( + shell: WeakEntity, +) -> impl Fn(&ClickEvent, &mut Window, &mut App) + 'static { + move |_, window, app| { + let _ = shell.update(app, |view, cx| view.dismiss_modal(window, cx)); + } +} + +impl RenderOnce for SettingsModal { + fn render(mut self, window: &mut Window, app: &mut App) -> impl IntoElement { + let viewport = window.viewport_size(); + let (vw, vh) = (f32::from(viewport.width), f32::from(viewport.height)); + let compact = vw < 720.0; + let width = (vw - if compact { 32.0 } else { 48.0 }).clamp(0.0, 920.0); + let height = (vh - if compact { 32.0 } else { 64.0 }).clamp(0.0, 680.0); + let selection = + window.use_keyed_state("desktop.settings.selection", app, |_, _| Page::Appearance); + let page = *selection.read(app); + let dark = self.shell.read(app).state().theme == ThemeMode::Dark; + let mut palette = self.palette; + // Keep secondary text readable on both the pane and the segmented control. + palette.text_muted = ui::hsla(0.0, 0.0, if dark { 0.62 } else { 0.42 }, 1.0); + palette.bg_elevated.l = palette.bg_elevated.l.max(0.25); + self.palette = palette; + let shell = self.shell.downgrade(); + let theme = if dark { &DARK_THEME } else { &LIGHT_THEME }; + let body = div() + .flex() + .flex_col() + .items_center() + .w_full() + .flex_shrink(0.0) + .p(px(if compact { 16.0 } else { 24.0 })) + .pt(px(if compact { 20.0 } else { 40.0 })) + .child(self.render_page(page, width < 800.0, compact, app)); + let pane = scroll_area("desktop.settings.scroll", page.id(), body, window, app) + .flex_1() + .min_w_0(); + let mut colors = palette; + colors.scrim = rgba_to_hsla(theme.background_modal); + let modal = div() + .id(MODAL_ID) + .snapshot(MODAL_ID, Some("desktop.shell.root")) + .relative() + .w(px(width)) + .h(px(height)) + .track_focus(&self.focus) + .occlude() + .overflow_hidden() + .rounded(px(12.0)) + .bg(rgba_to_hsla(theme.background_overlay_panel)) + .border_1() + .border_color(palette.border) + .shadow( + theme + .shadow("--affine-popover-shadow") + .into_iter() + .flatten() + .map(|layer| { + BoxShadow::new( + px(layer.offset_x), + px(layer.offset_y), + ui::hsla(0.0, 0.0, layer.r, layer.a), + ) + .blur_radius(px(layer.blur_radius)) + .spread_radius(px(layer.spread_radius)) + }) + .collect(), + ) + .font_family(FONT_SANS) + .text_color(palette.text_primary) + .flex(); + div() + .absolute() + .top_0() + .left_0() + .size_full() + .flex() + .items_center() + .justify_center() + .child(backdrop( + "desktop.settings.backdrop", + colors, + dismiss(shell.clone()), + )) + .child( + (if compact { + modal.flex_col() + } else { + modal.flex_row() + }) + .child(navigation(compact, page, &selection, palette, window, app)) + .child(pane) + .child( + button("desktop.settings.close") + .hover(move |style| style.bg(palette.bg_pill)) + .absolute() + .top(px(12.0)) + .right(px(12.0)) + .size(px(28.0)) + .justify_center() + .on_click(dismiss(shell)) + .child( + svg() + .data(CLOSE_ICON_SVG) + .size(px(20.0)) + .text_color(palette.text_primary), + ), + ), + ) + } +} + +fn scroll_area( + id: &'static str, + state_id: &'static str, + body: Div, + window: &mut Window, + app: &mut App, +) -> Stateful
{ + let state = window.use_keyed_state(state_id, app, |_, _| { + Rc::new(Cell::new(ScrollAxis::default())) + }); + let axis = state.read(app).clone(); + let scroll = axis.clone(); + div() + .id(id) + .snapshot(id, Some(MODAL_ID)) + .flex() + .flex_col() + .min_h_0() + .overflow_hidden() + .on_scroll(move |event, window, app| { + let mut scroll = axis.get(); + let (_, dy) = wheel_delta(event, 0.0); + if scroll.apply(dy) { + axis.set(scroll); + window.refresh(); + } + app.stop_propagation(); + }) + .child(ScrollBody { + body: body.into_any_element(), + axis: scroll, + }) +} + +struct ScrollBody { + body: ui::AnyElement, + axis: Rc>, +} + +impl ui::Element for ScrollBody { + type RequestLayoutState = (); + type PrepaintState = (); + fn id(&self) -> Option { + None + } + + fn source_location(&self) -> Option<&'static std::panic::Location<'static>> { + None + } + + fn request_layout( + &mut self, + _: Option<&ui::GlobalElementId>, + _: Option<&ui::InspectorElementId>, + window: &mut Window, + app: &mut App, + ) -> (ui::LayoutId, ()) { + (self.body.request_layout(window, app), ()) + } + + fn prepaint( + &mut self, + _: Option<&ui::GlobalElementId>, + _: Option<&ui::InspectorElementId>, + bounds: ui::Bounds, + _: &mut (), + window: &mut Window, + app: &mut App, + ) { + let max = f32::from(bounds.size.height - window.content_mask().bounds.size.height); + let axis = ScrollAxis::new(self.axis.get().offset, max); + self.axis.set(axis); + window.with_element_offset(ui::point(px(0.0), px(-axis.offset)), |window| { + self.body.prepaint(window, app); + }); + } + + fn paint( + &mut self, + _: Option<&ui::GlobalElementId>, + _: Option<&ui::InspectorElementId>, + _: ui::Bounds, + _: &mut (), + _: &mut (), + window: &mut Window, + app: &mut App, + ) { + self.body.paint(window, app); + } +} + +fn navigation( + compact: bool, + page: Page, + selection: &Entity, + palette: DesktopPalette, + window: &mut Window, + app: &mut App, +) -> impl IntoElement { + let item = |tab: Page| { + let selection = selection.clone(); + let button = button(tab.id()) + .bg(if page == tab { + palette.bg_pill + } else { + ui::transparent_black() + }) + .hover(move |style| style.bg(palette.bg_pill)) + .px(px(if compact { 6.0 } else { 8.0 })) + .h(px(30.0)) + .flex_shrink(0.0) + .text_size(px(if compact { 13.0 } else { 14.0 })) + .gap(px(10.0)) + .on_click(move |_, _, app| { + selection.update(app, |selection, cx| { + if *selection != tab { + *selection = tab; + cx.notify(); + } + }); + }); + (if compact { + button + } else { + let icon = tab.icon(); + button.child( + svg() + .data(icon.svg.as_bytes()) + .size(px(16.0)) + .text_color(palette.text_muted), + ) + }) + .child(tab.labels().0) + }; + let title = div() + .px(px(8.0)) + .text_size(px(18.0)) + .line_height(px(26.0)) + .font_weight(FontWeight::SEMIBOLD) + .child("Settings"); + let nav = div() .flex() .flex_col() - .w_full() - .gap(px(12.0)) .flex_shrink(0.0) - .child( - // Concentric inner card: R_inner = 38 - 16 = 22px + .bg(palette.bg_surface) + .p(px(12.0)); + if compact { + nav.gap(px(6.0)) + .child(title) + .child( + div().flex().flex_row().flex_wrap().gap(px(4.0)).children( + Page::GENERAL + .into_iter() + .chain(Page::WORKSPACE) + .chain([Page::About]) + .map(item), + ), + ) + .into_any_element() + } else { + let group = |label, tabs: &[Page]| { div() .flex() .flex_col() - .p(px(18.0)) - .rounded(px(22.0)) - .bg(palette.bg_surface) - .border_1() - .border_color(palette.border) - .gap(px(12.0)) + .flex_shrink(0.0) + .gap(px(4.0)) .child( div() - .text_size(px(13.5)) - .font_weight(FontWeight::SEMIBOLD) - .text_color(palette.text_primary) - .child("Appearance"), + .px(px(8.0)) + .text_size(px(14.0)) + .line_height(px(22.0)) + .text_color(palette.text_muted) + .child(label), ) - .child( - div() - .flex() - .flex_row() - .flex_wrap() - .gap(px(8.0)) - .items_center() - .justify_between() - .child( - div() - .text_size(px(13.0)) - .text_color(palette.text_secondary) - .child("Color Theme"), - ) - .child( - div() - .flex() - .flex_row() - .gap(px(6.0)) - .child(theme_pill( - "desktop.settings.theme.system", - "System", - pref == ThemePreference::System, - ThemePreference::System, - palette, - cx, - )) - .child(theme_pill( - "desktop.settings.theme.light", - "Light", - pref == ThemePreference::Light, - ThemePreference::Light, - palette, - cx, - )) - .child(theme_pill( - "desktop.settings.theme.dark", - "Dark", - pref == ThemePreference::Dark, - ThemePreference::Dark, - palette, - cx, - )), - ), - ), + .children(tabs.iter().copied().map(item)) + }; + let body = nav + .min_h(ui::relative(1.0)) + .pt(px(18.0)) + .gap(px(16.0)) + .child(title) + .child(group("General", &Page::GENERAL)) + .child(group("Workspace", &Page::WORKSPACE)) + .child(div().flex_1()) + .child(item(Page::About)); + scroll_area( + "desktop.settings.navigation", + "desktop.settings.navigation.state", + body, + window, + app, ) - .child( - // Concentric inner card: R_inner = 38 - 16 = 22px - div() - .flex() - .flex_col() - .p(px(18.0)) - .rounded(px(22.0)) - .bg(palette.bg_surface) - .border_1() - .border_color(palette.border) - .gap(px(8.0)) - .child( - div() - .text_size(px(13.5)) - .font_weight(FontWeight::SEMIBOLD) - .text_color(palette.text_primary) - .child("Workspace & Local Drafts"), - ) - .child(info_row("Active Workspace", ws.name.clone(), palette)) - .child(info_row( - "Storage Mode", - ws.kind.badge().to_string(), - palette, - )) - .child(info_row( - "Draft Store Namespace", - draft_store.namespace().to_string(), - palette, - )) - .child(info_row( - "Local Unpublished Drafts", - draft_store.draft_count().to_string(), - palette, - )) - .child( - info_row( - "SHA-256 Deduplicated Blobs", - draft_store.attachment_count().to_string(), - palette, - ) - .snapshot( - "desktop.settings.deduplicated_blobs", - Some("desktop.settings.modal"), - ), - ), - ); - - let mut scroll = div() - .id("desktop.settings.scroll") - .w_full() - .flex_1() - .min_h_0(); - scroll.style().overflow.y = Some(ui::Overflow::Scroll); + .w(px(SIDEBAR_WIDTH)) + .flex_shrink(0.0) + .into_any_element() + } +} +fn button(id: &'static str) -> Stateful
{ div() - .absolute() - .top_0() - .left_0() - .w_full() - .h_full() + .id(id) + .snapshot(id, Some(MODAL_ID)) + .rounded(px(8.0)) .flex() .items_center() - .justify_center() - .child(backdrop( - "desktop.settings.backdrop", - palette, - cx.listener(|this, _, window, cx| { - this.dismiss_modal(window, cx); - }), - )) - .child( - div() - .id("desktop.settings.modal") - .snapshot("desktop.settings.modal", Some("desktop.shell.root")) - .w(px(width)) - .h(px(height)) - .track_focus(focus) - .occlude() - .overflow_hidden() - .rounded(px(38.0)) - .bg(palette.bg_elevated) - .border_1() - .border_color(palette.border_strong) - .font_family(FONT_SANS) - .flex() - .flex_col() - .p(px(16.0)) - .gap(px(14.0)) - .child( - div() - .flex() - .flex_row() - .items_center() - .justify_between() - .px(px(4.0)) - .pt(px(4.0)) - .child( - div() - .text_size(px(16.0)) - .font_weight(FontWeight::SEMIBOLD) - .text_color(palette.text_primary) - .child("Settings"), - ) - .child( - div() - .id("desktop.settings.close") - .snapshot("desktop.settings.close", Some("desktop.settings.modal")) - .w(px(36.0)) - .h(px(36.0)) - .rounded(px(18.0)) - .bg(palette.bg_pill) - .flex() - .items_center() - .justify_center() - .on_click(cx.listener(|this, _, window, cx| { - this.dismiss_modal(window, cx); - })) - .child( - svg() - .data(CLOSE_ICON_SVG) - .w(px(14.0)) - .h(px(14.0)) - .text_color(palette.text_primary), - ), - ), - ) - .child(scroll.child(content)), - ) + .cursor_pointer() +} + +struct WorkspaceSummary { + name: String, + access: &'static str, + sync: &'static str, + documents: usize, +} + +impl WorkspaceSummary { + fn from_store(store: &DesktopDocStore) -> Self { + let workspace = store.active_workspace(); + let read_only = store.is_readonly_sqlite(); + let (access, sync) = match (read_only, workspace.kind, workspace.synced) { + (true, _, _) => ("Read-only", "Read-only"), + (false, WorkspaceKind::Local, _) => ("Local", "Local"), + (false, WorkspaceKind::Cloud, true) => ("Cloud", "Synced to AFFiNE Cloud"), + (false, WorkspaceKind::Cloud, false) => ("Cloud", "Not synced"), + }; + Self { + name: workspace.name.clone(), + access, + sync, + documents: store.docs.len(), + } + } +} + +impl SettingsModal { + fn render_page( + &self, + page: Page, + stacked: bool, + compact: bool, + app: &App, + ) -> impl IntoElement + use<> { + use DocMode::{Edgeless, Page as PageMode}; + let palette = self.palette; + let values = |title, rows: &[(&'static str, &str)]| { + section( + title, + rows.iter() + .map(|&(label, value)| value_row(label, value.to_owned(), palette)), + palette, + ) + }; + let (title, description, sections) = match page { + Page::Appearance => ( + "Appearance settings", + "Customize your AFFiNE appearance", + self.appearance(stacked, compact, app), + ), + Page::Editor => ( + "Editor settings", + "Preferences for creating and editing documents", + vec![control_section( + "General", + "New Doc Default Mode", + "Choose how new documents open.", + self.picker( + [ + ("desktop.settings.mode.page", "Page", PageMode), + ("desktop.settings.mode.edgeless", "Edgeless", Edgeless), + ], + new_doc_default_mode(app), + |_, mode, app| app.set_global(NewDocMode(mode)), + ), + stacked, + palette, + )], + ), + Page::Shortcuts => ( + "Keyboard shortcuts", + "Shortcuts for the workbench and editor", + shortcuts(app.platform_info().is_mac(), palette), + ), + Page::Language => ( + "Language", + "Desktop display language", + vec![values(None, &[("Display language", "English")])], + ), + Page::Preference => ( + "Workspace preference", + "Your current workspace and its connection", + vec![values( + Some("Workspace"), + &[ + ("Workspace name", &self.workspace.name), + ("Sync status", self.workspace.sync), + ], + )], + ), + Page::Storage => ( + "Storage", + "Review workspace storage and export availability", + vec![ + values( + Some("Workspace storage"), + &[ + ("Workspace type", self.workspace.access), + ("Documents", &self.workspace.documents.to_string()), + ], + ), + values( + Some("Export"), + &[("Workspace export", "Unavailable in this desktop shell")], + ), + ], + ), + Page::About => ( + "About AFFiNE", + "AFFiNE Desktop", + vec![values( + Some("Version"), + &[("Desktop shell", env!("CARGO_PKG_VERSION"))], + )], + ), + }; + let mut content = div().w_full(); + content.style().max_size.width = Some(px(560.0).into()); + let title = div() + .snapshot(page.title_id(), Some(MODAL_ID)) + .text_size(px(16.0)) + .line_height(px(24.0)) + .font_weight(FontWeight::SEMIBOLD) + .child(title); + let header = div() + .flex() + .flex_col() + .gap(px(4.0)) + .child(title) + .child(description_text(description, palette)); + content + .text_size(px(14.0)) + .line_height(px(22.0)) + .flex() + .flex_col() + .gap(px(24.0)) + .child(header) + .children(sections) + } + + fn appearance(&self, stacked: bool, compact: bool, app: &App) -> Vec
{ + use ThemePreference::{Dark, Light, System}; + let palette = self.palette; + let scheme = [ + ("desktop.settings.theme.light", "Light", Light), + ("desktop.settings.theme.dark", "Dark", Dark), + ("desktop.settings.theme.system", "System", System), + ]; + let scheme = self.picker(scheme, self.pref, |state, pref, _| { + state.set_theme_preference(pref) + }); + let mut sections = vec![control_section( + "Theme", + "Color Scheme", + "Choose the color scheme used by AFFiNE.", + scheme, + stacked, + palette, + )]; + if !compact { + let show_sidebar = self.shell.read(app).state().sidebar.open; + let visibility = [ + ("desktop.settings.sidebar.show", "Show", true), + ("desktop.settings.sidebar.hide", "Hide", false), + ]; + let visibility = self.picker(visibility, show_sidebar, |state, visible, _| { + state.sidebar.open = visible; + }); + sections.push(control_section( + "Sidebar", + "Show Sidebar", + "Keep the workspace navigation visible.", + visibility, + stacked, + palette, + )); + } + sections + } + + fn picker( + &self, + choices: impl IntoIterator, + selected: T, + apply: impl Fn(&mut crate::DesktopAppState, T, &mut App) + Copy + 'static, + ) -> Div { + let palette = self.palette; + segmented( + choices.into_iter().map(|(id, label, value)| { + let shell = self.shell.downgrade(); + segment(id, label, selected == value, palette).on_click(move |_, _, app| { + let _ = shell.update(app, |view, cx| { + apply(&mut view.state, value, cx); + cx.notify(); + }); + }) + }), + palette, + ) + } +} + +fn description_text(text: &'static str, palette: DesktopPalette) -> Div { + div() + .text_size(px(12.0)) + .line_height(px(20.0)) + .text_color(palette.text_muted) + .child(text) +} + +fn section( + title: Option<&'static str>, + rows: impl IntoIterator, + palette: DesktopPalette, +) -> Div { + div() + .flex() + .flex_col() + .gap(px(12.0)) + .border_t_1() + .border_color(palette.border) + .pt(px(20.0)) + .children(title.map(|title| { + div() + .text_size(px(14.0)) + .line_height(px(22.0)) + .text_color(palette.text_muted) + .font_weight(FontWeight::MEDIUM) + .child(title) + })) + .children(rows) +} + +fn value_row( + label: &'static str, + value: impl Into, + palette: DesktopPalette, +) -> Div { + let mut value = div() + .min_w_0() + .text_color(palette.text_muted) + .child(value.into()); + value.style().max_size.width = Some(ui::relative(1.0).into()); + div() + .flex() + .flex_wrap() + .justify_between() + .items_start() + .gap(px(8.0)) + .child(div().font_weight(FontWeight::MEDIUM).child(label)) + .child(value) +} + +fn control_section( + title: &'static str, + label: &'static str, + description: &'static str, + control: impl IntoElement, + stacked: bool, + palette: DesktopPalette, +) -> Div { + let row = div().flex().gap(px(8.0)); + let row = (if stacked { + row.flex_col().items_start() + } else { + row.items_start().justify_between() + }) + .child( + div() + .flex() + .flex_col() + .min_w_0() + .gap(px(4.0)) + .child(div().font_weight(FontWeight::SEMIBOLD).child(label)) + .child(description_text(description, palette)), + ) + .child(control); + section(Some(title), [row], palette) +} + +fn segmented( + children: impl IntoIterator>, + palette: DesktopPalette, +) -> Div { + let mut track = div().w(px(250.0)); + track.style().max_size.width = Some(ui::relative(1.0).into()); + track + .flex() + .flex_shrink(0.0) + .rounded(px(10.0)) + .bg(palette.bg_pill) + .p(px(2.0)) + .gap(px(2.0)) + .children(children) +} + +fn segment( + id: &'static str, + label: &'static str, + selected: bool, + palette: DesktopPalette, +) -> ui::Stateful
{ + let fill = palette + .bg_elevated + .opacity(if selected { 1.0 } else { 0.0 }); + button(id) + .flex_1() + .justify_center() + .bg(fill) + .hover(move |style| style.bg(if selected { fill } else { palette.bg_hover })) + .h(px(28.0)) + .text_size(px(12.0)) + .font_weight(FontWeight::SEMIBOLD) + .text_color(palette.text_primary) + .child(label) +} + +fn shortcuts(mac: bool, palette: DesktopPalette) -> Vec
{ + let (command, shift, alt) = if mac { + ("⌘", "⇧", "⌥") + } else { + ("Ctrl+", "Shift+", "Alt+") + }; + let shortcut = |label, chord: String| { + div() + .flex() + .items_start() + .justify_between() + .gap(px(8.0)) + .child(div().min_w_0().child(label)) + .child( + div() + .flex_shrink(0.0) + .rounded(px(4.0)) + .px(px(6.0)) + .bg(palette.bg_pill) + .text_size(px(12.0)) + .child(chord), + ) + }; + vec![ + section( + Some("General"), + [shortcut("Quick Search", format!("{command}K"))], + palette, + ), + section( + Some("Editor"), + [ + shortcut("Undo", format!("{command}Z")), + shortcut("Redo", format!("{command}{shift}Z")), + shortcut("Bold", format!("{command}B")), + shortcut("Italic", format!("{command}I")), + shortcut("Underline", format!("{command}U")), + shortcut("Heading 1–6", format!("{command}{alt}1–6")), + shortcut("Bulleted list", format!("{command}{alt}8")), + shortcut("Numbered list", format!("{command}{alt}9")), + shortcut("Code block", format!("{command}{alt}C")), + ], + palette, + ), + ] +} + +#[cfg(test)] +mod tests { + use std::path::Path; + + use ui::{ + AppContext, Application, ApplicationHandle, Bounds, Context, HeadlessPlatform, + HeadlessWindow, Pixels, Point, Window, WindowBounds, WindowHandle, WindowOptions, point, + px, size, + }; + use ui_tokens::ThemeMode; + + use crate::{DesktopAppState, DesktopModal, DesktopRoute, register_desktop_fonts}; + + use super::*; + + struct Session { + app: ApplicationHandle, + window: HeadlessWindow, + handle: WindowHandle, + } + + impl Session { + fn new(width: u32, height: u32, theme: ThemeMode) -> Self { + let platform = HeadlessPlatform::new(width, height, 1.0); + let app = Application::with_platform(platform.clone()).run_embedded(|_| {}); + let handle = app.update(|cx| { + register_desktop_fonts(cx).expect("desktop fonts"); + cx.open_window( + WindowOptions { + window_bounds: Some(WindowBounds::Windowed(Bounds::new( + point(px(0.0), px(0.0)), + size(px(width as f32), px(height as f32)), + ))), + ..WindowOptions::default() + }, + |_, cx| { + cx.new(|cx| { + let mut state = DesktopAppState::seed_showcase(theme, "2026-12-02"); + state.navigate_active_pane(DesktopRoute::Trash); + state.open_settings(); + DesktopShellView::new(state, cx) + }) + }, + ) + .expect("desktop window") + }); + let window = platform + .windows() + .into_iter() + .next() + .expect("headless window"); + window.request_frame(); + Self { + app, + window, + handle, + } + } + + fn shell( + &self, + f: impl FnOnce(&mut DesktopShellView, &mut Window, &mut Context) -> T, + ) -> T { + self.app + .update(|cx| self.handle.update(cx, f).expect("desktop view")) + } + + fn bounds(&self, id: &str) -> Bounds { + self.shell(|_, window, _| { + let bounds = window.frame_snapshot().elements[id].bounds; + Bounds::new( + point(px(bounds.x), px(bounds.y)), + size(px(bounds.width), px(bounds.height)), + ) + }) + } + + fn click(&self, id: &str) { + self.click_point(self.bounds(id).center()); + } + + fn click_point(&self, at: Point) { + self.window.simulate_click(at); + self.window.request_frame(); + } + + fn capture(&self, path: &Path) { + self.shell(|_, window, _| { + window + .render_to_image() + .expect("offscreen render") + .save(path) + .expect("saving frame"); + }); + } + } + + #[test] + fn settings_layout_navigation_theme_and_dismissal() { + let evidence = std::env::var_os("AFFINE_SETTINGS_EVIDENCE").map(std::path::PathBuf::from); + if let Some(dir) = &evidence { + std::fs::create_dir_all(dir).expect("settings evidence folder"); + } + for (width, height, panel_width, panel_height) in [ + (1280, 800, 920.0, 680.0), + (720, 560, 672.0, 496.0), + (390, 844, 358.0, 680.0), + (320, 640, 288.0, 608.0), + ] { + for (name, theme) in [("light", ThemeMode::Light), ("dark", ThemeMode::Dark)] { + let app = Session::new(width, height, theme); + let panel = app.bounds(MODAL_ID); + assert_eq!(panel.size, size(px(panel_width), px(panel_height))); + assert_eq!( + panel.center(), + point(px(width as f32 / 2.0), px(height as f32 / 2.0)) + ); + if width >= 720 { + assert_eq!( + app.bounds("desktop.settings.navigation").size.width, + px(SIDEBAR_WIDTH) + ); + } + for page in Page::GENERAL + .into_iter() + .chain(Page::WORKSPACE) + .chain([Page::About]) + { + let tab = app.bounds(page.id()); + assert!(tab.left() >= panel.left() && tab.right() <= panel.right()); + assert!(tab.top() >= panel.top() && tab.bottom() <= panel.bottom()); + app.click(page.id()); + if let Some(dir) = &evidence + && width != 720 + { + app.capture(&dir.join(format!( + "after-{width}x{height}-{name}-{}.png", + page.labels().0.to_lowercase() + ))); + } + } + app.click(Page::Editor.id()); + app.shell(|_, _, cx| assert_eq!(new_doc_default_mode(cx), DocMode::Page)); + for (id, mode) in [ + ("desktop.settings.mode.edgeless", DocMode::Edgeless), + ("desktop.settings.mode.page", DocMode::Page), + ("desktop.settings.mode.edgeless", DocMode::Edgeless), + ] { + app.click(id); + app.shell(|_, _, cx| assert_eq!(new_doc_default_mode(cx), mode)); + } + if let Some(dir) = &evidence + && width != 720 + { + app.capture( + &dir.join(format!("after-{width}x{height}-{name}-editor-edgeless.png")), + ); + } + app.click(Page::Appearance.id()); + let picks = [ + ( + "desktop.settings.theme.dark", + ThemePreference::Dark, + ThemeMode::Dark, + ), + ( + "desktop.settings.theme.light", + ThemePreference::Light, + ThemeMode::Light, + ), + ]; + for (id, pref, mode) in picks { + app.click(id); + app.shell(|view, _, _| { + assert_eq!(view.state.theme_preference, pref); + assert_eq!(view.state.theme, mode); + assert_eq!(view.state.modal, Some(DesktopModal::Settings)); + }); + } + app.click("desktop.settings.theme.system"); + app.shell(|view, _, _| { + assert_eq!(view.state.theme_preference, ThemePreference::System); + }); + app.click_point(point(panel.right() - px(4.0), panel.center().y)); + app.shell(|view, _, _| assert_eq!(view.state.modal, Some(DesktopModal::Settings))); + app.click("desktop.settings.close"); + app.shell(|view, window, cx| { + assert_eq!(view.state.modal, None); + assert_eq!(new_doc_default_mode(cx), DocMode::Edgeless); + view.open_settings(window, cx); + }); + app.window.request_frame(); + app.click_point(point(px(4.0), px(4.0))); + app.shell(|view, _, _| assert_eq!(view.state.modal, None)); + } + } + } + + #[test] + fn settings_scrolls_content_without_moving_navigation() { + let app = Session::new(390, 460, ThemeMode::Light); + app.click(Page::Shortcuts.id()); + let tab = app.bounds(Page::Shortcuts.id()); + let title_id = Page::Shortcuts.title_id(); + let before = app.bounds(&title_id).top(); + let position = app.bounds("desktop.settings.scroll").center(); + app.window + .dispatch_input(ui::PlatformInput::MouseMove(ui::MouseMoveEvent { + position, + pressed_button: None, + modifiers: ui::Modifiers::default(), + })); + app.window + .dispatch_input(ui::PlatformInput::Scroll(ui::ScrollEvent { + position, + delta: ui::ScrollDelta::Pixels(point(px(0.0), px(-90.0))), + modifiers: ui::Modifiers::default(), + touch_phase: ui::TouchPhase::Moved, + source: ui::ScrollSource::Pointer, + })); + app.window.request_frame(); + app.shell(|_, window, _| { + assert!( + window + .frame_snapshot() + .elements + .get(&title_id) + .is_none_or(|heading| px(heading.bounds.y) < before) + ); + }); + assert_eq!(app.bounds(Page::Shortcuts.id()), tab); + app.click(Page::Appearance.id()); + app.click(Page::Shortcuts.id()); + assert_eq!(app.bounds(&title_id).top(), before); + } } diff --git a/packages/frontend/desktop/src/workbench/interaction_tests.rs b/packages/frontend/desktop/src/workbench/interaction_tests.rs index 4e3d307d07..8473b0d851 100644 --- a/packages/frontend/desktop/src/workbench/interaction_tests.rs +++ b/packages/frontend/desktop/src/workbench/interaction_tests.rs @@ -226,7 +226,8 @@ fn search_and_settings_own_keyboard_and_pointer_until_dismissed() { assert_eq!(view.state.modal, modal); assert_eq!(view.state.quick_search.open, modal.is_none()); }); - app.window.simulate_click(point(px(600.0), px(80.0))); + app.window + .simulate_click(point(bounds.center().x, bounds.top() / 2.0)); app.window.request_frame(); app.shell(|view, window, _| { assert!(focused.is_focused(window)); @@ -334,16 +335,6 @@ fn narrow_workbench_drawer_and_centered_modals_fit_the_viewport() { assert_eq!(bounds.center().y, px(height as f32 / 2.0)); assert!(bounds.top() >= px(16.0)); assert!(bounds.bottom() <= px(height as f32 - 16.0)); - if card == "desktop.settings.modal" { - app.shell(|_, window, _| { - let row = - &window.frame_snapshot().elements["desktop.settings.deduplicated_blobs"]; - assert!( - row.bounds.y + row.bounds.height - <= row.content_mask.y + row.content_mask.height - ); - }); - } app.key("escape"); }