vcom/internal/tui/modal/stack.go

226 lines
6 KiB
Go
Raw Normal View History

// Package modal is the modal stack of the new UI (design §7.5): Dialog
// (structured confirm), Input (one or more text fields), List (choices,
// help, logs). While a modal is open it gets every key and mouse event; a
// click outside it is ignored, nothing under it changes.
//
// A modal never closes itself through side effects: Key/Mouse return an
// Outcome, the Stack pops it and runs the onClose callback given to Push.
// Stage 6.2/6.4 add Progress, Conflict and Prompt modals in this package.
package modal
import (
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/ansi"
"vcom/internal/tui/geom"
"vcom/internal/tui/keys"
"vcom/internal/tui/style"
)
// Outcome of a key or mouse event.
type Outcome struct {
Close bool
Cancelled bool // closed by Esc/q/Ctrl+C or a Cancel button
Value any
}
// Keep leaves the modal open.
var Keep = Outcome{}
// Done closes with value v.
func Done(v any) Outcome { return Outcome{Close: true, Value: v} }
// Cancel closes as cancelled.
func Cancel() Outcome { return Outcome{Close: true, Cancelled: true} }
// Modal is one layer of the stack.
type Modal interface {
// Context selects the key table (keys.CtxModalDialog, ...).
Context() keys.Context
// Key gets the resolved action ("" if the key is unbound) and the raw
// key (text fields, button hotkeys).
Key(a keys.ActionID, k tea.KeyMsg) (Outcome, tea.Cmd)
// Mouse gets an event at (x, y) relative to the modal's top-left
// corner; w×h is the space View was given.
Mouse(m tea.MouseMsg, x, y, w, h int, st *style.Styles) (Outcome, tea.Cmd)
// View renders the modal into at most maxW×maxH cells; a modal taller
// than that scrolls itself.
View(maxW, maxH int, st *style.Styles) string
}
type entry struct {
id uint64
m Modal
onClose func(Outcome) tea.Cmd
}
// Stack of modals; the top one owns input.
type Stack struct {
entries []entry
nextID uint64
}
// Push opens m; onClose (may be nil) runs when it closes, with its outcome.
func (s *Stack) Push(m Modal, onClose func(Outcome) tea.Cmd) uint64 {
s.nextID++
s.entries = append(s.entries, entry{id: s.nextID, m: m, onClose: onClose})
return s.nextID
}
// Len is the number of open modals.
func (s *Stack) Len() int { return len(s.entries) }
// Top returns the top modal (nil if none).
func (s *Stack) Top() Modal {
if len(s.entries) == 0 {
return nil
}
return s.entries[len(s.entries)-1].m
}
// TopID returns the id of the top modal (0 if none).
func (s *Stack) TopID() uint64 {
if len(s.entries) == 0 {
return 0
}
return s.entries[len(s.entries)-1].id
}
// Has reports whether modal id is open.
func (s *Stack) Has(id uint64) bool {
for _, e := range s.entries {
if e.id == id {
return true
}
}
return false
}
// Get returns modal id.
func (s *Stack) Get(id uint64) Modal {
for _, e := range s.entries {
if e.id == id {
return e.m
}
}
return nil
}
// CloseByID removes modal id without running onClose (e.g. its job ended);
// it reports whether it was open.
func (s *Stack) CloseByID(id uint64) bool {
for i, e := range s.entries {
if e.id == id {
s.entries = append(s.entries[:i], s.entries[i+1:]...)
return true
}
}
return false
}
func (s *Stack) finish(o Outcome, cmd tea.Cmd) tea.Cmd {
if !o.Close || len(s.entries) == 0 {
return cmd
}
e := s.entries[len(s.entries)-1]
s.entries = s.entries[:len(s.entries)-1]
if e.onClose != nil {
return tea.Batch(cmd, e.onClose(o))
}
return cmd
}
// Key routes a key to the top modal.
func (s *Stack) Key(a keys.ActionID, k tea.KeyMsg) tea.Cmd {
top := s.Top()
if top == nil {
return nil
}
return s.finish(top.Key(a, k))
}
// Mouse routes a mouse event to the top modal if it is inside the modal's
// box (screen is the area modals are centered in); events outside are
// swallowed.
func (s *Stack) Mouse(m tea.MouseMsg, screen geom.Rect, st *style.Styles) tea.Cmd {
top := s.Top()
if top == nil {
return nil
}
box := s.Box(screen, st)
if !box.Contains(m.X, m.Y) {
return nil
}
return s.finish(top.Mouse(m, m.X-box.X, m.Y-box.Y, screen.W, screen.H, st))
}
// Box is where the top modal is drawn inside screen.
func (s *Stack) Box(screen geom.Rect, st *style.Styles) geom.Rect {
top := s.Top()
if top == nil {
return geom.Rect{}
}
v := top.View(screen.W, screen.H, st)
return screen.Center(lipgloss.Width(v), lipgloss.Height(v))
}
// Render draws the top modal centered over base (screen-sized).
func (s *Stack) Render(base string, screen geom.Rect, st *style.Styles) string {
top := s.Top()
if top == nil {
return base
}
v := top.View(screen.W, screen.H, st)
box := screen.Center(lipgloss.Width(v), lipgloss.Height(v))
return Overlay(base, v, box.X, box.Y)
}
// Overlay draws box over base with its top-left corner at (x, y).
func Overlay(base, box string, x, y int) string {
lines := strings.Split(base, "\n")
for i, bl := range strings.Split(box, "\n") {
row := y + i
if row < 0 || row >= len(lines) {
continue
}
line := lines[row]
w := ansi.StringWidth(line)
if w < x {
line += strings.Repeat(" ", x-w)
w = x
}
left := ansi.Truncate(line, x, "")
right := ansi.TruncateLeft(line, x+ansi.StringWidth(bl), "")
lines[row] = left + bl + right
}
return strings.Join(lines, "\n")
}
// frame wraps content in the modal border with a title.
func frame(st *style.Styles, title string, body []string, maxW int) string {
content := strings.Join(body, "\n")
if title != "" {
content = st.ModalTitle.Render(ansi.Truncate(title, max(1, maxW-4), "…")) + "\n" + content
}
return st.Modal.Render(content)
}
// innerWidth is the content width for a modal given maxW (border + padding).
func innerWidth(maxW, want int) int {
return max(8, min(want, maxW-4))
}
// clip returns at most n lines starting at off and whether lines were cut
// above/below.
func clip(lines []string, off, n int) (out []string, above, below bool) {
if n <= 0 {
return nil, len(lines) > 0, false
}
off = max(0, min(off, max(0, len(lines)-n)))
end := min(len(lines), off+n)
return lines[off:end], off > 0, end < len(lines)
}