From f4a5d4aab34a6b19dce1624aac702384071a8d66 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Wed, 7 Aug 2024 11:08:58 -0700 Subject: [PATCH] rework window size workaround --- go.mod | 2 +- go.sum | 4 +-- main.go | 47 ++++++++++----------------------- xresize.c | 68 ------------------------------------------------ xresize.h | 3 --- xresize_other.go | 5 ---- xresize_x11.go | 15 ----------- 7 files changed, 17 insertions(+), 127 deletions(-) delete mode 100644 xresize.c delete mode 100644 xresize.h delete mode 100644 xresize_other.go delete mode 100644 xresize_x11.go diff --git a/go.mod b/go.mod index 6b3a8fd..7dda478 100644 --- a/go.mod +++ b/go.mod @@ -54,4 +54,4 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect ) -replace fyne.io/fyne/v2 v2.5.0 => github.com/dweymouth/fyne/v2 v2.3.0-rc1.0.20240805144743-24df77c3cc7e +replace fyne.io/fyne/v2 v2.5.0 => github.com/dweymouth/fyne/v2 v2.3.0-rc1.0.20240807180232-d2b0dad0b17d diff --git a/go.sum b/go.sum index bf5e9fd..d718ac7 100644 --- a/go.sum +++ b/go.sum @@ -81,8 +81,8 @@ github.com/dweymouth/fyne-lyrics v0.0.0-20240528234907-15eee7ce5e64 h1:RUIrnGY03 github.com/dweymouth/fyne-lyrics v0.0.0-20240528234907-15eee7ce5e64/go.mod h1:3YrjFDHMlhCsSZ/OvmJCxWm9QHSgOVWZBxnraZz9Z7c= github.com/dweymouth/fyne-tooltip v0.2.0 h1:6Zy3gryctuPoQfYf8Xp3tjenioebMt11NBGW/QXIvxE= github.com/dweymouth/fyne-tooltip v0.2.0/go.mod h1:zEgy7p9tSVIuy2GufFbOCoK3Q04zhyDPOotlU4G3Ma4= -github.com/dweymouth/fyne/v2 v2.3.0-rc1.0.20240805144743-24df77c3cc7e h1:FSTLNY9xV0+4/x9jKPXqUpwPZfhkAMz5ZnzLBkRirMw= -github.com/dweymouth/fyne/v2 v2.3.0-rc1.0.20240805144743-24df77c3cc7e/go.mod h1:9D4oT3NWeG+MLi/lP7ItZZyujHC/qqMJpoGTAYX5Uqc= +github.com/dweymouth/fyne/v2 v2.3.0-rc1.0.20240807180232-d2b0dad0b17d h1:A8HSVm7wn1aEMdT/8YPtuP5XGwasqZ5kxcdBhIfxXvU= +github.com/dweymouth/fyne/v2 v2.3.0-rc1.0.20240807180232-d2b0dad0b17d/go.mod h1:9D4oT3NWeG+MLi/lP7ItZZyujHC/qqMJpoGTAYX5Uqc= github.com/dweymouth/go-jellyfin v0.0.0-20240517151952-5ceca61cb645 h1:KzqSaQwG3HsTZQlEtkp0BeUy9vmYZ0rq0B15qIPSiBs= github.com/dweymouth/go-jellyfin v0.0.0-20240517151952-5ceca61cb645/go.mod h1:fcUagHBaQnt06GmBAllNE0J4O/7064zXRWdqnTTtVjI= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= diff --git a/main.go b/main.go index 27013fc..ba54ce3 100644 --- a/main.go +++ b/main.go @@ -5,14 +5,12 @@ import ( "fmt" "log" "os" - "runtime" - "time" + "sync" "github.com/dweymouth/supersonic/backend" "github.com/dweymouth/supersonic/res" "github.com/dweymouth/supersonic/ui" - "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/lang" ) @@ -59,39 +57,22 @@ func main() { } else { mainWindow.Controller.DoConnectToServerWorkflow(defaultServer) } - - // hacky workaround for https://github.com/fyne-io/fyne/issues/4964 - if runtime.GOOS == "linux" { - time.Sleep(350 * time.Millisecond) - canvas := mainWindow.Window.Canvas() - size := canvas.Size() - desired := mainWindow.DesiredSize() - if !inDelta(size, desired, 1) { - // window drawn at incorrect size on startup - scale := canvas.Scale() - for i := 0; i < 3 && !inDelta(size, desired, 1); i++ { - if i > 0 { - // if resize didn't work the first time, try again with slightly - // different desired size - desired.Subtract(fyne.NewSize(2, 2)) - } - SendResizeToPID(os.Getpid(), int(desired.Width*scale), int(desired.Height*scale)) - time.Sleep(100 * time.Millisecond) - size = canvas.Size() - } - } - } - }() + + // slightly hacky workaround for https://github.com/fyne-io/fyne/issues/4964 + workaroundWindowSize := sync.OnceFunc(func() { + s := mainWindow.DesiredSize() + scale := mainWindow.Window.Canvas().Scale() + s.Width *= scale + s.Height *= scale + // exported in Supersonic Fyne fork + mainWindow.Window.ProcessResized(int(s.Width), int(s.Height)) + }) + fyneApp.Lifecycle().SetOnEnteredForeground(func() { + workaroundWindowSize() + }) mainWindow.ShowAndRun() log.Println("Running shutdown tasks...") myApp.Shutdown() } - -func inDelta(a, b fyne.Size, delta float32) bool { - diffW := a.Width - b.Width - diffH := a.Height - b.Height - return diffW < delta && diffW > -delta && - diffH < delta && diffH > -delta -} diff --git a/xresize.c b/xresize.c deleted file mode 100644 index 7ea7ef1..0000000 --- a/xresize.c +++ /dev/null @@ -1,68 +0,0 @@ -//go:build linux && !wayland - -#include -#include -#include -#include -#include - -#include "xresize.h" - -// thanks ChatGPT ;) -int find_windows_by_pid(Display *display, Window root, pid_t pid, Window* out, int n) { - Window *children; - unsigned int nchildren; - if (!XQueryTree(display, root, &root, &root, &children, &nchildren)) { - return 0; - } - - for (unsigned int i = 0; i < nchildren; i++) { - Atom pidAtom = XInternAtom(display, "_NET_WM_PID", True); - if (pidAtom != None) { - Atom type; - int format; - unsigned long nitems, bytes_after; - unsigned char *prop_pid = NULL; - - if (XGetWindowProperty(display, children[i], pidAtom, 0, 1, False, XA_CARDINAL, - &type, &format, &nitems, &bytes_after, &prop_pid) == Success && prop_pid) { - if (pid == *((pid_t *)prop_pid)) { - out[n++] = children[i]; - } - XFree(prop_pid); - } - } - - n = find_windows_by_pid(display, children[i], pid, out, n); - } - - XFree(children); - return n; -} - -void send_resize_event(Display *display, Window window, int width, int height) { - XResizeWindow(display, window, width, height); - XFlush(display); -} - -int send_resize_to_pid(int pid, int w, int h) { - Display *display = XOpenDisplay(NULL); - if (!display) { - return 1; - } - int ret = 0; - Window windows[128]; - Window root = DefaultRootWindow(display); - - int n = find_windows_by_pid(display, root, pid, &windows[0], 0); - if (n > 0) { - for (int i = 0; i < n; i++) { - send_resize_event(display, windows[i], w, h); - } - } else { - ret = 1; - } - - XCloseDisplay(display); - return ret; -} diff --git a/xresize.h b/xresize.h deleted file mode 100644 index a772b34..0000000 --- a/xresize.h +++ /dev/null @@ -1,3 +0,0 @@ -//go:build linux && !wayland - -int send_resize_to_pid(int pid, int w, int h); diff --git a/xresize_other.go b/xresize_other.go deleted file mode 100644 index 13ee3a7..0000000 --- a/xresize_other.go +++ /dev/null @@ -1,5 +0,0 @@ -//go:build !linux || wayland - -package main - -func SendResizeToPID(pid, w, h int) {} diff --git a/xresize_x11.go b/xresize_x11.go deleted file mode 100644 index 3a66f97..0000000 --- a/xresize_x11.go +++ /dev/null @@ -1,15 +0,0 @@ -//go:build linux && !wayland - -package main - -/* -#cgo LDFLAGS: -lX11 -#include "xresize.h" -*/ -import ( - "C" -) - -func SendResizeToPID(pid, w, h int) { - C.send_resize_to_pid(C.int(pid), C.int(w), C.int(h)) -}