2023-12-13 20:07:22 +01:00
|
|
|
package gioui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-04-26 09:35:16 +02:00
|
|
|
"sync/atomic"
|
2023-12-13 20:07:22 +01:00
|
|
|
|
|
|
|
"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
|
|
|
|
|
2024-04-26 09:35:16 +02:00
|
|
|
url *atomic.Value
|
|
|
|
changed *atomic.Bool
|
|
|
|
status *atomic.Value
|
|
|
|
title *atomic.Value
|
2023-12-13 20:07:22 +01:00
|
|
|
}
|
|
|
|
|
2024-04-26 09:35:16 +02:00
|
|
|
func (b *Browser) Layout(gtx layout.Context) layout.Dimensions {
|
2023-12-13 20:07:22 +01:00
|
|
|
events := gtx.Events(&b.tag)
|
|
|
|
for _, evt := range events {
|
|
|
|
switch ev := evt.(type) {
|
|
|
|
case webviewer.TitleEvent:
|
2024-04-26 09:35:16 +02:00
|
|
|
b.title.Store(ev.Title)
|
2023-12-13 20:07:22 +01:00
|
|
|
case webviewer.NavigationEvent:
|
2024-04-26 09:35:16 +02:00
|
|
|
b.url.Store(ev.URL)
|
2023-12-13 20:07:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
logger.Debug(ctx, "drawing")
|
|
|
|
|
|
|
|
webviewer.WebViewOp{Tag: &b.tag}.Push(gtx.Ops)
|
|
|
|
|
2024-04-23 09:59:29 +02:00
|
|
|
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)
|
2023-12-13 20:07:22 +01:00
|
|
|
|
2024-04-26 09:35:16 +02:00
|
|
|
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)
|
2023-12-13 20:07:22 +01:00
|
|
|
}
|
2024-04-26 09:35:16 +02:00
|
|
|
|
|
|
|
return layout.Dimensions{Size: gtx.Constraints.Max}
|
2023-12-13 20:07:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load implements browser.Browser.
|
|
|
|
func (b *Browser) Load(url string) error {
|
2024-04-26 09:35:16 +02:00
|
|
|
b.url.Store(url)
|
|
|
|
b.changed.Store(true)
|
|
|
|
b.status.Store(browser.StatusCasting)
|
2023-12-13 20:07:22 +01:00
|
|
|
|
|
|
|
b.window.Invalidate()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status implements browser.Browser.
|
|
|
|
func (b *Browser) Status() (browser.Status, error) {
|
2024-04-26 09:35:16 +02:00
|
|
|
return b.status.Load().(browser.Status), nil
|
2023-12-13 20:07:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Title implements browser.Browser.
|
|
|
|
func (b *Browser) Title() (string, error) {
|
2024-04-26 09:35:16 +02:00
|
|
|
return b.title.Load().(string), nil
|
2023-12-13 20:07:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// URL implements browser.Browser.
|
|
|
|
func (b *Browser) URL() (string, error) {
|
2024-04-26 09:35:16 +02:00
|
|
|
return b.url.Load().(string), nil
|
2023-12-13 20:07:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset implements browser.Browser.
|
|
|
|
func (b *Browser) Reset(url string) error {
|
2024-04-26 09:35:16 +02:00
|
|
|
b.url.Store(url)
|
|
|
|
b.changed.Store(true)
|
|
|
|
b.status.Store(browser.StatusIdle)
|
2023-12-13 20:07:22 +01:00
|
|
|
|
|
|
|
b.window.Invalidate()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBrowser(window *app.Window) *Browser {
|
2024-04-26 09:35:16 +02:00
|
|
|
b := &Browser{
|
2023-12-13 20:07:22 +01:00
|
|
|
window: window,
|
2024-04-26 09:35:16 +02:00
|
|
|
url: &atomic.Value{},
|
|
|
|
changed: &atomic.Bool{},
|
|
|
|
status: &atomic.Value{},
|
|
|
|
title: &atomic.Value{},
|
2023-12-13 20:07:22 +01:00
|
|
|
}
|
2024-04-26 09:35:16 +02:00
|
|
|
|
|
|
|
b.url.Store("")
|
|
|
|
b.title.Store("")
|
|
|
|
b.changed.Store(false)
|
|
|
|
b.status.Store(browser.StatusIdle)
|
|
|
|
|
|
|
|
return b
|
2023-12-13 20:07:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ browser.Browser = &Browser{}
|