119 lines
2.3 KiB
Go
119 lines
2.3 KiB
Go
package gioui
|
|
|
|
import (
|
|
"context"
|
|
"sync/atomic"
|
|
|
|
"forge.cadoles.com/arcad/arcast/pkg/browser"
|
|
"gioui.org/app"
|
|
"gioui.org/f32"
|
|
"gioui.org/layout"
|
|
"github.com/gioui-plugins/gio-plugins/webviewer"
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
)
|
|
|
|
type Browser struct {
|
|
window *app.Window
|
|
tag int
|
|
|
|
url *atomic.Value
|
|
changed *atomic.Bool
|
|
status *atomic.Value
|
|
title *atomic.Value
|
|
}
|
|
|
|
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.Store(ev.Title)
|
|
case webviewer.NavigationEvent:
|
|
b.url.Store(ev.URL)
|
|
}
|
|
}
|
|
|
|
ctx := context.Background()
|
|
logger.Debug(ctx, "drawing")
|
|
|
|
webviewer.WebViewOp{Tag: &b.tag}.Push(gtx.Ops)
|
|
|
|
webviewer.OffsetOp{
|
|
Point: f32.Point{
|
|
X: 0,
|
|
Y: 0,
|
|
},
|
|
}.Add(gtx.Ops)
|
|
|
|
webviewer.RectOp{
|
|
Size: f32.Point{
|
|
X: float32(gtx.Constraints.Max.X),
|
|
Y: float32(gtx.Constraints.Max.Y),
|
|
},
|
|
}.Add(gtx.Ops)
|
|
|
|
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.url.Store(url)
|
|
b.changed.Store(true)
|
|
b.status.Store(browser.StatusCasting)
|
|
|
|
b.window.Invalidate()
|
|
|
|
return nil
|
|
}
|
|
|
|
// Status implements browser.Browser.
|
|
func (b *Browser) Status() (browser.Status, error) {
|
|
return b.status.Load().(browser.Status), nil
|
|
}
|
|
|
|
// Title implements browser.Browser.
|
|
func (b *Browser) Title() (string, error) {
|
|
return b.title.Load().(string), nil
|
|
}
|
|
|
|
// URL implements browser.Browser.
|
|
func (b *Browser) URL() (string, error) {
|
|
return b.url.Load().(string), nil
|
|
}
|
|
|
|
// Reset implements browser.Browser.
|
|
func (b *Browser) Reset(url string) error {
|
|
b.url.Store(url)
|
|
b.changed.Store(true)
|
|
b.status.Store(browser.StatusIdle)
|
|
|
|
b.window.Invalidate()
|
|
|
|
return nil
|
|
}
|
|
|
|
func NewBrowser(window *app.Window) *Browser {
|
|
b := &Browser{
|
|
window: window,
|
|
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{}
|