diff --git a/packages/frontend/desktop/src/workbench/interaction.rs b/packages/frontend/desktop/src/workbench/interaction.rs index 66e4a5cd69..d40c7ae170 100644 --- a/packages/frontend/desktop/src/workbench/interaction.rs +++ b/packages/frontend/desktop/src/workbench/interaction.rs @@ -449,7 +449,7 @@ pub(super) enum ResizeTarget { #[derive(Clone, Copy)] pub(super) struct ResizeDrag { - target: ResizeTarget, + pub(super) target: ResizeTarget, pointer_x: f32, start: f32, } @@ -478,6 +478,29 @@ impl ResizeDrag { } } +impl DesktopShellView { + pub(super) fn start_resize( + &mut self, + target: ResizeTarget, + x: f32, + event: &ui::MouseDownEvent, + cx: &mut Context, + ) { + let start = match target { + ResizeTarget::Sidebar => x, + ResizeTarget::FloatingSidebar => self.state.sidebar.width, + ResizeTarget::Split(_) => self.state.split.split_ratio, + }; + self.resize = Some(ResizeDrag { + target, + pointer_x: event.position.x.into(), + start, + }); + cx.stop_propagation(); + cx.notify(); + } +} + pub(super) fn resize_handle( target: ResizeTarget, x: f32, @@ -485,13 +508,41 @@ pub(super) fn resize_handle( palette: DesktopPalette, cx: &mut Context, ) -> impl IntoElement + use<> { - let id = match target { - ResizeTarget::Sidebar | ResizeTarget::FloatingSidebar => "desktop.sidebar.resize", - ResizeTarget::Split(_) => "desktop.split.resize", + resize_handle_with_state(target, x, top, false, palette, cx) +} + +pub(super) fn resize_handle_with_state( + target: ResizeTarget, + x: f32, + top: f32, + resizing: bool, + palette: DesktopPalette, + cx: &mut Context, +) -> impl IntoElement + use<> { + const GROUP: &str = "sidebar-resize"; + const HIT_WIDTH: f32 = 8.0; + + let accent = palette.accent; + let line_width = if resizing { 4.0 } else { 2.0 }; + let line = div() + .absolute() + .top_0() + .bottom(px(0.0)) + .left(px((HIT_WIDTH - line_width) / 2.0)) + .w(px(line_width)) + .rounded(px(line_width)); + let line = if resizing { + line.bg(accent) + } else { + line.group_hover(GROUP, move |style| style.bg(accent)) }; div() - .id(id) - .snapshot(id, Some("desktop.shell.root")) + .id(0x5349_4445_4241_5201u64) + .snapshot( + ui::snap_id!("desktop.sidebar.resize"), + Some("desktop.shell.root"), + ) + .group(GROUP) .absolute() .top(px(top)) .bottom(px(if matches!(target, ResizeTarget::FloatingSidebar) { @@ -499,26 +550,15 @@ pub(super) fn resize_handle( } else { 0.0 })) - .left(px((x - 4.0).max(0.0))) - .w(px(8.0)) + .left(px((x - HIT_WIDTH / 2.0).max(0.0))) + .w(px(HIT_WIDTH)) .occlude() .cursor(CursorStyle::ResizeColumn) - .hover(move |style| style.bg(palette.accent_soft)) + .child(line) .on_mouse_down( MouseButton::Left, cx.listener(move |this, event: &ui::MouseDownEvent, _, cx| { - let start = match target { - ResizeTarget::Sidebar => x, - ResizeTarget::FloatingSidebar => this.state.sidebar.width, - ResizeTarget::Split(_) => this.state.split.split_ratio, - }; - this.resize = Some(ResizeDrag { - target, - pointer_x: event.position.x.into(), - start, - }); - cx.stop_propagation(); - cx.notify(); + this.start_resize(target, x, event, cx); }), ) } @@ -610,3 +650,90 @@ pub(super) mod sidebar_width_preference { mod sidebar_width_preference { pub(super) fn save(_: f32) {} } + +#[cfg(test)] +mod tests { + use super::*; + use crate::{DesktopAppState, palette_for}; + use anyhow::{Context as _, Result}; + use ui::{ + AppContext, Application, Bounds, Entity, HeadlessPlatform, Render, WindowBounds, + WindowOptions, point, size, + }; + use ui_tokens::ThemeMode; + + struct SidebarHandle { + shell: Entity, + target: ResizeTarget, + palette: DesktopPalette, + } + + impl Render for SidebarHandle { + fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { + let handle = self.shell.update(cx, |_, cx| { + resize_handle_with_state(self.target, 40.0, 8.0, true, self.palette, cx) + }); + div().size_full().bg(self.palette.bg_canvas).child(handle) + } + } + + #[test] + fn sidebar_resize_state_draws_a_centered_four_pixel_line_without_hover() -> Result<()> { + for (theme, name, accent) in [ + (ThemeMode::Light, "light", [30, 150, 235, 255]), + (ThemeMode::Dark, "dark", [28, 158, 228, 255]), + ] { + for (target, kind) in [ + (ResizeTarget::Sidebar, "fixed"), + (ResizeTarget::FloatingSidebar, "floating"), + ] { + let platform = HeadlessPlatform::new(80, 80, 1.0); + let app = Application::with_platform(platform.clone()).run_embedded(|_| {}); + let window = app.update(|cx| { + cx.open_window( + WindowOptions { + window_bounds: Some(WindowBounds::Windowed(Bounds::new( + point(px(0.0), px(0.0)), + size(px(80.0), px(80.0)), + ))), + ..WindowOptions::default() + }, + |_, cx| { + let shell = cx.new(|cx| { + DesktopShellView::new( + DesktopAppState::seed_showcase(theme, "2026-12-02"), + cx, + ) + }); + cx.new(|_| SidebarHandle { + shell, + target, + palette: palette_for(theme), + }) + }, + ) + })?; + platform + .windows() + .first() + .context("sidebar fixture")? + .request_frame(); + let image = + app.update(|cx| window.update(cx, |_, window, _| window.render_to_image()))??; + for x in 32..48 { + assert_eq!( + image.get_pixel(x, 40).0 == accent, + (38..42).contains(&x), + "{name} {kind} at x={x}" + ); + } + assert_ne!(image.get_pixel(38, 8).0, accent, "{name} {kind} corner"); + if let Some(dir) = std::env::var_os("RESIZE_EVIDENCE_DIR") { + let dir = std::path::PathBuf::from(dir); + image.save(dir.join(format!("{name}-{kind}-active.png")))?; + } + } + } + Ok(()) + } +} diff --git a/packages/frontend/desktop/src/workbench/topbar/tabs.rs b/packages/frontend/desktop/src/workbench/topbar/tabs.rs index da3885600d..5e6501bc45 100644 --- a/packages/frontend/desktop/src/workbench/topbar/tabs.rs +++ b/packages/frontend/desktop/src/workbench/topbar/tabs.rs @@ -16,7 +16,7 @@ use crate::{ use super::{ APP_TABS_HEIGHT, ControlId, DesktopPalette, DesktopShellView, button, icon_button, - interaction::{ResizeTarget, close_tab_button, resize_handle}, + interaction::{ResizeTarget, close_tab_button}, sidebar_switch, }; @@ -570,7 +570,8 @@ fn render_tab_range( } const SPLIT_RESIZE_GROUPS: [&str; 2] = ["split-resize-0", "split-resize-1"]; -const RESIZE_HIT_WIDTH: f32 = 8.0; +const RESIZE_HIT_WIDTH: f32 = 10.0; +const SPLIT_RESIZE_ID: u64 = 0x5350_4c49_5400_0000; const RESIZE_LINE_WIDTH: f32 = 3.0; #[derive(Clone, Copy)] @@ -590,81 +591,93 @@ impl ThreePaneDrag { } } +fn split_divider( + center: f32, + top: f32, + group: &'static str, + primary: ui::Hsla, + resizing: bool, +) -> ui::Div { + let glow = BoxShadow::new(px(0.0), px(12.0), primary.opacity(0.15)) + .blur_radius(px(21.0)) + .spread_radius(px(4.0)); + let shadow = [glow].into_iter().collect(); + let line = div() + .absolute() + .top_0() + .bottom(px(0.0)) + .left(px((RESIZE_HIT_WIDTH - RESIZE_LINE_WIDTH) / 2.0)) + .w(px(RESIZE_LINE_WIDTH)) + .rounded(px(RESIZE_LINE_WIDTH / 2.0)); + let line = if resizing { + line.bg(primary).shadow(shadow) + } else { + line.group_hover(group, move |style| style.bg(primary).shadow(shadow)) + }; + div() + .group(group) + .absolute() + .top(px(top)) + .bottom(px(0.0)) + .left(px((center - RESIZE_HIT_WIDTH / 2.0).max(0.0))) + .w(px(RESIZE_HIT_WIDTH)) + .occlude() + .cursor(CursorStyle::ResizeColumn) + .child(line) +} + pub(in crate::workbench) fn render_split_dividers( shell: &DesktopShellView, x: f32, top: f32, width: f32, - palette: DesktopPalette, + _palette: DesktopPalette, cx: &mut Context, ) -> Vec { - if width <= 0.0 { + let pane_count = shell.state.split.panes.len(); + if width <= 0.0 || !matches!(pane_count, 2 | 3) { return Vec::new(); } let widths = shell.state.split.pane_fractions(); - if shell.state.split.panes.len() == 2 { - return vec![ - resize_handle( - ResizeTarget::Split(width), - x + width * widths[0], - top, - palette, - cx, - ) - .into_any_element(), - ]; - } - if shell.state.split.panes.len() != 3 { - return Vec::new(); - } - // The web split view shows a 3px `button/primary` line with a soft glow on hover and while - // it resizes. let primary = crate::theme_color( crate::theme_tokens_for(shell.state.theme), "--affine-v2-button-primary", Rgba::new(30, 150, 235, 255), ); - let glow = BoxShadow::new(px(0.0), px(12.0), primary.opacity(0.15)) - .blur_radius(px(21.0)) - .spread_radius(px(4.0)); + if pane_count == 2 { + let center = x + width * widths[0]; + let resizing = shell + .resize + .is_some_and(|drag| matches!(drag.target, ResizeTarget::Split(_))); + return vec![ + split_divider(center, top, "split-resize", primary, resizing) + .id(SPLIT_RESIZE_ID) + .snapshot( + ui::snap_id!("desktop.split.resize"), + Some("desktop.shell.root"), + ) + .on_mouse_down( + MouseButton::Left, + cx.listener(move |shell, event: &ui::MouseDownEvent, _, cx| { + shell.start_resize(ResizeTarget::Split(width), center, event, cx); + }), + ) + .into_any_element(), + ]; + } let mut elements = Vec::new(); for (index, ratio) in [widths[0], widths[0] + widths[1]].into_iter().enumerate() { let center = x + width * ratio; - let group = SPLIT_RESIZE_GROUPS[index]; let resizing = shell .three_pane_drag .is_some_and(|drag| drag.index == index); - let line = div() - .absolute() - .top(px(0.0)) - .bottom(px(0.0)) - .left(px((RESIZE_HIT_WIDTH - RESIZE_LINE_WIDTH) / 2.0)) - .w(px(RESIZE_LINE_WIDTH)) - .rounded(px(RESIZE_LINE_WIDTH / 2.0)) - .map(|line| { - let shadow = [glow.clone()].into_iter().collect(); - if resizing { - line.bg(primary).shadow(shadow) - } else { - line.group_hover(group, move |s| s.bg(primary).shadow(shadow)) - } - }); elements.push( - div() - .id(("desktop.split.resize", index)) + split_divider(center, top, SPLIT_RESIZE_GROUPS[index], primary, resizing) + .id(SPLIT_RESIZE_ID + 1 + index as u64) .snapshot( ui::snap_id!("desktop.split.resize.{index}"), Some("desktop.shell.root"), ) - .group(group) - .absolute() - .top(px(top)) - .bottom(px(0.0)) - .left(px((center - RESIZE_HIT_WIDTH / 2.0).max(0.0))) - .w(px(RESIZE_HIT_WIDTH)) - .occlude() - .cursor(CursorStyle::ResizeColumn) - .child(line) .on_mouse_down( MouseButton::Left, cx.listener(move |shell, event: &ui::MouseDownEvent, _, cx| { @@ -1377,6 +1390,7 @@ mod tests { assert_eq!(state.split.panes.len(), 3); let shell = Shell::with_state(state); let handle = shell.bounds("desktop.split.resize.0").expect("divider"); + assert_eq!(handle.width, RESIZE_HIT_WIDTH); let row = handle.y + 200.0; let center = point(px(handle.x + handle.width / 2.0), px(row)); let pixel = |image: &image::RgbaImage, x: f32| image.get_pixel(x as u32, row as u32).0; @@ -1384,13 +1398,14 @@ mod tests { |p: [u8; 4]| (0..3).all(|i| (f32::from(p[i]) - PRIMARY[i]).abs() <= 3.0); // Columns of the divider hit area that show the primary color. let line = |image: &image::RgbaImage, left: f32| { - (0..8) + (0..RESIZE_HIT_WIDTH as u32) .filter(|&dx| is_primary(pixel(image, left + dx as f32))) .count() }; // Whether the glow tints the column `dx` pixels past the hit area. let glow = |rest: &image::RgbaImage, image: &image::RgbaImage, left: f32, dx: f32| { - let (a, b) = (pixel(rest, left + 8.0 + dx), pixel(image, left + 8.0 + dx)); + let x = left + RESIZE_HIT_WIDTH + dx; + let (a, b) = (pixel(rest, x), pixel(image, x)); (0..3).any(|i| a[i] != b[i]) }; let changed = |a: &image::RgbaImage, b: &image::RgbaImage, left: f32| { diff --git a/packages/frontend/desktop/tests/resize_handles_visual_parity.rs b/packages/frontend/desktop/tests/resize_handles_visual_parity.rs new file mode 100644 index 0000000000..36523b2c9f --- /dev/null +++ b/packages/frontend/desktop/tests/resize_handles_visual_parity.rs @@ -0,0 +1,252 @@ +use std::path::{Path, PathBuf}; + +use affine_desktop::{DesktopAppState, DesktopShellView, register_desktop_fonts}; +use anyhow::{Context as _, Result}; +use image::RgbaImage; +use ui::{ + AppContext, Application, ApplicationHandle, Bounds, HeadlessPlatform, HeadlessWindow, + MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, PlatformInput, Point, + SnapshotRect, WindowBounds, WindowHandle, WindowOptions, point, px, size, +}; +use ui_tokens::ThemeMode; + +const SPLIT: &str = "desktop.split.resize"; +const SIDEBAR: &str = "desktop.sidebar.resize"; +const SPLIT_COLOR: [u8; 3] = [30, 150, 235]; + +#[test] +fn two_pane_divider_retains_its_line_and_glow_through_pointer_capture() -> Result<()> { + for theme in [ThemeMode::Light, ThemeMode::Dark] { + let desktop = Desktop::new(theme, false)?; + let handle = desktop.bounds(SPLIT)?; + assert_eq!(handle.width, 10.0); + let y = (handle.y + 200.0) as u32; + let center = handle.x + handle.width / 2.0; + desktop.move_to(point(px(center - 80.0), px(y as f32)), None); + let idle = desktop.image()?; + assert_eq!(line_width(&idle, handle, y, SPLIT_COLOR), 0); + + // The outermost pixel of the hit area must also start a drag. + let edge = point(px(handle.x + 0.5), px(y as f32)); + desktop.move_to(edge, None); + let hovered = desktop.image()?; + assert!((2..=3).contains(&line_width(&hovered, handle, y, SPLIT_COLOR))); + let shadow_x = (handle.x + handle.width + 4.0) as u32; + assert_ne!(hovered.get_pixel(shadow_x, y), idle.get_pixel(shadow_x, y)); + + desktop.mouse_down(edge); + let target = point(edge.x + px(40.0), edge.y); + desktop.move_to(target, Some(MouseButton::Left)); + let moved = desktop.bounds(SPLIT)?; + assert!((moved.x - handle.x - 40.0).abs() < 1.0); + let dragging = desktop.image()?; + assert!((2..=3).contains(&line_width(&dragging, moved, y, SPLIT_COLOR))); + + desktop.mouse_up(target); + desktop.move_to(point(target.x - px(80.0), target.y), None); + assert_eq!(desktop.bounds(SPLIT)?.x, moved.x); + let released = desktop.image()?; + assert_eq!(line_width(&released, moved, y, SPLIT_COLOR), 0); + let shadow_x = (moved.x + moved.width + 4.0) as u32; + assert_ne!( + dragging.get_pixel(shadow_x, y), + released.get_pixel(shadow_x, y) + ); + } + Ok(()) +} + +#[test] +fn fixed_and_floating_sidebars_show_only_the_centered_accent_line_on_hover() -> Result<()> { + for (theme, color) in [ + (ThemeMode::Light, [30, 150, 235]), + (ThemeMode::Dark, [28, 158, 228]), + ] { + for floating in [false, true] { + let desktop = Desktop::new(theme, floating)?; + let handle = desktop.bounds(SIDEBAR)?; + assert_eq!(handle.width, 8.0); + assert_eq!( + handle.y + handle.height, + if floating { 792.0 } else { 800.0 } + ); + let (x, y) = ((handle.x + 4.0) as u32, (handle.y + 200.0) as u32); + let idle = desktop.image()?; + desktop.move_to(point(px(x as f32), px(y as f32)), None); + let hover = desktop.image()?; + assert_eq!(line_width(&idle, handle, y, color), 0); + assert_eq!(line_width(&hover, handle, y, color), 2); + for sample_x in x - 16..x + 16 { + if (x - 1..x + 1).contains(&sample_x) { + assert!(is_color(hover.get_pixel(sample_x, y).0, color)); + } else { + assert_eq!(idle.get_pixel(sample_x, y), hover.get_pixel(sample_x, y)); + } + } + + let start = point(px(x as f32), px(y as f32)); + let target = point(start.x + px(36.0), start.y); + desktop.mouse_down(start); + desktop.move_to(target, Some(MouseButton::Left)); + desktop.mouse_up(target); + assert_eq!(desktop.bounds(SIDEBAR)?.x, handle.x + 36.0); + } + } + Ok(()) +} + +#[test] +#[ignore = "writes offscreen evidence to RESIZE_EVIDENCE_DIR"] +fn capture_workbench_resize_handles() -> Result<()> { + let dir = + PathBuf::from(std::env::var_os("RESIZE_EVIDENCE_DIR").context("set RESIZE_EVIDENCE_DIR")?); + std::fs::create_dir_all(&dir)?; + for (theme, name) in [(ThemeMode::Light, "light"), (ThemeMode::Dark, "dark")] { + let desktop = Desktop::new(theme, false)?; + let split = desktop.center(SPLIT)?; + desktop.move_to(split, None); + desktop.save(&dir, name, "split-hover")?; + desktop.mouse_down(split); + desktop.move_to(point(split.x + px(40.0), split.y), Some(MouseButton::Left)); + desktop.save(&dir, name, "split-drag")?; + desktop.mouse_up(point(split.x + px(40.0), split.y)); + + for (floating, state) in [(false, "fixed"), (true, "floating")] { + let desktop = Desktop::new(theme, floating)?; + desktop.move_to(desktop.center(SIDEBAR)?, None); + desktop.save(&dir, name, state)?; + } + } + Ok(()) +} + +fn is_color(pixel: [u8; 4], color: [u8; 3]) -> bool { + pixel[3] == 255 + && pixel + .into_iter() + .zip(color) + .all(|(actual, expected)| actual.abs_diff(expected) <= 2) +} + +fn line_width(image: &RgbaImage, handle: SnapshotRect, y: u32, color: [u8; 3]) -> usize { + let start = handle.x as u32; + (start..start + handle.width as u32) + .filter(|&x| is_color(image.get_pixel(x, y).0, color)) + .count() +} + +struct Desktop { + app: ApplicationHandle, + handle: WindowHandle, + window: HeadlessWindow, +} + +impl Desktop { + fn new(theme: ThemeMode, floating: bool) -> Result { + let mut state = DesktopAppState::seed_showcase(theme, "2026-12-02"); + state.open_doc_in_split("doc-sync"); + state.sidebar.open = !floating; + let platform = HeadlessPlatform::new(1280, 800, 1.0); + let app = Application::with_platform(platform.clone()).run_embedded(|_| {}); + let handle = app.update(|cx| { + register_desktop_fonts(cx)?; + cx.open_window( + WindowOptions { + window_bounds: Some(WindowBounds::Windowed(Bounds::new( + point(px(0.0), px(0.0)), + size(px(1280.0), px(800.0)), + ))), + ..WindowOptions::default() + }, + |_, cx| cx.new(|cx| DesktopShellView::new(state, cx)), + ) + })?; + let window = platform + .windows() + .into_iter() + .next() + .context("desktop window")?; + let desktop = Self { + app, + handle, + window, + }; + desktop.frame(); + if floating { + desktop.move_to(point(px(2.0), px(160.0)), None); + } + Ok(desktop) + } + + fn frame(&self) { + self.window.request_frame(); + self.window.request_frame(); + } + + fn bounds(&self, id: &str) -> Result { + self.app + .update(|cx| { + self.handle.update(cx, |_, window, _| { + window + .frame_snapshot() + .elements + .get(id) + .map(|element| element.bounds) + }) + })? + .with_context(|| format!("find {id}")) + } + + fn center(&self, id: &str) -> Result> { + let bounds = self.bounds(id)?; + Ok(point( + px(bounds.x + bounds.width / 2.0), + px(bounds.y + bounds.height / 2.0), + )) + } + + fn move_to(&self, position: Point, pressed_button: Option) { + self.window + .dispatch_input(PlatformInput::MouseMove(MouseMoveEvent { + position, + pressed_button, + ..Default::default() + })); + self.frame(); + } + + fn mouse_down(&self, position: Point) { + self.window + .dispatch_input(PlatformInput::MouseDown(MouseDownEvent { + position, + button: MouseButton::Left, + click_count: 1, + ..Default::default() + })); + self.frame(); + } + + fn mouse_up(&self, position: Point) { + self.window + .dispatch_input(PlatformInput::MouseUp(MouseUpEvent { + position, + button: MouseButton::Left, + click_count: 1, + ..Default::default() + })); + self.frame(); + } + + fn image(&self) -> Result { + self.app.update(|cx| { + self.handle + .update(cx, |_, window, _| window.render_to_image()) + })? + } + + fn save(&self, dir: &Path, theme: &str, state: &str) -> Result<()> { + self.image()? + .save(dir.join(format!("{theme}-{state}.png")))?; + Ok(()) + } +}