mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-06 11:42:26 +03:00
perf(desktop): draw only visible document rows
Measure the rows along the scroll path to keep wheel movement in pixels. Keep the visible document in place when the list or its width changes.
This commit is contained in:
1 parent
ea73ac3757
commit
9a594dc907
6 files changed
+1290
-430
No files matched your search
@@ -0,0 +1,124 @@
|
||||
use affine_store::directory::DocMode;
|
||||
use ui::{
|
||||
Div, InteractiveElement, ParentElement, SharedString, Stateful, StatefulInteractiveElement,
|
||||
Styled, div, prelude::FluentBuilder as _, px,
|
||||
};
|
||||
|
||||
use super::{Controls, DocAction, Menu, Popup};
|
||||
use crate::{
|
||||
DesktopPalette,
|
||||
views_extra::{button, doc_order::DocOrder, vertical_scroll},
|
||||
};
|
||||
|
||||
pub(super) fn render_menu(
|
||||
popup: &Popup,
|
||||
controls: &Controls,
|
||||
palette: DesktopPalette,
|
||||
(writable, can_split): (bool, bool),
|
||||
tags: Vec<(String, u32, usize)>,
|
||||
selected: Vec<String>,
|
||||
) -> Stateful<Div> {
|
||||
let state = controls.state.clone();
|
||||
let mut menu = div()
|
||||
.id("desktop.explorer.menu")
|
||||
.snapshot("desktop.explorer.menu", Some("desktop.workbench"))
|
||||
.occlude()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.w(px(192.0))
|
||||
.p(px(4.0))
|
||||
.rounded(px(8.0))
|
||||
.border_1()
|
||||
.border_color(palette.border)
|
||||
.bg(palette.bg_elevated);
|
||||
menu.interactivity().on_mouse_down_out(move |_, _, app| {
|
||||
state.update(app, |state, cx| {
|
||||
state.popup = None;
|
||||
cx.notify();
|
||||
});
|
||||
});
|
||||
let option = |label: SharedString, enabled| {
|
||||
let mut row = button(label.clone(), label, palette, enabled);
|
||||
row.style().justify_content = Some(ui::JustifyContent::FlexStart);
|
||||
row
|
||||
};
|
||||
match &popup.menu {
|
||||
Menu::Order => {
|
||||
for (label, order) in [
|
||||
("Updated", DocOrder::Updated),
|
||||
("Created", DocOrder::Created),
|
||||
("Title", DocOrder::Title),
|
||||
] {
|
||||
for (direction, ascending) in [("ascending", true), ("descending", false)] {
|
||||
menu = menu.child(
|
||||
option(format!("{label} {direction}").into(), true).on_click(
|
||||
controls.change(move |state, _| {
|
||||
state.order = order;
|
||||
state.ascending = ascending;
|
||||
state.popup = None;
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Menu::Filter => {
|
||||
for (label, filter) in [
|
||||
("All", None),
|
||||
("Page", Some(DocMode::Page)),
|
||||
("Edgeless", Some(DocMode::Edgeless)),
|
||||
] {
|
||||
menu = menu.child(option(label.into(), true).on_click(controls.change(
|
||||
move |state, _| {
|
||||
state.filter = filter;
|
||||
state.popup = None;
|
||||
},
|
||||
)));
|
||||
}
|
||||
}
|
||||
Menu::Row(id) => {
|
||||
menu = menu
|
||||
.child(
|
||||
option("Open in center peek".into(), true)
|
||||
.on_click(controls.doc_action(vec![id.clone()], DocAction::Peek)),
|
||||
)
|
||||
.when(can_split, |menu| {
|
||||
menu.child(
|
||||
option("Open in split view".into(), true)
|
||||
.snapshot("desktop.explorer.menu.split", Some("desktop.workbench"))
|
||||
.on_click(controls.doc_action(vec![id.clone()], DocAction::Split)),
|
||||
)
|
||||
})
|
||||
.child(
|
||||
option("Move to Trash".into(), writable)
|
||||
.snapshot("desktop.explorer.menu.trash", Some("desktop.explorer.menu"))
|
||||
.when(writable, |button| {
|
||||
button
|
||||
.text_color(palette.danger)
|
||||
.on_click(controls.doc_action(vec![id.clone()], DocAction::Trash))
|
||||
}),
|
||||
);
|
||||
}
|
||||
Menu::Tag => {
|
||||
if tags.is_empty() {
|
||||
menu = menu.child(option("No workspace tags".into(), false));
|
||||
} else {
|
||||
menu.style().max_size.height = Some(px(224.0).into());
|
||||
menu = menu.child(vertical_scroll(
|
||||
"desktop.explorer.tag_picker",
|
||||
div()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.children(tags.into_iter().map(|(tag, _, _)| {
|
||||
option(tag.clone().into(), writable).when(writable, |button| {
|
||||
button.on_click(
|
||||
controls.doc_action(selected.clone(), DocAction::Tag(tag)),
|
||||
)
|
||||
})
|
||||
})),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
menu
|
||||
}
|
||||
+58
-320
@@ -1,21 +1,29 @@
|
||||
mod menu;
|
||||
mod rows;
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use affine_store::directory::DocMode;
|
||||
use ui::{
|
||||
AnyElement, App, ClickEvent, Context, Div, Entity, FontWeight, InteractiveElement, IntoElement,
|
||||
ParentElement, Pixels, Point, RenderOnce, SharedString, Stateful, StatefulInteractiveElement,
|
||||
Styled, Window, div, prelude::FluentBuilder as _, px, svg,
|
||||
App, ClickEvent, Context, Entity, FontWeight, InteractiveElement, IntoElement, ParentElement,
|
||||
Pixels, Point, RenderOnce, StatefulInteractiveElement, Styled, Window, div,
|
||||
prelude::FluentBuilder as _, px, svg,
|
||||
};
|
||||
use ui_tokens::{FONT_SANS, icons::MORE_HORIZONTAL};
|
||||
use ui_tokens::FONT_SANS;
|
||||
|
||||
use menu::render_menu;
|
||||
use rows::{doc_row, empty};
|
||||
|
||||
use super::{
|
||||
PaneLocation, button, display_time,
|
||||
PaneLocation, button,
|
||||
doc_order::{DocOrder, order_docs},
|
||||
menu_button, selection_icon, tag_chip, update_tag, vertical_scroll,
|
||||
menu_button,
|
||||
scroll::{DocRowKey, RowStyle, virtual_scroll},
|
||||
update_tag, vertical_scroll,
|
||||
};
|
||||
use crate::{
|
||||
COLLECTION_ICON_SVG, DOC_ICON_SVG, DesktopFlexExt, DesktopPalette, EDGELESS_ICON_SVG,
|
||||
TAG_ICON_SVG, doc_store::DesktopDocMeta, split_view::DesktopRoute, workbench::DesktopShellView,
|
||||
COLLECTION_ICON_SVG, DesktopPalette, TAG_ICON_SVG, doc_store::DesktopDocMeta,
|
||||
split_view::DesktopRoute, workbench::DesktopShellView,
|
||||
};
|
||||
|
||||
pub(crate) fn render_doc_list_view(
|
||||
@@ -265,12 +273,14 @@ impl RenderOnce for DocsExplorer {
|
||||
.collect()
|
||||
};
|
||||
let docs = order_docs(store, available, state.filter, state.order, state.ascending);
|
||||
let shown: Vec<_> = docs.iter().map(|doc| doc.id.clone()).collect();
|
||||
let selected: Vec<_> = shown
|
||||
.iter()
|
||||
.filter(|id| state.selected.contains(*id))
|
||||
.cloned()
|
||||
.collect();
|
||||
let selected: Vec<_> = if state.selecting {
|
||||
docs.iter()
|
||||
.filter(|doc| state.selected.contains(&doc.id))
|
||||
.map(|doc| doc.id.clone())
|
||||
.collect()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
let writable = !store.is_readonly_sqlite();
|
||||
let mut root = div()
|
||||
.relative()
|
||||
@@ -334,6 +344,7 @@ impl RenderOnce for DocsExplorer {
|
||||
palette,
|
||||
writable,
|
||||
)
|
||||
.snapshot("desktop.explorer.select", Some("desktop.workbench"))
|
||||
.when(writable, |button| {
|
||||
button.on_click(controls.change(|state, _| {
|
||||
state.selecting = !state.selecting;
|
||||
@@ -353,7 +364,12 @@ impl RenderOnce for DocsExplorer {
|
||||
);
|
||||
}
|
||||
if state.selecting && !overview && writable {
|
||||
let all_selected = !shown.is_empty() && shown.len() == selected.len();
|
||||
let all_selected = !docs.is_empty() && docs.len() == selected.len();
|
||||
let shown = if all_selected {
|
||||
BTreeSet::new()
|
||||
} else {
|
||||
docs.iter().map(|doc| doc.id.clone()).collect()
|
||||
};
|
||||
root = root.child(
|
||||
div()
|
||||
.flex()
|
||||
@@ -368,12 +384,9 @@ impl RenderOnce for DocsExplorer {
|
||||
palette,
|
||||
true,
|
||||
)
|
||||
.snapshot("desktop.explorer.select_all", Some("desktop.workbench"))
|
||||
.on_click(controls.change(move |state, _| {
|
||||
state.selected = if all_selected {
|
||||
BTreeSet::new()
|
||||
} else {
|
||||
shown.iter().cloned().collect()
|
||||
};
|
||||
state.selected = shown.clone();
|
||||
})),
|
||||
)
|
||||
.child(
|
||||
@@ -394,6 +407,7 @@ impl RenderOnce for DocsExplorer {
|
||||
palette,
|
||||
!selected.is_empty(),
|
||||
)
|
||||
.snapshot("desktop.explorer.trash", Some("desktop.workbench"))
|
||||
.when(!selected.is_empty(), |button| {
|
||||
button.on_click(controls.doc_action(selected.clone(), DocAction::Trash))
|
||||
}),
|
||||
@@ -468,45 +482,33 @@ impl RenderOnce for DocsExplorer {
|
||||
}
|
||||
} else if docs.is_empty() {
|
||||
content = content.child(empty("No documents", palette));
|
||||
} else {
|
||||
let colors = &store.tag_colors;
|
||||
let cards = docs.iter().map(|doc| {
|
||||
doc_item(
|
||||
doc,
|
||||
colors,
|
||||
state.grid,
|
||||
(
|
||||
state.selecting && writable,
|
||||
state.selected.contains(&doc.id),
|
||||
),
|
||||
palette,
|
||||
&controls,
|
||||
)
|
||||
});
|
||||
if state.grid {
|
||||
let mut cards = cards.peekable();
|
||||
while let Some(first) = cards.next() {
|
||||
let second = cards
|
||||
.next()
|
||||
.unwrap_or_else(|| div().flex_1().into_any_element());
|
||||
content = content.child(
|
||||
div()
|
||||
.flex()
|
||||
.w_full()
|
||||
.gap(px(12.0))
|
||||
.pb(px(12.0))
|
||||
.child(first)
|
||||
.child(second),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
content = content.children(cards);
|
||||
}
|
||||
}
|
||||
let scroll_key = format!("desktop.explorer.scroll.{}", self.pane_idx);
|
||||
root = root.child(vertical_scroll(scroll_key, content));
|
||||
root = if overview || docs.is_empty() {
|
||||
root.child(vertical_scroll(scroll_key, content))
|
||||
} else {
|
||||
let render_controls = controls.clone();
|
||||
let grid = state.grid;
|
||||
let selecting = state.selecting && writable;
|
||||
let style = if grid {
|
||||
RowStyle::Grid { selecting }
|
||||
} else {
|
||||
RowStyle::List { selecting }
|
||||
};
|
||||
root.child(virtual_scroll(
|
||||
scroll_key,
|
||||
controls.location.key("desktop.explorer.scroll"),
|
||||
docs.into_iter().map(DocRowKey::explorer).collect(),
|
||||
style,
|
||||
move |keys, app| doc_row(keys, grid, writable, palette, &render_controls, app),
|
||||
))
|
||||
};
|
||||
if let Some(popup) = &state.popup {
|
||||
let tags = store.all_tags_with_counts();
|
||||
let tags = if matches!(popup.menu, Menu::Tag) {
|
||||
store.all_tags_with_counts()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
root = root.child(ui::deferred(ui::anchored().position(popup.position).child(
|
||||
render_menu(
|
||||
popup,
|
||||
@@ -521,267 +523,3 @@ impl RenderOnce for DocsExplorer {
|
||||
root.into_any_element()
|
||||
}
|
||||
}
|
||||
|
||||
fn empty(label: &'static str, palette: DesktopPalette) -> Div {
|
||||
div()
|
||||
.flex()
|
||||
.justify_center()
|
||||
.py(px(64.0))
|
||||
.text_color(palette.text_muted)
|
||||
.child(label)
|
||||
}
|
||||
|
||||
fn doc_item(
|
||||
doc: &DesktopDocMeta,
|
||||
colors: &std::collections::BTreeMap<String, u32>,
|
||||
grid: bool,
|
||||
(selecting, selected): (bool, bool),
|
||||
palette: DesktopPalette,
|
||||
controls: &Controls,
|
||||
) -> AnyElement {
|
||||
let id = doc.id.clone();
|
||||
let menu_id = id.clone();
|
||||
let check_id = id.clone();
|
||||
let aux_id = id.clone();
|
||||
let group: SharedString = format!("desktop.explorer.hover.{id}").into();
|
||||
let icon = if doc.default_mode == DocMode::Edgeless {
|
||||
EDGELESS_ICON_SVG
|
||||
} else {
|
||||
DOC_ICON_SVG
|
||||
};
|
||||
let base = div()
|
||||
.id(format!("desktop.list.doc.{id}"))
|
||||
.snapshot(
|
||||
format_args!("desktop.list.doc.{id}"),
|
||||
Some("desktop.workbench"),
|
||||
)
|
||||
.group(group.clone())
|
||||
.min_w_0()
|
||||
.rounded(px(6.0))
|
||||
.cursor_pointer()
|
||||
.hover(move |style| style.bg(palette.bg_hover))
|
||||
.bg(if selected {
|
||||
palette.bg_active
|
||||
} else {
|
||||
ui::transparent_black()
|
||||
});
|
||||
let mut base = if selecting {
|
||||
base.on_click(controls.change(move |state, _| {
|
||||
if !state.selected.remove(&check_id) {
|
||||
state.selected.insert(check_id.clone());
|
||||
}
|
||||
}))
|
||||
} else {
|
||||
base.on_click(controls.doc_action(vec![id.clone()], DocAction::Open))
|
||||
};
|
||||
base.interactivity()
|
||||
.on_aux_click(controls.open_popup(move || Menu::Row(aux_id.clone())));
|
||||
let more = div()
|
||||
.id(format!("desktop.explorer.row.menu.{id}"))
|
||||
.size(px(28.0))
|
||||
.flex_shrink(0.0)
|
||||
.flex()
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.rounded(px(6.0))
|
||||
.cursor_pointer()
|
||||
.hover(move |style| style.bg(palette.bg_pill))
|
||||
.group_hover(group, |mut style| {
|
||||
style.visibility = Some(ui::Visibility::Visible);
|
||||
style
|
||||
})
|
||||
.on_click(controls.open_popup(move || Menu::Row(menu_id.clone())))
|
||||
.child(
|
||||
svg()
|
||||
.data(MORE_HORIZONTAL.svg.as_bytes())
|
||||
.size(px(18.0))
|
||||
.text_color(palette.text_secondary),
|
||||
);
|
||||
let mut more = more;
|
||||
more.style().visibility = Some(ui::Visibility::Hidden);
|
||||
let title = if doc.title.is_empty() {
|
||||
"Untitled".to_string()
|
||||
} else {
|
||||
doc.title.clone()
|
||||
};
|
||||
let title_row = div()
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap(px(8.0))
|
||||
.min_w_0()
|
||||
.when(selecting, |row| {
|
||||
row.child(selection_icon(selected, palette))
|
||||
})
|
||||
.child(
|
||||
svg()
|
||||
.data(icon)
|
||||
.size(px(18.0))
|
||||
.flex_shrink(0.0)
|
||||
.text_color(palette.text_secondary),
|
||||
)
|
||||
.child(
|
||||
div()
|
||||
.flex_1()
|
||||
.min_w_0()
|
||||
.truncate()
|
||||
.text_size(px(14.0))
|
||||
.font_weight(FontWeight::MEDIUM)
|
||||
.child(title),
|
||||
);
|
||||
let tags = div().flex().flex_wrap().gap(px(4.0)).min_w_0().children(
|
||||
doc.tags
|
||||
.iter()
|
||||
.map(|tag| tag_chip(tag, colors.get(tag).copied(), palette)),
|
||||
);
|
||||
let timestamp = div()
|
||||
.flex_shrink(0.0)
|
||||
.text_size(px(12.0))
|
||||
.text_color(palette.text_muted)
|
||||
.child(display_time(Some(doc.updated_at_ms)));
|
||||
if grid {
|
||||
base.flex()
|
||||
.flex_1()
|
||||
.flex_col()
|
||||
.min_h(px(148.0))
|
||||
.p(px(12.0))
|
||||
.gap(px(8.0))
|
||||
.border_1()
|
||||
.border_color(palette.border)
|
||||
.child(title_row.child(more))
|
||||
.child(
|
||||
div()
|
||||
.flex_1()
|
||||
.min_h_0()
|
||||
.truncate()
|
||||
.text_size(px(13.0))
|
||||
.text_color(palette.text_muted)
|
||||
.child(doc.summary.clone()),
|
||||
)
|
||||
.child(tags)
|
||||
.child(timestamp)
|
||||
} else {
|
||||
base.flex()
|
||||
.flex_wrap()
|
||||
.items_center()
|
||||
.gap(px(8.0))
|
||||
.min_h(px(38.0))
|
||||
.px(px(8.0))
|
||||
.py(px(4.0))
|
||||
.child(title_row.flex_1().min_w(px(96.0)))
|
||||
.child(tags)
|
||||
.child(timestamp)
|
||||
.child(more)
|
||||
}
|
||||
.into_any_element()
|
||||
}
|
||||
|
||||
fn render_menu(
|
||||
popup: &Popup,
|
||||
controls: &Controls,
|
||||
palette: DesktopPalette,
|
||||
(writable, can_split): (bool, bool),
|
||||
tags: Vec<(String, u32, usize)>,
|
||||
selected: Vec<String>,
|
||||
) -> Stateful<Div> {
|
||||
let state = controls.state.clone();
|
||||
let mut menu = div()
|
||||
.id("desktop.explorer.menu")
|
||||
.snapshot("desktop.explorer.menu", Some("desktop.workbench"))
|
||||
.occlude()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.w(px(192.0))
|
||||
.p(px(4.0))
|
||||
.rounded(px(8.0))
|
||||
.border_1()
|
||||
.border_color(palette.border)
|
||||
.bg(palette.bg_elevated);
|
||||
menu.interactivity().on_mouse_down_out(move |_, _, app| {
|
||||
state.update(app, |state, cx| {
|
||||
state.popup = None;
|
||||
cx.notify();
|
||||
});
|
||||
});
|
||||
let option = |label: SharedString, enabled| {
|
||||
let mut row = button(label.clone(), label, palette, enabled);
|
||||
row.style().justify_content = Some(ui::JustifyContent::FlexStart);
|
||||
row
|
||||
};
|
||||
match &popup.menu {
|
||||
Menu::Order => {
|
||||
for (label, order) in [
|
||||
("Updated", DocOrder::Updated),
|
||||
("Created", DocOrder::Created),
|
||||
("Title", DocOrder::Title),
|
||||
] {
|
||||
for (direction, ascending) in [("ascending", true), ("descending", false)] {
|
||||
menu = menu.child(
|
||||
option(format!("{label} {direction}").into(), true).on_click(
|
||||
controls.change(move |state, _| {
|
||||
state.order = order;
|
||||
state.ascending = ascending;
|
||||
state.popup = None;
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Menu::Filter => {
|
||||
for (label, filter) in [
|
||||
("All", None),
|
||||
("Page", Some(DocMode::Page)),
|
||||
("Edgeless", Some(DocMode::Edgeless)),
|
||||
] {
|
||||
menu = menu.child(option(label.into(), true).on_click(controls.change(
|
||||
move |state, _| {
|
||||
state.filter = filter;
|
||||
state.popup = None;
|
||||
},
|
||||
)));
|
||||
}
|
||||
}
|
||||
Menu::Row(id) => {
|
||||
menu = menu
|
||||
.child(
|
||||
option("Open in center peek".into(), true)
|
||||
.on_click(controls.doc_action(vec![id.clone()], DocAction::Peek)),
|
||||
)
|
||||
.when(can_split, |menu| {
|
||||
menu.child(
|
||||
option("Open in split view".into(), true)
|
||||
.snapshot("desktop.explorer.menu.split", Some("desktop.workbench"))
|
||||
.on_click(controls.doc_action(vec![id.clone()], DocAction::Split)),
|
||||
)
|
||||
})
|
||||
.child(
|
||||
option("Move to Trash".into(), writable).when(writable, |button| {
|
||||
button
|
||||
.text_color(palette.danger)
|
||||
.on_click(controls.doc_action(vec![id.clone()], DocAction::Trash))
|
||||
}),
|
||||
);
|
||||
}
|
||||
Menu::Tag => {
|
||||
if tags.is_empty() {
|
||||
menu = menu.child(option("No workspace tags".into(), false));
|
||||
} else {
|
||||
menu.style().max_size.height = Some(px(224.0).into());
|
||||
menu = menu.child(vertical_scroll(
|
||||
"desktop.explorer.tag_picker",
|
||||
div()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.children(tags.into_iter().map(|(tag, _, _)| {
|
||||
option(tag.clone().into(), writable).when(writable, |button| {
|
||||
button.on_click(
|
||||
controls.doc_action(selected.clone(), DocAction::Tag(tag)),
|
||||
)
|
||||
})
|
||||
})),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
menu
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
use affine_store::directory::DocMode;
|
||||
use ui::{
|
||||
AnyElement, App, Div, FontWeight, InteractiveElement, IntoElement, ParentElement, SharedString,
|
||||
StatefulInteractiveElement, Styled, div, prelude::FluentBuilder as _, px, svg,
|
||||
};
|
||||
use ui_tokens::icons::MORE_HORIZONTAL;
|
||||
|
||||
use super::{Controls, DocAction, Menu};
|
||||
use crate::{
|
||||
DOC_ICON_SVG, DesktopFlexExt, DesktopPalette, EDGELESS_ICON_SVG,
|
||||
doc_store::DesktopDocMeta,
|
||||
views_extra::{
|
||||
display_time,
|
||||
scroll::{DocRowKey, doc_identity},
|
||||
selection_icon, tag_chip,
|
||||
},
|
||||
};
|
||||
|
||||
pub(super) fn doc_row(
|
||||
keys: &[DocRowKey],
|
||||
grid: bool,
|
||||
writable: bool,
|
||||
palette: DesktopPalette,
|
||||
controls: &Controls,
|
||||
app: &App,
|
||||
) -> AnyElement {
|
||||
let store = &controls.shell.read(app).state.store;
|
||||
let state = controls.state.read(app);
|
||||
let mut cards = keys.iter().filter_map(|key| {
|
||||
let doc = &store.get_doc(&key.id)?.meta;
|
||||
Some(doc_item(
|
||||
doc,
|
||||
&store.tag_colors,
|
||||
grid,
|
||||
(
|
||||
state.selecting && writable,
|
||||
state.selected.contains(&doc.id),
|
||||
),
|
||||
palette,
|
||||
controls,
|
||||
))
|
||||
});
|
||||
let Some(first) = cards.next() else {
|
||||
return div().into_any_element();
|
||||
};
|
||||
if grid {
|
||||
div()
|
||||
.flex()
|
||||
.w_full()
|
||||
.gap(px(12.0))
|
||||
.pb(px(12.0))
|
||||
.child(first)
|
||||
.child(
|
||||
cards
|
||||
.next()
|
||||
.unwrap_or_else(|| div().flex_1().into_any_element()),
|
||||
)
|
||||
.into_any_element()
|
||||
} else {
|
||||
first
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn empty(label: &'static str, palette: DesktopPalette) -> Div {
|
||||
div()
|
||||
.flex()
|
||||
.justify_center()
|
||||
.py(px(64.0))
|
||||
.text_color(palette.text_muted)
|
||||
.child(label)
|
||||
}
|
||||
|
||||
fn doc_item(
|
||||
doc: &DesktopDocMeta,
|
||||
colors: &std::collections::BTreeMap<String, u32>,
|
||||
grid: bool,
|
||||
(selecting, selected): (bool, bool),
|
||||
palette: DesktopPalette,
|
||||
controls: &Controls,
|
||||
) -> AnyElement {
|
||||
let id = doc.id.clone();
|
||||
let menu_id = id.clone();
|
||||
let check_id = id.clone();
|
||||
let aux_id = id.clone();
|
||||
let identity = doc_identity(&id);
|
||||
let group: SharedString = format!("desktop.explorer.hover.{identity}").into();
|
||||
let icon = if doc.default_mode == DocMode::Edgeless {
|
||||
EDGELESS_ICON_SVG
|
||||
} else {
|
||||
DOC_ICON_SVG
|
||||
};
|
||||
let base = div()
|
||||
.id(("DesktopExplorerRow", identity))
|
||||
.snapshot(
|
||||
ui::snap_id!("desktop.list.doc.{id}"),
|
||||
Some("desktop.workbench"),
|
||||
)
|
||||
.group(group.clone())
|
||||
.min_w_0()
|
||||
.rounded(px(6.0))
|
||||
.cursor_pointer()
|
||||
.hover(move |style| style.bg(palette.bg_hover))
|
||||
.bg(if selected {
|
||||
palette.bg_active
|
||||
} else {
|
||||
ui::transparent_black()
|
||||
});
|
||||
let mut base = if selecting {
|
||||
base.on_click(controls.change(move |state, _| {
|
||||
if !state.selected.remove(&check_id) {
|
||||
state.selected.insert(check_id.clone());
|
||||
}
|
||||
}))
|
||||
} else {
|
||||
base.on_click(controls.doc_action(vec![id.clone()], DocAction::Open))
|
||||
};
|
||||
base.interactivity()
|
||||
.on_aux_click(controls.open_popup(move || Menu::Row(aux_id.clone())));
|
||||
let more = div()
|
||||
.id(("DesktopExplorerMenu", identity))
|
||||
.size(px(28.0))
|
||||
.flex_shrink(0.0)
|
||||
.flex()
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.rounded(px(6.0))
|
||||
.cursor_pointer()
|
||||
.hover(move |style| style.bg(palette.bg_pill))
|
||||
.group_hover(group, |mut style| {
|
||||
style.visibility = Some(ui::Visibility::Visible);
|
||||
style
|
||||
})
|
||||
.on_click(controls.open_popup(move || Menu::Row(menu_id.clone())))
|
||||
.child(
|
||||
svg()
|
||||
.data(MORE_HORIZONTAL.svg.as_bytes())
|
||||
.size(px(18.0))
|
||||
.text_color(palette.text_secondary),
|
||||
);
|
||||
let mut more = more;
|
||||
more.style().visibility = Some(ui::Visibility::Hidden);
|
||||
let title = if doc.title.is_empty() {
|
||||
"Untitled".to_string()
|
||||
} else {
|
||||
doc.title.clone()
|
||||
};
|
||||
let title_row = div()
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap(px(8.0))
|
||||
.min_w_0()
|
||||
.when(selecting, |row| {
|
||||
row.child(selection_icon(selected, palette))
|
||||
})
|
||||
.child(
|
||||
svg()
|
||||
.data(icon)
|
||||
.size(px(18.0))
|
||||
.flex_shrink(0.0)
|
||||
.text_color(palette.text_secondary),
|
||||
)
|
||||
.child(
|
||||
div()
|
||||
.flex_1()
|
||||
.min_w_0()
|
||||
.truncate()
|
||||
.text_size(px(14.0))
|
||||
.font_weight(FontWeight::MEDIUM)
|
||||
.child(title),
|
||||
);
|
||||
let tags = div().flex().flex_wrap().gap(px(4.0)).min_w_0().children(
|
||||
doc.tags
|
||||
.iter()
|
||||
.map(|tag| tag_chip(tag, colors.get(tag).copied(), palette)),
|
||||
);
|
||||
let timestamp = div()
|
||||
.flex_shrink(0.0)
|
||||
.text_size(px(12.0))
|
||||
.text_color(palette.text_muted)
|
||||
.child(display_time(Some(doc.updated_at_ms)));
|
||||
if grid {
|
||||
base.flex()
|
||||
.flex_1()
|
||||
.flex_col()
|
||||
.min_h(px(148.0))
|
||||
.p(px(12.0))
|
||||
.gap(px(8.0))
|
||||
.border_1()
|
||||
.border_color(palette.border)
|
||||
.child(title_row.child(more))
|
||||
.child(
|
||||
div()
|
||||
.flex_1()
|
||||
.min_h_0()
|
||||
.truncate()
|
||||
.text_size(px(13.0))
|
||||
.text_color(palette.text_muted)
|
||||
.child(doc.summary.clone()),
|
||||
)
|
||||
.child(tags)
|
||||
.child(timestamp)
|
||||
} else {
|
||||
base.flex()
|
||||
.flex_wrap()
|
||||
.items_center()
|
||||
.gap(px(8.0))
|
||||
.min_h(px(38.0))
|
||||
.px(px(8.0))
|
||||
.py(px(4.0))
|
||||
.child(title_row.flex_1().min_w(px(96.0)))
|
||||
.child(tags)
|
||||
.child(timestamp)
|
||||
.child(more)
|
||||
}
|
||||
.into_any_element()
|
||||
}
|
||||
@@ -1,11 +1,20 @@
|
||||
use std::{cell::Cell, rc::Rc};
|
||||
use std::{
|
||||
cell::{Cell, RefCell},
|
||||
collections::{HashMap, VecDeque},
|
||||
hash::{DefaultHasher, Hash, Hasher},
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
use blocksuite_editor::scroll::{ScrollAxis, wheel_delta};
|
||||
use ui::{
|
||||
AnyElement, App, ElementId, InteractiveElement, IntoElement, ParentElement, RenderOnce, Styled,
|
||||
Window, canvas, div, px,
|
||||
AnyElement, App, AvailableSpace, ElementId, InteractiveElement, IntoElement, ParentElement,
|
||||
Pixels, RenderOnce, Styled, TextStyle, Window, canvas, div, px,
|
||||
};
|
||||
|
||||
use ui_kit::{ScrollAnchor, VirtualList, VirtualListMetrics, VirtualWindow};
|
||||
|
||||
use crate::doc_store::DesktopDocMeta;
|
||||
|
||||
pub(super) fn vertical_scroll(
|
||||
id: impl Into<ElementId>,
|
||||
content: impl IntoElement,
|
||||
@@ -81,3 +90,422 @@ impl RenderOnce for ScrollView {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn doc_identity(id: &str) -> u64 {
|
||||
let mut hasher = DefaultHasher::new();
|
||||
id.hash(&mut hasher);
|
||||
hasher.finish()
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
pub(super) struct DocRowKey {
|
||||
pub(super) id: String,
|
||||
content: u64,
|
||||
}
|
||||
|
||||
impl DocRowKey {
|
||||
pub(super) fn explorer(doc: &DesktopDocMeta) -> Self {
|
||||
Self::new(
|
||||
doc,
|
||||
(
|
||||
&doc.tags,
|
||||
&doc.summary,
|
||||
doc.default_mode == affine_store::directory::DocMode::Page,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn trash(doc: &DesktopDocMeta) -> Self {
|
||||
Self::new(doc, ())
|
||||
}
|
||||
|
||||
fn new(doc: &DesktopDocMeta, content: impl Hash) -> Self {
|
||||
let mut hasher = DefaultHasher::new();
|
||||
(&doc.title, doc.updated_at_ms, content).hash(&mut hasher);
|
||||
Self {
|
||||
id: doc.id.clone(),
|
||||
content: hasher.finish(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub(super) enum RowStyle {
|
||||
List { selecting: bool },
|
||||
Grid { selecting: bool },
|
||||
Trash,
|
||||
}
|
||||
|
||||
impl RowStyle {
|
||||
fn columns(self) -> usize {
|
||||
if matches!(self, Self::Grid { .. }) {
|
||||
2
|
||||
} else {
|
||||
1
|
||||
}
|
||||
}
|
||||
|
||||
fn min_height(self) -> f32 {
|
||||
if matches!(self, Self::Grid { .. }) {
|
||||
160.0
|
||||
} else {
|
||||
38.0
|
||||
}
|
||||
}
|
||||
|
||||
fn bottom_padding(self) -> f32 {
|
||||
if self == Self::Trash { 0.0 } else { 20.0 }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub(super) struct RowGeometry {
|
||||
pub(super) width: Pixels,
|
||||
pub(super) rem: Pixels,
|
||||
pub(super) scale: f32,
|
||||
pub(super) text: TextStyle,
|
||||
pub(super) style: RowStyle,
|
||||
}
|
||||
|
||||
pub(super) struct RowLayout {
|
||||
documents: Vec<DocRowKey>,
|
||||
geometry: Option<RowGeometry>,
|
||||
list: VirtualList,
|
||||
measured: Vec<bool>,
|
||||
first_unmeasured: usize,
|
||||
scroll: f32,
|
||||
wheel: Vec<f32>,
|
||||
pending_anchor: Option<ScrollAnchor>,
|
||||
}
|
||||
|
||||
impl Default for RowLayout {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
documents: Vec::new(),
|
||||
geometry: None,
|
||||
list: VirtualList::fixed(0, 0.0),
|
||||
measured: Vec::new(),
|
||||
first_unmeasured: 0,
|
||||
scroll: 0.0,
|
||||
wheel: Vec::new(),
|
||||
pending_anchor: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RowLayout {
|
||||
pub(super) fn configure(&mut self, documents: Vec<DocRowKey>, geometry: RowGeometry) {
|
||||
let same_geometry = self.geometry.as_ref() == Some(&geometry);
|
||||
if same_geometry && self.documents == documents {
|
||||
return;
|
||||
}
|
||||
|
||||
let columns = geometry.style.columns();
|
||||
let old_columns = self.geometry.as_ref().map_or(1, |old| old.style.columns());
|
||||
let anchor = self.list.capture_anchor(self.scroll).and_then(|anchor| {
|
||||
let positions: HashMap<_, _> = documents
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, key)| (&key.id, index / columns))
|
||||
.collect();
|
||||
let start = anchor.index * old_columns;
|
||||
let (before, from_start) = self.documents.split_at(start);
|
||||
let (row, after) = from_start.split_at(from_start.len().min(old_columns));
|
||||
let locate = |key: &DocRowKey| positions.get(&key.id).copied();
|
||||
row.iter()
|
||||
.find_map(locate)
|
||||
.map(|index| ScrollAnchor { index, ..anchor })
|
||||
.or_else(|| {
|
||||
after
|
||||
.iter()
|
||||
.chain(before.iter().rev())
|
||||
.find_map(locate)
|
||||
.map(|index| ScrollAnchor {
|
||||
index,
|
||||
offset_in_item: 0.0,
|
||||
})
|
||||
})
|
||||
});
|
||||
let estimate = geometry.style.min_height();
|
||||
let row_count = documents.len().div_ceil(columns);
|
||||
let mut measured = vec![false; row_count];
|
||||
self.list = if same_geometry {
|
||||
let heights: HashMap<_, _> = self
|
||||
.documents
|
||||
.chunks(old_columns)
|
||||
.enumerate()
|
||||
.filter(|(index, _)| self.measured[*index])
|
||||
.filter_map(|(index, row)| Some((row, self.list.item_height(index)?)))
|
||||
.collect();
|
||||
VirtualList::variable(
|
||||
documents
|
||||
.chunks(columns)
|
||||
.zip(&mut measured)
|
||||
.map(|(row, measured)| {
|
||||
let height = heights.get(row);
|
||||
*measured = height.is_some();
|
||||
height.copied().unwrap_or(estimate)
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
} else {
|
||||
VirtualList::fixed(row_count, estimate)
|
||||
};
|
||||
self.first_unmeasured = measured
|
||||
.iter()
|
||||
.position(|measured| !measured)
|
||||
.unwrap_or(row_count);
|
||||
self.measured = measured;
|
||||
self.geometry = Some(geometry);
|
||||
self.documents = documents;
|
||||
self.pending_anchor = anchor;
|
||||
if anchor.is_none() {
|
||||
self.scroll = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
fn padding(&self) -> f32 {
|
||||
self.geometry
|
||||
.as_ref()
|
||||
.map_or(0.0, |g| g.style.bottom_padding())
|
||||
}
|
||||
|
||||
fn limit(&self, viewport: f32) -> f32 {
|
||||
(self.list.total_height() + self.padding() - viewport).max(0.0)
|
||||
}
|
||||
|
||||
pub(super) fn queue_wheel(&mut self, delta: f32, viewport: f32) -> bool {
|
||||
if delta == 0.0 || !delta.is_finite() {
|
||||
return false;
|
||||
}
|
||||
if self.wheel.is_empty() {
|
||||
let at_top = delta > 0.0 && self.scroll == 0.0;
|
||||
let at_bottom = delta < 0.0
|
||||
&& self.first_unmeasured == self.list.len()
|
||||
&& self.scroll >= self.limit(viewport);
|
||||
if at_top || at_bottom {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
self.wheel.push(delta);
|
||||
true
|
||||
}
|
||||
|
||||
pub(super) fn window(&self, height: f32, overscan: f32) -> VirtualWindow {
|
||||
// Give the list only the viewport space that excludes the bottom padding.
|
||||
let scroll = self.scroll;
|
||||
let height = height.min((self.list.total_height() - scroll).max(0.0));
|
||||
self.list.window(height, scroll, overscan)
|
||||
}
|
||||
|
||||
pub(super) fn prepare_window(
|
||||
&mut self,
|
||||
viewport: f32,
|
||||
overscan: f32,
|
||||
mut measure: impl FnMut(usize, f32, &[DocRowKey]) -> f32,
|
||||
) -> VirtualWindow {
|
||||
if let Some(anchor) = self.pending_anchor.take() {
|
||||
self.measure_through(anchor.index + 1, 0.0, &mut measure);
|
||||
self.scroll = self.resolve(anchor);
|
||||
}
|
||||
|
||||
if !self.wheel.is_empty() {
|
||||
self.measure_through(0, self.scroll + viewport, &mut measure);
|
||||
self.scroll = self.scroll.min(self.limit(viewport));
|
||||
}
|
||||
let mut wheel = std::mem::take(&mut self.wheel).into_iter().peekable();
|
||||
while let Some(delta) = wheel.next() {
|
||||
let target = (self.scroll - delta).max(0.0);
|
||||
if wheel.peek().is_none() {
|
||||
self.scroll = target;
|
||||
} else {
|
||||
self.measure_through(0, target + viewport, &mut measure);
|
||||
self.scroll = target.min(self.limit(viewport));
|
||||
}
|
||||
}
|
||||
self.measure_through(0, self.scroll + viewport + overscan, &mut measure);
|
||||
self.scroll = self.scroll.min(self.limit(viewport));
|
||||
self.window(viewport, overscan)
|
||||
}
|
||||
|
||||
fn measure_through(
|
||||
&mut self,
|
||||
row_count: usize,
|
||||
bottom: f32,
|
||||
measure: &mut impl FnMut(usize, f32, &[DocRowKey]) -> f32,
|
||||
) {
|
||||
let mut index = self.first_unmeasured;
|
||||
let Some(mut top) = self.list.item_offset(index) else {
|
||||
return;
|
||||
};
|
||||
if index >= row_count && top >= bottom {
|
||||
return;
|
||||
}
|
||||
|
||||
// Complete the prefix in row order so wheel offsets use measured heights.
|
||||
let columns = self.geometry.as_ref().map_or(1, |g| g.style.columns());
|
||||
let mut updated = None;
|
||||
while index < self.measured.len() && (index < row_count || top < bottom) {
|
||||
let previous = self.list.item_height(index).unwrap_or_default();
|
||||
let height = if self.measured[index] {
|
||||
previous
|
||||
} else {
|
||||
self.measured[index] = true;
|
||||
let height = measure(index, top, self.row(index, columns));
|
||||
if height != previous {
|
||||
let heights = updated.get_or_insert_with(|| match self.list.metrics() {
|
||||
VirtualListMetrics::Fixed { count, item_height } => {
|
||||
vec![*item_height; *count]
|
||||
}
|
||||
VirtualListMetrics::Variable { heights, .. } => heights.clone(),
|
||||
});
|
||||
heights[index] = height;
|
||||
}
|
||||
height
|
||||
};
|
||||
top += height;
|
||||
index += 1;
|
||||
}
|
||||
self.first_unmeasured = self.measured[index..]
|
||||
.iter()
|
||||
.position(|measured| !measured)
|
||||
.map_or(self.measured.len(), |next| index + next);
|
||||
if let Some(heights) = updated {
|
||||
self.list = VirtualList::variable(heights);
|
||||
}
|
||||
}
|
||||
|
||||
fn row(&self, index: usize, columns: usize) -> &[DocRowKey] {
|
||||
let start = index * columns;
|
||||
&self.documents[start..(start + columns).min(self.documents.len())]
|
||||
}
|
||||
|
||||
fn resolve(&self, anchor: ScrollAnchor) -> f32 {
|
||||
let start = self.list.item_offset(anchor.index).unwrap_or_default();
|
||||
let height = self.list.item_height(anchor.index).unwrap_or_default();
|
||||
(start + anchor.offset_in_item).min((start + height).next_down().max(start))
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn virtual_scroll(
|
||||
id: impl Into<ElementId>,
|
||||
state_key: impl Into<ElementId>,
|
||||
documents: Vec<DocRowKey>,
|
||||
style: RowStyle,
|
||||
render: impl Fn(&[DocRowKey], &App) -> AnyElement + 'static,
|
||||
) -> impl IntoElement {
|
||||
ui::ViewElement::new(VirtualScroll {
|
||||
id: id.into(),
|
||||
state_key: state_key.into(),
|
||||
documents,
|
||||
style,
|
||||
render,
|
||||
})
|
||||
}
|
||||
|
||||
struct VirtualScroll<F> {
|
||||
id: ElementId,
|
||||
state_key: ElementId,
|
||||
documents: Vec<DocRowKey>,
|
||||
style: RowStyle,
|
||||
render: F,
|
||||
}
|
||||
|
||||
impl<F: Fn(&[DocRowKey], &App) -> AnyElement + 'static> RenderOnce for VirtualScroll<F> {
|
||||
fn render(self, window: &mut Window, app: &mut App) -> impl IntoElement {
|
||||
let state = window.use_keyed_state(self.state_key, app, |_, _| {
|
||||
Rc::new(RefCell::new(RowLayout::default()))
|
||||
});
|
||||
let rows = Rc::clone(state.read(app));
|
||||
let viewport = Rc::new(Cell::new(0.0_f32));
|
||||
let wheel_viewport = Rc::clone(&viewport);
|
||||
let style = self.style;
|
||||
let draw = canvas(
|
||||
move |bounds, window, app| {
|
||||
let height: f32 = bounds.size.height.into();
|
||||
viewport.set(height);
|
||||
let mut rows = rows.borrow_mut();
|
||||
rows.configure(
|
||||
self.documents,
|
||||
RowGeometry {
|
||||
width: bounds.size.width,
|
||||
rem: window.rem_size(),
|
||||
scale: window.scale_factor(),
|
||||
text: window.text_style(),
|
||||
style,
|
||||
},
|
||||
);
|
||||
let overscan = 120.0;
|
||||
let available = ui::size(bounds.size.width.into(), AvailableSpace::MaxContent);
|
||||
let layout_row = |keys: &[DocRowKey], window: &mut Window, app: &mut App| {
|
||||
let content = (self.render)(keys, app);
|
||||
let mut element = div()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.w_full()
|
||||
.child(content)
|
||||
.into_any_element();
|
||||
let height = element.layout_as_root(available, window, app).height.into();
|
||||
(element, height)
|
||||
};
|
||||
let mut measured = VecDeque::new();
|
||||
let mounted = rows.prepare_window(height, overscan, |index, top, keys| {
|
||||
let (element, row_height) = layout_row(keys, window, app);
|
||||
// Keep enough elements for the viewport if the wheel reaches the end.
|
||||
let before = top - height - 2.0 * overscan;
|
||||
while measured.front().is_some_and(|&(_, end, _)| end <= before) {
|
||||
measured.pop_front();
|
||||
}
|
||||
measured.push_back((index, top + row_height, element));
|
||||
row_height
|
||||
});
|
||||
let columns = style.columns();
|
||||
let mut elements = Vec::with_capacity(mounted.visible_range.len());
|
||||
for index in mounted.visible_range {
|
||||
while measured.front().is_some_and(|&(row, _, _)| row < index) {
|
||||
measured.pop_front();
|
||||
}
|
||||
let element = if measured.front().is_some_and(|&(row, _, _)| row == index) {
|
||||
measured.pop_front().map(|(_, _, element)| element)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
.unwrap_or_else(|| layout_row(rows.row(index, columns), window, app).0);
|
||||
let top = rows.list.item_offset(index).unwrap_or_default() - rows.scroll;
|
||||
elements.push((element, bounds.origin + ui::point(px(0.0), px(top))));
|
||||
}
|
||||
drop(rows);
|
||||
for (element, origin) in &mut elements {
|
||||
element.prepaint_at(*origin, window, app);
|
||||
}
|
||||
elements
|
||||
},
|
||||
|_, mut elements, window, app| {
|
||||
for (element, _) in &mut elements {
|
||||
element.paint(window, app);
|
||||
}
|
||||
},
|
||||
)
|
||||
.size_full();
|
||||
|
||||
div()
|
||||
.id(self.id)
|
||||
.flex()
|
||||
.flex_col()
|
||||
.flex_1()
|
||||
.min_h_0()
|
||||
.w_full()
|
||||
.overflow_hidden()
|
||||
.on_scroll(move |event, _, app| {
|
||||
state.update(app, |rows, cx| {
|
||||
let mut rows = rows.borrow_mut();
|
||||
let (_, dy) = wheel_delta(event, 0.0);
|
||||
if rows.queue_wheel(dy, wheel_viewport.get()) {
|
||||
cx.stop_propagation();
|
||||
cx.notify();
|
||||
}
|
||||
});
|
||||
})
|
||||
.child(draw)
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
use affine_store::directory::DocMode;
|
||||
use ui::{
|
||||
AnyWindowHandle, AppContext, Application, ApplicationHandle, Bounds, Context, HeadlessPlatform,
|
||||
HeadlessWindow, Keystroke, MouseButton, MouseDownEvent, MouseUpEvent, Pixels, PlatformInput,
|
||||
@@ -5,6 +6,7 @@ use ui::{
|
||||
};
|
||||
use ui_tokens::ThemeMode;
|
||||
|
||||
use super::scroll::{DocRowKey, RowGeometry, RowLayout, RowStyle};
|
||||
use crate::{DesktopAppState, DesktopRoute, DesktopShellView, register_desktop_fonts};
|
||||
|
||||
struct Shell {
|
||||
@@ -15,6 +17,13 @@ struct Shell {
|
||||
|
||||
impl Shell {
|
||||
fn new(width: u32) -> Self {
|
||||
Self::with_state(
|
||||
width,
|
||||
DesktopAppState::seed_showcase(ThemeMode::Light, "2026-12-02"),
|
||||
)
|
||||
}
|
||||
|
||||
fn with_state(width: u32, state: DesktopAppState) -> Self {
|
||||
let platform = HeadlessPlatform::new(width, 800, 1.0);
|
||||
let app = Application::with_platform(platform.clone()).run_embedded(|_| {});
|
||||
let handle = app.update(|cx| {
|
||||
@@ -27,14 +36,7 @@ impl Shell {
|
||||
))),
|
||||
..WindowOptions::default()
|
||||
},
|
||||
|_, cx| {
|
||||
cx.new(|cx| {
|
||||
DesktopShellView::new(
|
||||
DesktopAppState::seed_showcase(ThemeMode::Light, "2026-12-02"),
|
||||
cx,
|
||||
)
|
||||
})
|
||||
},
|
||||
|_, cx| cx.new(|cx| DesktopShellView::new(state, cx)),
|
||||
)
|
||||
.expect("desktop window")
|
||||
});
|
||||
@@ -60,10 +62,8 @@ impl Shell {
|
||||
}
|
||||
|
||||
fn position(&self, id: &str) -> Point<Pixels> {
|
||||
self.update(|_, window, _| {
|
||||
let b = window.frame_snapshot().elements[id].bounds;
|
||||
point(px(b.x + b.width / 2.0), px(b.y + b.height / 2.0))
|
||||
})
|
||||
let b = self.bounds(id);
|
||||
point(px(b.x + b.width / 2.0), px(b.y + b.height / 2.0))
|
||||
}
|
||||
|
||||
fn click(&self, id: &str) {
|
||||
@@ -90,6 +90,22 @@ impl Shell {
|
||||
self.window.request_frame();
|
||||
}
|
||||
|
||||
fn scroll(&self, x: f32, y: f32, delta: f32) {
|
||||
self.window
|
||||
.dispatch_input(PlatformInput::Scroll(ui::ScrollEvent {
|
||||
position: point(px(x), px(y)),
|
||||
delta: ui::ScrollDelta::Pixels(point(px(0.0), px(delta))),
|
||||
modifiers: ui::Modifiers::default(),
|
||||
touch_phase: ui::TouchPhase::Moved,
|
||||
source: ui::ScrollSource::Pointer,
|
||||
}));
|
||||
self.window.request_frame();
|
||||
}
|
||||
|
||||
fn bounds(&self, id: &str) -> ui::SnapshotRect {
|
||||
self.update(|_, window, _| window.frame_snapshot().elements[id].bounds)
|
||||
}
|
||||
|
||||
fn key(&self, key: &'static str) {
|
||||
self.app.update(|cx| {
|
||||
AnyWindowHandle::from(self.handle)
|
||||
@@ -174,15 +190,7 @@ fn explorer_overview_links_return_from_collection_and_tag_details() {
|
||||
#[test]
|
||||
fn trash_confirmation_blocks_shortcuts_and_escape_leaves_the_trash_unchanged() {
|
||||
let app = Shell::new(1280);
|
||||
app.window
|
||||
.dispatch_input(PlatformInput::Scroll(ui::ScrollEvent {
|
||||
position: point(px(120.0), px(500.0)),
|
||||
delta: ui::ScrollDelta::Pixels(point(px(0.0), px(-1000.0))),
|
||||
modifiers: ui::Modifiers::default(),
|
||||
touch_phase: ui::TouchPhase::Moved,
|
||||
source: ui::ScrollSource::Pointer,
|
||||
}));
|
||||
app.window.request_frame();
|
||||
app.scroll(120.0, 500.0, -1000.0);
|
||||
app.click("desktop.sidebar.trash");
|
||||
assert_eq!(app.route(), DesktopRoute::Trash);
|
||||
let id = app.update(|shell, _, _| shell.state.store.trashed_docs()[0].id.clone());
|
||||
@@ -222,3 +230,324 @@ fn explorer_row_menu_offers_split_only_at_or_above_640_pixels() {
|
||||
assert_eq!(app.contains("desktop.explorer.menu.split"), split);
|
||||
}
|
||||
}
|
||||
|
||||
const ROW_TIME: i64 = 1_900_000_000_000;
|
||||
|
||||
fn append_rows(state: &mut DesktopAppState, count: i64, start: i64, trashed: bool) -> Vec<String> {
|
||||
(0..count)
|
||||
.map(|i| {
|
||||
let time = start + i;
|
||||
let id = state.store.create_doc(
|
||||
format!("Document {i} with a long title for the test"),
|
||||
DocMode::Page,
|
||||
None,
|
||||
time,
|
||||
);
|
||||
if trashed {
|
||||
assert!(state.store.trash_doc(&id, time));
|
||||
}
|
||||
id
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn virtual_rows_keep_spacers_and_anchor_after_height_order_and_document_changes() {
|
||||
let mut state = DesktopAppState::seed_showcase(ThemeMode::Light, "2026-12-02");
|
||||
append_rows(&mut state, 8, ROW_TIME, false);
|
||||
let docs = state.store.visible_docs();
|
||||
let keys: Vec<_> = docs.into_iter().map(DocRowKey::explorer).collect();
|
||||
let geometry = |width| RowGeometry {
|
||||
width: px(width),
|
||||
rem: px(16.0),
|
||||
scale: 1.0,
|
||||
text: ui::TextStyle::default(),
|
||||
style: RowStyle::List { selecting: false },
|
||||
};
|
||||
let mut layout = RowLayout::default();
|
||||
let viewport = 76.0;
|
||||
let measured = |_, _, row: &[DocRowKey]| {
|
||||
if row[0].id == keys[0].id {
|
||||
58.0
|
||||
} else if row[0].id == keys[1].id {
|
||||
82.0
|
||||
} else {
|
||||
38.0
|
||||
}
|
||||
};
|
||||
layout.configure(keys.clone(), geometry(700.0));
|
||||
assert!(layout.queue_wheel(-70.0, viewport));
|
||||
let window = layout.prepare_window(viewport, 0.0, measured);
|
||||
assert_eq!(window.visible_range.start, 1);
|
||||
assert_eq!(window.leading_spacer, 58.0);
|
||||
assert_eq!(layout.window(40.0, 0.0).visible_range, 1..2);
|
||||
|
||||
let mut reordered = keys.clone();
|
||||
reordered.swap(1, 2);
|
||||
layout.configure(reordered.clone(), geometry(700.0));
|
||||
layout.prepare_window(viewport, 0.0, measured);
|
||||
assert_eq!(layout.window(40.0, 0.0).visible_range, 2..3);
|
||||
|
||||
layout.configure(reordered.clone(), geometry(320.0));
|
||||
layout.prepare_window(
|
||||
viewport,
|
||||
0.0,
|
||||
|index, _, _| {
|
||||
if index == 2 { 96.0 } else { 38.0 }
|
||||
},
|
||||
);
|
||||
assert_eq!(layout.window(80.0, 0.0).visible_range, 2..3);
|
||||
assert_eq!(layout.window(85.0, 0.0).visible_range, 2..4);
|
||||
|
||||
reordered.remove(2);
|
||||
layout.configure(reordered.clone(), geometry(320.0));
|
||||
let final_index = reordered.len() - 1;
|
||||
let after_removal = |index, _, _: &[DocRowKey]| match index {
|
||||
2 => 70.0,
|
||||
index if index == final_index => 80.0,
|
||||
_ => 38.0,
|
||||
};
|
||||
layout.prepare_window(viewport, 0.0, after_removal);
|
||||
assert_eq!(layout.window(69.0, 0.0).visible_range, 2..3);
|
||||
|
||||
assert!(layout.queue_wheel(-1_000_000.0, viewport));
|
||||
let last = layout.prepare_window(viewport, 0.0, after_removal);
|
||||
assert_eq!(last.visible_range.end, reordered.len());
|
||||
assert_eq!(last.trailing_spacer, 0.0);
|
||||
assert_eq!(last.visible_range, final_index..reordered.len());
|
||||
let mounted_height = 80.0;
|
||||
assert_eq!(
|
||||
last.leading_spacer + mounted_height + last.trailing_spacer,
|
||||
last.total_height
|
||||
);
|
||||
assert_eq!(
|
||||
layout.prepare_window(viewport, 0.0, |_, _, _| panic!("row was already measured")),
|
||||
last
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explorer_shift_click_and_row_menu_keep_the_document_identity() {
|
||||
let mut state = DesktopAppState::seed_showcase(ThemeMode::Light, "2026-12-02");
|
||||
let docs = append_rows(&mut state, 60, ROW_TIME, false);
|
||||
state.navigate_active_pane(DesktopRoute::AllDocs);
|
||||
let app = Shell::with_state(1280, state);
|
||||
let target = docs.last().unwrap();
|
||||
let row = format!("desktop.list.doc.{target}");
|
||||
let position = app.position(&row);
|
||||
let modifiers = ui::Modifiers {
|
||||
shift: true,
|
||||
..Default::default()
|
||||
};
|
||||
app.window
|
||||
.dispatch_input(PlatformInput::MouseDown(MouseDownEvent {
|
||||
button: MouseButton::Left,
|
||||
position,
|
||||
modifiers,
|
||||
click_count: 1,
|
||||
..Default::default()
|
||||
}));
|
||||
app.window
|
||||
.dispatch_input(PlatformInput::MouseUp(MouseUpEvent {
|
||||
button: MouseButton::Left,
|
||||
position,
|
||||
modifiers,
|
||||
click_count: 1,
|
||||
}));
|
||||
app.window.request_frame();
|
||||
assert!(app.contains("desktop.peek.modal"));
|
||||
app.click("desktop.peek.close");
|
||||
|
||||
app.right_click(&row);
|
||||
assert!(app.contains("desktop.explorer.menu.trash"));
|
||||
app.scroll(850.0, 600.0, -100_000.0);
|
||||
assert!(!app.contains(&row));
|
||||
assert!(app.contains("desktop.list.doc.doc-sync"));
|
||||
app.click("desktop.explorer.menu.trash");
|
||||
app.update(|shell, _, _| {
|
||||
assert!(shell.state.store.get_doc(target).unwrap().meta.trashed);
|
||||
assert!(!shell.state.store.get_doc(&docs[0]).unwrap().meta.trashed);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explorer_select_all_includes_every_document_in_the_collection() {
|
||||
let mut state = DesktopAppState::seed_showcase(ThemeMode::Light, "2026-12-02");
|
||||
let docs = append_rows(&mut state, 48, ROW_TIME, false);
|
||||
let selected: Vec<_> = docs.iter().step_by(2).cloned().collect();
|
||||
state
|
||||
.store
|
||||
.collections
|
||||
.push(crate::doc_store::DesktopCollection {
|
||||
id: "virtual-list-test".into(),
|
||||
name: "Selected test documents".into(),
|
||||
rules: vec![affine_client::domain::CollectionRule::IncludeDocIds(
|
||||
selected.clone(),
|
||||
)],
|
||||
});
|
||||
state.navigate_active_pane(DesktopRoute::Collection("virtual-list-test".into()));
|
||||
let app = Shell::with_state(1280, state);
|
||||
assert!(!app.contains(&format!("desktop.list.doc.{}", selected[0])));
|
||||
app.click("desktop.explorer.select");
|
||||
app.click("desktop.explorer.select_all");
|
||||
app.click("desktop.explorer.trash");
|
||||
app.update(|shell, _, _| {
|
||||
for (index, id) in docs.iter().enumerate() {
|
||||
assert_eq!(
|
||||
shell.state.store.get_doc(id).unwrap().meta.trashed,
|
||||
index.is_multiple_of(2)
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trash_confirmation_keeps_its_document_after_the_row_leaves_the_window() {
|
||||
let mut state = DesktopAppState::seed_showcase(ThemeMode::Light, "2026-12-02");
|
||||
let mut target = append_rows(&mut state, 1, ROW_TIME, true);
|
||||
let target = target.pop().unwrap();
|
||||
let anchor = append_rows(&mut state, 1, ROW_TIME + 100, true)
|
||||
.pop()
|
||||
.unwrap();
|
||||
state.navigate_active_pane(DesktopRoute::Trash);
|
||||
let app = Shell::with_state(1280, state);
|
||||
app.click(&format!("desktop.trash.delete.{target}"));
|
||||
assert!(app.contains("desktop.trash.confirm"));
|
||||
app.update(|shell, _, cx| {
|
||||
append_rows(&mut shell.state, 48, ROW_TIME + 10, true);
|
||||
cx.notify();
|
||||
});
|
||||
app.window.request_frame();
|
||||
assert!(!app.contains(&format!("desktop.trash.row.{target}")));
|
||||
app.click("desktop.trash.confirm.delete");
|
||||
app.update(|shell, _, _| {
|
||||
assert!(shell.state.store.get_doc(&target).is_none());
|
||||
assert!(shell.state.store.get_doc(&anchor).unwrap().meta.trashed);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trash_rows_measure_wrapped_commands_at_narrow_width() {
|
||||
let mut state = DesktopAppState::seed_showcase(ThemeMode::Light, "2026-12-02");
|
||||
let id = append_rows(&mut state, 1, ROW_TIME, true).pop().unwrap();
|
||||
let row = format!("desktop.trash.row.{id}");
|
||||
state.navigate_active_pane(DesktopRoute::Trash);
|
||||
let wide = Shell::with_state(1280, state);
|
||||
let mut state = DesktopAppState::seed_showcase(ThemeMode::Light, "2026-12-02");
|
||||
let id = append_rows(&mut state, 1, ROW_TIME, true).pop().unwrap();
|
||||
state.navigate_active_pane(DesktopRoute::Trash);
|
||||
let narrow = Shell::with_state(380, state);
|
||||
assert!(narrow.bounds(&format!("desktop.trash.row.{id}")).height > wide.bounds(&row).height);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explorer_grid_keeps_two_columns_and_reaches_an_odd_final_row() {
|
||||
let mut state = DesktopAppState::seed_showcase(ThemeMode::Light, "2026-12-02");
|
||||
let existing = state.store.visible_docs().len();
|
||||
let count = if existing.is_multiple_of(2) { 41 } else { 40 };
|
||||
let docs = append_rows(&mut state, count, ROW_TIME, false);
|
||||
let last = state.store.visible_docs().last().unwrap().id.clone();
|
||||
state.navigate_active_pane(DesktopRoute::AllDocs);
|
||||
let app = Shell::with_state(1280, state);
|
||||
app.click("desktop.explorer.grid");
|
||||
let left = app.bounds(&format!("desktop.list.doc.{}", docs[docs.len() - 1]));
|
||||
let right = app.bounds(&format!("desktop.list.doc.{}", docs[docs.len() - 2]));
|
||||
assert_eq!(left.y, right.y);
|
||||
assert_eq!(left.x + left.width + 12.0, right.x);
|
||||
app.scroll(850.0, 600.0, -100_000.0);
|
||||
assert!(!app.contains(&format!("desktop.list.doc.{}", docs[docs.len() - 1])));
|
||||
let final_row = app.bounds(&format!("desktop.list.doc.{last}"));
|
||||
assert_eq!(final_row.x, left.x);
|
||||
assert!(final_row.width < right.x + right.width - final_row.x);
|
||||
assert!(final_row.y + final_row.height < 800.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn variable_rows_keep_pixel_scroll_independent_of_wheel_step_size() {
|
||||
fn setup(style: RowStyle, width: u32) -> Shell {
|
||||
let trash = style == RowStyle::Trash;
|
||||
let mut state = DesktopAppState::seed_showcase(ThemeMode::Light, "2026-12-02");
|
||||
let ids = append_rows(&mut state, 96, ROW_TIME, trash);
|
||||
if !trash {
|
||||
for (index, id) in ids.iter().enumerate() {
|
||||
let tag_count = if matches!(style, RowStyle::Grid { .. }) {
|
||||
5 + index % 2
|
||||
} else {
|
||||
8 + index % 5
|
||||
};
|
||||
let tags = (0..tag_count)
|
||||
.map(|tag| format!("project-label-{tag}"))
|
||||
.collect();
|
||||
state.store.docs.get_mut(id).unwrap().meta.tags = tags;
|
||||
}
|
||||
state.store.touch_meta();
|
||||
}
|
||||
state.navigate_active_pane(if trash {
|
||||
DesktopRoute::Trash
|
||||
} else {
|
||||
DesktopRoute::AllDocs
|
||||
});
|
||||
let shell = Shell::with_state(width, state);
|
||||
if matches!(style, RowStyle::Grid { .. }) {
|
||||
shell.click("desktop.explorer.grid");
|
||||
}
|
||||
shell
|
||||
}
|
||||
|
||||
fn first_visible(shell: &Shell, prefix: &str) -> (String, f32, f32) {
|
||||
shell.update(|_, window, _| {
|
||||
let snapshot = window.frame_snapshot();
|
||||
let (id, row) = snapshot
|
||||
.elements
|
||||
.iter()
|
||||
.filter(|(id, row)| {
|
||||
id.starts_with(prefix)
|
||||
&& row.bounds.y + row.bounds.height > row.content_mask.y
|
||||
&& row.bounds.y < row.content_mask.y + row.content_mask.height
|
||||
})
|
||||
.min_by(|(_, a), (_, b)| {
|
||||
a.bounds
|
||||
.y
|
||||
.total_cmp(&b.bounds.y)
|
||||
.then(a.bounds.x.total_cmp(&b.bounds.x))
|
||||
})
|
||||
.expect("visible document row");
|
||||
(id.clone(), row.bounds.y, row.bounds.height)
|
||||
})
|
||||
}
|
||||
|
||||
for (style, width, prefix, minimum, distance) in [
|
||||
(RowStyle::Trash, 380, "desktop.trash.row.", 38.0, 2_100_i32),
|
||||
(
|
||||
RowStyle::Grid { selecting: false },
|
||||
380,
|
||||
"desktop.list.doc.",
|
||||
148.0,
|
||||
2_100,
|
||||
),
|
||||
(
|
||||
RowStyle::List { selecting: false },
|
||||
640,
|
||||
"desktop.list.doc.",
|
||||
38.0,
|
||||
5_000,
|
||||
),
|
||||
] {
|
||||
let single = setup(style, width);
|
||||
let stepped = setup(style, width);
|
||||
let start = first_visible(&single, prefix);
|
||||
assert!(start.2 > minimum, "{prefix}: {start:?}");
|
||||
let x = width as f32 * 0.75;
|
||||
single.scroll(x, 410.0, -distance as f32);
|
||||
for step in 0..15 {
|
||||
let delta = (step + 1) * distance / 15 - step * distance / 15;
|
||||
stepped.scroll(x, 410.0, -delta as f32);
|
||||
}
|
||||
let landing = first_visible(&single, prefix);
|
||||
assert_ne!(landing.0, start.0, "{prefix}");
|
||||
assert_eq!(landing, first_visible(&stepped, prefix), "{prefix}");
|
||||
|
||||
single.scroll(x, 410.0, distance as f32);
|
||||
assert_eq!(first_visible(&single, prefix), start, "{prefix}");
|
||||
}
|
||||
}
|
||||
@@ -8,11 +8,14 @@ use ui::{
|
||||
use ui_tokens::FONT_SANS;
|
||||
|
||||
use super::{
|
||||
PaneLocation, button, button_with_fill, display_time, max_width, selection_icon,
|
||||
vertical_scroll,
|
||||
PaneLocation, button, button_with_fill, display_time, max_width,
|
||||
scroll::{DocRowKey, RowStyle, doc_identity, virtual_scroll},
|
||||
selection_icon, vertical_scroll,
|
||||
};
|
||||
use crate::{
|
||||
DesktopPalette, TRASH_ICON_SVG, doc_store::DesktopDocStore, state::DesktopAppState,
|
||||
DesktopPalette, TRASH_ICON_SVG,
|
||||
doc_store::{DesktopDocMeta, DesktopDocStore},
|
||||
state::DesktopAppState,
|
||||
workbench::DesktopShellView,
|
||||
};
|
||||
|
||||
@@ -60,6 +63,9 @@ pub(crate) fn capture_trash_confirm_key(
|
||||
return false;
|
||||
}
|
||||
let shell_id = cx.entity_id();
|
||||
cx.global_mut::<TrashConfirmations>()
|
||||
.0
|
||||
.retain(|_, (_, selection)| selection.upgrade().is_some());
|
||||
let Some((location, owner)) = cx.global::<TrashConfirmations>().0.get(&shell_id) else {
|
||||
return false;
|
||||
};
|
||||
@@ -184,14 +190,13 @@ impl RenderOnce for TrashView {
|
||||
let docs = store.trashed_docs();
|
||||
let writable = !store.is_readonly_sqlite();
|
||||
let state = controls.selection.read(app);
|
||||
let all: Vec<_> = docs.iter().map(|doc| doc.id.clone()).collect();
|
||||
let selected: Vec<_> = all
|
||||
let selected: Vec<_> = docs
|
||||
.iter()
|
||||
.filter(|id| state.selected.contains(*id))
|
||||
.cloned()
|
||||
.filter(|doc| state.selected.contains(&doc.id))
|
||||
.map(|doc| doc.id.clone())
|
||||
.collect();
|
||||
let has_rows = !all.is_empty();
|
||||
let all_selected = has_rows && all.len() == selected.len();
|
||||
let has_rows = !docs.is_empty();
|
||||
let all_selected = has_rows && docs.len() == selected.len();
|
||||
let has_selection = !selected.is_empty();
|
||||
let mut heading = div()
|
||||
.flex()
|
||||
@@ -223,6 +228,7 @@ impl RenderOnce for TrashView {
|
||||
.snapshot("desktop.trash.empty", Some("desktop.trash.view"))
|
||||
.text_color(palette.danger);
|
||||
heading = if writable {
|
||||
let all: Vec<_> = docs.iter().map(|doc| doc.id.clone()).collect();
|
||||
heading
|
||||
.child(select_all.on_click(controls.action(
|
||||
all.clone(),
|
||||
@@ -290,89 +296,42 @@ impl RenderOnce for TrashView {
|
||||
);
|
||||
}
|
||||
|
||||
let mut rows = div().flex().flex_col().w_full();
|
||||
if !has_rows {
|
||||
rows = rows.child(
|
||||
let request = state
|
||||
.pending
|
||||
.as_ref()
|
||||
.map(|request| (request.ids.len(), request.empty_trash));
|
||||
let scroll_key = controls.location.key("desktop.trash.scroll");
|
||||
root = if has_rows {
|
||||
let render_controls = controls.clone();
|
||||
root.child(virtual_scroll(
|
||||
scroll_key,
|
||||
controls.location.key("desktop.trash.rows"),
|
||||
docs.into_iter().map(DocRowKey::trash).collect(),
|
||||
RowStyle::Trash,
|
||||
move |keys, app| {
|
||||
let store = &render_controls.shell.read(app).state.store;
|
||||
let selection = render_controls.selection.read(app);
|
||||
keys.first()
|
||||
.and_then(|key| store.get_doc(&key.id))
|
||||
.map_or_else(
|
||||
|| div().into_any_element(),
|
||||
|doc| {
|
||||
trash_row(&doc.meta, selection, palette, writable, &render_controls)
|
||||
},
|
||||
)
|
||||
},
|
||||
))
|
||||
} else {
|
||||
root.child(vertical_scroll(
|
||||
scroll_key,
|
||||
div()
|
||||
.py(px(64.0))
|
||||
.flex()
|
||||
.justify_center()
|
||||
.text_color(palette.text_muted)
|
||||
.child("Trash is empty."),
|
||||
);
|
||||
}
|
||||
for doc in docs {
|
||||
let checked = state.selected.contains(&doc.id);
|
||||
let select = button(
|
||||
format!("desktop.trash.select.{}", doc.id),
|
||||
"",
|
||||
palette,
|
||||
writable,
|
||||
)
|
||||
.child(selection_icon(checked, palette));
|
||||
let select = if writable {
|
||||
select.on_click(controls.action(vec![doc.id.clone()], |selection, _, ids| {
|
||||
if let Some(id) = ids.into_iter().next()
|
||||
&& !selection.selected.remove(&id)
|
||||
{
|
||||
selection.selected.insert(id);
|
||||
}
|
||||
}))
|
||||
} else {
|
||||
select
|
||||
};
|
||||
rows = rows.child(
|
||||
div()
|
||||
.id(format!("desktop.trash.row.{}", doc.id))
|
||||
.snapshot(
|
||||
format_args!("desktop.trash.row.{}", doc.id),
|
||||
Some("desktop.workbench"),
|
||||
)
|
||||
.flex()
|
||||
.flex_wrap()
|
||||
.items_center()
|
||||
.gap(px(8.0))
|
||||
.py(px(5.0))
|
||||
.rounded(px(6.0))
|
||||
.hover(move |style| style.bg(palette.bg_hover))
|
||||
.child(select)
|
||||
.child(
|
||||
div()
|
||||
.flex_1()
|
||||
.min_w(px(100.0))
|
||||
.truncate()
|
||||
.child(doc.title.clone()),
|
||||
)
|
||||
.child(
|
||||
div()
|
||||
.text_size(px(12.0))
|
||||
.text_color(palette.text_muted)
|
||||
.child(display_time(Some(doc.updated_at_ms))),
|
||||
)
|
||||
.child(restore_button(
|
||||
format!("desktop.trash.restore.{}", doc.id),
|
||||
vec![doc.id.clone()],
|
||||
palette,
|
||||
writable,
|
||||
&controls,
|
||||
))
|
||||
.child(delete_button(
|
||||
format!("desktop.trash.delete.{}", doc.id),
|
||||
vec![doc.id.clone()],
|
||||
palette,
|
||||
writable,
|
||||
&controls,
|
||||
)),
|
||||
);
|
||||
}
|
||||
let request = state
|
||||
.pending
|
||||
.as_ref()
|
||||
.map(|request| (request.ids.len(), request.empty_trash));
|
||||
root = root.child(vertical_scroll(
|
||||
controls.location.key("desktop.trash.scroll"),
|
||||
rows,
|
||||
));
|
||||
))
|
||||
};
|
||||
if let Some((count, empty_trash)) = request
|
||||
&& self.pane_idx == self.shell.read(app).state.split.active_pane
|
||||
&& app.has_global::<TrashConfirmations>()
|
||||
@@ -393,6 +352,72 @@ impl RenderOnce for TrashView {
|
||||
}
|
||||
}
|
||||
|
||||
fn trash_row(
|
||||
doc: &DesktopDocMeta,
|
||||
state: &TrashSelection,
|
||||
palette: DesktopPalette,
|
||||
writable: bool,
|
||||
controls: &TrashControls,
|
||||
) -> AnyElement {
|
||||
let identity = doc_identity(&doc.id);
|
||||
let checked = state.selected.contains(&doc.id);
|
||||
let select = button(("DesktopTrashSelect", identity), "", palette, writable)
|
||||
.child(selection_icon(checked, palette));
|
||||
let select = if writable {
|
||||
select.on_click(controls.action(vec![doc.id.clone()], |selection, _, ids| {
|
||||
if let Some(id) = ids.into_iter().next()
|
||||
&& !selection.selected.remove(&id)
|
||||
{
|
||||
selection.selected.insert(id);
|
||||
}
|
||||
}))
|
||||
} else {
|
||||
select
|
||||
};
|
||||
div()
|
||||
.id(("DesktopTrashRow", identity))
|
||||
.snapshot(
|
||||
ui::snap_id!("desktop.trash.row.{}", doc.id),
|
||||
Some("desktop.workbench"),
|
||||
)
|
||||
.flex()
|
||||
.flex_wrap()
|
||||
.items_center()
|
||||
.gap(px(8.0))
|
||||
.py(px(5.0))
|
||||
.rounded(px(6.0))
|
||||
.hover(move |style| style.bg(palette.bg_hover))
|
||||
.child(select)
|
||||
.child(
|
||||
div()
|
||||
.flex_1()
|
||||
.min_w(px(100.0))
|
||||
.truncate()
|
||||
.child(doc.title.clone()),
|
||||
)
|
||||
.child(
|
||||
div()
|
||||
.text_size(px(12.0))
|
||||
.text_color(palette.text_muted)
|
||||
.child(display_time(Some(doc.updated_at_ms))),
|
||||
)
|
||||
.child(restore_button(
|
||||
format!("desktop.trash.restore.{}", doc.id),
|
||||
vec![doc.id.clone()],
|
||||
palette,
|
||||
writable,
|
||||
controls,
|
||||
))
|
||||
.child(delete_button(
|
||||
format!("desktop.trash.delete.{}", doc.id),
|
||||
vec![doc.id.clone()],
|
||||
palette,
|
||||
writable,
|
||||
controls,
|
||||
))
|
||||
.into_any_element()
|
||||
}
|
||||
|
||||
fn restore_button(
|
||||
id: String,
|
||||
ids: Vec<String>,
|
||||
|
||||
Reference in new issue
Block a user