shithub: mycel

Download patch

ref: 860953fd9253e756a7427e31af262bf5c942fe14
parent: 3662834cbb526bcf9617b3221892d38a4d58deb7
author: Philip Silva <philip.silva@protonmail.com>
date: Sat Jan 22 18:00:17 EST 2022

Add stop button

--- a/browser/browser.go
+++ b/browser/browser.go
@@ -2,6 +2,7 @@
 
 import (
 	"9fans.net/go/draw"
+	"context"
 	"errors"
 	"fmt"
 	"github.com/psilva261/opossum"
@@ -1495,6 +1496,9 @@
 }
 
 type Browser struct {
+	ctx    context.Context
+	cancel context.CancelFunc
+
 	history.History
 	dui      *duit.DUI
 	Website  *Website
@@ -1605,6 +1609,15 @@
 	}
 }
 
+func (b *Browser) Ctx() context.Context {
+	return b.ctx
+}
+
+func (b *Browser) Cancel() {
+	b.cancel()
+	b.loading = false
+}
+
 func (b *Browser) Loading() bool {
 	return b.loading
 }
@@ -1621,6 +1634,10 @@
 
 // LoadUrl after from location field,
 func (b *Browser) LoadUrl(url *url.URL) (e duit.Event) {
+	if b.cancel != nil {
+		b.cancel()
+	}
+	b.ctx, b.cancel = context.WithCancel(context.Background())
 	b.loading = true
 	go b.loadUrl(url)
 	e.Consumed = true
@@ -1710,7 +1727,7 @@
 
 func (b *Browser) get(uri *url.URL, isNewOrigin bool) (buf []byte, contentType opossum.ContentType, err error) {
 	log.Infof("Get %v", uri.String())
-	req, err := http.NewRequest("GET", uri.String(), nil)
+	req, err := http.NewRequestWithContext(b.ctx, "GET", uri.String(), nil)
 	if err != nil {
 		return
 	}
@@ -1740,7 +1757,7 @@
 func (b *Browser) PostForm(uri *url.URL, data url.Values) (buf []byte, contentType opossum.ContentType, err error) {
 	b.StatusCh <- "Posting..."
 	fb := strings.NewReader(escapeValues(b.Website.ContentType, data).Encode())
-	req, err := http.NewRequest("POST", uri.String(), fb)
+	req, err := http.NewRequestWithContext(b.ctx, "POST", uri.String(), fb)
 	if err != nil {
 		return
 	}
--- a/browser/website.go
+++ b/browser/website.go
@@ -30,6 +30,9 @@
 		browser.StatusCh <- ""
 	}()
 	pass := func(htm string, csss ...string) (*html.Node, map[*html.Node]style.Map) {
+		if f.Ctx().Err() != nil {
+			return nil, nil
+		}
 
 		if debugPrintHtml {
 			log.Printf("%v\n", htm)
@@ -101,6 +104,7 @@
 		fs.SetDOM(nt)
 		log.Infof("JS pipeline start")
 		js.Stop()
+		js.SetFetcher(f)
 		jsProcessed, changed, err := processJS2()
 		if changed && err == nil {
 			htm = jsProcessed
--- a/cmd/opossum/main.go
+++ b/cmd/opossum/main.go
@@ -76,10 +76,9 @@
 func (n *Nav) Render() []*duit.Kid {
 	uis := []duit.UI{
 		&duit.Grid{
-			Columns: 2,
-			Padding: duit.NSpace(2, duit.SpaceXY(5, 3)),
-			Halign:  []duit.Halign{duit.HalignLeft, duit.HalignRight},
-			Valign:  []duit.Valign{duit.ValignMiddle, duit.ValignMiddle},
+			Columns: 3,
+			Halign:  []duit.Halign{duit.HalignLeft, duit.HalignLeft, duit.HalignRight},
+			Valign:  []duit.Valign{duit.ValignMiddle, duit.ValignMiddle, duit.ValignMiddle},
 			Kids: duit.NewKids(
 				&duit.Button{
 					Text:  "Back",
@@ -86,6 +85,16 @@
 					Font:  browser.Style.Font(),
 					Click: b.Back,
 				},
+				&duit.Button{
+					Text:  "Stop",
+					Font:  browser.Style.Font(),
+					Click: func() duit.Event {
+						b.Cancel()
+						return duit.Event{
+							Consumed: true,
+						}
+					},
+				},
 				&duit.Box{
 					Kids: duit.NewKids(
 						n.LocationField,
@@ -157,12 +166,6 @@
 			Text: c.text,
 		},
 	)
-}
-
-type Loading struct{}
-
-func (l *Loading) Render() []*duit.Kid {
-	return nil
 }
 
 func render() {
--- a/js/js.go
+++ b/js/js.go
@@ -26,15 +26,13 @@
 
 var (
 	fetcher opossum.Fetcher
-	nt      *nodes.Node
 
 	service string
 	cancel  context.CancelFunc
 )
 
-func NewJS(html string, fetcher opossum.Fetcher, nn *nodes.Node) {
-	nt = nn
-	return
+func SetFetcher(f opossum.Fetcher) {
+	fetcher = f
 }
 
 func call(fn, cmd string, args ...string) (resp string, err error) {
@@ -64,7 +62,7 @@
 	log.Infof("Start gojafs")
 
 	var ctx context.Context
-	ctx, cancel = context.WithCancel(context.Background())
+	ctx, cancel = context.WithCancel(fetcher.Ctx())
 	cmd := exec.CommandContext(ctx, "gojafs", args...)
 	cmd.Stderr = os.Stderr
 
--- a/js/js_unix.go
+++ b/js/js_unix.go
@@ -20,7 +20,7 @@
 	log.Infof("Init...")
 	conn, err := client.DialService(service)
 	if err != nil {
-		log.Fatalf("dial: %v", err)
+		return fmt.Errorf("dial: %v", err)
 	}
 	u, err := user.Current()
 	if err != nil {
@@ -29,7 +29,7 @@
 	un := u.Username
 	fsys, err = conn.Attach(nil, un, "")
 	if err != nil {
-		log.Fatalf("attach: %v", err)
+		return fmt.Errorf("attach: %v", err)
 	}
 	return
 }
--- a/opossum.go
+++ b/opossum.go
@@ -2,6 +2,7 @@
 
 import (
 	"bytes"
+	"context"
 	"github.com/psilva261/opossum/logger"
 	"golang.org/x/text/encoding"
 	"golang.org/x/text/encoding/htmlindex"
@@ -13,6 +14,7 @@
 )
 
 type Fetcher interface {
+	Ctx() context.Context
 	Origin() *url.URL
 
 	// LinkedUrl relative to current page