cloudaxe-opencode/packages/tui/internal/components/dialog/help.go

81 lines
2.1 KiB
Go
Raw Normal View History

package dialog
import (
2025-06-12 10:35:40 +00:00
tea "github.com/charmbracelet/bubbletea/v2"
2025-06-22 11:44:23 +00:00
"github.com/sst/opencode/internal/app"
commandsComponent "github.com/sst/opencode/internal/components/commands"
2025-06-12 21:00:20 +00:00
"github.com/sst/opencode/internal/components/modal"
2025-06-12 10:35:40 +00:00
"github.com/sst/opencode/internal/layout"
2025-06-04 14:20:42 +00:00
"github.com/sst/opencode/internal/theme"
"github.com/sst/opencode/internal/viewport"
)
2025-06-12 21:00:20 +00:00
type helpDialog struct {
2025-06-22 11:44:23 +00:00
width int
height int
modal *modal.Modal
app *app.App
commandsComponent commandsComponent.CommandsComponent
2025-06-22 19:28:08 +00:00
viewport viewport.Model
}
2025-06-12 21:00:20 +00:00
func (h *helpDialog) Init() tea.Cmd {
2025-06-30 19:04:48 +00:00
return h.viewport.Init()
}
2025-06-12 21:00:20 +00:00
func (h *helpDialog) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
2025-06-22 19:28:08 +00:00
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
2025-06-12 21:00:20 +00:00
h.width = msg.Width
h.height = msg.Height
2025-07-10 20:56:19 +00:00
// Set viewport size with some padding for the modal, but cap at reasonable width
maxWidth := min(80, msg.Width-8)
h.viewport = viewport.New(viewport.WithWidth(maxWidth-4), viewport.WithHeight(msg.Height-6))
h.commandsComponent.SetSize(maxWidth-4, msg.Height-6)
}
2025-06-22 19:28:08 +00:00
// Update viewport content
h.viewport.SetContent(h.commandsComponent.View())
// Update viewport
var vpCmd tea.Cmd
h.viewport, vpCmd = h.viewport.Update(msg)
cmds = append(cmds, vpCmd)
return h, tea.Batch(cmds...)
}
2025-06-12 21:00:20 +00:00
func (h *helpDialog) View() string {
2025-04-28 13:46:09 +00:00
t := theme.CurrentTheme()
2025-07-11 11:03:21 +00:00
h.commandsComponent.SetBackgroundColor(t.BackgroundPanel())
2025-06-22 19:28:08 +00:00
return h.viewport.View()
}
2025-06-12 21:00:20 +00:00
func (h *helpDialog) Render(background string) string {
return h.modal.Render(h.View(), background)
}
2025-06-12 21:00:20 +00:00
func (h *helpDialog) Close() tea.Cmd {
return nil
}
2025-06-12 21:00:20 +00:00
type HelpDialog interface {
layout.Modal
}
2025-06-22 11:44:23 +00:00
func NewHelpDialog(app *app.App) HelpDialog {
2025-06-22 19:28:08 +00:00
vp := viewport.New(viewport.WithHeight(12))
2025-06-12 21:00:20 +00:00
return &helpDialog{
2025-06-22 19:28:08 +00:00
app: app,
commandsComponent: commandsComponent.New(app,
2025-07-11 11:03:21 +00:00
commandsComponent.WithBackground(theme.CurrentTheme().BackgroundPanel()),
2025-06-22 19:28:08 +00:00
commandsComponent.WithShowAll(true),
commandsComponent.WithKeybinds(true),
),
2025-07-10 20:56:19 +00:00
modal: modal.New(modal.WithTitle("Help"), modal.WithMaxWidth(80)),
2025-06-22 19:28:08 +00:00
viewport: vp,
2025-06-12 21:00:20 +00:00
}
}