ref: f8c9bd648275f2b20c6b2656dfcc5a1800cde026
parent: 554ea7adc9d345f391bcf1a720939ecb6c262009
author: Philip Silva <[email protected]>
date: Wed Feb 3 18:37:36 EST 2021
Remember scroll position after js click
--- a/browser/browser.go
+++ b/browser/browser.go
@@ -50,7 +50,7 @@
buf []byte
})
var log *logger.Logger
-var scroller *duit.Scroll
+var scroller *Scroll
var display *draw.Display
func SetLogger(l *logger.Logger) {
@@ -493,8 +493,10 @@
return
}
+ offset := scroller.Offset
browser.Website.html = res
browser.Website.layout(browser, ClickRelayout)
+ scroller.Offset = offset
dui.MarkLayout(dui.Top.UI)
dui.MarkDraw(dui.Top.UI)
dui.Render()
@@ -1054,7 +1056,7 @@
switch v := ui.(type) {
case nil:
panic("null")
- case *duit.Scroll:
+ case *Scroll:
traverseTree(r+1, v.Kid.UI, f)
case *duit.Box:
for _, kid := range v.Kids {
--- a/browser/scroll.go
+++ b/browser/scroll.go
@@ -37,7 +37,7 @@
barR image.Rectangle
barActiveR image.Rectangle
childR image.Rectangle
- offset int // current scroll offset in pixels
+ Offset int // current scroll offset in pixels
img *draw.Image // for child to draw on
scrollbarSize int
lastMouseUI duit.UI
@@ -113,7 +113,7 @@
barR := ui.barR.Add(orig)
img.Draw(barR, bg, nil, image.ZP)
barH := h * h / uih
- barY := ui.offset * h / uih
+ barY := ui.Offset * h / uih
ui.barActiveR = ui.barR
ui.barActiveR.Min.Y += barY
ui.barActiveR.Max.Y = ui.barActiveR.Min.Y + barH
@@ -143,7 +143,7 @@
} else if ui.Kid.Draw == duit.Dirty {
ui.img.Draw(ui.img.R, dui.Background, nil, image.ZP)
}
- m.Point = m.Point.Add(image.Pt(-ui.childR.Min.X, ui.offset))
+ m.Point = m.Point.Add(image.Pt(-ui.childR.Min.X, ui.Offset))
if ui.Kid.Draw != duit.Clean {
if force {
ui.Kid.Draw = duit.Dirty
@@ -151,15 +151,15 @@
ui.Kid.UI.Draw(dui, &ui.Kid, ui.img, image.ZP, m, ui.Kid.Draw == duit.Dirty)
ui.Kid.Draw = duit.Clean
}
- img.Draw(ui.childR.Add(orig), ui.img, nil, image.Pt(0, ui.offset))
+ img.Draw(ui.childR.Add(orig), ui.img, nil, image.Pt(0, ui.Offset))
}
func (ui *Scroll) scroll(delta int) bool {
- o := ui.offset
- ui.offset += delta
- ui.offset = maximum(0, ui.offset)
- ui.offset = minimum(ui.offset, maximum(0, ui.Kid.R.Dy()-ui.childR.Dy()))
- return o != ui.offset
+ o := ui.Offset
+ ui.Offset += delta
+ ui.Offset = maximum(0, ui.Offset)
+ ui.Offset = minimum(ui.Offset, maximum(0, ui.Kid.R.Dy()-ui.childR.Dy()))
+ return o != ui.Offset
}
func (ui *Scroll) scrollKey(k rune) (consumed bool) {
@@ -191,12 +191,12 @@
case duit.Button1:
return ui.scroll(-m.Y)
case duit.Button2:
- offset := m.Y * ui.Kid.R.Dy() / ui.barR.Dy()
- offsetMax := ui.Kid.R.Dy() - ui.childR.Dy()
- offset = maximum(0, minimum(offset, offsetMax))
- o := ui.offset
- ui.offset = offset
- return o != ui.offset
+ Offset := m.Y * ui.Kid.R.Dy() / ui.barR.Dy()
+ OffsetMax := ui.Kid.R.Dy() - ui.childR.Dy()
+ Offset = maximum(0, minimum(Offset, OffsetMax))
+ o := ui.Offset
+ ui.Offset = Offset
+ return o != ui.Offset
case duit.Button3:
return ui.scroll(m.Y)
}
@@ -223,9 +223,9 @@
}
if m.Point.In(ui.childR) {
nOrigM := origM
- nOrigM.Point = nOrigM.Point.Add(image.Pt(-ui.scrollbarSize, ui.offset))
+ nOrigM.Point = nOrigM.Point.Add(image.Pt(-ui.scrollbarSize, ui.Offset))
nm := m
- nm.Point = nm.Point.Add(image.Pt(-ui.scrollbarSize, ui.offset))
+ nm.Point = nm.Point.Add(image.Pt(-ui.scrollbarSize, ui.Offset))
r = ui.Kid.UI.Mouse(dui, &ui.Kid, nm, nOrigM, image.ZP)
ui.warpScroll(dui, self, r.Warp, orig)
scrolled := false
@@ -256,7 +256,7 @@
}
}
if m.Point.In(ui.childR) {
- m.Point = m.Point.Add(image.Pt(-ui.scrollbarSize, ui.offset))
+ m.Point = m.Point.Add(image.Pt(-ui.scrollbarSize, ui.Offset))
r = ui.Kid.UI.Key(dui, &ui.Kid, k, m, image.ZP)
ui.warpScroll(dui, self, r.Warp, orig)
scrolled := false
@@ -274,13 +274,13 @@
return
}
- offset := ui.offset
- if warp.Y < ui.offset {
- ui.offset = maximum(0, warp.Y-dui.Scale(40))
- } else if warp.Y > ui.offset+ui.r.Dy() {
- ui.offset = minimum(ui.Kid.R.Dy()-ui.r.Dy(), warp.Y+dui.Scale(40)-ui.r.Dy())
+ Offset := ui.Offset
+ if warp.Y < ui.Offset {
+ ui.Offset = maximum(0, warp.Y-dui.Scale(40))
+ } else if warp.Y > ui.Offset+ui.r.Dy() {
+ ui.Offset = minimum(ui.Kid.R.Dy()-ui.r.Dy(), warp.Y+dui.Scale(40)-ui.r.Dy())
}
- if offset != ui.offset {
+ if Offset != ui.Offset {
if self != nil {
self.Draw = duit.Dirty
} else {
@@ -287,7 +287,7 @@
dui.MarkDraw(ui)
}
}
- warp.Y -= ui.offset
+ warp.Y -= ui.Offset
warp.X += orig.X + ui.scrollbarSize
warp.Y += orig.Y
}
@@ -336,7 +336,7 @@
}
func (ui *Scroll) Print(self *duit.Kid, indent int) {
- what := fmt.Sprintf("Scroll offset=%d childR=%v", ui.offset, ui.childR)
+ what := fmt.Sprintf("Scroll Offset=%d childR=%v", ui.Offset, ui.childR)
duit.PrintUI(what, self, indent)
ui.Kid.UI.Print(&ui.Kid, indent+1)
}
--- a/browser/website.go
+++ b/browser/website.go
@@ -150,7 +150,7 @@
body := grep(doc, "body")
log.Printf("Layout website...")
- scroller = duit.NewScroll(
+ scroller = NewScroll(
NodeToBox(0, browser, nodes.NewNodeTree(body, style.Map{}, nodeMap, &nodes.Node{})),
)
numElements := 0
@@ -161,7 +161,7 @@
log.Printf("Layouting done (%v elements created)", numElements)
if numElements < 10 {
log.Errorf("Less than 10 elements layouted, seems css processing failed. Will layout without css")
- scroller = duit.NewScroll(
+ scroller = NewScroll(
NodeToBox(0, browser, nodes.NewNodeTree(body, style.Map{}, make(map[*html.Node]style.Map), nil)),
)
w.UI = scroller