vcom/internal/tui/update.go
vrubelroman 40b8722d58 feat(tui): App skeleton with mode machine, async navigation and effects (6.1.5)
The new UI's root model: Screen x viewer focus x native selection x
filter edit x modal stack, one inputOwner, keys resolved through the
registry, an action table that features extend from register_*.go.
Directory reads and previews run in Cmds with tickets: Update never
blocks (N-P1-1), stale results are dropped (U-P1-13), failed navigation
keeps the panel and a vanished directory climbs up (U-P1-8). q closes
layers before asking to quit (U-P1-16), PgDn pages (U-P1-6), the theme
list previews live and saves with config.SetKey. Mouse mode is
reconciled after every Update; View only reads state.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 09:11:39 +03:00

154 lines
3.6 KiB
Go

package tui
import (
"time"
tea "github.com/charmbracelet/bubbletea"
"vcom/internal/tui/keys"
"vcom/internal/tui/modal"
)
// doubleClick is the max interval between clicks of a double click.
const doubleClick = 400 * time.Millisecond
// Update is the single entry point of the model. It never blocks: all I/O
// runs in Cmds (N-P1-1).
func (a *App) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmd := a.update(msg)
a.relayout()
return a, tea.Batch(cmd, a.reconcile())
}
func (a *App) update(msg tea.Msg) tea.Cmd {
switch m := msg.(type) {
case tea.WindowSizeMsg:
a.w, a.h = m.Width, m.Height
return nil
case tea.KeyMsg:
return a.key(m)
case tea.MouseMsg:
return a.mouse(m)
case listingMsg:
return a.onListing(m)
case previewTickMsg:
return a.onPreviewTick(m)
case previewMsg:
return a.onPreview(m)
case noteExpiredMsg:
a.notes.Expire(m.id)
return nil
case themeSavedMsg:
return a.onThemeSaved(m)
case jobEventMsg:
return a.onJobEvent(m)
case execDoneMsg:
a.applied.mouse = mouseUnknown // the external program may have reset it (U-P1-3)
return nil
}
for _, h := range a.handlers {
if cmd, ok := h(a, msg); ok {
return cmd
}
}
return nil
}
func (a *App) key(k tea.KeyMsg) tea.Cmd {
a.notes.KeyPressed()
if a.nativeSelect && k.Type == tea.KeyEsc && a.modals.Len() == 0 {
return a.toggleNativeSelect()
}
ctx := a.inputOwner()
act, ok := a.keys.Resolve(ctx, k)
if ok && act == keys.Pending {
return nil
}
switch {
case a.modals.Len() > 0:
return a.modals.Key(act, k)
case ctx == keys.CtxFilterEdit:
return a.filterKey(act, ok, k)
case ok:
return a.dispatch(act)
}
return nil
}
// mouse routes mouse events: everything to the top modal while one is open
// (clicks outside it are ignored); otherwise by Layout hit-test.
func (a *App) mouse(m tea.MouseMsg) tea.Cmd {
if a.modals.Len() > 0 {
return a.modals.Mouse(m, a.layout.Screen, a.styles)
}
if a.nativeSelect {
return nil
}
switch m.Button {
case tea.MouseButtonWheelUp, tea.MouseButtonWheelDown:
d := 3
if m.Button == tea.MouseButtonWheelUp {
d = -3
}
if a.screen == ScreenView || (a.screen == ScreenInfo && a.layout.Preview.Contains(m.X, m.Y)) {
a.viewer.Scroll(d)
return nil
}
if i, ok := a.layout.paneAt(m.X, m.Y); ok {
a.panels[i].Scroll(d)
if i == a.active {
return a.selectionChanged()
}
}
return nil
}
if m.Action != tea.MouseActionPress || a.screen == ScreenView {
return nil
}
i, ok := a.layout.paneAt(m.X, m.Y)
if !ok {
return nil
}
if m.Button == tea.MouseButtonRight {
if a.screen == ScreenInfo {
return a.setScreen(ScreenBrowse)
}
return a.setScreen(ScreenInfo)
}
if m.Button != tea.MouseButtonLeft || a.filter != nil && i != a.active {
return nil
}
if i != a.active {
a.active = i
}
lr := a.listRect(i)
if !lr.Contains(m.X, m.Y) {
return a.selectionChanged()
}
row, ok := a.panels[i].RowAt(m.Y - lr.Y)
if !ok {
return a.selectionChanged()
}
now := a.now()
double := a.lastClick.pane == i && a.lastClick.row == row && now.Sub(a.lastClick.at) <= doubleClick
a.lastClick = click{at: now, pane: i, row: row}
a.panels[i].SetCursor(row)
if double {
a.lastClick = click{}
return a.dispatch(keys.NavOpen)
}
return a.selectionChanged()
}
// openModal pushes m; a filter being edited is committed first (the filter
// line never coexists with a modal).
func (a *App) openModal(m modal.Modal, onClose func(modal.Outcome) tea.Cmd) uint64 {
if a.filter != nil {
a.commitFilter()
}
a.keys.Reset()
return a.modals.Push(m, onClose)
}
// execDoneMsg is sent after an external program (editor, run) returns.
type execDoneMsg struct{ Err error }