/
element.go
73 lines (62 loc) · 2.02 KB
/
element.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
package termui
import (
"github.com/boombuler/termui/css"
"github.com/nsf/termbox-go"
)
// Gravity defines the horizontal and vertical orientation of an element.
type Gravity byte
const (
// NoGravity -> element is centered horizontal and vertical
NoGravity Gravity = 0
// Top pulls the element to the top
Top Gravity = 1 << iota
// Left pulls the element to the left
Left
// Right pulls the element to the right
Right
// Bottom pulls the element to the top bottom
Bottom
horizontal = Left | Right
vertical = Top | Bottom
)
// Element must be implemented by any UI element.
type Element interface {
css.Styleable
// Measure gets the "wanted" size of the element based on the available size
Measure(availableWidth, availableHeight int) (width int, height int)
// Arrange sets the final size for the Element end tells it to Arrange itself
Arrange(finalWidth, finalHeight int)
// Render renders the element on the given Renderer
Render(fn Renderer)
// Width returns the width of the border
Width() int
// Height returns the height of the element
Height() int
// SetParent sets the parent element of this element
SetParent(e Element)
}
type KeyHandler interface {
Element
// HandleKey is called by the ui system if the element is asked to process a key. It should return true if the key
// was successfully handled and should not be processed by other elements.
HandleKey(k termbox.Key, ch rune) bool
}
// FocusElement is an element which can get the input focus.
type FocusElement interface {
KeyHandler
// SetFocused is called by the ui system to indicate that the element has the focus.
SetFocused(v bool)
}
// BaseElement helps implementing the Element interface for ui elements. It handles classes, the ID and also the parent element.
type BaseElement struct {
css.IDAndClasses
parent Element
}
// SetParent sets the parent element of this element
func (be *BaseElement) SetParent(e Element) {
be.parent = e
}
// Parent returns the parent of the element
func (be *BaseElement) Parent() css.Styleable {
return be.parent
}