ref: ccb87a870380861973edfe358e9df1b163a50eb6
parent: 3d58c884fa97567ebb1b8b47bc8ceb8c75b2f680
author: Philip Silva <philip.silva@protonmail.com>
date: Fri Apr 2 10:20:14 EDT 2021
history remembers scroll position
--- a/browser/browser.go
+++ b/browser/browser.go
@@ -13,6 +13,7 @@
"net/http/cookiejar"
"net/url"
"github.com/psilva261/opossum"
+ "github.com/psilva261/opossum/browser/history"
"github.com/psilva261/opossum/img"
"github.com/psilva261/opossum/logger"
"github.com/psilva261/opossum/nodes"
@@ -1266,37 +1267,8 @@
}
}
-type History struct {
- urls []*url.URL
-}
-
-func (h History) URL() *url.URL {
- return h.urls[len(h.urls)-1]
-}
-
-func (h *History) Push(u *url.URL) {
- if len(h.urls) > 0 && h.urls[len(h.urls)-1].String() == u.String() {
- return
- }
- h.urls = append(h.urls, u)
-}
-
-func (h *History) Back() {
- if len(h.urls) > 1 {
- h.urls = h.urls[:len(h.urls)-1]
- }
-}
-
-func (h *History) String() string {
- addrs := make([]string, len(h.urls))
- for i, u := range h.urls {
- addrs[i] = u.String()
- }
- return strings.Join(addrs, ", ")
-}
-
type Browser struct {
- History
+ history.History
dui *duit.DUI
Website *Website
StatusBar *duit.Label
@@ -1348,7 +1320,7 @@
if err != nil {
log.Fatalf("parse: %v", err)
}
- b.History.Push(u)
+ b.History.Push(u, 0)
buf, _, err := b.get(u, true)
if err != nil {
@@ -1399,7 +1371,7 @@
}
func (b *Browser) Back() (e duit.Event) {
- if len(b.History.urls) > 0 && !b.loading {
+ if !b.loading {
b.loading = true
b.History.Back()
b.LocationField.Text = b.History.URL().String()
@@ -1497,6 +1469,7 @@
}
})
PrintTree(b.Website.UI)
+ scroller.Offset = b.History.Scroll()
dui.MarkLayout(dui.Top.UI)
dui.MarkDraw(dui.Top.UI)
dui.Render()
@@ -1564,7 +1537,11 @@
buf = contentType.Utf8(buf)
}
if isNewOrigin {
- b.History.Push(resp.Request.URL)
+ of := 0
+ if scroller != nil {
+ of = scroller.Offset
+ }
+ b.History.Push(resp.Request.URL, of)
log.Printf("b.History is now %s", b.History.String())
b.LocationField.Text = b.URL().String()
}
@@ -1587,7 +1564,7 @@
return nil, opossum.ContentType{}, fmt.Errorf("error loading %v: %w", uri, err)
}
defer resp.Body.Close()
- b.History.Push(resp.Request.URL)
+ b.History.Push(resp.Request.URL, scroller.Offset)
buf, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, opossum.ContentType{}, fmt.Errorf("error reading")
--- a/browser/browser_test.go
+++ b/browser/browser_test.go
@@ -127,7 +127,7 @@
if err != nil {
panic(err.Error())
}
- b.History.Push(origin)
+ b.History.Push(origin, 0)
res, err := b.LinkedUrl(i.href)
if err != nil {
panic(err.Error())
@@ -171,7 +171,7 @@
if err != nil {
log.Fatalf("parse: %v", err)
}
- b.History.Push(u)
+ b.History.Push(u, 0)
nt := nodes.NewNodeTree(body, style.Map{}, nodeMap, nil)
boxed := NodeToBox(0, b, nt)
numInputs := 0
@@ -201,7 +201,7 @@
if err != nil {
return nil, nil, fmt.Errorf("parse url: %w", err)
}
- b.History.Push(u)
+ b.History.Push(u, 0)
nm, err := style.FetchNodeMap(doc, style.AddOnCSS, 1280)
if err != nil {
return nil, nil, fmt.Errorf("FetchNodeMap: %w", err)
@@ -387,27 +387,6 @@
el := NewTextArea(ta)
// Trigger key press to trigger call to changed
el.Key(nil, nil, 'a', draw.Mouse{}, image.Point{})
-}
-
-func TestHistoryNoDup(t *testing.T) {
- h := History{}
- uris := []string{
- "https://example.com",
- "https://example.com/a",
- "https://example.com/a",
- "https://example.com/b",
- "https://example.com/b",
- }
- for _, uri := range uris {
- u, err := url.Parse(uri)
- if err != nil {
- t.Error()
- }
- h.Push(u)
- }
- if len(h.urls) != 3 {
- t.Error()
- }
}
func TestNewPicture(t *testing.T) {
--- /dev/null
+++ b/browser/history/history.go
@@ -1,0 +1,52 @@
+package history
+
+import (
+ "net/url"
+ "strings"
+)
+
+type History struct {
+ items []Item
+}
+
+func (h History) URL() *url.URL {
+ return h.items[len(h.items)-1].URL
+}
+
+func (h *History) Push(u *url.URL, oldScroll int) {
+ if len(h.items) > 0 {
+ if h.items[len(h.items)-1].URL.String() == u.String() {
+ return
+ }
+ h.setScroll(oldScroll)
+ }
+ it := Item{u, 0}
+ h.items = append(h.items, it)
+}
+
+func (h *History) Back() {
+ if len(h.items) > 1 {
+ h.items = h.items[:len(h.items)-1]
+ }
+}
+
+func (h *History) String() string {
+ addrs := make([]string, len(h.items))
+ for i, it := range h.items {
+ addrs[i] = it.URL.String()
+ }
+ return strings.Join(addrs, ", ")
+}
+
+func (h *History) Scroll() int {
+ return h.items[len(h.items)-1].Scroll
+}
+
+func (h *History) setScroll(s int) {
+ h.items[len(h.items)-1].Scroll = s
+}
+
+type Item struct {
+ *url.URL
+ Scroll int
+}
--- /dev/null
+++ b/browser/history/history_test.go
@@ -1,0 +1,27 @@
+package history
+
+import (
+ "net/url"
+ "testing"
+)
+
+func TestNoDup(t *testing.T) {
+ h := History{}
+ uris := []string{
+ "https://example.com",
+ "https://example.com/a",
+ "https://example.com/a",
+ "https://example.com/b",
+ "https://example.com/b",
+ }
+ for _, uri := range uris {
+ u, err := url.Parse(uri)
+ if err != nil {
+ t.Error()
+ }
+ h.Push(u, 0)
+ }
+ if len(h.items) != 3 {
+ t.Error()
+ }
+}