From f4a5d4aab34a6b19dce1624aac702384071a8d66 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Wed, 7 Aug 2024 11:08:58 -0700 Subject: [PATCH 1/4] 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 6b3a8fd2..7dda4787 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 bf5e9fde..d718ac7d 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 27013fcc..ba54ce31 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 7ea7ef14..00000000 --- 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 a772b346..00000000 --- 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 13ee3a75..00000000 --- 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 3a66f971..00000000 --- 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)) -} From 1ff9d2d026b62f8d1c084c4eb9c848963397a268 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Wed, 7 Aug 2024 12:46:11 -0700 Subject: [PATCH 2/4] use Resize twice --- main.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index ba54ce31..482f84bc 100644 --- a/main.go +++ b/main.go @@ -6,11 +6,13 @@ import ( "log" "os" "sync" + "time" "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" ) @@ -61,12 +63,10 @@ func main() { // slightly hacky workaround for https://github.com/fyne-io/fyne/issues/4964 workaroundWindowSize := sync.OnceFunc(func() { + time.Sleep(10 * time.Millisecond) 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)) + mainWindow.Window.Resize(s.Subtract(fyne.NewSize(2, 0))) + mainWindow.Window.Resize(s) // back to desired size }) fyneApp.Lifecycle().SetOnEnteredForeground(func() { workaroundWindowSize() From 697aa911f505b634c5123623a8c92c0f5159eb82 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Thu, 8 Aug 2024 08:22:23 -0700 Subject: [PATCH 3/4] adjust sleeps, add debug logs --- main.go | 10 ++++++++-- ui/mainwindow.go | 6 +++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 482f84bc..dc18e17d 100644 --- a/main.go +++ b/main.go @@ -63,14 +63,20 @@ func main() { // slightly hacky workaround for https://github.com/fyne-io/fyne/issues/4964 workaroundWindowSize := sync.OnceFunc(func() { - time.Sleep(10 * time.Millisecond) + time.Sleep(50 * time.Millisecond) s := mainWindow.DesiredSize() - mainWindow.Window.Resize(s.Subtract(fyne.NewSize(2, 0))) + log.Printf("canvas size is %+v, desired size is %+v\n", mainWindow.Window.Canvas().Size(), s) + mainWindow.Window.Resize(s.Subtract(fyne.NewSize(4, 0))) + log.Printf("canvas size is %+v, desired size is %+v\n", mainWindow.Window.Canvas().Size(), s) + time.Sleep(30 * time.Millisecond) mainWindow.Window.Resize(s) // back to desired size + time.Sleep(30 * time.Millisecond) + log.Printf("canvas size is %+v, desired size is %+v\n", mainWindow.Window.Canvas().Size(), s) }) fyneApp.Lifecycle().SetOnEnteredForeground(func() { workaroundWindowSize() }) + mainWindow.ShowAndRun() log.Println("Running shutdown tasks...") diff --git a/ui/mainwindow.go b/ui/mainwindow.go index 05683b5c..f954dad6 100644 --- a/ui/mainwindow.go +++ b/ui/mainwindow.go @@ -81,9 +81,6 @@ func NewMainWindow(fyneApp fyne.App, appName, displayAppName, appVersion string, } m.BottomPanel = NewBottomPanel(app.PlaybackManager, app.ImageManager, m.Controller) - m.container = container.NewBorder(nil, m.BottomPanel, nil, nil, m.BrowsingPane) - m.Window.SetContent(fynetooltip.AddWindowToolTipLayer(m.container, m.Window.Canvas())) - m.setInitialSize() app.PlaybackManager.OnSongChange(func(item mediaprovider.MediaItem, _ *mediaprovider.Track) { if item == nil { m.Window.SetTitle(displayAppName) @@ -138,6 +135,9 @@ func NewMainWindow(fyneApp fyne.App, appName, displayAppName, appVersion string, m.BrowsingPane.DisableNavigationButtons() m.addShortcuts() + m.container = container.NewBorder(nil, m.BottomPanel, nil, nil, m.BrowsingPane) + m.Window.SetContent(fynetooltip.AddWindowToolTipLayer(m.container, m.Window.Canvas())) + m.setInitialSize() m.Window.SetCloseIntercept(func() { m.SaveWindowSize() if app.Config.Application.CloseToSystemTray && m.HaveSystemTray() { From ca90bf50da71e879964c7983437eb6fd87a6e0c7 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Thu, 8 Aug 2024 08:40:07 -0700 Subject: [PATCH 4/4] increase sleep, remove debug prints --- main.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/main.go b/main.go index dc18e17d..4d027242 100644 --- a/main.go +++ b/main.go @@ -65,13 +65,9 @@ func main() { workaroundWindowSize := sync.OnceFunc(func() { time.Sleep(50 * time.Millisecond) s := mainWindow.DesiredSize() - log.Printf("canvas size is %+v, desired size is %+v\n", mainWindow.Window.Canvas().Size(), s) mainWindow.Window.Resize(s.Subtract(fyne.NewSize(4, 0))) - log.Printf("canvas size is %+v, desired size is %+v\n", mainWindow.Window.Canvas().Size(), s) - time.Sleep(30 * time.Millisecond) + time.Sleep(50 * time.Millisecond) mainWindow.Window.Resize(s) // back to desired size - time.Sleep(30 * time.Millisecond) - log.Printf("canvas size is %+v, desired size is %+v\n", mainWindow.Window.Canvas().Size(), s) }) fyneApp.Lifecycle().SetOnEnteredForeground(func() { workaroundWindowSize()