From 35585959f5fa786e28a3a69837ab996250c529af Mon Sep 17 00:00:00 2001 From: William Petit Date: Fri, 26 Apr 2024 09:35:16 +0200 Subject: [PATCH] feat(android): do not use sync.Mutex --- cmd/mobile/main.go | 6 +-- go.mod | 4 +- go.sum | 7 +--- pkg/browser/gioui/browser.go | 81 ++++++++++++++++-------------------- 4 files changed, 40 insertions(+), 58 deletions(-) diff --git a/cmd/mobile/main.go b/cmd/mobile/main.go index 112b79c..15173ae 100644 --- a/cmd/mobile/main.go +++ b/cmd/mobile/main.go @@ -15,7 +15,6 @@ import ( "gioui.org/layout" "gioui.org/op" "github.com/gioui-plugins/gio-plugins/plugin" - "github.com/gioui-plugins/gio-plugins/webviewer/webview" "github.com/pkg/errors" "gitlab.com/wpetit/goweb/logger" ) @@ -25,9 +24,9 @@ const packageName = "com.cadoles.arcast_player" func main() { ctx := context.Background() - webview.SetDebug(true) window := app.NewWindow( app.Fullscreen.Option(), + app.AnyOrientation.Option(), ) browser := gioui.NewBrowser(window) @@ -37,7 +36,6 @@ func main() { for { evt := window.NextEvent() plugin.Install(window, evt) - switch evt := evt.(type) { case system.DestroyEvent: os.Exit(0) @@ -45,7 +43,7 @@ func main() { case system.FrameEvent: gtx := layout.NewContext(ops, evt) browser.Layout(gtx) - evt.Frame(gtx.Ops) + evt.Frame(ops) } } }() diff --git a/go.mod b/go.mod index 7b47b72..4db6730 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,7 @@ go 1.21.4 require ( gioui.org v0.4.1 - github.com/davecgh/go-spew v1.1.1 - github.com/gioui-plugins/gio-plugins v0.0.0-20230625001848-8f18aae6c91c + github.com/gioui-plugins/gio-plugins v0.0.0-20240323070753-3331d8c2df5d github.com/go-chi/cors v1.2.1 github.com/gorilla/websocket v1.5.1 github.com/grandcat/zeroconf v1.0.1-0.20230119201135-e4f60f8407b1 @@ -28,7 +27,6 @@ require ( github.com/go-playground/universal-translator v0.18.0 // indirect github.com/go-text/typesetting v0.0.0-20230803102845-24e03d8b5372 // indirect github.com/inkeliz/go_inkwasm v0.0.0-20220912074516-049d3472c98a // indirect - github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e // indirect github.com/jedib0t/go-pretty/v6 v6.4.9 // indirect github.com/leodido/go-urn v1.2.1 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect diff --git a/go.sum b/go.sum index 7da9c7d..3b0db09 100644 --- a/go.sum +++ b/go.sum @@ -32,8 +32,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/gioui-plugins/gio-plugins v0.0.0-20230625001848-8f18aae6c91c h1:naFDaf0CvDEYZ3Zpxx20DY/cCvBQqKwsV7ZzBt3M/bU= -github.com/gioui-plugins/gio-plugins v0.0.0-20230625001848-8f18aae6c91c/go.mod h1:nBuRsi6udr2x6eorarLHtRkoRaWBICt+WzaE7zQXgYY= +github.com/gioui-plugins/gio-plugins v0.0.0-20240323070753-3331d8c2df5d h1:8b7owUJ8sNmgqEk+1d7ylr3TCH3vliCvY/6ycfize8o= +github.com/gioui-plugins/gio-plugins v0.0.0-20240323070753-3331d8c2df5d/go.mod h1:3XVleuCdPpdajFL+ASh2wmXZNskitXQQ4jhVss0VHZg= github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk= github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4= @@ -62,8 +62,6 @@ github.com/inkeliz/go_inkwasm v0.0.0-20220912074516-049d3472c98a h1:uZklbtdSPrDL github.com/inkeliz/go_inkwasm v0.0.0-20220912074516-049d3472c98a/go.mod h1:LPI3Qojj7OgTyc2R4RPB6BuMSgjoOXCObwnDzz1SOVk= github.com/jaevor/go-nanoid v1.3.0 h1:nD+iepesZS6pr3uOVf20vR9GdGgJW1HPaR46gtrxzkg= github.com/jaevor/go-nanoid v1.3.0/go.mod h1:SI+jFaPuddYkqkVQoNGHs81navCtH388TcrH0RqFKgY= -github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e h1:Q3+PugElBCf4PFpxhErSzU3/PY5sFL5Z6rfv4AbGAck= -github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e/go.mod h1:alcuEEnZsY1WQsagKhZDsoPCRoOijYqhZvPwLG0kzVs= github.com/jedib0t/go-pretty/v6 v6.4.9 h1:vZ6bjGg2eBSrJn365qlxGcaWu09Id+LHtrfDWlB2Usc= github.com/jedib0t/go-pretty/v6 v6.4.9/go.mod h1:Ndk3ase2CkQbXLLNf5QDHoYb6J9WtVfmHZu9n8rk2xs= github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= @@ -144,7 +142,6 @@ golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20200810151505-1b9f1253b3ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/pkg/browser/gioui/browser.go b/pkg/browser/gioui/browser.go index 0a0f939..7917dcb 100644 --- a/pkg/browser/gioui/browser.go +++ b/pkg/browser/gioui/browser.go @@ -2,7 +2,7 @@ package gioui import ( "context" - "sync" + "sync/atomic" "forge.cadoles.com/arcad/arcast/pkg/browser" "gioui.org/app" @@ -16,26 +16,20 @@ type Browser struct { window *app.Window tag int - url string - changed bool - - status browser.Status - title string - - mutex sync.Mutex + url *atomic.Value + changed *atomic.Bool + status *atomic.Value + title *atomic.Value } -func (b *Browser) Layout(gtx layout.Context) { - b.mutex.Lock() - defer b.mutex.Unlock() - +func (b *Browser) Layout(gtx layout.Context) layout.Dimensions { events := gtx.Events(&b.tag) for _, evt := range events { switch ev := evt.(type) { case webviewer.TitleEvent: - b.title = ev.Title + b.title.Store(ev.Title) case webviewer.NavigationEvent: - b.url = ev.URL + b.url.Store(ev.URL) } } @@ -58,21 +52,20 @@ func (b *Browser) Layout(gtx layout.Context) { }, }.Add(gtx.Ops) - if b.changed { - logger.Debug(ctx, "url changed", logger.F("url", b.url)) - webviewer.NavigateOp{URL: b.url}.Add(gtx.Ops) - b.changed = false + if b.changed.CompareAndSwap(true, false) { + url := b.url.Load().(string) + logger.Debug(ctx, "url changed", logger.F("url", url)) + webviewer.NavigateOp{URL: url}.Add(gtx.Ops) } + + return layout.Dimensions{Size: gtx.Constraints.Max} } // Load implements browser.Browser. func (b *Browser) Load(url string) error { - b.mutex.Lock() - defer b.mutex.Unlock() - - b.url = url - b.changed = true - b.status = browser.StatusCasting + b.url.Store(url) + b.changed.Store(true) + b.status.Store(browser.StatusCasting) b.window.Invalidate() @@ -81,36 +74,24 @@ func (b *Browser) Load(url string) error { // Status implements browser.Browser. func (b *Browser) Status() (browser.Status, error) { - b.mutex.Lock() - defer b.mutex.Unlock() - - return b.status, nil + return b.status.Load().(browser.Status), nil } // Title implements browser.Browser. func (b *Browser) Title() (string, error) { - b.mutex.Lock() - defer b.mutex.Unlock() - - return b.title, nil + return b.title.Load().(string), nil } // URL implements browser.Browser. func (b *Browser) URL() (string, error) { - b.mutex.Lock() - defer b.mutex.Unlock() - - return b.url, nil + return b.url.Load().(string), nil } // Reset implements browser.Browser. func (b *Browser) Reset(url string) error { - b.mutex.Lock() - defer b.mutex.Unlock() - - b.url = url - b.changed = true - b.status = browser.StatusIdle + b.url.Store(url) + b.changed.Store(true) + b.status.Store(browser.StatusIdle) b.window.Invalidate() @@ -118,12 +99,20 @@ func (b *Browser) Reset(url string) error { } func NewBrowser(window *app.Window) *Browser { - return &Browser{ + b := &Browser{ window: window, - url: "", - changed: true, - status: browser.StatusIdle, + url: &atomic.Value{}, + changed: &atomic.Bool{}, + status: &atomic.Value{}, + title: &atomic.Value{}, } + + b.url.Store("") + b.title.Store("") + b.changed.Store(false) + b.status.Store(browser.StatusIdle) + + return b } var _ browser.Browser = &Browser{}