-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfocusable.go
More file actions
145 lines (110 loc) · 3.64 KB
/
focusable.go
File metadata and controls
145 lines (110 loc) · 3.64 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package orvyn
import (
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/halsten-dev/orvyn/theme"
)
// Focusable interface represents the capacity to get and loose the focus.
type Focusable interface {
// Updatable to be able to update a focusable widget.
Updatable
// Activable to be able to activate or deactivate widget.
Activable
// OnFocus is called to make the widget react to the focus gain.
OnFocus()
// OnBlur is called to make the widget react to the loss of focus.
OnBlur()
// OnEnterInput is called when the widget enters the input mode.
// Input mode means that all the tea.Msg will be managed by the widget.
// Returns a tea.Cmd.
OnEnterInput() tea.Cmd
// OnExitInput is called when the widget exits the input mode.
// Returns a tea.Cmd.
OnExitInput() tea.Cmd
// IsFocused return true if the widget is currently focused.
// A widget can be focused without being in input mode.
IsFocused() bool
// IsInputting return true if the widget is in input mode.
// Input mode means that all the tea.Msg will be managed by the widget.
IsInputting() bool
// GetFocusKeybind can return a *key.Binding if the widget should be
// able to get directly focused with one key.
GetFocusKeybind() *key.Binding
// GetEnterInputKeybind can return a *key.Binding if the widget should be
// able to enter the input mode.
// Input mode means that all the tea.Msg will be managed by the widget.
GetEnterInputKeybind() *key.Binding
// GetExitInputKeybind returns by default "Esc". Can be overridden.
GetExitInputKeybind() key.Binding
// CanExitInputting returns true if the widget can exit his inputting state.
CanExitInputting() bool
// setFocused allows to set the focused value.
setFocused(bool)
// setInputting allows to set the inputting value.
setInputting(bool)
// SetFocusedStyle allows to set the style when the widget is focused. By default theme.FocusedWidgetStyleID.
SetFocusedStyle(lipgloss.Style)
// SetBlurredStyle allows to set the style when the widget is blurred. By default theme.BlurredWidgetStyleID.
SetBlurredStyle(lipgloss.Style)
}
// BaseFocusable can be integrated to a Widget to make it focusable.
type BaseFocusable struct {
widget Widget
focused bool
inputting bool
focusedStyle lipgloss.Style
blurredStyle lipgloss.Style
}
func NewBaseFocusable(widget Widget) BaseFocusable {
t := GetTheme()
return BaseFocusable{
widget: widget,
focused: false,
inputting: false,
focusedStyle: t.Style(theme.FocusedWidgetStyleID),
blurredStyle: t.Style(theme.BlurredWidgetStyleID),
}
}
func (b *BaseFocusable) OnFocus() {
b.widget.SetStyle(b.focusedStyle)
}
func (b *BaseFocusable) OnBlur() {
b.widget.SetStyle(b.blurredStyle)
}
func (b *BaseFocusable) IsFocused() bool {
return b.focused
}
func (b *BaseFocusable) IsInputting() bool {
return b.inputting
}
func (b *BaseFocusable) OnEnterInput() tea.Cmd {
return nil
}
func (b *BaseFocusable) OnExitInput() tea.Cmd {
return nil
}
func (b *BaseFocusable) GetFocusKeybind() *key.Binding {
return nil
}
func (b *BaseFocusable) GetEnterInputKeybind() *key.Binding {
return nil
}
func (b *BaseFocusable) GetExitInputKeybind() key.Binding {
return key.NewBinding(key.WithKeys("esc"))
}
func (b *BaseFocusable) CanExitInputting() bool {
return true
}
func (b *BaseFocusable) setFocused(focused bool) {
b.focused = focused
}
func (b *BaseFocusable) setInputting(input bool) {
b.inputting = input
}
func (b *BaseFocusable) SetFocusedStyle(style lipgloss.Style) {
b.focusedStyle = style
}
func (b *BaseFocusable) SetBlurredStyle(style lipgloss.Style) {
b.blurredStyle = style
}