فهرست منبع

Improve the mouse handling to window dragging

Johannes Zellner 7 ماه پیش
والد
کامیت
574076f768
1فایلهای تغییر یافته به همراه24 افزوده شده و 12 حذف شده
  1. 24 12
      src/main.rs

+ 24 - 12
src/main.rs

@@ -27,23 +27,36 @@ const WINDOW_DRAG_ATTR: &str = "data-webruntime-window-drag";
 /// Lazily created shared profile; one store for every origin (WebKit partitions internally).
 const APP_NETWORK_SESSION_KEY: &str = "webruntime-network-session";
 
+/// JS bridge: on mousedown inside a `[data-webruntime-window-drag]` region, we
+/// wait one animation frame to let the page's own handlers call preventDefault().
+/// If the event was NOT prevented and no interactive element was the direct
+/// target, we fire the native drag message. A synthetic pointerup is dispatched
+/// first so WebKit's pointer state machine stays clean after begin_move steals
+/// the grab.
 fn window_drag_bridge_source() -> String {
     format!(
         r#"(function () {{
   const attr = "{attr}";
-  document.addEventListener(
-    "pointerdown",
-    function (e) {{
-      if (e.button !== 0) return;
-      var el = e.target;
-      if (!el || typeof el.closest !== "function") return;
-      if (!el.closest("[" + attr + "]")) return;
+  const interactive = "A,BUTTON,INPUT,TEXTAREA,SELECT,LABEL,SUMMARY,DETAILS,AUDIO,VIDEO";
+  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 (ex) {{}}
-    }},
-    true
-  );
+      }} catch(_) {{}}
+    }});
+  }}, false);
 }})();"#,
         attr = WINDOW_DRAG_ATTR,
         handler = WINDOW_DRAG_MESSAGE_HANDLER,
@@ -51,7 +64,6 @@ fn window_drag_bridge_source() -> String {
 }
 
 /// Called when embedded JS reports a press on `[data-webruntime-window-drag]`.
-/// Uses the core pointer position so `begin_move` still matches an in-progress grab.
 fn begin_move_from_pointer_on_window(window: &ApplicationWindow) {
     let Some(display) = gdk::Display::default() else {
         return;