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 123
/
padder.go
71 lines (56 loc) · 1.74 KB
/
padder.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
package tui
import "image"
var _ Widget = &Padder{}
// Padder is a widget to fill out space.
// It adds empty space of a specified size to the outside of its contained Widget.
type Padder struct {
widget Widget
padding image.Point
}
// NewPadder returns a new Padder.
// The enclosed Widget is given horizontal margin of x on the right and x on the left,
// and a vertical margin of y on the top and y on the bottom.
func NewPadder(x, y int, w Widget) *Padder {
return &Padder{
widget: w,
padding: image.Point{x, y},
}
}
// Draw draws the padded widget.
func (p *Padder) Draw(painter *Painter) {
painter.Translate(p.padding.X, p.padding.Y)
defer painter.Restore()
p.widget.Draw(painter)
}
// Size returns the size of the padded widget.
func (p *Padder) Size() image.Point {
return p.widget.Size().Add(p.padding.Mul(2))
}
// MinSizeHint returns the minimum size the widget is allowed to be.
func (p *Padder) MinSizeHint() image.Point {
return p.widget.MinSizeHint().Add(p.padding.Mul(2))
}
// SizeHint returns the recommended size for the padded widget.
func (p *Padder) SizeHint() image.Point {
return p.widget.SizeHint().Add(p.padding.Mul(2))
}
// SizePolicy returns the default layout behavior.
func (p *Padder) SizePolicy() (SizePolicy, SizePolicy) {
return p.widget.SizePolicy()
}
// Resize updates the size of the padded widget.
func (p *Padder) Resize(size image.Point) {
p.widget.Resize(size.Sub(p.padding.Mul(2)))
}
// OnKeyEvent handles key events.
func (p *Padder) OnKeyEvent(ev KeyEvent) {
p.widget.OnKeyEvent(ev)
}
// SetFocused set the focus on the widget.
func (p *Padder) SetFocused(f bool) {
p.widget.SetFocused(f)
}
// IsFocused returns true if the widget is focused.
func (p *Padder) IsFocused() bool {
return p.widget.IsFocused()
}