-
Notifications
You must be signed in to change notification settings - Fork 6
/
widget.go
59 lines (49 loc) · 1.53 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
// Copyright (c) 2016 Company 0, LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package ttk
import (
"errors"
"github.com/nsf/termbox-go"
)
// Widget is the base structure of all widgets.
type Widget struct {
w *Window
x int
y int
}
var (
// ErrWidgetNotRegistered is generated when a NewWidget call is made
// and the widget was not registered. Widgets must be registered so
// that applications may define their own and fully participate in ttk
// rules.
ErrWidgetNotRegistered = errors.New("widget not registered")
// registeredWidgets contains the registered widgets types.
registeredWidgets = make(map[string]func(*Window, int, int) (Widgeter,
error))
)
type Visibility int
const (
VisibilityGet Visibility = iota
VisibilityHide
VisibilityShow
)
// Widgeter is the generic Widget interface. All widgets shall conform to it.
// Since the Widgeter functions are called from queue context the Widget must
// take care to not call blocking queue context calls.
type Widgeter interface {
CanFocus() bool // return true if widget can focus
Focus() // Focus on widget
Render() // Render the widget
Resize() // Resize the widget
KeyHandler(termbox.Event) bool // handle key strokes
Visibility(Visibility) Visibility // show/hide widget
}
// MakeWidget creates a generic Widget structure.
func MakeWidget(w *Window, x, y int) Widget {
return Widget{
w: w,
x: x,
y: y,
}
}