From 07a3ba93265abc17bf2113da3cb33765535ef3bb Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Sun, 9 Feb 2025 10:54:42 +0000 Subject: [PATCH] Update Fyne and correct our thread handling to work with the new `fyne.Do` requirement --- cmd/fyneterm/main.go | 32 ++++++++++++++------------------ go.mod | 3 ++- go.sum | 6 ++++-- internal/widget/termgrid.go | 4 +++- output.go | 14 ++++++++------ term.go | 16 +++++++++------- 6 files changed, 40 insertions(+), 35 deletions(-) diff --git a/cmd/fyneterm/main.go b/cmd/fyneterm/main.go index ce6b1e0..2dcff30 100644 --- a/cmd/fyneterm/main.go +++ b/cmd/fyneterm/main.go @@ -29,11 +29,13 @@ func setupListener(t *terminal.Terminal, w fyne.Window) { for { config := <-listen - if config.Title == "" { - w.SetTitle(termTitle()) - } else { - w.SetTitle(termTitle() + ": " + config.Title) - } + fyne.Do(func() { + if config.Title == "" { + w.SetTitle(termTitle()) + } else { + w.SetTitle(termTitle() + ": " + config.Title) + } + }) } }() t.AddListener(listen) @@ -74,18 +76,12 @@ func newTerminalWindow(a fyne.App, th fyne.Theme, debug bool) fyne.Window { img.FillMode = canvas.ImageFillContain over := canvas.NewRectangle(th.Color(termOverlay, a.Settings().ThemeVariant())) - ch := make(chan fyne.Settings) - go func() { - for { - <-ch - - bg.FillColor = theme.Color(theme.ColorNameBackground) - bg.Refresh() - over.FillColor = th.Color(termOverlay, a.Settings().ThemeVariant()) - over.Refresh() - } - }() - a.Settings().AddChangeListener(ch) + a.Settings().AddListener(func(s fyne.Settings) { + bg.FillColor = theme.Color(theme.ColorNameBackground) + bg.Refresh() + over.FillColor = th.Color(termOverlay, s.ThemeVariant()) + over.Refresh() + }) t := terminal.New() t.SetDebug(debug) @@ -121,7 +117,7 @@ func newTerminalWindow(a fyne.App, th fyne.Theme, debug bool) fyne.Window { if err != nil { fyne.LogError("Failure in terminal", err) } - w.Close() + fyne.Do(w.Close) }() return w diff --git a/go.mod b/go.mod index ef9cdd9..49247b7 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/fyne-io/terminal go 1.19 require ( - fyne.io/fyne/v2 v2.5.3-rc7.0.20250120111557-621fc220edd7 + fyne.io/fyne/v2 v2.5.5-0.20250208233333-9cdc38456780 github.com/ActiveState/termtest/conpty v0.5.0 github.com/creack/pty v1.1.21 github.com/stretchr/testify v1.10.0 @@ -27,6 +27,7 @@ require ( github.com/gopherjs/gopherjs v1.17.2 // indirect github.com/jeandeaual/go-locale v0.0.0-20241217141322-fcc2cadd6f08 // indirect github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e // indirect + github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect github.com/nicksnyder/go-i18n/v2 v2.4.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rymdport/portal v0.3.0 // indirect diff --git a/go.sum b/go.sum index 815839d..3865fa5 100644 --- a/go.sum +++ b/go.sum @@ -37,8 +37,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs= cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -fyne.io/fyne/v2 v2.5.3-rc7.0.20250120111557-621fc220edd7 h1:6xIwnZtyOen//wxgOhAIounJsLZwdQQgtP8DJNAH5vU= -fyne.io/fyne/v2 v2.5.3-rc7.0.20250120111557-621fc220edd7/go.mod h1:0GOXKqyvNwk3DLmsFu9v0oYM0ZcD1ysGnlHCerKoAmo= +fyne.io/fyne/v2 v2.5.5-0.20250208233333-9cdc38456780 h1:jmgXLVGxO+qaz3DSyzXRMZhTIZciyTf1RP7j12Q6INU= +fyne.io/fyne/v2 v2.5.5-0.20250208233333-9cdc38456780/go.mod h1:6/uEYg4FEhspAcWgsokutm9wFMHDNSYuEHCKTYWSho8= fyne.io/systray v1.11.0 h1:D9HISlxSkx+jHSniMBR6fCFOUjk1x/OOOJLa9lJYAKg= fyne.io/systray v1.11.0/go.mod h1:RVwqP9nYMo7h5zViCBHri2FgjXF7H2cub7MAq4NSoLs= github.com/ActiveState/termtest/conpty v0.5.0 h1:JLUe6YDs4Jw4xNPCU+8VwTpniYOGeKzQg4SM2YHQNA8= @@ -232,6 +232,8 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo= github.com/neelance/sourcemap v0.0.0-20200213170602-2833bce08e4c/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= github.com/nicksnyder/go-i18n/v2 v2.4.0 h1:3IcvPOAvnCKwNm0TB0dLDTuawWEj+ax/RERNC+diLMM= github.com/nicksnyder/go-i18n/v2 v2.4.0/go.mod h1:nxYSZE9M0bf3Y70gPQjN9ha7XNHX7gMc814+6wVyEI4= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= diff --git a/internal/widget/termgrid.go b/internal/widget/termgrid.go index 6497553..871b345 100644 --- a/internal/widget/termgrid.go +++ b/internal/widget/termgrid.go @@ -80,7 +80,9 @@ func (t *TermGrid) runBlink() { return case <-ticker.C: blinking = !blinking - t.refreshBlink(blinking) + fyne.Do(func() { + t.refreshBlink(blinking) + }) } } }() diff --git a/output.go b/output.go index bef8222..f09d5ed 100644 --- a/output.go +++ b/output.go @@ -5,6 +5,7 @@ import ( "time" "unicode/utf8" + "fyne.io/fyne/v2" "fyne.io/fyne/v2/widget" widget2 "github.com/fyne-io/terminal/internal/widget" ) @@ -156,7 +157,6 @@ func (t *Terminal) handleOutput(buf []byte) []byte { // check to see which charset to use if t.useG1CharSet { t.handleOutputChar(charSetMap[t.g1Charset](r)) - } else { t.handleOutputChar(charSetMap[t.g0Charset](r)) } @@ -266,11 +266,13 @@ func (t *Terminal) handleOutputChar(r rune) { func (t *Terminal) ringBell() { t.bell = true - t.Refresh() + fyne.Do(t.Refresh) - time.Sleep(time.Millisecond * 300) - t.bell = false - t.Refresh() + go func() { + time.Sleep(time.Millisecond * 300) + t.bell = false + fyne.Do(t.Refresh) + }() } func (t *Terminal) scrollUp() { @@ -305,7 +307,7 @@ func handleOutputBackspace(t *Terminal) { } func handleOutputBell(t *Terminal) { - go t.ringBell() + t.ringBell() } func handleOutputCarriageReturn(t *Terminal) { diff --git a/term.go b/term.go index e75dc82..f26bbbb 100644 --- a/term.go +++ b/term.go @@ -243,7 +243,7 @@ func (t *Terminal) Resize(s fyne.Size) { } t.onConfigure() - go t.updatePTYSize() + t.updatePTYSize() } // SetDebug turns on output about terminal codes and other errors if the parameter is `true`. @@ -362,8 +362,7 @@ func (t *Terminal) run() { // wait for cmd (shell) to exit, populates ProcessState.ExitCode t.cmd.Wait() } - // this is the pre-go 1.13 way to check for the read failing (terminal closed) - if err.Error() == "EOF" { + if err == io.EOF || err.Error() == "EOF" { break // term exit on macOS } else if err, ok := err.(*os.PathError); ok && err.Err.Error() == "input/output error" { break // broken pipe, terminal exit @@ -378,10 +377,13 @@ func (t *Terminal) run() { fullBuf = append(leftOver, buf[:num]...) num += lenLeftOver } - leftOver = t.handleOutput(fullBuf[:num]) - if len(leftOver) == 0 { - t.Refresh() - } + + fyne.DoAndWait(func() { + leftOver = t.handleOutput(fullBuf[:num]) + if len(leftOver) == 0 { + t.Refresh() + } + }) } }