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>
321 lines
7.6 KiB
Go
321 lines
7.6 KiB
Go
package tui
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"vcom/internal/tui/keys"
|
|
"vcom/internal/tui/notify"
|
|
"vcom/internal/tui/viewer"
|
|
"vcom/internal/vfs"
|
|
)
|
|
|
|
// inputOwner decides who gets the keyboard — the one place that does
|
|
// (design §7.3).
|
|
func (a *App) inputOwner() keys.Context {
|
|
switch {
|
|
case a.modals.Len() > 0:
|
|
return a.modals.Top().Context()
|
|
case a.filter != nil:
|
|
return keys.CtxFilterEdit
|
|
case a.screen == ScreenView:
|
|
return viewerCtx(a.viewer.Focus)
|
|
case a.screen == ScreenInfo && a.viewer.Focus != viewer.Scroll:
|
|
return viewerCtx(a.viewer.Focus)
|
|
case a.screen == ScreenInfo:
|
|
return keys.CtxInfo
|
|
}
|
|
return keys.CtxBrowse
|
|
}
|
|
|
|
// InputOwner is inputOwner for tests and features.
|
|
func (a *App) InputOwner() keys.Context { return a.inputOwner() }
|
|
|
|
func viewerCtx(f viewer.Focus) keys.Context {
|
|
switch f {
|
|
case viewer.Caret:
|
|
return keys.CtxCaret
|
|
case viewer.Visual:
|
|
return keys.CtxVisual
|
|
}
|
|
return keys.CtxViewScroll
|
|
}
|
|
|
|
// Invariants checks the allowed state combinations (design §7.2); tests
|
|
// call it after every step.
|
|
func (a *App) Invariants() error {
|
|
var errs []error
|
|
if a.viewer.Focus != viewer.Scroll && (a.screen == ScreenBrowse || a.viewer.Kind != viewer.Text) {
|
|
errs = append(errs, fmt.Errorf("viewer focus %d on screen %s with kind %d", a.viewer.Focus, a.screen, a.viewer.Kind))
|
|
}
|
|
if a.nativeSelect && a.screen == ScreenBrowse {
|
|
errs = append(errs, errors.New("native selection in Browse"))
|
|
}
|
|
if a.filter != nil && (a.screen == ScreenView || a.modals.Len() > 0) {
|
|
errs = append(errs, fmt.Errorf("filter edited on %s with %d modals", a.screen, a.modals.Len()))
|
|
}
|
|
if a.active != 0 && a.active != 1 {
|
|
errs = append(errs, fmt.Errorf("active pane %d", a.active))
|
|
}
|
|
for i, p := range a.panels {
|
|
if n := p.VisibleLen(); n > 0 && (p.Cursor() < 0 || p.Cursor() >= n) {
|
|
errs = append(errs, fmt.Errorf("pane %d cursor %d of %d", i, p.Cursor(), n))
|
|
}
|
|
}
|
|
return errors.Join(errs...)
|
|
}
|
|
|
|
// setScreen switches the screen; leaving Info/View resets the viewer focus
|
|
// and native selection.
|
|
func (a *App) setScreen(s Screen) tea.Cmd {
|
|
if s == a.screen {
|
|
return nil
|
|
}
|
|
prev := a.screen
|
|
a.screen = s
|
|
if s == ScreenBrowse || (prev == ScreenView && s == ScreenInfo) {
|
|
a.viewer.Focus = viewer.Scroll
|
|
a.nativeSelect = false
|
|
}
|
|
if s == ScreenView && a.filter != nil {
|
|
a.commitFilter()
|
|
}
|
|
a.relayout()
|
|
switch s {
|
|
case ScreenInfo:
|
|
a.previewFor = ""
|
|
return a.requestPreview(false)
|
|
case ScreenBrowse:
|
|
a.req.Cancel(ReqKey{Kind: ReqPreview})
|
|
a.req.Cancel(ReqKey{Kind: ReqPreviewDebounce})
|
|
a.previewFor = ""
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// openView is F3/v: the selected file full screen.
|
|
func (a *App) openView() tea.Cmd {
|
|
e, ok := a.Active().Selected()
|
|
if !ok || e.Parent || e.Navigable() {
|
|
return a.notify(notify.Info, "Not a file")
|
|
}
|
|
a.viewBack = a.screen
|
|
a.screen = ScreenView
|
|
a.viewer.Focus = viewer.Scroll
|
|
if a.filter != nil {
|
|
a.commitFilter()
|
|
}
|
|
a.relayout()
|
|
a.previewFor = ""
|
|
return a.requestPreview(true)
|
|
}
|
|
|
|
// Viewer focus transitions (design §7.2).
|
|
|
|
func (a *App) viewerFocus() tea.Cmd {
|
|
if !a.viewer.EnterCaret() {
|
|
return a.notify(notify.Info, "Preview is not text")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *App) viewerCaret() tea.Cmd {
|
|
switch a.viewer.Focus {
|
|
case viewer.Visual:
|
|
a.viewer.Focus = viewer.Caret
|
|
case viewer.Scroll:
|
|
if !a.viewer.EnterCaret() {
|
|
return a.notify(notify.Info, "Not text")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *App) viewerVisual() tea.Cmd {
|
|
if !a.viewer.EnterVisual() {
|
|
return a.notify(notify.Info, "Not text")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// viewerBack is Esc: one focus step back, then close.
|
|
func (a *App) viewerBack() tea.Cmd {
|
|
if a.nativeSelect {
|
|
return a.toggleNativeSelect()
|
|
}
|
|
switch a.viewer.Focus {
|
|
case viewer.Visual:
|
|
a.viewer.Focus = viewer.Caret
|
|
return nil
|
|
case viewer.Caret:
|
|
a.viewer.Focus = viewer.Scroll
|
|
return nil
|
|
}
|
|
return a.viewerClose()
|
|
}
|
|
|
|
// viewerClose is q/F3: close View, or drop the focus in Info.
|
|
func (a *App) viewerClose() tea.Cmd {
|
|
a.nativeSelect = false
|
|
if a.screen == ScreenView {
|
|
a.viewer.Focus = viewer.Scroll
|
|
back := a.viewBack
|
|
a.screen = back
|
|
a.relayout()
|
|
a.previewFor = ""
|
|
if back == ScreenInfo {
|
|
return a.requestPreview(false)
|
|
}
|
|
return nil
|
|
}
|
|
a.viewer.Focus = viewer.Scroll
|
|
return nil
|
|
}
|
|
|
|
func (a *App) toggleNativeSelect() tea.Cmd {
|
|
if a.screen == ScreenBrowse {
|
|
return nil
|
|
}
|
|
a.nativeSelect = !a.nativeSelect
|
|
if a.nativeSelect {
|
|
return a.notify(notify.Info, "Terminal mouse selection: Ctrl+T or Esc to end")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Filter line.
|
|
|
|
func (a *App) startFilter() tea.Cmd {
|
|
ti := textinput.New()
|
|
ti.Prompt = "/"
|
|
ti.SetValue(a.Active().Filter())
|
|
ti.CursorEnd()
|
|
ti.Focus()
|
|
a.filter = &ti
|
|
a.relayout()
|
|
return nil
|
|
}
|
|
|
|
func (a *App) commitFilter() {
|
|
a.filter = nil
|
|
a.relayout()
|
|
}
|
|
|
|
// filterKey handles a key while the filter line owns input.
|
|
func (a *App) filterKey(act keys.ActionID, ok bool, k tea.KeyMsg) tea.Cmd {
|
|
p := a.Active()
|
|
if ok {
|
|
switch act {
|
|
case keys.FilterCommit:
|
|
a.commitFilter()
|
|
case keys.FilterCancel:
|
|
p.SetFilter("")
|
|
a.commitFilter()
|
|
case keys.FilterClearAll:
|
|
a.filter.SetValue("")
|
|
p.SetFilter("")
|
|
case keys.NavUp:
|
|
p.MoveCursor(-1)
|
|
case keys.NavDown:
|
|
p.MoveCursor(1)
|
|
}
|
|
return a.selectionChanged()
|
|
}
|
|
ti, cmd := a.filter.Update(k)
|
|
a.filter = &ti
|
|
p.SetFilter(ti.Value())
|
|
return tea.Batch(cmd, a.selectionChanged())
|
|
}
|
|
|
|
// Preview requests (design §7.7): cursor moves are debounced 100 ms, one
|
|
// preview in flight per app, stale results dropped.
|
|
|
|
const previewDebounce = 100 * time.Millisecond
|
|
|
|
type previewTickMsg struct{ Ticket Ticket }
|
|
|
|
type previewMsg struct {
|
|
Ticket Ticket
|
|
For string
|
|
Content viewer.Content
|
|
Err error
|
|
}
|
|
|
|
// selectionChanged is called after anything that may move the cursor.
|
|
func (a *App) selectionChanged() tea.Cmd {
|
|
if a.screen != ScreenInfo {
|
|
return nil
|
|
}
|
|
return a.requestPreview(false)
|
|
}
|
|
|
|
func (a *App) previewKey() (string, vfs.Location, vfs.FileInfo, bool) {
|
|
p := a.Active()
|
|
e, ok := p.Selected()
|
|
if !ok || e.Parent {
|
|
return "", vfs.Location{}, vfs.FileInfo{}, false
|
|
}
|
|
loc := p.Location()
|
|
return fmt.Sprintf("%s\x00%s\x00%s", loc.FS.Volume(), loc.Path, e.Name), loc, e.FileInfo, true
|
|
}
|
|
|
|
func (a *App) requestPreview(now bool) tea.Cmd {
|
|
key, loc, fi, ok := a.previewKey()
|
|
if !ok {
|
|
a.req.Cancel(ReqKey{Kind: ReqPreview})
|
|
a.req.Cancel(ReqKey{Kind: ReqPreviewDebounce})
|
|
a.previewFor = ""
|
|
a.viewer.Clear()
|
|
return nil
|
|
}
|
|
if key == a.previewFor {
|
|
return nil
|
|
}
|
|
a.previewFor = key
|
|
if !now {
|
|
tk, _ := a.req.Begin(ReqKey{Kind: ReqPreviewDebounce})
|
|
return tea.Tick(previewDebounce, func(time.Time) tea.Msg { return previewTickMsg{tk} })
|
|
}
|
|
return a.startPreview(key, loc, fi)
|
|
}
|
|
|
|
func (a *App) startPreview(key string, loc vfs.Location, fi vfs.FileInfo) tea.Cmd {
|
|
tk, ctx := a.req.Begin(ReqKey{Kind: ReqPreview})
|
|
build := a.deps.Preview
|
|
limit := a.deps.Config.Preview.MaxPreviewBytes
|
|
return func() tea.Msg {
|
|
c, err := build(ctx, loc, fi, limit)
|
|
return previewMsg{Ticket: tk, For: key, Content: c, Err: err}
|
|
}
|
|
}
|
|
|
|
func (a *App) onPreviewTick(m previewTickMsg) tea.Cmd {
|
|
if !a.req.Accept(m.Ticket) {
|
|
return nil
|
|
}
|
|
key, loc, fi, ok := a.previewKey()
|
|
if !ok || key != a.previewFor {
|
|
return nil
|
|
}
|
|
return a.startPreview(key, loc, fi)
|
|
}
|
|
|
|
func (a *App) onPreview(m previewMsg) tea.Cmd {
|
|
if !a.req.Accept(m.Ticket) {
|
|
return nil // stale: must not touch the viewer (U-P1-13)
|
|
}
|
|
if m.Err != nil {
|
|
if errors.Is(m.Err, context.Canceled) {
|
|
return nil
|
|
}
|
|
a.viewer.SetContent(viewer.Content{Kind: viewer.Other, Lines: []string{"Preview failed: " + cleanErr(m.Err).Error()}})
|
|
return nil
|
|
}
|
|
a.viewer.SetContent(m.Content)
|
|
return nil
|
|
}
|