From 75dd527ec340339341b25bc6d5cc4b5aa7720772 Mon Sep 17 00:00:00 2001 From: Jacalz Date: Wed, 23 Aug 2023 12:52:06 +0200 Subject: [PATCH] Last batch of glfw driver cleanups I have now gone though all of the code to try and understand how it all fits together. While doing so, I fixed up some things I noticed could be improved. --- internal/driver/glfw/canvas.go | 23 +++++++++-------------- internal/driver/glfw/driver.go | 2 +- internal/driver/glfw/driver_desktop.go | 24 ++++++++---------------- internal/driver/glfw/driver_web.go | 2 +- internal/driver/glfw/loop.go | 2 +- internal/driver/glfw/menu_bar.go | 11 +++++------ 6 files changed, 25 insertions(+), 39 deletions(-) diff --git a/internal/driver/glfw/canvas.go b/internal/driver/glfw/canvas.go index d1f8a93530..fbd294de54 100644 --- a/internal/driver/glfw/canvas.go +++ b/internal/driver/glfw/canvas.go @@ -197,7 +197,7 @@ func (c *glCanvas) reloadScale() { } c.Lock() - c.scale = c.context.(*window).calculatedScale() + c.scale = w.calculatedScale() c.Unlock() c.SetDirty() @@ -237,8 +237,7 @@ func (c *glCanvas) buildMenu(w *window, m *fyne.MainMenu) { func (c *glCanvas) canvasSize(contentSize fyne.Size) fyne.Size { canvasSize := contentSize.Add(fyne.NewSize(0, c.menuHeight())) if c.Padded() { - pad := theme.Padding() * 2 - canvasSize = canvasSize.Add(fyne.NewSize(pad, pad)) + return canvasSize.Add(fyne.NewSquareSize(theme.Padding() * 2)) } return canvasSize } @@ -246,7 +245,7 @@ func (c *glCanvas) canvasSize(contentSize fyne.Size) fyne.Size { func (c *glCanvas) contentPos() fyne.Position { contentPos := fyne.NewPos(0, c.menuHeight()) if c.Padded() { - contentPos = contentPos.Add(fyne.NewPos(theme.Padding(), theme.Padding())) + return contentPos.Add(fyne.NewSquareOffsetPos(theme.Padding())) } return contentPos } @@ -254,20 +253,17 @@ func (c *glCanvas) contentPos() fyne.Position { func (c *glCanvas) contentSize(canvasSize fyne.Size) fyne.Size { contentSize := fyne.NewSize(canvasSize.Width, canvasSize.Height-c.menuHeight()) if c.Padded() { - pad := theme.Padding() * 2 - contentSize = contentSize.Subtract(fyne.NewSize(pad, pad)) + return contentSize.Subtract(fyne.NewSquareSize(theme.Padding() * 2)) } return contentSize } func (c *glCanvas) menuHeight() float32 { - switch c.menu { - case nil: - // no menu or native menu -> does not consume space on the canvas - return 0 - default: - return c.menu.MinSize().Height + if c.menu == nil { + return 0 // no menu or native menu -> does not consume space on the canvas } + + return c.menu.MinSize().Height } func (c *glCanvas) overlayChanged() { @@ -340,10 +336,9 @@ func (c *glCanvas) applyThemeOutOfTreeObjects() { } func newCanvas() *glCanvas { - c := &glCanvas{scale: 1.0, texScale: 1.0} + c := &glCanvas{scale: 1.0, texScale: 1.0, padded: true} c.Initialize(c, c.overlayChanged) c.setContent(&canvas.Rectangle{FillColor: theme.BackgroundColor()}) - c.padded = true c.debug = fyne.CurrentApp().Settings().BuildType() == fyne.BuildDebug return c } diff --git a/internal/driver/glfw/driver.go b/internal/driver/glfw/driver.go index 2ac723286b..0b81826a44 100644 --- a/internal/driver/glfw/driver.go +++ b/internal/driver/glfw/driver.go @@ -164,7 +164,7 @@ func (d *gLDriver) Run() { panic("Run() or ShowAndRun() must be called from main goroutine") } - go catchTerm(d) + go d.catchTerm() d.runGL() } diff --git a/internal/driver/glfw/driver_desktop.go b/internal/driver/glfw/driver_desktop.go index 464863ffca..6d6fd635cc 100644 --- a/internal/driver/glfw/driver_desktop.go +++ b/internal/driver/glfw/driver_desktop.go @@ -55,12 +55,8 @@ func (d *gLDriver) SetSystemTrayMenu(m *fyne.Menu) { // the only way we know the app was asked to quit is if this window is asked to close... w := d.CreateWindow("SystrayMonitor") w.(*window).create() - w.SetCloseIntercept(func() { - d.Quit() - }) - w.SetOnClosed(func() { - systray.Quit() - }) + w.SetCloseIntercept(d.Quit) + w.SetOnClosed(systray.Quit) }) d.refreshSystray(m) @@ -169,14 +165,12 @@ func (d *gLDriver) CurrentKeyModifiers() fyne.KeyModifier { return d.currentKeyModifiers } -func catchTerm(d *gLDriver) { - terminateSignals := make(chan os.Signal, 1) - signal.Notify(terminateSignals, syscall.SIGINT, syscall.SIGTERM) +func (d *gLDriver) catchTerm() { + terminateSignal := make(chan os.Signal, 1) + signal.Notify(terminateSignal, syscall.SIGINT, syscall.SIGTERM) - for range terminateSignals { - d.Quit() - break - } + <-terminateSignal + d.Quit() } func addMissingQuitForMenu(menu *fyne.Menu, d *gLDriver) { @@ -194,9 +188,7 @@ func addMissingQuitForMenu(menu *fyne.Menu, d *gLDriver) { } for _, item := range menu.Items { if item.IsQuit && item.Action == nil { - item.Action = func() { - d.Quit() - } + item.Action = d.Quit } } } diff --git a/internal/driver/glfw/driver_web.go b/internal/driver/glfw/driver_web.go index 3e48328a09..1ecc09da62 100644 --- a/internal/driver/glfw/driver_web.go +++ b/internal/driver/glfw/driver_web.go @@ -9,5 +9,5 @@ func (d *gLDriver) SetSystemTrayMenu(m *fyne.Menu) { // no-op for mobile apps using this driver } -func catchTerm(_ *gLDriver) { +func (d *gLDriver) catchTerm() { } diff --git a/internal/driver/glfw/loop.go b/internal/driver/glfw/loop.go index 48572ef223..34c5c2ecc4 100644 --- a/internal/driver/glfw/loop.go +++ b/internal/driver/glfw/loop.go @@ -186,7 +186,7 @@ func (d *gLDriver) runGL() { func (d *gLDriver) repaintWindow(w *window) { canvas := w.canvas w.RunWithContext(func() { - if w.canvas.EnsureMinSize() { + if canvas.EnsureMinSize() { w.viewLock.Lock() w.shouldExpand = true w.viewLock.Unlock() diff --git a/internal/driver/glfw/menu_bar.go b/internal/driver/glfw/menu_bar.go index af9f344b32..2c5cb9714f 100644 --- a/internal/driver/glfw/menu_bar.go +++ b/internal/driver/glfw/menu_bar.go @@ -68,9 +68,7 @@ func (b *MenuBar) Toggle() { } func (b *MenuBar) activateChild(item *menuBarItem) { - if !b.active { - b.active = true - } + b.active = true if item.Child() != nil { item.Child().DeactivateChild() } @@ -142,13 +140,14 @@ func (r *menuBarRenderer) Layout(size fyne.Size) { } else { r.underlay.Resize(fyne.NewSize(0, 0)) } - r.cont.Resize(fyne.NewSize(size.Width-2*theme.InnerPadding(), size.Height)) - r.cont.Move(fyne.NewPos(theme.InnerPadding(), 0)) + innerPadding := theme.InnerPadding() + r.cont.Resize(fyne.NewSize(size.Width-2*innerPadding, size.Height)) + r.cont.Move(fyne.NewPos(innerPadding, 0)) if item := r.b.activeItem; item != nil { if item.Child().Size().IsZero() { item.Child().Resize(item.Child().MinSize()) } - item.Child().Move(fyne.NewPos(item.Position().X+theme.InnerPadding(), item.Size().Height)) + item.Child().Move(fyne.NewPos(item.Position().X+innerPadding, item.Size().Height)) } r.background.Resize(size) }