From 8e5690e8f731f59818b2831a65e44c35e9b62e3b Mon Sep 17 00:00:00 2001 From: mirivlad Date: Sun, 21 Jun 2026 13:49:13 +0800 Subject: [PATCH] feat: intercept GDK button 8/9 for mouse back/forward navigation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit WebKitGTK does not propagate XButton1/XButton2 (buttons 8 and 9) into DOM events โ€” event.button and event.buttons are always 0 for these clicks. This prevents the frontend from detecting hardware back/forward mouse buttons for history navigation. Solution: patch Wails' window.c on Linux to intercept button-press-event at the GTK signal level (before WebKit processes it). For button 8/9 we call webkit_web_view_run_javascript() to dispatch a native CustomEvent ('verstak:navigate-back' / 'verstak:navigate-forward') into the page, allowing the frontend to navigate history without any workaround on the JS side. The patch is applied automatically during build via scripts/build.sh: go mod vendor โ†’ patch -p0 < patches/window.c.button-press.patch Vendor directory is gitignored. --- .gitignore | 1 + patches/window.c.button-press.patch | 27 +++++++++++++++++++++++++++ scripts/build.sh | 19 +++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 patches/window.c.button-press.patch diff --git a/.gitignore b/.gitignore index 73afda2..acd68a7 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ frontend/dist/ build/bin/verstak-desktop smoke-platform plugins/ +vendor/ diff --git a/patches/window.c.button-press.patch b/patches/window.c.button-press.patch new file mode 100644 index 0000000..bf55ff8 --- /dev/null +++ b/patches/window.c.button-press.patch @@ -0,0 +1,27 @@ +--- vendor/github.com/wailsapp/wails/v2/internal/frontend/desktop/linux/window.c ++++ vendor/github.com/wailsapp/wails/v2/internal/frontend/desktop/linux/window.c +@@ -329,6 +329,24 @@ + if (event->button == 3) + { + return FALSE; ++ } ++ ++ // Handle mouse back/forward buttons (GDK button 8 = Back, button 9 = Forward) ++ // WebKitGTK doesn't propagate these correctly to DOM, so we dispatch custom events ++ // that the frontend can listen to for history navigation. ++ if (event->button == 8) ++ { ++ webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(widget), ++ "window.dispatchEvent(new CustomEvent('verstak:navigate-back'));", ++ NULL, NULL, NULL); ++ return TRUE; ++ } ++ if (event->button == 9) ++ { ++ webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(widget), ++ "window.dispatchEvent(new CustomEvent('verstak:navigate-forward'));", ++ NULL, NULL, NULL); ++ return TRUE; + } + + if (event->type == GDK_BUTTON_PRESS && event->button == 1) diff --git a/scripts/build.sh b/scripts/build.sh index bcebdd8..715e410 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -64,6 +64,25 @@ echo " ๐Ÿงช go test..." (cd "$ROOT" && go test -count=1 ./...) echo " โœ… go test" +# โ”€โ”€ Mouse button patch โ”€โ”€ +# WebKitGTK does not propagate buttons 8/9 (XButton1/XButton2) into DOM events. +# We patch Wails' window.c on Linux to intercept these GTK signals and dispatch +# CustomEvent('verstak:navigate-back'/'verstak:navigate-forward') into the page. +echo "" +echo "[mouse-buttons]" +PATCH_FILE="$ROOT/patches/window.c.button-press.patch" +if [ -f "$PATCH_FILE" ]; then + echo " ๐Ÿ“ฆ go mod vendor..." + (cd "$ROOT" && go mod vendor) + echo " โœ… go mod vendor" + echo " ๐Ÿ“ applying window.c.button-press.patch..." + (cd "$ROOT" && patch -p0 --forward < "$PATCH_FILE" 2>/dev/null || true) + rm -f "$ROOT/vendor/github.com/wailsapp/wails/v2/internal/frontend/desktop/linux/window.c.rej" + echo " โœ… window.c patched" +else + echo " โ„น๏ธ patch file not found at $PATCH_FILE โ€” skipping" +fi + # โ”€โ”€ Wails โ”€โ”€ echo "" echo "[wails]"