button.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package core
  2. import (
  3. "github.com/charmbracelet/bubbles/key"
  4. tea "github.com/charmbracelet/bubbletea"
  5. "github.com/charmbracelet/lipgloss"
  6. "github.com/kujtimiihoxha/termai/internal/tui/styles"
  7. )
  8. // ButtonKeyMap defines key bindings for the button component
  9. type ButtonKeyMap struct {
  10. Enter key.Binding
  11. }
  12. // DefaultButtonKeyMap returns default key bindings for the button
  13. func DefaultButtonKeyMap() ButtonKeyMap {
  14. return ButtonKeyMap{
  15. Enter: key.NewBinding(
  16. key.WithKeys("enter"),
  17. key.WithHelp("enter", "select"),
  18. ),
  19. }
  20. }
  21. // ShortHelp returns keybinding help
  22. func (k ButtonKeyMap) ShortHelp() []key.Binding {
  23. return []key.Binding{k.Enter}
  24. }
  25. // FullHelp returns full help info for keybindings
  26. func (k ButtonKeyMap) FullHelp() [][]key.Binding {
  27. return [][]key.Binding{
  28. {k.Enter},
  29. }
  30. }
  31. // ButtonState represents the state of a button
  32. type ButtonState int
  33. const (
  34. // ButtonNormal is the default state
  35. ButtonNormal ButtonState = iota
  36. // ButtonHovered is when the button is focused/hovered
  37. ButtonHovered
  38. // ButtonPressed is when the button is being pressed
  39. ButtonPressed
  40. // ButtonDisabled is when the button is disabled
  41. ButtonDisabled
  42. )
  43. // ButtonVariant defines the visual style variant of a button
  44. type ButtonVariant int
  45. const (
  46. // ButtonPrimary uses primary color styling
  47. ButtonPrimary ButtonVariant = iota
  48. // ButtonSecondary uses secondary color styling
  49. ButtonSecondary
  50. // ButtonDanger uses danger/error color styling
  51. ButtonDanger
  52. // ButtonWarning uses warning color styling
  53. ButtonWarning
  54. // ButtonNeutral uses neutral color styling
  55. ButtonNeutral
  56. )
  57. // ButtonMsg is sent when a button is clicked
  58. type ButtonMsg struct {
  59. ID string
  60. Payload any
  61. }
  62. // ButtonCmp represents a clickable button component
  63. type ButtonCmp struct {
  64. id string
  65. label string
  66. width int
  67. height int
  68. state ButtonState
  69. variant ButtonVariant
  70. keyMap ButtonKeyMap
  71. payload any
  72. style lipgloss.Style
  73. hoverStyle lipgloss.Style
  74. }
  75. // NewButtonCmp creates a new button component
  76. func NewButtonCmp(id, label string) *ButtonCmp {
  77. b := &ButtonCmp{
  78. id: id,
  79. label: label,
  80. state: ButtonNormal,
  81. variant: ButtonPrimary,
  82. keyMap: DefaultButtonKeyMap(),
  83. width: len(label) + 4, // add some padding
  84. height: 1,
  85. }
  86. b.updateStyles()
  87. return b
  88. }
  89. // WithVariant sets the button variant
  90. func (b *ButtonCmp) WithVariant(variant ButtonVariant) *ButtonCmp {
  91. b.variant = variant
  92. b.updateStyles()
  93. return b
  94. }
  95. // WithPayload sets the payload sent with button events
  96. func (b *ButtonCmp) WithPayload(payload any) *ButtonCmp {
  97. b.payload = payload
  98. return b
  99. }
  100. // WithWidth sets a custom width
  101. func (b *ButtonCmp) WithWidth(width int) *ButtonCmp {
  102. b.width = width
  103. b.updateStyles()
  104. return b
  105. }
  106. // updateStyles recalculates styles based on current state and variant
  107. func (b *ButtonCmp) updateStyles() {
  108. // Base styles
  109. b.style = styles.Regular.
  110. Padding(0, 1).
  111. Width(b.width).
  112. Align(lipgloss.Center).
  113. BorderStyle(lipgloss.RoundedBorder())
  114. b.hoverStyle = b.style.
  115. Bold(true)
  116. // Variant-specific styling
  117. switch b.variant {
  118. case ButtonPrimary:
  119. b.style = b.style.
  120. Foreground(styles.Base).
  121. Background(styles.Primary).
  122. BorderForeground(styles.Primary)
  123. b.hoverStyle = b.hoverStyle.
  124. Foreground(styles.Base).
  125. Background(styles.Blue).
  126. BorderForeground(styles.Blue)
  127. case ButtonSecondary:
  128. b.style = b.style.
  129. Foreground(styles.Base).
  130. Background(styles.Secondary).
  131. BorderForeground(styles.Secondary)
  132. b.hoverStyle = b.hoverStyle.
  133. Foreground(styles.Base).
  134. Background(styles.Mauve).
  135. BorderForeground(styles.Mauve)
  136. case ButtonDanger:
  137. b.style = b.style.
  138. Foreground(styles.Base).
  139. Background(styles.Error).
  140. BorderForeground(styles.Error)
  141. b.hoverStyle = b.hoverStyle.
  142. Foreground(styles.Base).
  143. Background(styles.Red).
  144. BorderForeground(styles.Red)
  145. case ButtonWarning:
  146. b.style = b.style.
  147. Foreground(styles.Text).
  148. Background(styles.Warning).
  149. BorderForeground(styles.Warning)
  150. b.hoverStyle = b.hoverStyle.
  151. Foreground(styles.Text).
  152. Background(styles.Peach).
  153. BorderForeground(styles.Peach)
  154. case ButtonNeutral:
  155. b.style = b.style.
  156. Foreground(styles.Text).
  157. Background(styles.Grey).
  158. BorderForeground(styles.Grey)
  159. b.hoverStyle = b.hoverStyle.
  160. Foreground(styles.Text).
  161. Background(styles.DarkGrey).
  162. BorderForeground(styles.DarkGrey)
  163. }
  164. // Disabled style override
  165. if b.state == ButtonDisabled {
  166. b.style = b.style.
  167. Foreground(styles.SubText0).
  168. Background(styles.LightGrey).
  169. BorderForeground(styles.LightGrey)
  170. }
  171. }
  172. // SetSize sets the button size
  173. func (b *ButtonCmp) SetSize(width, height int) {
  174. b.width = width
  175. b.height = height
  176. b.updateStyles()
  177. }
  178. // Focus sets the button to focused state
  179. func (b *ButtonCmp) Focus() tea.Cmd {
  180. if b.state != ButtonDisabled {
  181. b.state = ButtonHovered
  182. }
  183. return nil
  184. }
  185. // Blur sets the button to normal state
  186. func (b *ButtonCmp) Blur() tea.Cmd {
  187. if b.state != ButtonDisabled {
  188. b.state = ButtonNormal
  189. }
  190. return nil
  191. }
  192. // Disable sets the button to disabled state
  193. func (b *ButtonCmp) Disable() {
  194. b.state = ButtonDisabled
  195. b.updateStyles()
  196. }
  197. // Enable enables the button if disabled
  198. func (b *ButtonCmp) Enable() {
  199. if b.state == ButtonDisabled {
  200. b.state = ButtonNormal
  201. b.updateStyles()
  202. }
  203. }
  204. // IsDisabled returns whether the button is disabled
  205. func (b *ButtonCmp) IsDisabled() bool {
  206. return b.state == ButtonDisabled
  207. }
  208. // IsFocused returns whether the button is focused
  209. func (b *ButtonCmp) IsFocused() bool {
  210. return b.state == ButtonHovered
  211. }
  212. // Init initializes the button
  213. func (b *ButtonCmp) Init() tea.Cmd {
  214. return nil
  215. }
  216. // Update handles messages and user input
  217. func (b *ButtonCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  218. // Skip updates if disabled
  219. if b.state == ButtonDisabled {
  220. return b, nil
  221. }
  222. switch msg := msg.(type) {
  223. case tea.KeyMsg:
  224. // Handle key presses when focused
  225. if b.state == ButtonHovered {
  226. switch {
  227. case key.Matches(msg, b.keyMap.Enter):
  228. b.state = ButtonPressed
  229. return b, func() tea.Msg {
  230. return ButtonMsg{
  231. ID: b.id,
  232. Payload: b.payload,
  233. }
  234. }
  235. }
  236. }
  237. }
  238. return b, nil
  239. }
  240. // View renders the button
  241. func (b *ButtonCmp) View() string {
  242. if b.state == ButtonHovered || b.state == ButtonPressed {
  243. return b.hoverStyle.Render(b.label)
  244. }
  245. return b.style.Render(b.label)
  246. }