feat(android): do not use sync.Mutex
arcad/arcast/pipeline/head This commit is unstable Details

This commit is contained in:
wpetit 2024-04-26 09:35:16 +02:00
parent 0fcf0b6cc0
commit 35585959f5
4 changed files with 40 additions and 58 deletions

View File

@ -15,7 +15,6 @@ import (
"gioui.org/layout" "gioui.org/layout"
"gioui.org/op" "gioui.org/op"
"github.com/gioui-plugins/gio-plugins/plugin" "github.com/gioui-plugins/gio-plugins/plugin"
"github.com/gioui-plugins/gio-plugins/webviewer/webview"
"github.com/pkg/errors" "github.com/pkg/errors"
"gitlab.com/wpetit/goweb/logger" "gitlab.com/wpetit/goweb/logger"
) )
@ -25,9 +24,9 @@ const packageName = "com.cadoles.arcast_player"
func main() { func main() {
ctx := context.Background() ctx := context.Background()
webview.SetDebug(true)
window := app.NewWindow( window := app.NewWindow(
app.Fullscreen.Option(), app.Fullscreen.Option(),
app.AnyOrientation.Option(),
) )
browser := gioui.NewBrowser(window) browser := gioui.NewBrowser(window)
@ -37,7 +36,6 @@ func main() {
for { for {
evt := window.NextEvent() evt := window.NextEvent()
plugin.Install(window, evt) plugin.Install(window, evt)
switch evt := evt.(type) { switch evt := evt.(type) {
case system.DestroyEvent: case system.DestroyEvent:
os.Exit(0) os.Exit(0)
@ -45,7 +43,7 @@ func main() {
case system.FrameEvent: case system.FrameEvent:
gtx := layout.NewContext(ops, evt) gtx := layout.NewContext(ops, evt)
browser.Layout(gtx) browser.Layout(gtx)
evt.Frame(gtx.Ops) evt.Frame(ops)
} }
} }
}() }()

4
go.mod
View File

@ -4,8 +4,7 @@ go 1.21.4
require ( require (
gioui.org v0.4.1 gioui.org v0.4.1
github.com/davecgh/go-spew v1.1.1 github.com/gioui-plugins/gio-plugins v0.0.0-20240323070753-3331d8c2df5d
github.com/gioui-plugins/gio-plugins v0.0.0-20230625001848-8f18aae6c91c
github.com/go-chi/cors v1.2.1 github.com/go-chi/cors v1.2.1
github.com/gorilla/websocket v1.5.1 github.com/gorilla/websocket v1.5.1
github.com/grandcat/zeroconf v1.0.1-0.20230119201135-e4f60f8407b1 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-playground/universal-translator v0.18.0 // indirect
github.com/go-text/typesetting v0.0.0-20230803102845-24e03d8b5372 // 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/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/jedib0t/go-pretty/v6 v6.4.9 // indirect
github.com/leodido/go-urn v1.2.1 // indirect github.com/leodido/go-urn v1.2.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect

7
go.sum
View File

@ -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.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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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-20240323070753-3331d8c2df5d h1:8b7owUJ8sNmgqEk+1d7ylr3TCH3vliCvY/6ycfize8o=
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/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 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= 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= 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/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 h1:nD+iepesZS6pr3uOVf20vR9GdGgJW1HPaR46gtrxzkg=
github.com/jaevor/go-nanoid v1.3.0/go.mod h1:SI+jFaPuddYkqkVQoNGHs81navCtH388TcrH0RqFKgY= 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 h1:vZ6bjGg2eBSrJn365qlxGcaWu09Id+LHtrfDWlB2Usc=
github.com/jedib0t/go-pretty/v6 v6.4.9/go.mod h1:Ndk3ase2CkQbXLLNf5QDHoYb6J9WtVfmHZu9n8rk2xs= 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= 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 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= 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-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-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-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

View File

@ -2,7 +2,7 @@ package gioui
import ( import (
"context" "context"
"sync" "sync/atomic"
"forge.cadoles.com/arcad/arcast/pkg/browser" "forge.cadoles.com/arcad/arcast/pkg/browser"
"gioui.org/app" "gioui.org/app"
@ -16,26 +16,20 @@ type Browser struct {
window *app.Window window *app.Window
tag int tag int
url string url *atomic.Value
changed bool changed *atomic.Bool
status *atomic.Value
status browser.Status title *atomic.Value
title string
mutex sync.Mutex
} }
func (b *Browser) Layout(gtx layout.Context) { func (b *Browser) Layout(gtx layout.Context) layout.Dimensions {
b.mutex.Lock()
defer b.mutex.Unlock()
events := gtx.Events(&b.tag) events := gtx.Events(&b.tag)
for _, evt := range events { for _, evt := range events {
switch ev := evt.(type) { switch ev := evt.(type) {
case webviewer.TitleEvent: case webviewer.TitleEvent:
b.title = ev.Title b.title.Store(ev.Title)
case webviewer.NavigationEvent: 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) }.Add(gtx.Ops)
if b.changed { if b.changed.CompareAndSwap(true, false) {
logger.Debug(ctx, "url changed", logger.F("url", b.url)) url := b.url.Load().(string)
webviewer.NavigateOp{URL: b.url}.Add(gtx.Ops) logger.Debug(ctx, "url changed", logger.F("url", url))
b.changed = false webviewer.NavigateOp{URL: url}.Add(gtx.Ops)
} }
return layout.Dimensions{Size: gtx.Constraints.Max}
} }
// Load implements browser.Browser. // Load implements browser.Browser.
func (b *Browser) Load(url string) error { func (b *Browser) Load(url string) error {
b.mutex.Lock() b.url.Store(url)
defer b.mutex.Unlock() b.changed.Store(true)
b.status.Store(browser.StatusCasting)
b.url = url
b.changed = true
b.status = browser.StatusCasting
b.window.Invalidate() b.window.Invalidate()
@ -81,36 +74,24 @@ func (b *Browser) Load(url string) error {
// Status implements browser.Browser. // Status implements browser.Browser.
func (b *Browser) Status() (browser.Status, error) { func (b *Browser) Status() (browser.Status, error) {
b.mutex.Lock() return b.status.Load().(browser.Status), nil
defer b.mutex.Unlock()
return b.status, nil
} }
// Title implements browser.Browser. // Title implements browser.Browser.
func (b *Browser) Title() (string, error) { func (b *Browser) Title() (string, error) {
b.mutex.Lock() return b.title.Load().(string), nil
defer b.mutex.Unlock()
return b.title, nil
} }
// URL implements browser.Browser. // URL implements browser.Browser.
func (b *Browser) URL() (string, error) { func (b *Browser) URL() (string, error) {
b.mutex.Lock() return b.url.Load().(string), nil
defer b.mutex.Unlock()
return b.url, nil
} }
// Reset implements browser.Browser. // Reset implements browser.Browser.
func (b *Browser) Reset(url string) error { func (b *Browser) Reset(url string) error {
b.mutex.Lock() b.url.Store(url)
defer b.mutex.Unlock() b.changed.Store(true)
b.status.Store(browser.StatusIdle)
b.url = url
b.changed = true
b.status = browser.StatusIdle
b.window.Invalidate() b.window.Invalidate()
@ -118,12 +99,20 @@ func (b *Browser) Reset(url string) error {
} }
func NewBrowser(window *app.Window) *Browser { func NewBrowser(window *app.Window) *Browser {
return &Browser{ b := &Browser{
window: window, window: window,
url: "", url: &atomic.Value{},
changed: true, changed: &atomic.Bool{},
status: browser.StatusIdle, 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{} var _ browser.Browser = &Browser{}