Przeglądaj źródła

Improve the window drag handling

Johannes Zellner 6 miesięcy temu
rodzic
commit
61320800e0
1 zmienionych plików z 53 dodań i 22 usunięć
  1. 53 22
      src/main.rs

+ 53 - 22
src/main.rs

@@ -28,7 +28,7 @@ use webkit6::{
 /// JavaScript `window.webkit.messageHandlers.<name>` must match this identifier.
 const WINDOW_DRAG_MESSAGE_HANDLER: &str = "appiphanyWindowDrag";
 
-/// Mark draggable chrome in your HTML, e.g. `<div data-appiphany-window-drag>…</div>`.
+/// Mark draggable regions in HTML, e.g. `<div data-appiphany-window-drag>…</div>`.
 const WINDOW_DRAG_ATTR: &str = "data-appiphany-window-drag";
 
 /// Host detection for embedded webapps (set on `<html>` at document start).
@@ -43,45 +43,76 @@ const HOST_MARKER_ATTR: &str = "data-appiphany";
 /// Lazily created shared profile; one store for every origin (WebKit partitions internally).
 const APP_NETWORK_SESSION_KEY: &str = "appiphany-network-session";
 
-/// Injected at document start: sets `html[data-appiphany]`, `window.appiphany`, and the
-/// window-drag bridge (mousedown inside `[data-appiphany-window-drag]` …).
+/// Injected at document start: sets `html[data-appiphany]` and exposes `window.appiphany` API.
+/// Enables window dragging from non-interactive elements inside `[data-appiphany-window-drag]`.
 fn appiphany_document_start_script_source() -> String {
     format!(
         r#"(function () {{
   try {{
     document.documentElement.setAttribute("{host_attr}", "");
-    window.appiphany = {{ version: "{version}" }};
   }} catch (_) {{}}
-  const attr = "{attr}";
-  const interactive = "A,BUTTON,INPUT,TEXTAREA,SELECT,LABEL,SUMMARY,DETAILS,AUDIO,VIDEO";
+
+  window.appiphany = {{ version: "{version}" }};
+
+  var dragState = null;
+  var dragAttr = "{drag_attr}";
+  var interactive = "A,BUTTON,INPUT,TEXTAREA,SELECT,LABEL,SUMMARY,DETAILS,AUDIO,VIDEO";
+
+  function isInteractive(el) {{
+    if (!el || el === document.documentElement || el === document.body) return false;
+    if (el.isContentEditable) return true;
+    if (el.closest && el.closest(interactive)) return true;
+    if (el.onclick || el.onmousedown || el.onmouseup) return true;
+    return false;
+  }}
+
+  function beginNativeDrag() {{
+    try {{
+      window.webkit.messageHandlers.{handler}.postMessage(null);
+    }} catch (_) {{}}
+  }}
+
+  function onMouseMove(e) {{
+    if (!dragState) return;
+    var dx = e.clientX - dragState.startX;
+    var dy = e.clientY - dragState.startY;
+    if (Math.abs(dx) > 3 || Math.abs(dy) > 3) {{
+      cleanup();
+      beginNativeDrag();
+    }}
+  }}
+
+  function cleanup() {{
+    if (dragState) {{
+      document.removeEventListener("mousemove", onMouseMove, true);
+      document.removeEventListener("mouseup", cleanup, true);
+      dragState = null;
+    }}
+  }}
+
   document.addEventListener("mousedown", function (e) {{
     if (e.button !== 0) return;
     var el = e.target;
     if (!el || typeof el.closest !== "function") return;
-    if (!el.closest("[" + attr + "]")) return;
-    if (el.closest(interactive)) return;
-    if (el.isContentEditable) return;
-    var ev = e;
-    requestAnimationFrame(function () {{
-      if (ev.defaultPrevented) return;
-      try {{
-        el.dispatchEvent(new PointerEvent("pointerup", {{bubbles:true}}));
-        el.dispatchEvent(new MouseEvent("mouseup", {{bubbles:true}}));
-      }} catch(_) {{}}
-      try {{
-        window.webkit.messageHandlers.{handler}.postMessage(null);
-      }} catch(_) {{}}
-    }});
+    if (!el.closest("[" + dragAttr + "]")) return;
+    if (isInteractive(el)) return;
+    cleanup();
+    dragState = {{
+      startX: e.clientX,
+      startY: e.clientY
+    }};
+    document.addEventListener("mousemove", onMouseMove, true);
+    document.addEventListener("mouseup", cleanup, true);
   }}, false);
 }})();"#,
         host_attr = HOST_MARKER_ATTR,
         version = env!("CARGO_PKG_VERSION"),
-        attr = WINDOW_DRAG_ATTR,
+        drag_attr = WINDOW_DRAG_ATTR,
         handler = WINDOW_DRAG_MESSAGE_HANDLER,
     )
 }
 
-/// Called when embedded JS reports a press on `[data-appiphany-window-drag]`.
+/// Called when the web app requests window dragging via `window.appiphany.startWindowDrag()`.
 fn begin_move_from_pointer_on_window(window: &ApplicationWindow) {
     let Some(display) = gdk::Display::default() else {
         return;