mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-05 11:15:01 +03:00
The docs explorer grid shows the number of columns that the web masonry calculates from the width of the pane. A card has the width of its column and a height of 1.26 times that width. The last card of a row keeps its column width. Grid cards are 24 px apart. List rows are 12 px apart, and a group header has 12 px above and below it. The side padding follows the width of the pane, as on the web. The explorer row cache no longer depends on the grid mode or the width. A resize that changes the number of columns only splits the rows again. In a window that is less than 280 px high, the explorer controls wrap and become smaller, and the side padding is 12 px. Each control keeps its full width, so you can click it. The trash keeps its row keys until the document views change. The "Select all" button shows only in the select mode, as on the web. Escape or "Done" ends the select mode. The trash has no "Empty Trash" button, as on the web. To delete all trashed documents, use "Select all" and "Delete permanently". After "Clear" empties the selection, the trash stays in the select mode. Then the select bar disables "Restore" and "Delete permanently", and a click on a document title selects the document. The trash opens the read-only preview only outside the select mode.
460 lines
17 KiB
Rust
460 lines
17 KiB
Rust
//! Docs explorer and trash rows over a large workspace: the per-frame allocations of the row keys,
|
|
//! the list spacing, the row menu button, and the explorer controls in short windows.
|
|
|
|
use std::{
|
|
alloc::{GlobalAlloc, Layout, System},
|
|
cell::Cell,
|
|
};
|
|
|
|
use affine_desktop::{
|
|
DesktopAppState, DesktopRoute, DesktopShellView, palette_for, register_desktop_fonts,
|
|
};
|
|
use affine_store::directory::DocMode;
|
|
use ui::{
|
|
AppContext, Application, ApplicationHandle, Bounds, HeadlessPlatform, HeadlessWindow, Hsla,
|
|
MouseButton, MouseDownEvent, MouseMoveEvent, PlatformInput, Rgba, SnapshotRect, WindowBounds,
|
|
WindowHandle, WindowOptions, point, px, size,
|
|
};
|
|
use ui_tokens::ThemeMode;
|
|
|
|
thread_local! {
|
|
/// Allocation calls and requested bytes of this thread while `COUNTING` is set.
|
|
static COUNTS: Cell<(u64, u64)> = const { Cell::new((0, 0)) };
|
|
static COUNTING: Cell<bool> = const { Cell::new(false) };
|
|
}
|
|
|
|
/// The system allocator with per-thread totals, so a test can count what one frame allocates.
|
|
struct CountingAllocator;
|
|
|
|
fn record(bytes: usize) {
|
|
if COUNTING.try_with(Cell::get).unwrap_or(false) {
|
|
let _ = COUNTS.try_with(|counts| {
|
|
let (calls, total) = counts.get();
|
|
counts.set((calls + 1, total + bytes as u64));
|
|
});
|
|
}
|
|
}
|
|
|
|
// SAFETY: Every call goes to `System` with its arguments unchanged, and the totals are `Cell`s
|
|
// in thread-local storage that need no allocation.
|
|
unsafe impl GlobalAlloc for CountingAllocator {
|
|
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
|
record(layout.size());
|
|
unsafe { System.alloc(layout) }
|
|
}
|
|
|
|
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
|
|
record(layout.size());
|
|
unsafe { System.alloc_zeroed(layout) }
|
|
}
|
|
|
|
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
|
unsafe { System.dealloc(ptr, layout) }
|
|
}
|
|
|
|
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
|
|
record(new_size);
|
|
unsafe { System.realloc(ptr, layout, new_size) }
|
|
}
|
|
}
|
|
|
|
#[global_allocator]
|
|
static ALLOCATOR: CountingAllocator = CountingAllocator;
|
|
|
|
const DOCS: usize = 1_024;
|
|
const SHOWCASE_TAGS: [&str; 4] = ["rust", "architecture", "release", "design"];
|
|
|
|
struct Shell {
|
|
app: ApplicationHandle,
|
|
handle: WindowHandle<DesktopShellView>,
|
|
window: HeadlessWindow,
|
|
/// The most recently updated document: the first row.
|
|
first: String,
|
|
}
|
|
|
|
impl Shell {
|
|
fn new(theme: ThemeMode, tags_per_doc: usize) -> Self {
|
|
Self::open(theme, tags_per_doc, DesktopRoute::AllDocs)
|
|
}
|
|
|
|
fn open(theme: ThemeMode, tags_per_doc: usize, route: DesktopRoute) -> Self {
|
|
Self::sized(theme, tags_per_doc, route, (1280, 800))
|
|
}
|
|
|
|
fn sized(theme: ThemeMode, tags_per_doc: usize, route: DesktopRoute, size: (u32, u32)) -> Self {
|
|
Self::with_docs(theme, tags_per_doc, route, size, DOCS)
|
|
}
|
|
|
|
fn with_docs(
|
|
theme: ThemeMode,
|
|
tags_per_doc: usize,
|
|
route: DesktopRoute,
|
|
(width, height): (u32, u32),
|
|
docs: usize,
|
|
) -> Self {
|
|
let trash = route == DesktopRoute::Trash;
|
|
let mut state = DesktopAppState::seed_showcase(theme, "2026-12-02");
|
|
let mut last = String::new();
|
|
for index in 0..docs {
|
|
last = state.store.create_doc(
|
|
format!("Tagged document {index:04}"),
|
|
DocMode::Page,
|
|
None,
|
|
1_900_000_000_000 + index as i64,
|
|
);
|
|
let doc = state.store.docs.get_mut(&last).expect("new document");
|
|
doc.meta.tags = SHOWCASE_TAGS[..tags_per_doc]
|
|
.iter()
|
|
.map(|tag| (*tag).to_owned())
|
|
.collect();
|
|
if trash {
|
|
assert!(state.store.trash_doc(&last, 1_900_000_000_000));
|
|
}
|
|
}
|
|
// A metadata edit after the tag writes builds the views again.
|
|
assert!(
|
|
state
|
|
.store
|
|
.rename_doc(&last, "Tagged document last", 1_900_000_010_000)
|
|
);
|
|
state.navigate_active_pane(route);
|
|
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| DesktopShellView::new(state, cx)),
|
|
)
|
|
.expect("desktop window")
|
|
});
|
|
let window = platform.windows().remove(0);
|
|
window.request_frame();
|
|
window.request_frame();
|
|
Self {
|
|
app,
|
|
handle,
|
|
window,
|
|
first: last,
|
|
}
|
|
}
|
|
|
|
/// Allocation calls and bytes of one full frame of the shell.
|
|
fn frame_allocations(&self) -> (u64, u64) {
|
|
self.app.update(|cx| {
|
|
self.handle
|
|
.update(cx, |_, _, cx| cx.notify())
|
|
.expect("desktop view")
|
|
});
|
|
COUNTS.with(|counts| counts.set((0, 0)));
|
|
COUNTING.with(|counting| counting.set(true));
|
|
self.window.request_frame();
|
|
COUNTING.with(|counting| counting.set(false));
|
|
COUNTS.with(Cell::get)
|
|
}
|
|
|
|
/// The bounds of the snapshot elements whose IDs start with `prefix`, from top to bottom.
|
|
fn column(&self, prefix: &str) -> Vec<SnapshotRect> {
|
|
self.app.update(|cx| {
|
|
self.handle
|
|
.update(cx, |_, window, _| {
|
|
let mut rects: Vec<_> = window
|
|
.frame_snapshot()
|
|
.elements
|
|
.iter()
|
|
.filter(|(id, _)| id.starts_with(prefix))
|
|
.map(|(_, element)| element.bounds)
|
|
.collect();
|
|
rects.sort_by(|a, b| a.y.total_cmp(&b.y));
|
|
rects
|
|
})
|
|
.expect("desktop view")
|
|
})
|
|
}
|
|
|
|
fn bounds(&self, id: &str) -> SnapshotRect {
|
|
self.app.update(|cx| {
|
|
self.handle
|
|
.update(cx, |_, window, _| {
|
|
window.frame_snapshot().elements[id].bounds
|
|
})
|
|
.expect("desktop view")
|
|
})
|
|
}
|
|
|
|
fn contains(&self, id: &str) -> bool {
|
|
self.app.update(|cx| {
|
|
self.handle
|
|
.update(cx, |_, window, _| {
|
|
window.frame_snapshot().elements.contains_key(id)
|
|
})
|
|
.expect("desktop view")
|
|
})
|
|
}
|
|
|
|
fn image(&self) -> image::RgbaImage {
|
|
self.app.update(|cx| {
|
|
self.handle
|
|
.update(cx, |_, window, _| {
|
|
window.render_to_image().expect("frame image")
|
|
})
|
|
.expect("desktop view")
|
|
})
|
|
}
|
|
|
|
/// The center of an element, after a check that the window and the clip show all of it.
|
|
fn visible_center(&self, id: &str, (width, height): (u32, u32)) -> (f32, f32) {
|
|
let (b, mask) = self.app.update(|cx| {
|
|
self.handle
|
|
.update(cx, |_, window, _| {
|
|
let element = &window.frame_snapshot().elements[id];
|
|
(element.bounds, element.content_mask)
|
|
})
|
|
.expect("desktop view")
|
|
});
|
|
let label = format!("{width}x{height}: {id} at {b:?} in {mask:?}");
|
|
assert!(b.width > 0.0 && b.height > 0.0, "{label}");
|
|
assert!(
|
|
b.x >= mask.x.max(0.0) - 0.5
|
|
&& b.y >= mask.y.max(0.0) - 0.5
|
|
&& b.right() <= mask.right().min(width as f32) + 0.5
|
|
&& b.bottom() <= mask.bottom().min(height as f32) + 0.5,
|
|
"{label}"
|
|
);
|
|
(b.x + b.width / 2.0, b.y + b.height / 2.0)
|
|
}
|
|
|
|
fn pointer(&self, x: f32, y: f32) {
|
|
self.window
|
|
.dispatch_input(PlatformInput::MouseMove(MouseMoveEvent {
|
|
position: point(px(x), px(y)),
|
|
..Default::default()
|
|
}));
|
|
self.window.request_frame();
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn explorer_frame_allocations_do_not_grow_with_the_tags_of_every_document() {
|
|
let mut frames = Vec::new();
|
|
for tags in [0, SHOWCASE_TAGS.len()] {
|
|
let shell = Shell::new(ThemeMode::Light, tags);
|
|
assert!(shell.contains(&format!("desktop.list.doc.{}", shell.first)));
|
|
// The first counted frame can still fill caches: count the second.
|
|
shell.frame_allocations();
|
|
let (calls, bytes) = shell.frame_allocations();
|
|
println!("{DOCS} documents x {tags} tags: {calls} allocations, {bytes} bytes per frame");
|
|
frames.push(calls);
|
|
}
|
|
// Only the mounted rows draw their tags. Before the row keys read the tag names in place,
|
|
// every frame allocated one name per tag of every document.
|
|
let extra = frames[1] - frames[0];
|
|
assert!(
|
|
extra < DOCS as u64,
|
|
"{extra} extra allocations per frame for {} tags",
|
|
DOCS * SHOWCASE_TAGS.len()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn trash_frame_allocations_do_not_grow_with_the_trashed_documents() {
|
|
let mut frames = Vec::new();
|
|
for docs in [64, DOCS] {
|
|
let shell = Shell::with_docs(ThemeMode::Light, 0, DesktopRoute::Trash, (1280, 800), docs);
|
|
assert!(!shell.column("desktop.trash.row.").is_empty());
|
|
shell.frame_allocations();
|
|
let (calls, bytes) = shell.frame_allocations();
|
|
println!("{docs} trashed documents: {calls} allocations, {bytes} bytes per frame");
|
|
frames.push(calls);
|
|
}
|
|
// Before the trash shared its row keys, every frame allocated a key for each document.
|
|
let extra = frames[1].saturating_sub(frames[0]);
|
|
assert!(
|
|
extra < (DOCS / 4) as u64,
|
|
"{extra} extra allocations per frame for {DOCS} trashed documents"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn explorer_list_spaces_group_headers_and_rows_like_the_web_list() {
|
|
let shell = Shell::new(ThemeMode::Light, 0);
|
|
let header = shell.column("desktop.explorer.group_header.")[0];
|
|
let rows = shell.column("desktop.list.doc.");
|
|
assert_eq!(header.height, 24.0);
|
|
// The web list puts 12 px under a group header and between the rows.
|
|
assert_eq!(rows[0].y - header.y, 24.0 + 12.0);
|
|
for pair in rows[..4].windows(2) {
|
|
assert!(pair[0].height >= 42.0);
|
|
assert_eq!(pair[1].y - pair[0].bottom(), 12.0);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn explorer_grid_columns_and_display_menu_follow_the_window_size() {
|
|
for (width, height, columns) in [
|
|
(1280, 800, 4),
|
|
(1024, 768, 3),
|
|
(568, 320, 2),
|
|
(568, 250, 2),
|
|
(400, 250, 1),
|
|
] {
|
|
let shell = Shell::sized(ThemeMode::Light, 0, DesktopRoute::AllDocs, (width, height));
|
|
let click = |id: &str| {
|
|
let target = shell.bounds(id);
|
|
let (x, y) = (
|
|
target.x + target.width / 2.0,
|
|
target.y + target.height / 2.0,
|
|
);
|
|
assert!(
|
|
target.x >= 0.0 && target.x + target.width <= width as f32 && y < height as f32,
|
|
"{width}x{height}: {id} at {target:?}"
|
|
);
|
|
shell.window.simulate_click(point(px(x), px(y)));
|
|
shell.window.request_frame();
|
|
};
|
|
click("desktop.explorer.grid");
|
|
let cards = shell.column("desktop.list.doc.");
|
|
let first_row = cards.iter().filter(|card| card.y == cards[0].y).count();
|
|
assert_eq!(first_row, columns, "{width}x{height}");
|
|
click("desktop.explorer.display");
|
|
assert!(
|
|
shell.contains("desktop.explorer.menu"),
|
|
"{width}x{height}: Display menu"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn explorer_controls_wrap_and_stay_clickable_in_short_windows() {
|
|
const CONTROLS: [&str; 7] = [
|
|
"desktop.explorer.nav.docs",
|
|
"desktop.explorer.nav.collections",
|
|
"desktop.explorer.nav.tags",
|
|
"desktop.explorer.display",
|
|
"desktop.explorer.select",
|
|
"desktop.explorer.new_doc",
|
|
"desktop.explorer.new_doc.menu",
|
|
];
|
|
for size in [(400, 279), (400, 250), (320, 250), (640, 204)] {
|
|
let open = || Shell::with_docs(ThemeMode::Light, 0, DesktopRoute::AllDocs, size, 8);
|
|
let shell = open();
|
|
for id in CONTROLS {
|
|
shell.visible_center(id, size);
|
|
}
|
|
// A short window still shows the center of the first document row under the controls.
|
|
let first = shell.column("desktop.list.doc.")[0];
|
|
assert!(
|
|
first.y + first.height / 2.0 < size.1 as f32,
|
|
"{size:?}: {first:?}"
|
|
);
|
|
if size == (640, 204) {
|
|
// The controls stay on one line, so that the rows keep the space.
|
|
let line = shell.bounds("desktop.explorer.nav.docs").y;
|
|
assert_eq!(shell.bounds("desktop.explorer.select").y, line, "{size:?}");
|
|
assert!(
|
|
shell.bounds("desktop.explorer.new_doc").y < line + 12.0,
|
|
"{size:?}"
|
|
);
|
|
}
|
|
|
|
for (id, shows) in [
|
|
("desktop.explorer.display", "desktop.explorer.menu"),
|
|
("desktop.explorer.new_doc.menu", "desktop.explorer.menu"),
|
|
("desktop.explorer.select", "desktop.explorer.selected"),
|
|
(
|
|
"desktop.explorer.nav.collections",
|
|
"desktop.explorer.group.0",
|
|
),
|
|
] {
|
|
let shell = open();
|
|
let (x, y) = shell.visible_center(id, size);
|
|
shell.window.simulate_click(point(px(x), px(y)));
|
|
shell.window.request_frame();
|
|
assert!(shell.contains(shows), "{size:?}: {id}");
|
|
}
|
|
}
|
|
}
|
|
|
|
fn rgb(color: Hsla) -> [f32; 3] {
|
|
let color = Rgba::from(color);
|
|
[color.r * 255.0, color.g * 255.0, color.b * 255.0]
|
|
}
|
|
|
|
#[test]
|
|
fn explorer_row_menu_button_is_a_vertical_icon_button_that_opens_and_closes_the_menu() {
|
|
for theme in [ThemeMode::Light, ThemeMode::Dark] {
|
|
let shell = Shell::new(theme, 0);
|
|
let palette = palette_for(theme);
|
|
let row = shell.bounds(&format!("desktop.list.doc.{}", shell.first));
|
|
shell.pointer(row.x + 40.0, row.y + row.height / 2.0);
|
|
let button = shell.bounds(&format!("desktop.explorer.doc_menu.{}", shell.first));
|
|
assert_eq!((button.width, button.height), (28.0, 28.0));
|
|
let row_hovered = shell.image();
|
|
|
|
// The glyph is three dots in a column: its ink is taller than wide.
|
|
let ink = (0..28u32)
|
|
.flat_map(|dy| (0..28u32).map(move |dx| (dx, dy)))
|
|
.filter(|&(dx, dy)| {
|
|
let pixel = row_hovered
|
|
.get_pixel(button.x as u32 + dx, button.y as u32 + dy)
|
|
.0;
|
|
let text = rgb(palette.text_secondary);
|
|
(0..3).all(|i| (f32::from(pixel[i]) - text[i]).abs() <= 48.0)
|
|
})
|
|
.collect::<Vec<_>>();
|
|
let span = |axis: fn(&(u32, u32)) -> u32| {
|
|
let values = ink.iter().map(axis);
|
|
values.clone().max().unwrap_or(0) - values.min().unwrap_or(0) + 1
|
|
};
|
|
let (width, height) = (span(|p| p.0), span(|p| p.1));
|
|
assert!(
|
|
!ink.is_empty() && height >= 2 * width,
|
|
"{theme:?}: glyph ink {width}x{height}"
|
|
);
|
|
|
|
// Hover paints the web hover overlay with a 4px radius: the pixel one in from the
|
|
// corner is inside a 4px corner and outside a 6px one.
|
|
shell.pointer(button.x + 14.0, button.y + 14.0);
|
|
let hovered = shell.image();
|
|
for (dx, dy) in [(1u32, 1u32), (1, 14)] {
|
|
let (x, y) = (button.x as u32 + dx, button.y as u32 + dy);
|
|
let under = row_hovered.get_pixel(x, y).0;
|
|
let alpha = Rgba::from(palette.hover_overlay).a;
|
|
let overlay = rgb(palette.hover_overlay);
|
|
let expected: [f32; 3] =
|
|
std::array::from_fn(|i| overlay[i] * alpha + f32::from(under[i]) * (1.0 - alpha));
|
|
let pixel = hovered.get_pixel(x, y).0;
|
|
let close = (0..3).all(|i| (f32::from(pixel[i]) - expected[i]).abs() <= 3.0);
|
|
assert!(
|
|
close,
|
|
"{theme:?} ({dx}, {dy}): {pixel:?}, expected {expected:?}"
|
|
);
|
|
}
|
|
|
|
shell
|
|
.window
|
|
.simulate_click(point(px(button.x + 14.0), px(button.y + 14.0)));
|
|
shell.window.request_frame();
|
|
assert!(shell.contains("desktop.explorer.menu"), "{theme:?}: menu");
|
|
shell
|
|
.window
|
|
.dispatch_input(PlatformInput::MouseDown(MouseDownEvent {
|
|
button: MouseButton::Left,
|
|
position: point(px(row.x + 40.0), px(790.0)),
|
|
click_count: 1,
|
|
..Default::default()
|
|
}));
|
|
shell.window.request_frame();
|
|
assert!(
|
|
!shell.contains("desktop.explorer.menu"),
|
|
"{theme:?}: menu closed"
|
|
);
|
|
}
|
|
}
|