-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.go
More file actions
78 lines (58 loc) · 1.73 KB
/
widget.go
File metadata and controls
78 lines (58 loc) · 1.73 KB
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
package orvyn
import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/halsten-dev/orvyn/theme"
)
// Widget interface defines behaviour of a widget.
type Widget interface {
// Init is called on the Widget when entering a Screen that contains it.
Init() tea.Cmd
// SetStyle allows to set the style of the base widget. By default theme.BlurredWidgetStyleID.
SetStyle(lipgloss.Style)
// GetStyle returns the widget style for custom rendering.
GetStyle() lipgloss.Style
// GetContentSize must be used when rendering the widget to get the real available size for the widgets content.
// Borders of the style have been taken into account.
GetContentSize() Size
// Updatable so Widget can be updated.
Updatable
// Renderable so Widget can be rendered.
Renderable
}
// BaseWidget helps building custom widgets.
type BaseWidget struct {
BaseRenderable
style lipgloss.Style
contentSize Size
}
// NewBaseWidget creates and returns a new BaseWidget.
func NewBaseWidget() BaseWidget {
w := BaseWidget{}
w.BaseRenderable = NewBaseRenderable()
w.style = GetTheme().Style(theme.BlurredWidgetStyleID)
return w
}
func (b *BaseWidget) Init() tea.Cmd {
return nil
}
func (b *BaseWidget) Update(msg tea.Msg) tea.Cmd {
return nil
}
func (b *BaseWidget) Resize(size Size) {
b.BaseRenderable.Resize(size)
size.Width -= b.style.GetHorizontalFrameSize()
size.Height -= b.style.GetVerticalFrameSize()
size.Width = max(size.Width, 0)
size.Height = max(size.Height, 0)
b.contentSize = size
}
func (b *BaseWidget) GetContentSize() Size {
return b.contentSize
}
func (b *BaseWidget) SetStyle(style lipgloss.Style) {
b.style = style
}
func (b *BaseWidget) GetStyle() lipgloss.Style {
return b.style
}