Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 60fce51

Browse files
committed
smoother scrolling
1 parent 4f3915a commit 60fce51

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

main.go

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ var (
5252
pad = image.Pt(15, 15)
5353
tagHeight = fsize*2 + fsize/2 - 2
5454
scrollX = 10
55+
56+
// Records the last time a scroll event was recieved so
57+
// a proper acceleration factor can be applied.
58+
lastscroll time.Time
59+
lastscrolldy int
60+
scrollmul = 1
5561
)
5662

5763
func abs(a int) int {
@@ -113,6 +119,31 @@ var (
113119
ftsize = flag.Int("ftsize", 16, "font size")
114120
)
115121

122+
func smoothscroll(act *win.Win, e mus.ScrollEvent){
123+
dp := act.Loc().Min
124+
sr := act.Buffer().Bounds()
125+
sr.Min.X += 10
126+
sr.Min.Y += 15
127+
dp.X += 10
128+
dp.Y += 15
129+
h := act.Frame.Font.Dy()
130+
if e.Button == -1 {
131+
for i := 0; i != -h*e.Dy; i-=e.Dy{
132+
act.Window().Upload(dp, act.Buffer(), sr.Add(image.Pt(0, i)))
133+
act.Window().Publish()
134+
act.Flush()
135+
}
136+
e.Dy = -e.Dy
137+
} else {
138+
for i := 0; i != h*e.Dy; i+=e.Dy{
139+
act.Window().Upload(dp, act.Buffer(), sr.Add(image.Pt(0, i)))
140+
act.Window().Publish()
141+
act.Flush()
142+
}
143+
}
144+
actTag.Body.Scroll(e.Dy)
145+
act.Refresh()
146+
}
116147
/*
117148
func vgaface() font.Face {
118149
data, err := ioutil.ReadFile("u_vga16.font")
@@ -326,11 +357,27 @@ func main() {
326357
}
327358
ck()
328359
case mus.ScrollEvent:
329-
if e.Button == -1 {
330-
e.Dy = -e.Dy
360+
tm := time.Now()
361+
if e.Dy == 0{
362+
lastscroll = tm
363+
lastscrolldy = 0
364+
continue
331365
}
332-
actTag.Body.Scroll(e.Dy * 2)
333-
ck()
366+
// if tm.Sub(lastscroll) > time.Second/7 {
367+
if text.Region3(int64(lastscrolldy), int64(e.Dy-7), int64(e.Dy+7)) != 0 {
368+
smoothscroll(act, e)
369+
ck()
370+
wind.SendFirst(mus.Drain{})
371+
wind.Send(mus.DrainStop{})
372+
} else {
373+
if e.Button == -1 {
374+
e.Dy = -e.Dy
375+
}
376+
actTag.Body.Scroll(e.Dy)
377+
ck()
378+
}
379+
lastscroll = tm
380+
lastscrolldy = e.Dy
334381
case mus.SweepEvent:
335382
switch context {
336383
case scrollbar:

0 commit comments

Comments
 (0)