toast.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package toast
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. tea "github.com/charmbracelet/bubbletea/v2"
  7. "github.com/charmbracelet/lipgloss/v2"
  8. "github.com/charmbracelet/lipgloss/v2/compat"
  9. "github.com/sst/opencode/internal/layout"
  10. "github.com/sst/opencode/internal/styles"
  11. "github.com/sst/opencode/internal/theme"
  12. )
  13. // ShowToastMsg is a message to display a toast notification
  14. type ShowToastMsg struct {
  15. Message string
  16. Title *string
  17. Color compat.AdaptiveColor
  18. Duration time.Duration
  19. }
  20. // DismissToastMsg is a message to dismiss a specific toast
  21. type DismissToastMsg struct {
  22. ID string
  23. }
  24. // Toast represents a single toast notification
  25. type Toast struct {
  26. ID string
  27. Message string
  28. Title *string
  29. Color compat.AdaptiveColor
  30. CreatedAt time.Time
  31. Duration time.Duration
  32. }
  33. // ToastManager manages multiple toast notifications
  34. type ToastManager struct {
  35. toasts []Toast
  36. }
  37. // NewToastManager creates a new toast manager
  38. func NewToastManager() *ToastManager {
  39. return &ToastManager{
  40. toasts: []Toast{},
  41. }
  42. }
  43. // Init initializes the toast manager
  44. func (tm *ToastManager) Init() tea.Cmd {
  45. return nil
  46. }
  47. // Update handles messages for the toast manager
  48. func (tm *ToastManager) Update(msg tea.Msg) (*ToastManager, tea.Cmd) {
  49. switch msg := msg.(type) {
  50. case ShowToastMsg:
  51. toast := Toast{
  52. ID: fmt.Sprintf("toast-%d", time.Now().UnixNano()),
  53. Title: msg.Title,
  54. Message: msg.Message,
  55. Color: msg.Color,
  56. CreatedAt: time.Now(),
  57. Duration: msg.Duration,
  58. }
  59. tm.toasts = append(tm.toasts, toast)
  60. // Return command to dismiss after duration
  61. return tm, tea.Tick(toast.Duration, func(t time.Time) tea.Msg {
  62. return DismissToastMsg{ID: toast.ID}
  63. })
  64. case DismissToastMsg:
  65. var newToasts []Toast
  66. for _, t := range tm.toasts {
  67. if t.ID != msg.ID {
  68. newToasts = append(newToasts, t)
  69. }
  70. }
  71. tm.toasts = newToasts
  72. }
  73. return tm, nil
  74. }
  75. // renderSingleToast renders a single toast notification
  76. func (tm *ToastManager) renderSingleToast(toast Toast) string {
  77. t := theme.CurrentTheme()
  78. baseStyle := styles.NewStyle().
  79. Foreground(t.Text()).
  80. Background(t.BackgroundElement()).
  81. Padding(1, 2)
  82. maxWidth := max(40, layout.Current.Viewport.Width/3)
  83. contentMaxWidth := max(maxWidth-6, 20)
  84. // Build content with wrapping
  85. var content strings.Builder
  86. if toast.Title != nil {
  87. titleStyle := styles.NewStyle().Foreground(toast.Color).
  88. Bold(true)
  89. content.WriteString(titleStyle.Render(*toast.Title))
  90. content.WriteString("\n")
  91. }
  92. // Wrap message text
  93. messageStyle := styles.NewStyle()
  94. contentWidth := lipgloss.Width(toast.Message)
  95. if contentWidth > contentMaxWidth {
  96. messageStyle = messageStyle.Width(contentMaxWidth)
  97. }
  98. content.WriteString(messageStyle.Render(toast.Message))
  99. // Render toast with max width
  100. return baseStyle.MaxWidth(maxWidth).Render(content.String())
  101. }
  102. // View renders all active toasts
  103. func (tm *ToastManager) View() string {
  104. if len(tm.toasts) == 0 {
  105. return ""
  106. }
  107. var toastViews []string
  108. for _, toast := range tm.toasts {
  109. toastView := tm.renderSingleToast(toast)
  110. toastViews = append(toastViews, toastView+"\n")
  111. }
  112. return strings.Join(toastViews, "\n")
  113. }
  114. // RenderOverlay renders the toasts as an overlay on the given background
  115. func (tm *ToastManager) RenderOverlay(background string) string {
  116. if len(tm.toasts) == 0 {
  117. return background
  118. }
  119. bgWidth := lipgloss.Width(background)
  120. bgHeight := lipgloss.Height(background)
  121. result := background
  122. // Start from top with 2 character padding
  123. currentY := 2
  124. // Render each toast individually
  125. for _, toast := range tm.toasts {
  126. // Render individual toast
  127. toastView := tm.renderSingleToast(toast)
  128. toastWidth := lipgloss.Width(toastView)
  129. toastHeight := lipgloss.Height(toastView)
  130. // Position at top-right with 2 character padding from right edge
  131. x := max(bgWidth-toastWidth-4, 0)
  132. // Check if toast fits vertically
  133. if currentY+toastHeight > bgHeight-2 {
  134. // No more room for toasts
  135. break
  136. }
  137. // Place this toast
  138. result = layout.PlaceOverlay(
  139. x,
  140. currentY,
  141. toastView,
  142. result,
  143. layout.WithOverlayBorder(),
  144. layout.WithOverlayBorderColor(toast.Color),
  145. )
  146. // Move down for next toast (add 1 for spacing between toasts)
  147. currentY += toastHeight + 1
  148. }
  149. return result
  150. }
  151. type ToastOptions struct {
  152. Title string
  153. Duration time.Duration
  154. }
  155. type toastOptions struct {
  156. title *string
  157. duration *time.Duration
  158. color *compat.AdaptiveColor
  159. }
  160. type ToastOption func(*toastOptions)
  161. func WithTitle(title string) ToastOption {
  162. return func(t *toastOptions) {
  163. t.title = &title
  164. }
  165. }
  166. func WithDuration(duration time.Duration) ToastOption {
  167. return func(t *toastOptions) {
  168. t.duration = &duration
  169. }
  170. }
  171. func WithColor(color compat.AdaptiveColor) ToastOption {
  172. return func(t *toastOptions) {
  173. t.color = &color
  174. }
  175. }
  176. func NewToast(message string, options ...ToastOption) tea.Cmd {
  177. t := theme.CurrentTheme()
  178. duration := 5 * time.Second
  179. color := t.Primary()
  180. opts := toastOptions{
  181. duration: &duration,
  182. color: &color,
  183. }
  184. for _, option := range options {
  185. option(&opts)
  186. }
  187. return func() tea.Msg {
  188. return ShowToastMsg{
  189. Message: message,
  190. Title: opts.title,
  191. Duration: *opts.duration,
  192. Color: *opts.color,
  193. }
  194. }
  195. }
  196. func NewInfoToast(message string, options ...ToastOption) tea.Cmd {
  197. options = append(options, WithColor(theme.CurrentTheme().Info()))
  198. return NewToast(
  199. message,
  200. options...,
  201. )
  202. }
  203. func NewSuccessToast(message string, options ...ToastOption) tea.Cmd {
  204. options = append(options, WithColor(theme.CurrentTheme().Success()))
  205. return NewToast(
  206. message,
  207. options...,
  208. )
  209. }
  210. func NewWarningToast(message string, options ...ToastOption) tea.Cmd {
  211. options = append(options, WithColor(theme.CurrentTheme().Warning()))
  212. return NewToast(
  213. message,
  214. options...,
  215. )
  216. }
  217. func NewErrorToast(message string, options ...ToastOption) tea.Cmd {
  218. options = append(options, WithColor(theme.CurrentTheme().Error()))
  219. return NewToast(
  220. message,
  221. options...,
  222. )
  223. }