mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-05 11:15:01 +03:00
refactor(editor): store the document menu in each overlay
This commit is contained in:
1 parent
f32d18af76
commit
391ab796b1
3 files changed
+303
-168
No files matched your search
@@ -1,12 +1,6 @@
|
||||
//! Shared `Open ▾` dropdown menu, host capability registry, and viewport split probe
|
||||
//! Shared `Open ▾` dropdown menu, host capabilities, and viewport split probe
|
||||
//! for `@reference` (`link_popup`) and `EmbedLinkedDoc` / Page `EmbedSyncedDoc` (`embed_toolbar`).
|
||||
|
||||
use std::{
|
||||
cell::{Cell, RefCell},
|
||||
collections::HashMap,
|
||||
rc::{Rc, Weak},
|
||||
};
|
||||
|
||||
use ui::{
|
||||
AnyElement, ClickEvent, Context, FontWeight, MouseButton, ScrollEvent, SharedString,
|
||||
SnapshotId, canvas, div, prelude::*, px,
|
||||
@@ -28,96 +22,32 @@ pub(crate) const SPLIT_VIEW_MIN_VIEWPORT_WIDTH: f32 = 640.0;
|
||||
/// Dropdown menu width matching JS `0.27.5` `OpenDocDropdownMenu` (`264px`).
|
||||
pub(crate) const OPEN_DOC_MENU_WIDTH: f32 = 264.0;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub(crate) struct OpenDocHostCapabilities {
|
||||
pub desktop_open_menu: bool,
|
||||
pub new_tab: bool,
|
||||
pub split_view: bool,
|
||||
pub center_peek: bool,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct OpenDocMenuState {
|
||||
caps: OpenDocHostCapabilities,
|
||||
viewport_split_allowed: Option<bool>,
|
||||
embed_open_menu_block: Option<String>,
|
||||
link_open_menu_open: bool,
|
||||
link_target_mode: Option<String>,
|
||||
/// Scroll offset of the open menu rows, and its limit when the window is too short.
|
||||
menu_scroll: f32,
|
||||
menu_max_scroll: f32,
|
||||
}
|
||||
|
||||
struct OpenDocRegistryEntry {
|
||||
alive: Weak<Cell<f32>>,
|
||||
state: OpenDocMenuState,
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static OPEN_DOC_STATES: RefCell<HashMap<usize, OpenDocRegistryEntry>> =
|
||||
RefCell::new(HashMap::new());
|
||||
}
|
||||
|
||||
fn overlay_key(overlays: &Overlays) -> usize {
|
||||
Rc::as_ptr(&overlays.table_axis_scroll) as usize
|
||||
}
|
||||
|
||||
fn with_open_doc_state<R>(overlays: &Overlays, f: impl FnOnce(&mut OpenDocMenuState) -> R) -> R {
|
||||
let key = overlay_key(overlays);
|
||||
OPEN_DOC_STATES.with(|cell| {
|
||||
let mut map = cell.borrow_mut();
|
||||
if let Some(entry) = map.get_mut(&key)
|
||||
&& entry.alive.strong_count() > 0
|
||||
{
|
||||
return f(&mut entry.state);
|
||||
}
|
||||
map.retain(|_, entry| entry.alive.strong_count() > 0);
|
||||
let entry = map.entry(key).or_insert_with(|| OpenDocRegistryEntry {
|
||||
alive: Rc::downgrade(&overlays.table_axis_scroll),
|
||||
state: OpenDocMenuState::default(),
|
||||
});
|
||||
f(&mut entry.state)
|
||||
})
|
||||
}
|
||||
|
||||
fn read_open_doc_state<R>(overlays: &Overlays, f: impl FnOnce(&OpenDocMenuState) -> R) -> R {
|
||||
let key = overlay_key(overlays);
|
||||
OPEN_DOC_STATES.with(|cell| {
|
||||
let map = cell.borrow();
|
||||
if let Some(entry) = map.get(&key)
|
||||
&& entry.alive.strong_count() > 0
|
||||
{
|
||||
f(&entry.state)
|
||||
} else {
|
||||
f(&OpenDocMenuState::default())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn embed_open_menu_flag(overlays: &Overlays, block_id: &str) -> bool {
|
||||
read_open_doc_state(overlays, |s| {
|
||||
s.embed_open_menu_block.as_deref() == Some(block_id)
|
||||
})
|
||||
overlays
|
||||
.open_doc_menu
|
||||
.borrow()
|
||||
.embed_open_menu_block
|
||||
.as_deref()
|
||||
== Some(block_id)
|
||||
}
|
||||
|
||||
pub(crate) fn clear_embed_open_menu_if_not(overlays: &Overlays, active_block: Option<&str>) {
|
||||
with_open_doc_state(overlays, |s| {
|
||||
if active_block.is_none_or(|id| s.embed_open_menu_block.as_deref() != Some(id)) {
|
||||
s.embed_open_menu_block = None;
|
||||
}
|
||||
});
|
||||
let mut menu = overlays.open_doc_menu.borrow_mut();
|
||||
if active_block.is_none_or(|id| menu.embed_open_menu_block.as_deref() != Some(id)) {
|
||||
menu.embed_open_menu_block = None;
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn link_open_menu_flag(overlays: &Overlays) -> bool {
|
||||
read_open_doc_state(overlays, |s| s.link_open_menu_open)
|
||||
overlays.open_doc_menu.borrow().link_open_menu_open
|
||||
}
|
||||
|
||||
pub(crate) fn clear_link_open_menu(overlays: &Overlays) {
|
||||
with_open_doc_state(overlays, |s| s.link_open_menu_open = false);
|
||||
overlays.open_doc_menu.borrow_mut().link_open_menu_open = false;
|
||||
}
|
||||
|
||||
pub(crate) fn link_target_mode(overlays: &Overlays) -> Option<String> {
|
||||
read_open_doc_state(overlays, |s| s.link_target_mode.clone())
|
||||
overlays.open_doc_menu.borrow().link_target_mode.clone()
|
||||
}
|
||||
|
||||
pub(crate) fn reference_target_mode_from_scene(
|
||||
@@ -203,7 +133,15 @@ pub(crate) fn build_link_popup_open_dropdown<V: OverlayHost>(
|
||||
}
|
||||
|
||||
pub(crate) fn clear_link_target_mode(overlays: &Overlays) {
|
||||
with_open_doc_state(overlays, |s| s.link_target_mode = None);
|
||||
overlays.open_doc_menu.borrow_mut().link_target_mode = None;
|
||||
}
|
||||
|
||||
impl OpenDocMenuState {
|
||||
fn clamp_scroll_to_content(&mut self, content_h: f32, inner_h: f32) -> f32 {
|
||||
self.menu_max_scroll = (content_h - inner_h).max(0.0);
|
||||
self.menu_scroll = self.menu_scroll.min(self.menu_max_scroll);
|
||||
self.menu_scroll
|
||||
}
|
||||
}
|
||||
|
||||
impl Overlays {
|
||||
@@ -216,21 +154,17 @@ impl Overlays {
|
||||
center_peek: bool,
|
||||
_in_peek: bool,
|
||||
) {
|
||||
with_open_doc_state(self, |s| {
|
||||
s.caps = OpenDocHostCapabilities {
|
||||
desktop_open_menu,
|
||||
new_tab,
|
||||
split_view,
|
||||
center_peek,
|
||||
};
|
||||
});
|
||||
self.open_doc_menu.get_mut().caps = OpenDocHostCapabilities {
|
||||
desktop_open_menu,
|
||||
new_tab,
|
||||
split_view,
|
||||
center_peek,
|
||||
};
|
||||
}
|
||||
|
||||
/// Record optional target mode (`"page"` / `"edgeless"`) for the active `@reference` popup.
|
||||
pub fn set_link_popup_target_mode(&mut self, mode: Option<String>) {
|
||||
with_open_doc_state(self, |s| {
|
||||
s.link_target_mode = mode;
|
||||
});
|
||||
self.open_doc_menu.get_mut().link_target_mode = mode;
|
||||
}
|
||||
|
||||
/// Whether the `Open ▾` dropdown is currently open for embed `block_id`.
|
||||
@@ -249,10 +183,9 @@ impl Overlays {
|
||||
false
|
||||
} else {
|
||||
self.hovered_tooltip = None;
|
||||
with_open_doc_state(self, |s| {
|
||||
s.embed_open_menu_block = Some(block_id.to_owned());
|
||||
s.menu_scroll = 0.0;
|
||||
});
|
||||
let menu = self.open_doc_menu.get_mut();
|
||||
menu.embed_open_menu_block = Some(block_id.to_owned());
|
||||
menu.menu_scroll = 0.0;
|
||||
self.block_submenu = Some(BlockToolbarSubmenu::EmbedView(block_id.to_owned()));
|
||||
true
|
||||
}
|
||||
@@ -278,22 +211,25 @@ impl Overlays {
|
||||
false
|
||||
} else {
|
||||
self.hovered_tooltip = None;
|
||||
with_open_doc_state(self, |s| {
|
||||
s.link_open_menu_open = true;
|
||||
s.menu_scroll = 0.0;
|
||||
});
|
||||
let menu = self.open_doc_menu.get_mut();
|
||||
menu.link_open_menu_open = true;
|
||||
menu.menu_scroll = 0.0;
|
||||
self.block_submenu = Some(BlockToolbarSubmenu::LinkPopupView);
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/// Scroll the rows of an `Open ▾` menu that a short window clips by a wheel `delta`.
|
||||
pub fn scroll_open_doc_menu(&mut self, delta: f32) -> bool {
|
||||
with_open_doc_state(self, |s| {
|
||||
let previous = s.menu_scroll;
|
||||
s.menu_scroll = (previous - delta).clamp(0.0, s.menu_max_scroll);
|
||||
previous != s.menu_scroll
|
||||
})
|
||||
/// Return the new-tab, split-view, and center-peek options for a host viewport.
|
||||
pub fn open_doc_destinations_for_viewport(
|
||||
&self,
|
||||
is_touch: bool,
|
||||
viewport_width: f32,
|
||||
) -> (bool, bool, bool) {
|
||||
open_doc_destinations(
|
||||
is_touch,
|
||||
self.open_doc_menu.borrow().caps,
|
||||
split_view_fits(viewport_width),
|
||||
)
|
||||
}
|
||||
|
||||
/// Switch from `@reference` `Open ▾` to the inline/card/embed view dropdown.
|
||||
@@ -305,24 +241,12 @@ impl Overlays {
|
||||
}
|
||||
|
||||
pub(crate) fn viewport_split_probe(overlays: &Overlays, split_rendered: bool) -> AnyElement {
|
||||
let key = overlay_key(overlays);
|
||||
let alive = Rc::downgrade(&overlays.table_axis_scroll);
|
||||
let menu = overlays.open_doc_menu.borrow();
|
||||
let measured = menu.viewport_split_allowed.get_or_init(Rc::default).clone();
|
||||
canvas(
|
||||
move |_, window, _| {
|
||||
let allowed = f32::from(window.viewport_size().width) >= SPLIT_VIEW_MIN_VIEWPORT_WIDTH;
|
||||
let changed = OPEN_DOC_STATES.with(|cell| {
|
||||
if alive.strong_count() == 0 {
|
||||
return false;
|
||||
}
|
||||
let mut map = cell.borrow_mut();
|
||||
if let Some(entry) = map.get_mut(&key) {
|
||||
let prev = entry.state.viewport_split_allowed.replace(allowed);
|
||||
prev.is_none_or(|p| p != allowed) && allowed != split_rendered
|
||||
} else {
|
||||
false
|
||||
}
|
||||
});
|
||||
if changed {
|
||||
let allowed = split_view_fits(f32::from(window.viewport_size().width));
|
||||
if measured.replace(Some(allowed)) != Some(allowed) && allowed != split_rendered {
|
||||
window.refresh();
|
||||
}
|
||||
},
|
||||
@@ -333,14 +257,33 @@ pub(crate) fn viewport_split_probe(overlays: &Overlays, split_rendered: bool) ->
|
||||
.into_any_element()
|
||||
}
|
||||
|
||||
fn split_view_fits(viewport_width: f32) -> bool {
|
||||
viewport_width >= SPLIT_VIEW_MIN_VIEWPORT_WIDTH
|
||||
}
|
||||
|
||||
fn open_doc_destinations(
|
||||
is_touch: bool,
|
||||
caps: OpenDocHostCapabilities,
|
||||
viewport_split_allowed: bool,
|
||||
) -> (bool, bool, bool) {
|
||||
if is_touch || !caps.desktop_open_menu {
|
||||
return (false, false, false);
|
||||
}
|
||||
(
|
||||
caps.new_tab,
|
||||
caps.split_view && viewport_split_allowed,
|
||||
caps.center_peek,
|
||||
)
|
||||
}
|
||||
|
||||
/// Whether the desktop split `Open | ▾` dropdown menu is active for this scene/host.
|
||||
pub(crate) fn desktop_open_menu_active(overlays: &Overlays, scene: &OverlayScene<'_>) -> bool {
|
||||
if scene.is_touch || scene.form_factor.is_touch_form_factor() {
|
||||
return false;
|
||||
}
|
||||
read_open_doc_state(overlays, |s| {
|
||||
s.caps.desktop_open_menu && (s.caps.new_tab || s.caps.split_view || s.caps.center_peek)
|
||||
})
|
||||
let (tab, split, peek) = open_doc_destinations(
|
||||
scene.is_touch || scene.form_factor.is_touch_form_factor(),
|
||||
overlays.open_doc_menu.borrow().caps,
|
||||
true,
|
||||
);
|
||||
tab || split || peek
|
||||
}
|
||||
|
||||
/// Returns `(new_tab, split_view, center_peek)` allowed for the current host and viewport.
|
||||
@@ -348,22 +291,17 @@ pub(crate) fn desktop_open_items_available(
|
||||
overlays: &Overlays,
|
||||
scene: &OverlayScene<'_>,
|
||||
) -> (bool, bool, bool) {
|
||||
if scene.is_touch || scene.form_factor.is_touch_form_factor() {
|
||||
return (false, false, false);
|
||||
}
|
||||
read_open_doc_state(overlays, |s| {
|
||||
if !s.caps.desktop_open_menu {
|
||||
return (false, false, false);
|
||||
}
|
||||
let vp_wide = s.viewport_split_allowed.unwrap_or_else(|| {
|
||||
f32::from(scene.pane_origin.x) + scene.window.0 >= SPLIT_VIEW_MIN_VIEWPORT_WIDTH
|
||||
});
|
||||
(
|
||||
s.caps.new_tab,
|
||||
s.caps.split_view && vp_wide,
|
||||
s.caps.center_peek,
|
||||
)
|
||||
})
|
||||
let menu = overlays.open_doc_menu.borrow();
|
||||
let viewport_split_allowed = menu
|
||||
.viewport_split_allowed
|
||||
.get()
|
||||
.and_then(|measured| measured.get())
|
||||
.unwrap_or_else(|| split_view_fits(f32::from(scene.pane_origin.x) + scene.window.0));
|
||||
open_doc_destinations(
|
||||
scene.is_touch || scene.form_factor.is_touch_form_factor(),
|
||||
menu.caps,
|
||||
viewport_split_allowed,
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn is_same_open_doc(
|
||||
@@ -371,15 +309,12 @@ pub(crate) fn is_same_open_doc(
|
||||
target_doc_id: &str,
|
||||
target_mode: Option<&str>,
|
||||
) -> bool {
|
||||
if target_doc_id != scene.snapshot.doc_id {
|
||||
return false;
|
||||
}
|
||||
let current_is_edgeless = scene.screen_transform.is_some();
|
||||
match target_mode {
|
||||
Some(m) if m.eq_ignore_ascii_case("edgeless") => current_is_edgeless,
|
||||
Some(m) if m.eq_ignore_ascii_case("page") => !current_is_edgeless,
|
||||
_ => true,
|
||||
}
|
||||
is_same_open_doc_target(
|
||||
&scene.snapshot.doc_id,
|
||||
scene.screen_transform.is_some(),
|
||||
target_doc_id,
|
||||
target_mode,
|
||||
)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
@@ -630,10 +565,7 @@ fn prepare_open_doc_menu(
|
||||
}
|
||||
// A window too short for every row clips the menu to its edge margins and scrolls the rows.
|
||||
let vp = scene.popover_viewport();
|
||||
let chrome_h = 2.0 * (TOOLBAR_MENU_PADDING + TOOLBAR_CHROME_BORDER);
|
||||
let menu_h = (chrome_h + rows.len() as f32 * TOOLBAR_MENU_ROW_H)
|
||||
.min((vp.h - 2.0 * POPOVER_EDGE_MARGIN).max(chrome_h + TOOLBAR_MENU_ROW_H))
|
||||
.round();
|
||||
let menu_h = open_doc_menu_height(rows.len(), vp.h);
|
||||
let anchor = overlays.toolbar_bounds.anchor(
|
||||
bar_snap,
|
||||
button_snap,
|
||||
@@ -652,6 +584,13 @@ fn prepare_open_doc_menu(
|
||||
(placed.rect, rows)
|
||||
}
|
||||
|
||||
fn open_doc_menu_height(row_count: usize, viewport_height: f32) -> f32 {
|
||||
let chrome_h = 2.0 * (TOOLBAR_MENU_PADDING + TOOLBAR_CHROME_BORDER);
|
||||
(chrome_h + row_count as f32 * TOOLBAR_MENU_ROW_H)
|
||||
.min((viewport_height - 2.0 * POPOVER_EDGE_MARGIN).max(chrome_h + TOOLBAR_MENU_ROW_H))
|
||||
.round()
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn open_menu_row_inner<V: OverlayHost>(
|
||||
id: SharedString,
|
||||
@@ -749,11 +688,10 @@ pub(crate) fn render_open_doc_dropdown<V: OverlayHost>(
|
||||
);
|
||||
let content_h = rows.len() as f32 * TOOLBAR_MENU_ROW_H;
|
||||
let inner_h = placed_rect.h - 2.0 * (TOOLBAR_MENU_PADDING + TOOLBAR_CHROME_BORDER);
|
||||
let scroll = with_open_doc_state(overlays, |s| {
|
||||
s.menu_max_scroll = (content_h - inner_h).max(0.0);
|
||||
s.menu_scroll = s.menu_scroll.min(s.menu_max_scroll);
|
||||
s.menu_scroll
|
||||
});
|
||||
let scroll = overlays
|
||||
.open_doc_menu
|
||||
.borrow_mut()
|
||||
.clamp_scroll_to_content(content_h, inner_h);
|
||||
let mut panel = toolbar_dropdown_panel(
|
||||
config.panel_id,
|
||||
placed_rect,
|
||||
@@ -798,3 +736,152 @@ pub(crate) fn render_open_doc_dropdown<V: OverlayHost>(
|
||||
};
|
||||
(placed_rect, panel)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn open_doc_menu_state_is_local_to_each_overlay() {
|
||||
let mut first = Overlays::new();
|
||||
let mut second = Overlays::new();
|
||||
first.set_open_doc_capabilities(true, true, false, false, false);
|
||||
second.set_open_doc_capabilities(true, false, true, true, false);
|
||||
assert!(first.toggle_embed_open_menu("first"));
|
||||
assert!(second.toggle_link_open_menu());
|
||||
second.set_link_popup_target_mode(Some("edgeless".into()));
|
||||
|
||||
let first_probe = {
|
||||
let menu = first.open_doc_menu.borrow();
|
||||
let probe = menu.viewport_split_allowed.get_or_init(Rc::default);
|
||||
probe.set(Some(true));
|
||||
Rc::downgrade(probe)
|
||||
};
|
||||
let second_probe = {
|
||||
let menu = second.open_doc_menu.borrow();
|
||||
let probe = menu.viewport_split_allowed.get_or_init(Rc::default);
|
||||
probe.set(Some(false));
|
||||
Rc::downgrade(probe)
|
||||
};
|
||||
first
|
||||
.open_doc_menu
|
||||
.get_mut()
|
||||
.clamp_scroll_to_content(90.0, 60.0);
|
||||
assert!(first.scroll_open_doc_menu(-24.0));
|
||||
|
||||
assert_eq!(
|
||||
first.open_doc_destinations_for_viewport(false, 640.0),
|
||||
(true, false, false)
|
||||
);
|
||||
assert_eq!(
|
||||
second.open_doc_destinations_for_viewport(false, 640.0),
|
||||
(false, true, true)
|
||||
);
|
||||
assert_eq!(
|
||||
first_probe.upgrade().and_then(|probe| probe.get()),
|
||||
Some(true)
|
||||
);
|
||||
assert_eq!(
|
||||
second_probe.upgrade().and_then(|probe| probe.get()),
|
||||
Some(false)
|
||||
);
|
||||
assert!(first.is_embed_open_menu_open("first"));
|
||||
assert!(!second.is_embed_open_menu_open("first"));
|
||||
assert!(!first.is_link_open_menu_open());
|
||||
assert!(second.is_link_open_menu_open());
|
||||
assert_eq!(link_target_mode(&first), None);
|
||||
assert_eq!(link_target_mode(&second).as_deref(), Some("edgeless"));
|
||||
assert_eq!(first.open_doc_menu.borrow().menu_scroll, 24.0);
|
||||
assert_eq!(second.open_doc_menu.borrow().menu_scroll, 0.0);
|
||||
|
||||
drop(first);
|
||||
assert!(first_probe.upgrade().is_none());
|
||||
assert!(second.is_link_open_menu_open());
|
||||
second.reset();
|
||||
assert!(!second.is_link_open_menu_open());
|
||||
assert_eq!(
|
||||
second.open_doc_destinations_for_viewport(false, 640.0),
|
||||
(false, true, true)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn open_doc_destinations_follow_viewport_touch_and_host_capabilities() {
|
||||
let mut overlays = Overlays::new();
|
||||
for (desktop, new_tab, split, peek, touch, width, expected) in [
|
||||
(true, true, true, true, false, 639.0, (true, false, true)),
|
||||
(true, true, true, true, false, 640.0, (true, true, true)),
|
||||
(true, true, true, true, true, 640.0, (false, false, false)),
|
||||
(false, true, true, true, false, 640.0, (false, false, false)),
|
||||
(true, true, false, false, false, 640.0, (true, false, false)),
|
||||
(true, false, true, false, false, 640.0, (false, true, false)),
|
||||
(true, false, false, true, false, 640.0, (false, false, true)),
|
||||
] {
|
||||
overlays.set_open_doc_capabilities(desktop, new_tab, split, peek, false);
|
||||
assert_eq!(
|
||||
overlays.open_doc_destinations_for_viewport(touch, width),
|
||||
expected
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn same_open_doc_requires_matching_document_and_view() {
|
||||
for (target, edgeless, mode, expected) in [
|
||||
("other", false, Some("page"), false),
|
||||
("doc", false, Some("page"), true),
|
||||
("doc", false, Some("edgeless"), false),
|
||||
("doc", true, Some("page"), false),
|
||||
("doc", true, Some("EDGELESS"), true),
|
||||
("doc", true, None, true),
|
||||
("doc", false, Some("unknown"), true),
|
||||
] {
|
||||
assert_eq!(
|
||||
is_same_open_doc_target("doc", edgeless, target, mode),
|
||||
expected
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn short_open_doc_menu_keeps_one_row_and_clamps_scroll() {
|
||||
let chrome_h = 2.0 * (TOOLBAR_MENU_PADDING + TOOLBAR_CHROME_BORDER);
|
||||
let row_h = TOOLBAR_MENU_ROW_H;
|
||||
let content_h = 4.0 * row_h;
|
||||
let full_h = (chrome_h + content_h).round();
|
||||
let short_h = (chrome_h + 2.0 * row_h).round();
|
||||
let mut overlays = Overlays::new();
|
||||
|
||||
assert_eq!(
|
||||
open_doc_menu_height(4, full_h + 2.0 * POPOVER_EDGE_MARGIN),
|
||||
full_h
|
||||
);
|
||||
assert_eq!(
|
||||
open_doc_menu_height(4, short_h + 2.0 * POPOVER_EDGE_MARGIN),
|
||||
short_h
|
||||
);
|
||||
assert_eq!(open_doc_menu_height(4, 0.0), (chrome_h + row_h).round());
|
||||
|
||||
let short_inner_h = short_h - chrome_h;
|
||||
let max_scroll = content_h - short_inner_h;
|
||||
overlays
|
||||
.open_doc_menu
|
||||
.get_mut()
|
||||
.clamp_scroll_to_content(content_h, short_inner_h);
|
||||
assert!(overlays.scroll_open_doc_menu(-content_h));
|
||||
assert_eq!(overlays.open_doc_menu.borrow().menu_scroll, max_scroll);
|
||||
assert!(!overlays.scroll_open_doc_menu(-1.0));
|
||||
assert!(overlays.scroll_open_doc_menu(content_h));
|
||||
assert_eq!(overlays.open_doc_menu.borrow().menu_scroll, 0.0);
|
||||
assert!(!overlays.scroll_open_doc_menu(1.0));
|
||||
assert!(overlays.scroll_open_doc_menu(-1.0));
|
||||
assert_eq!(
|
||||
overlays
|
||||
.open_doc_menu
|
||||
.get_mut()
|
||||
.clamp_scroll_to_content(content_h, full_h - chrome_h),
|
||||
0.0
|
||||
);
|
||||
assert!(!overlays.scroll_open_doc_menu(-1.0));
|
||||
}
|
||||
}
|
||||
@@ -139,7 +139,11 @@ pub(crate) use paint_page::align_glyph;
|
||||
use paint_page::{caret_world_rect, emit, hover_block, icon, icon_rotated, place, text_len};
|
||||
use toolbar::{FormatToolbar, utf16_to_byte};
|
||||
|
||||
use std::collections::{BTreeSet, HashMap};
|
||||
use std::{
|
||||
cell::{Cell, OnceCell, RefCell},
|
||||
collections::{BTreeSet, HashMap},
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
use blocksuite_playground_doc::read::{BlockContent, BlockNode, PageSnapshot};
|
||||
use ui::{Context, Motion, MotionCurve, Pixels, Point, Window};
|
||||
@@ -389,11 +393,47 @@ impl OverlayScene<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
struct OpenDocHostCapabilities {
|
||||
desktop_open_menu: bool,
|
||||
new_tab: bool,
|
||||
split_view: bool,
|
||||
center_peek: bool,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct OpenDocMenuState {
|
||||
caps: OpenDocHostCapabilities,
|
||||
// The canvas callback shares only the viewport measurement.
|
||||
viewport_split_allowed: OnceCell<Rc<Cell<Option<bool>>>>,
|
||||
embed_open_menu_block: Option<String>,
|
||||
link_open_menu_open: bool,
|
||||
link_target_mode: Option<String>,
|
||||
menu_scroll: f32,
|
||||
menu_max_scroll: f32,
|
||||
}
|
||||
|
||||
/// Check if the target points to the open document in its current view.
|
||||
pub fn is_same_open_doc_target(
|
||||
current_doc_id: &str,
|
||||
current_is_edgeless: bool,
|
||||
target_doc_id: &str,
|
||||
target_mode: Option<&str>,
|
||||
) -> bool {
|
||||
current_doc_id == target_doc_id
|
||||
&& match target_mode {
|
||||
Some(mode) if mode.eq_ignore_ascii_case("edgeless") => current_is_edgeless,
|
||||
Some(mode) if mode.eq_ignore_ascii_case("page") => !current_is_edgeless,
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Overlay and block-control state of the Page view.
|
||||
#[derive(Default)]
|
||||
pub struct Overlays {
|
||||
reset_generation: u64,
|
||||
toolbar_bounds: ToolbarBounds,
|
||||
open_doc_menu: RefCell<OpenDocMenuState>,
|
||||
pub table_axis_scroll: std::rc::Rc<std::cell::Cell<f32>>,
|
||||
pub(super) hovered: Option<String>,
|
||||
/// Wide Table/Database block whose body is under the pointer (used to reveal its horizontal
|
||||
|
||||
@@ -6,6 +6,14 @@ use crate::block_flow::FlowRect;
|
||||
use super::*;
|
||||
|
||||
impl Overlays {
|
||||
/// Scroll the rows of an `Open ▾` menu that a short window clips by a wheel `delta`.
|
||||
pub fn scroll_open_doc_menu(&mut self, delta: f32) -> bool {
|
||||
let menu = self.open_doc_menu.get_mut();
|
||||
let previous = menu.menu_scroll;
|
||||
menu.menu_scroll = (previous - delta).clamp(0.0, menu.menu_max_scroll);
|
||||
previous != menu.menu_scroll
|
||||
}
|
||||
|
||||
pub(super) fn open_slash_mut(&mut self) -> Option<&mut SlashMenu> {
|
||||
self.slash.as_mut().filter(|menu| !menu.presence.closing)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user