mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-05 11:15:01 +03:00
fix(desktop): Match Web settings navigation and font slider
This commit is contained in:
1 parent
1a7ad80429
commit
46c44feba2
4 files changed
+303
-14
No files matched your search
@@ -158,8 +158,8 @@ fn settings_apply_editor_font_to_both_split_panes_and_center_peek() {
|
||||
let track = elements["desktop.settings.font.size"].bounds;
|
||||
let thumb = elements["desktop.settings.font.size.thumb"].bounds;
|
||||
let fraction = f32::from(size - 12) / 12.0;
|
||||
let expected_left = track.x + (track.width - 12.0) * fraction;
|
||||
assert_eq!((thumb.width, thumb.height), (12.0, 12.0));
|
||||
let expected_left = track.x + (track.width - 14.0) * fraction;
|
||||
assert_eq!((thumb.width, thumb.height), (14.0, 14.0));
|
||||
assert!(
|
||||
(thumb.x - expected_left).abs() <= 0.5,
|
||||
"size {size}: thumb {thumb:?}, track {track:?}, expected_left {expected_left}"
|
||||
|
||||
@@ -251,11 +251,11 @@ pub(super) fn navigation(
|
||||
.border_color(ui::transparent_black())
|
||||
.focus_visible(move |style| style.border_color(accent))
|
||||
.bg(if page == tab {
|
||||
palette.bg_pill
|
||||
palette.hover_color
|
||||
} else {
|
||||
ui::transparent_black()
|
||||
})
|
||||
.hover(move |style| style.bg(palette.bg_pill))
|
||||
.hover(move |style| style.bg(palette.hover_color))
|
||||
.px(px(if compact { 5.0 } else { 7.0 }))
|
||||
.h(px(30.0))
|
||||
.flex_shrink(0.0)
|
||||
|
||||
@@ -9,7 +9,8 @@ use super::controls::{FocusStops, focus_stop};
|
||||
use crate::{DesktopFlexExt, DesktopPalette, DesktopShellView, state::EditorFontPreferences};
|
||||
|
||||
const SLIDER_ID: &str = "desktop.settings.font.size";
|
||||
const THUMB: f32 = 12.0;
|
||||
const THUMB: f32 = 14.0;
|
||||
const HIT_HEIGHT: f32 = 28.0;
|
||||
|
||||
struct FontSizeDrag;
|
||||
|
||||
@@ -27,6 +28,13 @@ pub(super) fn slider(
|
||||
window: &mut Window,
|
||||
app: &mut App,
|
||||
) -> impl IntoElement + use<> {
|
||||
let tokens = crate::theme_tokens_for(shell.read(app).state.theme);
|
||||
let track_color = crate::theme_color(
|
||||
tokens,
|
||||
"--affine-v2-layer-insideBorder-border",
|
||||
tokens.border,
|
||||
);
|
||||
let thumb_color = crate::theme_color(tokens, "--affine-v2-icon-primary", tokens.text_primary);
|
||||
let stop = focus_stop(SLIDER_ID, stops, window, app);
|
||||
let focus = stop.handle.clone();
|
||||
let pointer_focus = focus.clone();
|
||||
@@ -42,10 +50,10 @@ pub(super) fn slider(
|
||||
.snapshot("desktop.settings.font.size.thumb", Some(SLIDER_ID))
|
||||
.absolute()
|
||||
.left(ui::relative(fraction))
|
||||
.top(px(8.0))
|
||||
.top(px((HIT_HEIGHT - THUMB) / 2.0))
|
||||
.size(px(THUMB))
|
||||
.rounded(px(THUMB / 2.0))
|
||||
.bg(palette.accent);
|
||||
.bg(thumb_color);
|
||||
thumb.style().margin.left = Some(px(-THUMB * fraction).into());
|
||||
let focus_ring = focus.is_focused(window) && window.last_input_was_keyboard();
|
||||
let mut track = div()
|
||||
@@ -60,7 +68,7 @@ pub(super) fn slider(
|
||||
.flex_1()
|
||||
.min_w_0()
|
||||
.items_center()
|
||||
.h(px(28.0))
|
||||
.h(px(HIT_HEIGHT))
|
||||
.cursor_pointer()
|
||||
.on_mouse_down(MouseButton::Left, move |event, window, app| {
|
||||
window.focus(&pointer_focus, app);
|
||||
@@ -88,16 +96,16 @@ pub(super) fn slider(
|
||||
})
|
||||
.child(
|
||||
div()
|
||||
.w_full()
|
||||
.h(px(4.0))
|
||||
.rounded(px(2.0))
|
||||
.bg(palette.bg_pill)
|
||||
.flex_1()
|
||||
.mx(px(THUMB / 2.0))
|
||||
.h(px(1.0))
|
||||
.bg(track_color)
|
||||
.child(
|
||||
div()
|
||||
.w(ui::relative(fraction))
|
||||
.h_full()
|
||||
.rounded(px(2.0))
|
||||
.bg(palette.accent),
|
||||
.rounded(px(1.0))
|
||||
.bg(thumb_color),
|
||||
),
|
||||
)
|
||||
.child(thumb);
|
||||
|
||||
@@ -0,0 +1,281 @@
|
||||
//! Offscreen checks for the settings navigation and editor font size slider.
|
||||
|
||||
use affine_desktop::{DesktopAppState, DesktopShellView, palette_for, register_desktop_fonts};
|
||||
use anyhow::{Context as _, Result};
|
||||
use ui::{
|
||||
AnyWindowHandle, AppContext, Application, ApplicationHandle, Bounds, HeadlessPlatform,
|
||||
HeadlessWindow, Keystroke, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels,
|
||||
PlatformInput, Point, Rgba, SnapshotRect, WindowBounds, WindowHandle, WindowOptions, point, px,
|
||||
size,
|
||||
};
|
||||
use ui_tokens::{DARK_THEME, LIGHT_THEME, ThemeMode};
|
||||
|
||||
const THEMES: [ThemeMode; 2] = [ThemeMode::Light, ThemeMode::Dark];
|
||||
const EDITOR: &str = "desktop.settings.editor";
|
||||
const SHORTCUTS: &str = "desktop.settings.shortcuts";
|
||||
const SLIDER: &str = "desktop.settings.font.size";
|
||||
const THUMB: &str = "desktop.settings.font.size.thumb";
|
||||
const THUMB_SIZE: f32 = 14.0;
|
||||
|
||||
#[test]
|
||||
fn settings_navigation_and_font_slider_match_web_pixels() -> Result<()> {
|
||||
for theme in THEMES {
|
||||
let desktop = Desktop::open_editor_settings(theme)?;
|
||||
let editor = desktop.bounds(EDITOR)?;
|
||||
let shortcuts = desktop.bounds(SHORTCUTS)?;
|
||||
let track = desktop.bounds(SLIDER)?;
|
||||
let thumb = desktop.bounds(THUMB)?;
|
||||
let (x, y) = nav_sample(shortcuts);
|
||||
let rest = desktop.image()?.get_pixel(x, y).0;
|
||||
|
||||
desktop.move_to(point(px(x as f32 + 0.5), px(y as f32 + 0.5)), None);
|
||||
let image = desktop.image()?;
|
||||
|
||||
let hover = Rgba::from(palette_for(theme).hover_color);
|
||||
let mut expected = rest;
|
||||
for (channel, source) in expected.iter_mut().zip([hover.r, hover.g, hover.b]) {
|
||||
*channel =
|
||||
(source * 255.0 * hover.a + f32::from(*channel) * (1.0 - hover.a)).round() as u8;
|
||||
}
|
||||
assert_ne!(expected, rest, "{theme:?} hover changes the surface");
|
||||
for item in [editor, shortcuts] {
|
||||
let (x, y) = nav_sample(item);
|
||||
assert_eq!(
|
||||
image.get_pixel(x, y).0,
|
||||
expected,
|
||||
"{theme:?} navigation item"
|
||||
);
|
||||
}
|
||||
|
||||
assert_eq!(track.height, 28.0, "{theme:?} slider hit area");
|
||||
assert_eq!([thumb.width, thumb.height], [THUMB_SIZE; 2]);
|
||||
assert_eq!(thumb.y + thumb.height / 2.0, track.y + track.height / 2.0);
|
||||
let tokens = match theme {
|
||||
ThemeMode::Light => &LIGHT_THEME,
|
||||
ThemeMode::Dark => &DARK_THEME,
|
||||
};
|
||||
let color = |token| {
|
||||
let c = tokens
|
||||
.color(token)
|
||||
.with_context(|| format!("resolve {token}"))?;
|
||||
Ok::<_, anyhow::Error>([c.r, c.g, c.b, c.a])
|
||||
};
|
||||
let fill = color("--affine-v2-icon-primary")?;
|
||||
let border = color("--affine-v2-layer-insideBorder-border")?;
|
||||
let y = (track.y + track.height / 2.0) as u32;
|
||||
let center_x = (thumb.x + thumb.width / 2.0) as u32;
|
||||
assert_eq!(image.get_pixel(center_x, y).0, fill, "{theme:?} thumb");
|
||||
let track_rows = |x, color| {
|
||||
(track.y as u32..(track.y + track.height) as u32)
|
||||
.filter(|&row| image.get_pixel(x, row).0 == color)
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
let filled = track_rows((track.x + THUMB_SIZE) as u32, fill);
|
||||
let unfilled = track_rows((track.x + track.width - THUMB_SIZE) as u32, border);
|
||||
assert_eq!(filled.len(), 1, "{theme:?} filled track thickness");
|
||||
assert_eq!(
|
||||
unfilled, filled,
|
||||
"{theme:?} unfilled track thickness and center"
|
||||
);
|
||||
assert!(filled[0].abs_diff(y) <= 1, "{theme:?} track center");
|
||||
assert_ne!(image.get_pixel(track.x as u32, filled[0]).0, fill);
|
||||
assert_ne!(
|
||||
image
|
||||
.get_pixel((track.x + track.width - 1.0) as u32, filled[0])
|
||||
.0,
|
||||
border
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn font_slider_maps_clicks_drags_and_keys_to_exact_sizes() -> Result<()> {
|
||||
for theme in THEMES {
|
||||
let desktop = Desktop::open_editor_settings(theme)?;
|
||||
let track = desktop.bounds(SLIDER)?;
|
||||
let at_size = |size: u8| {
|
||||
let fraction = f32::from(size - 12) / 12.0;
|
||||
point(
|
||||
px(track.x + THUMB_SIZE / 2.0 + (track.width - THUMB_SIZE) * fraction),
|
||||
px(track.y + track.height / 2.0),
|
||||
)
|
||||
};
|
||||
for size in 12..=24 {
|
||||
let p = at_size(size);
|
||||
desktop.click(p);
|
||||
assert_eq!(desktop.font_size()?, size, "{theme:?} click");
|
||||
let thumb = desktop.bounds(THUMB)?;
|
||||
assert!((thumb.x + thumb.width / 2.0 - f32::from(p.x)).abs() <= 1.0);
|
||||
}
|
||||
|
||||
let start = at_size(24);
|
||||
desktop
|
||||
.window
|
||||
.dispatch_input(PlatformInput::MouseDown(MouseDownEvent {
|
||||
button: MouseButton::Left,
|
||||
position: start,
|
||||
modifiers: Default::default(),
|
||||
click_count: 1,
|
||||
first_mouse: false,
|
||||
}));
|
||||
desktop.move_to(at_size(22), Some(MouseButton::Left));
|
||||
for (p, size) in [
|
||||
(at_size(19), 19),
|
||||
(point(px(track.x - 10.0), start.y), 12),
|
||||
(point(px(track.x + track.width + 10.0), start.y), 24),
|
||||
] {
|
||||
desktop.move_to(p, Some(MouseButton::Left));
|
||||
assert_eq!(desktop.font_size()?, size, "{theme:?} drag");
|
||||
}
|
||||
desktop
|
||||
.window
|
||||
.dispatch_input(PlatformInput::MouseUp(MouseUpEvent {
|
||||
button: MouseButton::Left,
|
||||
position: at_size(24),
|
||||
modifiers: Default::default(),
|
||||
click_count: 1,
|
||||
}));
|
||||
desktop.move_to(at_size(18), None);
|
||||
assert_eq!(desktop.font_size()?, 24, "{theme:?} drag released");
|
||||
|
||||
for (key, size) in [
|
||||
("right", 24),
|
||||
("left", 23),
|
||||
("down", 22),
|
||||
("up", 23),
|
||||
("home", 12),
|
||||
("left", 12),
|
||||
("end", 24),
|
||||
] {
|
||||
desktop.app.update(|cx| {
|
||||
AnyWindowHandle::from(desktop.handle).update(cx, |_, window, cx| {
|
||||
window.dispatch_keystroke(Keystroke::parse(key).expect("valid key"), cx);
|
||||
})
|
||||
})?;
|
||||
desktop.frame();
|
||||
assert_eq!(desktop.font_size()?, size, "{theme:?} {key}");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "writes offscreen evidence to SETTINGS_EVIDENCE_DIR"]
|
||||
fn capture_settings_editor() -> Result<()> {
|
||||
let dir = std::path::PathBuf::from(
|
||||
std::env::var_os("SETTINGS_EVIDENCE_DIR").context("set SETTINGS_EVIDENCE_DIR")?,
|
||||
);
|
||||
std::fs::create_dir_all(&dir)?;
|
||||
for (theme, name) in [(ThemeMode::Light, "light"), (ThemeMode::Dark, "dark")] {
|
||||
let desktop = Desktop::open_editor_settings(theme)?;
|
||||
let (x, y) = nav_sample(desktop.bounds(SHORTCUTS)?);
|
||||
desktop.move_to(point(px(x as f32 + 0.5), px(y as f32 + 0.5)), None);
|
||||
desktop
|
||||
.image()?
|
||||
.save(dir.join(format!("settings-editor-{name}.png")))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn nav_sample(bounds: SnapshotRect) -> (u32, u32) {
|
||||
(
|
||||
(bounds.x + bounds.width - 16.0) as u32,
|
||||
(bounds.y + bounds.height / 2.0) as u32,
|
||||
)
|
||||
}
|
||||
|
||||
struct Desktop {
|
||||
platform: std::rc::Rc<HeadlessPlatform>,
|
||||
app: ApplicationHandle,
|
||||
handle: WindowHandle<DesktopShellView>,
|
||||
window: HeadlessWindow,
|
||||
}
|
||||
|
||||
impl Desktop {
|
||||
fn open_editor_settings(theme: ThemeMode) -> Result<Self> {
|
||||
let mut state = DesktopAppState::seed_showcase(theme, "2026-12-03");
|
||||
state.open_doc_in_active_pane("doc-arch");
|
||||
state.open_settings();
|
||||
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 {
|
||||
platform,
|
||||
app,
|
||||
handle,
|
||||
window,
|
||||
};
|
||||
desktop.frame();
|
||||
let editor = desktop.bounds(EDITOR)?;
|
||||
desktop.click(point(
|
||||
px(editor.x + editor.width / 2.0),
|
||||
px(editor.y + editor.height / 2.0),
|
||||
));
|
||||
Ok(desktop)
|
||||
}
|
||||
|
||||
fn frame(&self) {
|
||||
self.window.request_frame();
|
||||
self.platform.run_until_parked();
|
||||
}
|
||||
|
||||
fn bounds(&self, id: &str) -> Result<SnapshotRect> {
|
||||
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 image(&self) -> Result<image::RgbaImage> {
|
||||
self.app.update(|cx| {
|
||||
self.handle
|
||||
.update(cx, |_, window, _| window.render_to_image())
|
||||
})?
|
||||
}
|
||||
|
||||
fn font_size(&self) -> Result<u8> {
|
||||
self.app.update(|cx| {
|
||||
self.handle
|
||||
.update(cx, |shell, _, _| shell.state.editor_font.size())
|
||||
})
|
||||
}
|
||||
|
||||
fn click(&self, position: Point<Pixels>) {
|
||||
self.window.simulate_click(position);
|
||||
self.frame();
|
||||
}
|
||||
|
||||
fn move_to(&self, position: Point<Pixels>, pressed_button: Option<MouseButton>) {
|
||||
self.window
|
||||
.dispatch_input(PlatformInput::MouseMove(MouseMoveEvent {
|
||||
position,
|
||||
pressed_button,
|
||||
modifiers: Default::default(),
|
||||
}));
|
||||
self.frame();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user