summaryrefslogtreecommitdiffhomepage
path: root/internal/tui/components/core/button.go
blob: 090fbc1ee510fb6912a3b47a76556f5c1bf0140a (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package core

import (
	"github.com/charmbracelet/bubbles/key"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
	"github.com/kujtimiihoxha/termai/internal/tui/styles"
)

// ButtonKeyMap defines key bindings for the button component
type ButtonKeyMap struct {
	Enter key.Binding
}

// DefaultButtonKeyMap returns default key bindings for the button
func DefaultButtonKeyMap() ButtonKeyMap {
	return ButtonKeyMap{
		Enter: key.NewBinding(
			key.WithKeys("enter"),
			key.WithHelp("enter", "select"),
		),
	}
}

// ShortHelp returns keybinding help
func (k ButtonKeyMap) ShortHelp() []key.Binding {
	return []key.Binding{k.Enter}
}

// FullHelp returns full help info for keybindings
func (k ButtonKeyMap) FullHelp() [][]key.Binding {
	return [][]key.Binding{
		{k.Enter},
	}
}

// ButtonState represents the state of a button
type ButtonState int

const (
	// ButtonNormal is the default state
	ButtonNormal ButtonState = iota
	// ButtonHovered is when the button is focused/hovered
	ButtonHovered
	// ButtonPressed is when the button is being pressed
	ButtonPressed
	// ButtonDisabled is when the button is disabled
	ButtonDisabled
)

// ButtonVariant defines the visual style variant of a button
type ButtonVariant int

const (
	// ButtonPrimary uses primary color styling
	ButtonPrimary ButtonVariant = iota
	// ButtonSecondary uses secondary color styling
	ButtonSecondary
	// ButtonDanger uses danger/error color styling
	ButtonDanger
	// ButtonWarning uses warning color styling
	ButtonWarning
	// ButtonNeutral uses neutral color styling
	ButtonNeutral
)

// ButtonMsg is sent when a button is clicked
type ButtonMsg struct {
	ID      string
	Payload any
}

// ButtonCmp represents a clickable button component
type ButtonCmp struct {
	id         string
	label      string
	width      int
	height     int
	state      ButtonState
	variant    ButtonVariant
	keyMap     ButtonKeyMap
	payload    any
	style      lipgloss.Style
	hoverStyle lipgloss.Style
}

// NewButtonCmp creates a new button component
func NewButtonCmp(id, label string) *ButtonCmp {
	b := &ButtonCmp{
		id:      id,
		label:   label,
		state:   ButtonNormal,
		variant: ButtonPrimary,
		keyMap:  DefaultButtonKeyMap(),
		width:   len(label) + 4, // add some padding
		height:  1,
	}
	b.updateStyles()
	return b
}

// WithVariant sets the button variant
func (b *ButtonCmp) WithVariant(variant ButtonVariant) *ButtonCmp {
	b.variant = variant
	b.updateStyles()
	return b
}

// WithPayload sets the payload sent with button events
func (b *ButtonCmp) WithPayload(payload any) *ButtonCmp {
	b.payload = payload
	return b
}

// WithWidth sets a custom width
func (b *ButtonCmp) WithWidth(width int) *ButtonCmp {
	b.width = width
	b.updateStyles()
	return b
}

// updateStyles recalculates styles based on current state and variant
func (b *ButtonCmp) updateStyles() {
	// Base styles
	b.style = styles.Regular.
		Padding(0, 1).
		Width(b.width).
		Align(lipgloss.Center).
		BorderStyle(lipgloss.RoundedBorder())

	b.hoverStyle = b.style.
		Bold(true)

	// Variant-specific styling
	switch b.variant {
	case ButtonPrimary:
		b.style = b.style.
			Foreground(styles.Base).
			Background(styles.Primary).
			BorderForeground(styles.Primary)

		b.hoverStyle = b.hoverStyle.
			Foreground(styles.Base).
			Background(styles.Blue).
			BorderForeground(styles.Blue)

	case ButtonSecondary:
		b.style = b.style.
			Foreground(styles.Base).
			Background(styles.Secondary).
			BorderForeground(styles.Secondary)

		b.hoverStyle = b.hoverStyle.
			Foreground(styles.Base).
			Background(styles.Mauve).
			BorderForeground(styles.Mauve)

	case ButtonDanger:
		b.style = b.style.
			Foreground(styles.Base).
			Background(styles.Error).
			BorderForeground(styles.Error)

		b.hoverStyle = b.hoverStyle.
			Foreground(styles.Base).
			Background(styles.Red).
			BorderForeground(styles.Red)

	case ButtonWarning:
		b.style = b.style.
			Foreground(styles.Text).
			Background(styles.Warning).
			BorderForeground(styles.Warning)

		b.hoverStyle = b.hoverStyle.
			Foreground(styles.Text).
			Background(styles.Peach).
			BorderForeground(styles.Peach)

	case ButtonNeutral:
		b.style = b.style.
			Foreground(styles.Text).
			Background(styles.Grey).
			BorderForeground(styles.Grey)

		b.hoverStyle = b.hoverStyle.
			Foreground(styles.Text).
			Background(styles.DarkGrey).
			BorderForeground(styles.DarkGrey)
	}

	// Disabled style override
	if b.state == ButtonDisabled {
		b.style = b.style.
			Foreground(styles.SubText0).
			Background(styles.LightGrey).
			BorderForeground(styles.LightGrey)
	}
}

// SetSize sets the button size
func (b *ButtonCmp) SetSize(width, height int) {
	b.width = width
	b.height = height
	b.updateStyles()
}

// Focus sets the button to focused state
func (b *ButtonCmp) Focus() tea.Cmd {
	if b.state != ButtonDisabled {
		b.state = ButtonHovered
	}
	return nil
}

// Blur sets the button to normal state
func (b *ButtonCmp) Blur() tea.Cmd {
	if b.state != ButtonDisabled {
		b.state = ButtonNormal
	}
	return nil
}

// Disable sets the button to disabled state
func (b *ButtonCmp) Disable() {
	b.state = ButtonDisabled
	b.updateStyles()
}

// Enable enables the button if disabled
func (b *ButtonCmp) Enable() {
	if b.state == ButtonDisabled {
		b.state = ButtonNormal
		b.updateStyles()
	}
}

// IsDisabled returns whether the button is disabled
func (b *ButtonCmp) IsDisabled() bool {
	return b.state == ButtonDisabled
}

// IsFocused returns whether the button is focused
func (b *ButtonCmp) IsFocused() bool {
	return b.state == ButtonHovered
}

// Init initializes the button
func (b *ButtonCmp) Init() tea.Cmd {
	return nil
}

// Update handles messages and user input
func (b *ButtonCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	// Skip updates if disabled
	if b.state == ButtonDisabled {
		return b, nil
	}

	switch msg := msg.(type) {
	case tea.KeyMsg:
		// Handle key presses when focused
		if b.state == ButtonHovered {
			switch {
			case key.Matches(msg, b.keyMap.Enter):
				b.state = ButtonPressed
				return b, func() tea.Msg {
					return ButtonMsg{
						ID:      b.id,
						Payload: b.payload,
					}
				}
			}
		}
	}

	return b, nil
}

// View renders the button
func (b *ButtonCmp) View() string {
	if b.state == ButtonHovered || b.state == ButtonPressed {
		return b.hoverStyle.Render(b.label)
	}
	return b.style.Render(b.label)
}