This repository has been archived by the owner on Oct 13, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
scroll_area.go
76 lines (60 loc) · 1.66 KB
/
scroll_area.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package tui
import (
"image"
)
var _ Widget = &ScrollArea{}
// ScrollArea is a widget to fill out space.
type ScrollArea struct {
WidgetBase
Widget Widget
topLeft image.Point
autoscroll bool
}
// NewScrollArea returns a new ScrollArea.
func NewScrollArea(w Widget) *ScrollArea {
return &ScrollArea{
Widget: w,
}
}
// MinSizeHint returns the minimum size the widget is allowed to be.
func (s *ScrollArea) MinSizeHint() image.Point {
return image.Point{}
}
// SizeHint returns the size hint of the underlying widget.
func (s *ScrollArea) SizeHint() image.Point {
return image.Pt(15, 8)
}
// Scroll shifts the views over the content.
func (s *ScrollArea) Scroll(dx, dy int) {
s.topLeft.X += dx
s.topLeft.Y += dy
}
// ScrollToBottom ensures the bottom-most part of the scroll area is visible.
func (s *ScrollArea) ScrollToBottom() {
s.topLeft.Y = s.Widget.SizeHint().Y - s.Size().Y
}
// ScrollToTop resets the vertical scroll position.
func (s *ScrollArea) ScrollToTop() {
s.topLeft.Y = 0
}
// SetAutoscrollToBottom makes sure the content is scrolled to bottom on resize.
func (s *ScrollArea) SetAutoscrollToBottom(autoscroll bool) {
s.autoscroll = autoscroll
}
// Draw draws the scroll area.
func (s *ScrollArea) Draw(p *Painter) {
p.Translate(-s.topLeft.X, -s.topLeft.Y)
defer p.Restore()
off := image.Point{s.topLeft.X, s.topLeft.Y}
p.WithMask(image.Rectangle{Min: off, Max: s.Size().Add(off)}, func(p *Painter) {
s.Widget.Draw(p)
})
}
// Resize resizes the scroll area and the underlying widget.
func (s *ScrollArea) Resize(size image.Point) {
s.Widget.Resize(s.Widget.SizeHint())
s.WidgetBase.Resize(size)
if s.autoscroll {
s.ScrollToBottom()
}
}