Merge pull request #460 from dweymouth/resize-hack-rework

Linux window misdrawing workaround, redux
This commit is contained in:
Drew Weymouth
2024-08-08 15:48:40 -07:00
committed by GitHub
8 changed files with 20 additions and 128 deletions
+1 -1
View File
@@ -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
+2 -2
View File
@@ -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=
+14 -31
View File
@@ -5,7 +5,7 @@ import (
"fmt"
"log"
"os"
"runtime"
"sync"
"time"
"github.com/dweymouth/supersonic/backend"
@@ -59,39 +59,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() {
time.Sleep(50 * time.Millisecond)
s := mainWindow.DesiredSize()
mainWindow.Window.Resize(s.Subtract(fyne.NewSize(4, 0)))
time.Sleep(50 * time.Millisecond)
mainWindow.Window.Resize(s) // back to desired size
})
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
}
+3 -3
View File
@@ -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() {
-68
View File
@@ -1,68 +0,0 @@
//go:build linux && !wayland
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <unistd.h>
#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;
}
-3
View File
@@ -1,3 +0,0 @@
//go:build linux && !wayland
int send_resize_to_pid(int pid, int w, int h);
-5
View File
@@ -1,5 +0,0 @@
//go:build !linux || wayland
package main
func SendResizeToPID(pid, w, h int) {}
-15
View File
@@ -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))
}