mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-05 11:15:01 +03:00
fix(desktop): cancel tab drops outside the window
Clear the drop target when the pointer leaves the window. Keep sticky tab targets inside the window. Use the trailing spacer to move a tab to the end of its section. Keep the spacer indicator inside the viewport.
This commit is contained in:
1 parent
c7ba186a91
commit
1916183e6c
2 files changed
+275
-22
No files matched your search
@@ -19,11 +19,25 @@ const DROP_INDICATOR_WIDTH: f32 = 2.0;
|
||||
#[derive(Clone, Copy, Default)]
|
||||
pub(super) struct StripBounds {
|
||||
pub(super) origin_x: f32,
|
||||
pub(super) width: f32,
|
||||
pub(super) window_width: f32,
|
||||
pub(super) window_height: f32,
|
||||
pub(super) top: f32,
|
||||
pub(super) bottom: f32,
|
||||
}
|
||||
|
||||
impl StripBounds {
|
||||
pub(super) fn window_contains(self, x: f32, y: f32) -> bool {
|
||||
self.window_width > 0.0
|
||||
&& self.window_height > 0.0
|
||||
&& (0.0..=self.window_width).contains(&x)
|
||||
&& (0.0..=self.window_height).contains(&y)
|
||||
}
|
||||
|
||||
pub(super) fn viewport_width(self) -> f32 {
|
||||
(self.width - 2.0 * TAB_RING).max(0.0)
|
||||
}
|
||||
|
||||
pub(super) fn contains_y(self, y: f32) -> bool {
|
||||
self.bottom > self.top && (self.top..=self.bottom).contains(&y)
|
||||
}
|
||||
@@ -145,6 +159,7 @@ pub(super) fn render_drop_indicator(
|
||||
drag: &TabDrag,
|
||||
tabs: &[TabItem],
|
||||
scroll_x: f32,
|
||||
viewport: f32,
|
||||
palette: DesktopPalette,
|
||||
) -> Option<AnyElement> {
|
||||
if !drag.is_active() {
|
||||
@@ -153,6 +168,11 @@ pub(super) fn render_drop_indicator(
|
||||
let insert_at = drag.drop_index?;
|
||||
let from_pinned = tabs.iter().find(|tab| tab.id == drag.tab_id)?.pinned;
|
||||
let left = drop_indicator_x(layout, from_pinned, insert_at) - scroll_x;
|
||||
let left = if drag.in_spacer {
|
||||
left.min((TAB_RING + viewport - DROP_INDICATOR_WIDTH).max(0.0))
|
||||
} else {
|
||||
left
|
||||
};
|
||||
Some(
|
||||
div()
|
||||
.id("desktop.app_tabs.drop_indicator")
|
||||
@@ -230,9 +250,13 @@ fn next_drop_target(
|
||||
current_drop: Option<usize>,
|
||||
was_in_spacer: bool,
|
||||
) -> Option<(Option<usize>, bool)> {
|
||||
if !bounds.window_contains(pointer_x, pointer_y) {
|
||||
return Some((None, false));
|
||||
}
|
||||
let pane = &split.panes[0];
|
||||
let from_index = pane.tab_index(tab_id)?;
|
||||
if !bounds.contains_y(pointer_y) {
|
||||
let viewport_x = pointer_x - bounds.origin_x - TAB_RING;
|
||||
if !bounds.contains_y(pointer_y) || viewport_x < 0.0 {
|
||||
return Some(if was_in_spacer {
|
||||
(None, false)
|
||||
} else {
|
||||
@@ -240,15 +264,14 @@ fn next_drop_target(
|
||||
});
|
||||
}
|
||||
let layout = StripLayout::new(split);
|
||||
let content_x = pointer_x - bounds.origin_x - TAB_RING + scroll_x;
|
||||
if content_x < 0.0 {
|
||||
return Some(if was_in_spacer {
|
||||
(None, false)
|
||||
} else {
|
||||
(current_drop, false)
|
||||
});
|
||||
}
|
||||
let in_spacer = content_x > layout.content_width();
|
||||
let content_width = layout.content_width();
|
||||
let content_x = viewport_x + scroll_x;
|
||||
let in_spacer = viewport_x > bounds.viewport_width() || content_x > content_width;
|
||||
let content_x = if in_spacer {
|
||||
content_width + 1.0
|
||||
} else {
|
||||
content_x
|
||||
};
|
||||
let drop_index = drop_insertion_index(&layout, &pane.tabs, from_index, content_x);
|
||||
Some((drop_index, in_spacer))
|
||||
}
|
||||
@@ -336,7 +359,8 @@ pub(super) fn register_window_drag_listeners(
|
||||
};
|
||||
move_state.update(app, |stored, cx| {
|
||||
if let Some(drag) = &mut stored.drag {
|
||||
let changed = !drag.active || drag.drop_index != drop_index;
|
||||
let changed =
|
||||
!drag.active || drag.drop_index != drop_index || drag.in_spacer != in_spacer;
|
||||
if !drag.active {
|
||||
drag.previous_focus = previous_focus;
|
||||
}
|
||||
@@ -373,10 +397,10 @@ pub(super) fn register_window_drag_listeners(
|
||||
}
|
||||
let previous_focus = drag.previous_focus.take();
|
||||
restore_focus(previous_focus, &drag_focus, &shell_entity, window, app);
|
||||
if drag.active && !drag.canceled {
|
||||
let pointer_x = f32::from(event.position.x);
|
||||
let pointer_y = f32::from(event.position.y);
|
||||
let bounds = strip_bounds.get();
|
||||
let pointer_x = f32::from(event.position.x);
|
||||
let pointer_y = f32::from(event.position.y);
|
||||
let bounds = strip_bounds.get();
|
||||
if drag.active && !drag.canceled && bounds.window_contains(pointer_x, pointer_y) {
|
||||
shell_entity.update(app, |shell, cx| {
|
||||
if let Some((Some(insert_at), _)) = next_drop_target(
|
||||
&shell.state.split,
|
||||
|
||||
@@ -512,10 +512,9 @@ fn render_tab_labels(
|
||||
.as_ref()
|
||||
.filter(|drag| drag.is_active())
|
||||
.map(|drag| drag.tab_id);
|
||||
let drop_indicator = stored
|
||||
.drag
|
||||
.as_ref()
|
||||
.and_then(|drag| drag::render_drop_indicator(&layout, drag, &pane.tabs, scroll_x, palette));
|
||||
let drop_indicator = stored.drag.as_ref().and_then(|drag| {
|
||||
drag::render_drop_indicator(&layout, drag, &pane.tabs, scroll_x, viewport, palette)
|
||||
});
|
||||
let labels = render_tab_range(
|
||||
shell,
|
||||
&layout,
|
||||
@@ -564,12 +563,17 @@ fn render_tab_labels(
|
||||
move |bounds, window, app| {
|
||||
let strip_top = f32::from(bounds.origin.y)
|
||||
- (APP_TABS_HEIGHT - (TAB_HEIGHT + 2.0 * TAB_RING)) * 0.5;
|
||||
strip_bounds.set(drag::StripBounds {
|
||||
let window_size = window.viewport_size();
|
||||
let bounds = drag::StripBounds {
|
||||
origin_x: f32::from(bounds.origin.x),
|
||||
width: f32::from(bounds.size.width),
|
||||
window_width: f32::from(window_size.width),
|
||||
window_height: f32::from(window_size.height),
|
||||
top: strip_top,
|
||||
bottom: strip_top + APP_TABS_HEIGHT,
|
||||
});
|
||||
let width = f32::from(bounds.size.width) - 2.0 * TAB_RING;
|
||||
};
|
||||
strip_bounds.set(bounds);
|
||||
let width = bounds.viewport_width();
|
||||
if measure_state.read(app).measured != Some((content, width)) {
|
||||
window.defer(app, move |_, app| {
|
||||
measure_state.update(app, |stored, cx| {
|
||||
@@ -2096,4 +2100,229 @@ mod tests {
|
||||
shell.update(|view, _, _| SavedTabs::json(&view.state.split))
|
||||
);
|
||||
}
|
||||
|
||||
fn saved_tab_shell(
|
||||
count: usize,
|
||||
) -> anyhow::Result<(Shell, std::rc::Rc<MemoryTabStore>, String)> {
|
||||
use crate::split_view::DesktopRoute;
|
||||
|
||||
let store = std::rc::Rc::new(MemoryTabStore(Default::default()));
|
||||
let (mut state, sessions) = DesktopAppState::open_local(
|
||||
&Default::default(),
|
||||
ThemeMode::Light,
|
||||
"2026-12-02",
|
||||
1_000,
|
||||
)?;
|
||||
let ws_id = state.store.active_workspace().id.clone();
|
||||
state.navigate_active_pane(DesktopRoute::Doc("doc-arch".to_owned()));
|
||||
for index in 1..count {
|
||||
state.open_app_tab(DocViewMode::Page);
|
||||
if index == 3 {
|
||||
state.navigate_active_pane(DesktopRoute::Doc("doc-sync".to_owned()));
|
||||
}
|
||||
}
|
||||
state.split.panes[0].select_tab(0);
|
||||
state.split.sync_app_tab();
|
||||
let shell = Shell::with_state(state);
|
||||
shell.update(|view, _, cx| {
|
||||
view.bind_local_workspaces(sessions, store.clone(), 1_000, cx);
|
||||
});
|
||||
shell.frame();
|
||||
Ok((shell, store, ws_id))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dragging_left_of_scrolled_tab_viewport_keeps_only_sticky_targets() -> anyhow::Result<()> {
|
||||
use anyhow::Context as _;
|
||||
|
||||
use crate::workbench::LocalDocStore;
|
||||
|
||||
let (shell, store, ws_id) = saved_tab_shell(12)?;
|
||||
let ids_before = shell.tab_ids();
|
||||
let saved_before = Some(store.tabs(&ws_id).context("initial saved tabs")?);
|
||||
let first = shell
|
||||
.bounds("desktop.app_tabs.tab.0")
|
||||
.context("first tab")?;
|
||||
shell.wheel(-300.0, 0.0);
|
||||
let scrolled_first = shell
|
||||
.bounds("desktop.app_tabs.tab.0")
|
||||
.context("overscan tab")?;
|
||||
assert_eq!(scrolled_first.x, first.x - 300.0);
|
||||
|
||||
let back = shell
|
||||
.bounds("desktop.topbar.nav_back")
|
||||
.context("back button")?;
|
||||
let nav_x = back.x + back.width / 2.0;
|
||||
let target = shell
|
||||
.bounds("desktop.app_tabs.tab.4")
|
||||
.context("visible tab")?;
|
||||
let nav = point(px(nav_x), px(target.y + target.height / 2.0));
|
||||
assert!(nav_x < first.x - 20.0);
|
||||
|
||||
for target_x in [
|
||||
nav_x,
|
||||
shell.bounds("desktop.app_tabs.new").context("new tab")?.x + 8.0,
|
||||
] {
|
||||
drag_tab_step(&shell, "desktop.app_tabs.tab.3", target_x);
|
||||
shell.pointer(nav, Some(MouseButton::Left));
|
||||
assert!(!shell.has("desktop.app_tabs.drop_indicator"));
|
||||
shell.button(nav, false);
|
||||
assert_eq!(shell.tab_ids(), ids_before);
|
||||
assert_eq!(store.tabs(&ws_id), saved_before);
|
||||
}
|
||||
|
||||
drag_tab_step(
|
||||
&shell,
|
||||
"desktop.app_tabs.tab.3",
|
||||
target.x + target.width * 0.75,
|
||||
);
|
||||
let before = shell
|
||||
.bounds("desktop.app_tabs.drop_indicator")
|
||||
.context("sticky target")?;
|
||||
shell.pointer(nav, Some(MouseButton::Left));
|
||||
assert_eq!(
|
||||
shell
|
||||
.bounds("desktop.app_tabs.drop_indicator")
|
||||
.map(|bounds| bounds.x),
|
||||
Some(before.x)
|
||||
);
|
||||
shell.button(nav, false);
|
||||
let mut expected = ids_before;
|
||||
expected.swap(3, 4);
|
||||
assert_eq!(shell.tab_ids(), expected);
|
||||
let saved_after = store.tabs(&ws_id);
|
||||
assert_ne!(saved_after, saved_before);
|
||||
assert_eq!(
|
||||
saved_after,
|
||||
shell.update(|view, _, _| SavedTabs::json(&view.state.split))
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sticky_tab_drop_survives_in_window_body_but_clears_outside_window() -> anyhow::Result<()> {
|
||||
use anyhow::Context as _;
|
||||
|
||||
use crate::workbench::LocalDocStore;
|
||||
|
||||
let (shell, store, ws_id) = saved_tab_shell(12)?;
|
||||
let ids_before = shell.tab_ids();
|
||||
let saved_before = Some(store.tabs(&ws_id).context("initial saved tabs")?);
|
||||
let target = shell
|
||||
.bounds("desktop.app_tabs.tab.4")
|
||||
.context("fifth tab")?;
|
||||
let target_x = px(target.x + target.width * 0.75);
|
||||
let body = point(target_x, px(150.0));
|
||||
let viewport = shell.update(|_, window, _| window.viewport_size());
|
||||
let outside_right = point(viewport.width + px(80.0), body.y);
|
||||
let drag_into_body = || -> anyhow::Result<()> {
|
||||
drag_tab_step(&shell, "desktop.app_tabs.tab.3", f32::from(target_x));
|
||||
let indicator = shell
|
||||
.bounds("desktop.app_tabs.drop_indicator")
|
||||
.context("sticky target")?;
|
||||
shell.pointer(body, Some(MouseButton::Left));
|
||||
assert_eq!(
|
||||
shell
|
||||
.bounds("desktop.app_tabs.drop_indicator")
|
||||
.map(|bounds| bounds.x),
|
||||
Some(indicator.x)
|
||||
);
|
||||
Ok(())
|
||||
};
|
||||
|
||||
for outside in [
|
||||
outside_right,
|
||||
point(px(-40.0), body.y),
|
||||
point(px(300.0), px(-20.0)),
|
||||
point(px(300.0), viewport.height + px(40.0)),
|
||||
] {
|
||||
drag_into_body()?;
|
||||
shell.pointer(outside, Some(MouseButton::Left));
|
||||
assert!(!shell.has("desktop.app_tabs.drop_indicator"));
|
||||
shell.button(outside, false);
|
||||
assert_eq!(shell.tab_ids(), ids_before);
|
||||
assert_eq!(store.tabs(&ws_id), saved_before);
|
||||
}
|
||||
|
||||
drag_into_body()?;
|
||||
shell.button(outside_right, false);
|
||||
assert!(!shell.has("desktop.app_tabs.drop_indicator"));
|
||||
assert_eq!(shell.tab_ids(), ids_before);
|
||||
assert_eq!(store.tabs(&ws_id), saved_before);
|
||||
|
||||
drag_into_body()?;
|
||||
shell.button(body, false);
|
||||
let mut expected = ids_before;
|
||||
expected.swap(3, 4);
|
||||
assert_eq!(shell.tab_ids(), expected);
|
||||
let saved_after = store.tabs(&ws_id);
|
||||
assert_ne!(saved_after, saved_before);
|
||||
assert_eq!(
|
||||
saved_after,
|
||||
shell.update(|view, _, _| SavedTabs::json(&view.state.split))
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dragging_to_new_tab_button_moves_to_end_unless_pointer_leaves_strip() -> anyhow::Result<()> {
|
||||
use anyhow::Context as _;
|
||||
|
||||
use crate::workbench::LocalDocStore;
|
||||
|
||||
for (count, source, overflow) in [(3, 0, false), (12, 3, true)] {
|
||||
let (shell, store, ws_id) = saved_tab_shell(count)?;
|
||||
let ids_before = shell.tab_ids();
|
||||
let saved_before = Some(store.tabs(&ws_id).context("initial saved tabs")?);
|
||||
let first = shell
|
||||
.bounds("desktop.app_tabs.tab.0")
|
||||
.context("first tab")?;
|
||||
let content_width =
|
||||
shell.update(|view, _, _| StripLayout::new(&view.state.split).content_width());
|
||||
let viewport_width = shell.strip_end() - first.x - TAB_RING;
|
||||
assert_eq!(content_width > viewport_width, overflow);
|
||||
|
||||
let source_id = format!("desktop.app_tabs.tab.{source}");
|
||||
let plus = shell.bounds("desktop.app_tabs.new").context("new tab")?;
|
||||
let plus_x = plus.x + plus.width / 2.0;
|
||||
let to = point(px(plus_x), px(plus.y + plus.height / 2.0));
|
||||
let window_right = shell.update(|_, window, _| window.viewport_size().width);
|
||||
let outside_right = point(window_right + px(80.0), to.y);
|
||||
for release in [point(to.x, px(300.0)), outside_right] {
|
||||
drag_tab_step(&shell, &source_id, plus_x);
|
||||
assert!(shell.has("desktop.app_tabs.drop_indicator"));
|
||||
shell.pointer(release, Some(MouseButton::Left));
|
||||
assert!(!shell.has("desktop.app_tabs.drop_indicator"));
|
||||
shell.button(release, false);
|
||||
assert_eq!(shell.tab_ids(), ids_before);
|
||||
assert_eq!(store.tabs(&ws_id), saved_before);
|
||||
}
|
||||
|
||||
drag_tab_step(&shell, &source_id, plus_x);
|
||||
let indicator = shell
|
||||
.bounds("desktop.app_tabs.drop_indicator")
|
||||
.context("end indicator")?;
|
||||
if overflow {
|
||||
assert_eq!(indicator.x + indicator.width, shell.strip_end() - TAB_RING);
|
||||
let accent = rgb(crate::palette_for(ThemeMode::Light).accent);
|
||||
assert_pixel(
|
||||
&shell.image(),
|
||||
(indicator.x + 1.0, indicator.y + indicator.height / 2.0),
|
||||
accent,
|
||||
"overflow drop indicator accent",
|
||||
);
|
||||
}
|
||||
shell.button(to, false);
|
||||
let mut expected = ids_before;
|
||||
expected[source..].rotate_left(1);
|
||||
assert_eq!(shell.tab_ids(), expected);
|
||||
let saved_after = store.tabs(&ws_id);
|
||||
assert_ne!(saved_after, saved_before);
|
||||
assert_eq!(
|
||||
saved_after,
|
||||
shell.update(|view, _, _| SavedTabs::json(&view.state.split))
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user