mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 11:05:03 +00:00
A drag of art across the document tabs ended whenever the source document's revision changed. Since #7173, bringing a document with other assigned color profiles to the front raises every open document's revision so that each redraws in the new profiles, so holding art over such a tab switched to it and then canceled the drag before the drop. The drag now keeps the source document it left and ends only when that document's content changes (DocState::same_content). A redraw keeps it, and so does type measured again when the source comes back to the front, which replaces the document without editing it.
213 lines
8.4 KiB
Rust
213 lines
8.4 KiB
Rust
//! A selection dragged across document tabs remains editable and leaves its source intact.
|
||
|
||
use std::sync::Arc;
|
||
|
||
use egui::{Context, Id, Response};
|
||
use serde_json::json;
|
||
use vectorcraft_doc::{Document, NodeId};
|
||
use vectorcraft_geom::Point;
|
||
|
||
use crate::{VectorcraftApp, canvas, widgets};
|
||
|
||
const HOVER_SECONDS: f64 = 0.4;
|
||
|
||
#[derive(Clone)]
|
||
struct Origin {
|
||
uid: u64,
|
||
grab: Point,
|
||
}
|
||
|
||
#[derive(Clone)]
|
||
struct Artwork {
|
||
origin: Origin,
|
||
ids: Vec<NodeId>,
|
||
/// The source document when the drag left it. The drag ends when the source's content differs
|
||
/// from it ([`vectorcraft_engine::DocState::same_content`]). A redraw that raises its revision
|
||
/// (another document's assigned color profiles brought to the front) and a change that is not
|
||
/// an edit (its type measured again) keep it.
|
||
source: Arc<Document>,
|
||
target: u64,
|
||
}
|
||
|
||
#[derive(Clone)]
|
||
struct Hover {
|
||
uid: u64,
|
||
/// The hovered tab's view of its document (Window › New Window).
|
||
view: u32,
|
||
since: f64,
|
||
}
|
||
|
||
/// Key for the source document and original canvas grab point.
|
||
fn origin_id() -> Id {
|
||
Id::unique("document-drag-origin")
|
||
}
|
||
/// Key for the tab identity and dwell start time of the current hover.
|
||
fn hover_id() -> Id {
|
||
Id::unique("document-drag-hover")
|
||
}
|
||
/// Key for the pointer position latched after tab activation to prevent cascading switches.
|
||
fn activated_at_id() -> Id {
|
||
Id::unique("document-drag-activated-at")
|
||
}
|
||
|
||
/// Whether a Selection drag is carrying artwork across the document tabs, which then switch
|
||
/// documents and never move along the strip.
|
||
pub(crate) fn carrying_artwork(ctx: &Context) -> bool {
|
||
egui::DragAndDrop::payload::<Artwork>(ctx).is_some()
|
||
}
|
||
|
||
/// Record the Selection drag source and grab point, clearing any previous tab-hover timer.
|
||
pub(crate) fn begin(app: &VectorcraftApp, ctx: &Context, grab: Point) {
|
||
ctx.data_mut(|d| {
|
||
d.remove::<Origin>(origin_id());
|
||
d.remove::<Hover>(hover_id());
|
||
d.remove::<egui::Pos2>(activated_at_id());
|
||
if app.session.tool_id() == "selection"
|
||
&& let Some(st) = app.session.active()
|
||
{
|
||
d.insert_temp(origin_id(), Origin { uid: st.uid, grab });
|
||
}
|
||
});
|
||
}
|
||
|
||
/// Called before panels. Cancelled or abandoned transfers must never reach a later mouse-up.
|
||
pub(crate) fn cleanup(app: &VectorcraftApp, ctx: &Context) {
|
||
// Activating a tab can scroll the strip or change title widths. A tab moving under a
|
||
// stationary pointer must not start another hover; actual pointer movement rearms it.
|
||
let at = ctx.input(|i| i.pointer.interact_pos());
|
||
ctx.data_mut(|d| {
|
||
if d.get_temp::<egui::Pos2>(activated_at_id()) != at {
|
||
d.remove::<egui::Pos2>(activated_at_id());
|
||
}
|
||
});
|
||
if let Some(art) = egui::DragAndDrop::payload::<Artwork>(ctx) {
|
||
let valid = app.session.documents().iter().any(|d| d.uid == art.origin.uid && d.same_content(&art.source))
|
||
&& app.session.active().is_some_and(|d| d.uid == art.target);
|
||
let cancel = ctx.input(|i| {
|
||
!i.focused
|
||
|| i.key_pressed(egui::Key::Escape)
|
||
|| (!i.pointer.primary_down() && !i.pointer.primary_released())
|
||
|| i.multi_touch().is_some()
|
||
|| i.events.iter().any(|e| matches!(e, egui::Event::Touch { phase: egui::TouchPhase::Cancel, .. }))
|
||
});
|
||
let outside_release =
|
||
ctx.input(|i| i.pointer.primary_released() && !app.canvas_rect.is_some_and(|r| i.pointer.interact_pos().is_some_and(|p| r.contains(p))));
|
||
if !valid || cancel || outside_release || app.ui.dialog.is_some() {
|
||
egui::DragAndDrop::clear_payload(ctx);
|
||
}
|
||
}
|
||
if !ctx.input(|i| i.pointer.primary_down()) {
|
||
ctx.data_mut(|d| {
|
||
d.remove::<Origin>(origin_id());
|
||
d.remove::<Hover>(hover_id());
|
||
d.remove::<egui::Pos2>(activated_at_id());
|
||
});
|
||
}
|
||
}
|
||
|
||
/// Hover uses `contains_pointer`: the source canvas still owns egui's dragged widget. The tab is
|
||
/// window `win`'s: another view of the current document's is a tab to switch to as well.
|
||
pub(crate) fn hover_tab(app: &mut VectorcraftApp, resp: &Response, win: crate::doc_windows::Win, close: egui::Rect) -> bool {
|
||
let ctx = &resp.ctx;
|
||
let (index, view) = win;
|
||
let eligible = resp.contains_pointer()
|
||
&& ctx
|
||
.input(|i| i.pointer.primary_down() && !i.key_pressed(egui::Key::Escape) && i.pointer.interact_pos().is_some_and(|p| !close.contains(p)))
|
||
&& app.ui.dialog.is_none();
|
||
if !eligible || crate::doc_windows::current(app) == Some(win) || ctx.data(|d| d.get_temp::<egui::Pos2>(activated_at_id()).is_some()) {
|
||
return false;
|
||
}
|
||
let transfer = egui::DragAndDrop::has_payload_of_type::<Artwork>(ctx);
|
||
let art = egui::DragAndDrop::payload::<widgets::PanelDrag>(ctx).is_some_and(|p| matches!(*p, widgets::PanelDrag::Art(_)));
|
||
if !transfer && !art {
|
||
return false;
|
||
}
|
||
let Some(uid) = app.session.documents().get(index).map(|d| d.uid) else {
|
||
return false;
|
||
};
|
||
let now = ctx.input(|i| i.time);
|
||
let hover = ctx.data(|d| d.get_temp::<Hover>(hover_id())).filter(|h| h.uid == uid && h.view == view).unwrap_or(Hover { uid, view, since: now });
|
||
ctx.data_mut(|d| d.insert_temp(hover_id(), hover.clone()));
|
||
let remaining = HOVER_SECONDS - (now - hover.since);
|
||
if remaining > 0.0 {
|
||
ctx.request_repaint_after(std::time::Duration::try_from_secs_f64(remaining).unwrap_or(std::time::Duration::from_millis(400)));
|
||
return true;
|
||
}
|
||
if !transfer {
|
||
let Some(origin) = ctx.data(|d| d.get_temp::<Origin>(origin_id())) else {
|
||
return true;
|
||
};
|
||
if !app
|
||
.session
|
||
.active()
|
||
.is_some_and(|d| d.uid == origin.uid && d.interaction.as_ref().is_some_and(|i| i.label == "Move" || i.label == "Copy"))
|
||
{
|
||
return true;
|
||
}
|
||
if let Err(e) = app.session.cancel_interaction() {
|
||
app.status(e.to_string());
|
||
return true;
|
||
}
|
||
// Alt-drag previews use temporary duplicate ids; only the restored originals travel.
|
||
let Some((ids, source)) = app.session.active().map(|d| (d.selection.objects.clone(), d.doc.clone())) else {
|
||
return true;
|
||
};
|
||
if ids.is_empty() {
|
||
return true;
|
||
}
|
||
canvas::end_document_drag(ctx);
|
||
egui::DragAndDrop::set_payload(ctx, Artwork { origin, ids, source, target: uid });
|
||
}
|
||
if transfer && let Some(art) = egui::DragAndDrop::payload::<Artwork>(ctx) {
|
||
let mut art = (*art).clone();
|
||
art.target = uid;
|
||
egui::DragAndDrop::set_payload(ctx, art);
|
||
}
|
||
app.ui.home = None;
|
||
crate::doc_windows::show_window(app, win);
|
||
ctx.request_repaint();
|
||
let at = ctx.input(|i| i.pointer.interact_pos());
|
||
ctx.data_mut(|d| {
|
||
d.remove::<Hover>(hover_id());
|
||
if let Some(at) = at {
|
||
d.insert_temp(activated_at_id(), at);
|
||
}
|
||
});
|
||
true
|
||
}
|
||
|
||
/// Reset the dwell timer when no eligible inactive tab is under the artwork.
|
||
pub(crate) fn finish_tabs(ctx: &Context, hovering: bool) {
|
||
if !hovering {
|
||
ctx.data_mut(|d| d.remove::<Hover>(hover_id()));
|
||
}
|
||
}
|
||
|
||
/// Release artwork into the active canvas; returning to the source resumes Move or Alt-copy.
|
||
pub(crate) fn drop_on_canvas(app: &mut VectorcraftApp, resp: &Response, xf: &canvas::Xf) {
|
||
let Some(payload) = egui::DragAndDrop::payload::<Artwork>(&resp.ctx) else {
|
||
return;
|
||
};
|
||
let same = app.session.active().is_some_and(|d| d.uid == payload.origin.uid);
|
||
let copy = resp.ctx.input(|i| i.modifiers.alt);
|
||
if resp.contains_pointer() {
|
||
resp.ctx.set_cursor_icon(if same && !copy { egui::CursorIcon::Move } else { egui::CursorIcon::Copy });
|
||
}
|
||
if let Some(art) = resp.dnd_release_payload::<Artwork>()
|
||
&& let Some(at) = resp.ctx.input(|i| i.pointer.interact_pos()).map(|p| xf.to_doc(p))
|
||
{
|
||
let result = if same {
|
||
// Back in the source tab, resume the ordinary move (Alt copies).
|
||
app.run("object.move", json!({"ids": art.ids, "dx": at.x-art.origin.grab.x, "dy": at.y-art.origin.grab.y, "copy": copy}))
|
||
} else {
|
||
app.run(
|
||
"edit.dropSelection",
|
||
json!({"sourceDocument": art.origin.uid, "ids": art.ids, "grab": [art.origin.grab.x, art.origin.grab.y], "at": [at.x, at.y]}),
|
||
)
|
||
};
|
||
if let Err(e) = result {
|
||
app.status(e);
|
||
}
|
||
}
|
||
}
|