forked from marcusolsson/tui-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scroll_area.go
61 lines (48 loc) · 1.29 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
package tui
import (
"image"
)
var _ Widget = &ScrollArea{}
// ScrollArea is a widget to fill out space.
type ScrollArea struct {
WidgetBase
Widget Widget
topLeft image.Point
}
// 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.ZP
}
// SizeHint returns the size hint of the underlying widget.
func (s *ScrollArea) SizeHint() image.Point {
return image.Pt(15, 8)
}
// SizePolicy returns the default layout behavior.
func (s *ScrollArea) SizePolicy() (SizePolicy, SizePolicy) {
return Preferred, Preferred
}
// Scroll shifts the views over the content.
func (s *ScrollArea) Scroll(dx, dy int) {
s.topLeft.X += dx
s.topLeft.Y += dy
}
// 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.WidgetBase.Resize(size)
s.Widget.Resize(s.Widget.SizeHint())
}