forked from marcusolsson/tui-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.go
89 lines (72 loc) · 2.08 KB
/
widget.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
77
78
79
80
81
82
83
84
85
86
87
88
89
package tui
import "image"
// SizePolicy determines the space occupied by a widget.
type SizePolicy int
const (
// Preferred interprets the size hint as the preferred size.
Preferred SizePolicy = iota
// Minimum allows the widget to shrink down to the size hint.
Minimum
// Maximum allows the widget to grow up to the size hint.
Maximum
// Expanding makes the widget expand to the available space.
Expanding
)
// Widget defines common operations on widgets.
type Widget interface {
Draw(p *Painter)
MinSizeHint() image.Point
Size() image.Point
SizeHint() image.Point
SizePolicy() (SizePolicy, SizePolicy)
Resize(size image.Point)
OnKeyEvent(ev KeyEvent)
SetFocused(bool)
IsFocused() bool
}
// WidgetBase defines base attributes and operations for all widgets.
type WidgetBase struct {
size image.Point
sizePolicyX SizePolicy
sizePolicyY SizePolicy
focused bool
}
// Draw is an empty operation to fulfill the Widget interface.
func (w *WidgetBase) Draw(p *Painter) {
}
// SetFocused focuses the widget.
func (w *WidgetBase) SetFocused(f bool) {
w.focused = f
}
// IsFocused returns whether the widget is focused.
func (w *WidgetBase) IsFocused() bool {
return w.focused
}
// MinSizeHint returns the size below which the widget cannot shrink.
func (w *WidgetBase) MinSizeHint() image.Point {
return image.Point{1, 1}
}
// Size returns the current size of the widget.
func (w *WidgetBase) Size() image.Point {
return w.size
}
// SizeHint returns the size hint of the widget.
func (w *WidgetBase) SizeHint() image.Point {
return image.Point{}
}
// SetSizePolicy sets the size policy for horizontal and vertical directions.
func (w *WidgetBase) SetSizePolicy(h, v SizePolicy) {
w.sizePolicyX = h
w.sizePolicyY = v
}
// SizePolicy returns the current size policy.
func (w *WidgetBase) SizePolicy() (SizePolicy, SizePolicy) {
return w.sizePolicyX, w.sizePolicyY
}
// Resize sets the size of the widget.
func (w *WidgetBase) Resize(size image.Point) {
w.size = size
}
// OnKeyEvent is an empty operation to fulfill the Widget interface.
func (w *WidgetBase) OnKeyEvent(ev KeyEvent) {
}