vcom/internal/tui/app.go
vrubelroman 1bdff32a84
Some checks failed
CI / test (push) Failing after 6m3s
feat(tui): full preview, text viewer and image overlays (6.3)
2026-11-01 22:06:24 +03:00

186 lines
5.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package tui is the new user interface (design §7), enabled with
// --ui=next. The App is a tea.Model with an explicit mode machine (Screen ×
// viewer focus × native selection × filter edit × modal stack); one
// function decides who owns the keyboard (inputOwner), keys resolve through
// the keys.Registry, every async result carries a Ticket and is dropped
// when stale, and View only reads state.
//
// Extension points for stages 6.2–6.6 (each feature in its own files):
// - actions: registerOps/registerViewer/registerSSH/registerArchive/
// registerMisc add handlers to the action table (register_*.go);
// - messages: a.handle(fn) for the feature's own message types;
// - job events: a.onJob(fn) for jobs.Event;
// - key bindings: keys/bindings_<feature>.go;
// - modals: internal/tui/modal.
//
// Rules: Cmd functions never capture *App (only immutable inputs: Target,
// Ticket, options, leases); no file I/O outside vfs.FS (TestImportsGuard).
package tui
import (
"time"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"vcom/internal/theme"
"vcom/internal/tui/keys"
"vcom/internal/tui/modal"
"vcom/internal/tui/notify"
"vcom/internal/tui/panel"
"vcom/internal/tui/style"
"vcom/internal/tui/viewer"
)
// Screen is the main screen.
type Screen uint8
const (
ScreenBrowse Screen = iota
ScreenInfo
ScreenView
)
func (s Screen) String() string {
return [...]string{"browse", "info", "view"}[s]
}
// App is the root model.
type App struct {
deps Deps
keys *keys.Registry
styles *style.Styles
theme string
screen Screen
viewBack Screen
panels [2]*panel.Model
active int
filter *textinput.Model // non-nil while the filter line is edited
viewer viewer.Model
nativeSelect bool
modals modal.Stack
notes notify.Center
req Tracker
w, h int
layout Layout
applied effects
actions actionTable
handlers []func(*App, tea.Msg) (tea.Cmd, bool)
jobHooks []func(*App, jobEvent) tea.Cmd
jobSnaps map[uint64]jobSnap
// openFile handles Enter on a non-directory entry; set by stage 6.5
// (archives) and 6.6 (edit / run menu / xdg-open) in their hooks.
openFile func(*App, panel.Entry) tea.Cmd
ops *opsState // file operations and archives (stages 6.2, 6.5)
misc *miscState // session, external programs (stage 6.6)
previewFor string // what the viewer shows / is loading (location + name)
lastClick click
quitting bool
}
type click struct {
at time.Time
pane, row int
}
// New builds the App. Call Init (Bubble Tea does) to load the panels.
func New(d Deps) *App {
d.fill()
a := &App{deps: d, keys: keys.MustDefault(), jobSnaps: map[uint64]jobSnap{}}
a.setTheme(d.Config.UI.Theme)
if d.Config.Preview.WrapText {
a.viewer.ToggleWrap()
}
opts := a.panelOptions()
for i := range a.panels {
m := panel.NewMount(d.LocalFS, "/", nil, "", nil, nil)
a.panels[i] = panel.New(i, opts, []panel.Mount{m}, d.StartDirs[i])
}
a.actions = actionTable{}
registerCore(a.actions)
registerOps(a.actions)
registerViewer(a.actions)
registerSSH(a.actions)
registerArchive(a.actions)
registerMisc(a.actions)
registerOpsHooks(a)
registerViewerHooks(a)
registerSSHHooks(a)
registerArchiveHooks(a)
registerMiscHooks(a)
return a
}
func (a *App) panelOptions() panel.Options {
b := a.deps.Config.Browser
return panel.Options{
ShowHidden: b.ShowHidden,
DirsFirst: b.DirsFirst,
SortBy: b.Sort.By,
Reverse: b.Sort.Reverse,
ShowSize: b.Columns.Size,
ShowTime: b.Columns.Modified,
HumanSize: b.HumanReadableSize,
}
}
// setTheme switches the palette (unknown names fall back to the default).
func (a *App) setTheme(name string) {
p, err := theme.Resolve(name)
if err != nil {
p, _ = theme.Resolve("catppuccin-mocha")
name = p.Name
}
a.theme = name
a.styles = style.New(p)
}
// Init loads both panels.
func (a *App) Init() tea.Cmd {
var cmds []tea.Cmd
if a.deps.Notice != "" {
cmds = append(cmds, a.notify(notify.Warn, a.deps.Notice))
}
for i, p := range a.panels {
cmds = append(cmds, a.navigate(i, p.Here(), "", panel.NavReplace))
}
cmds = append(cmds, a.reconcile(), a.miscInit())
return tea.Batch(cmds...)
}
// Active returns the active panel.
func (a *App) Active() *panel.Model { return a.panels[a.active] }
// Passive returns the other panel.
func (a *App) Passive() *panel.Model { return a.panels[1-a.active] }
// Panel returns panel i.
func (a *App) Panel(i int) *panel.Model { return a.panels[i] }
// Screen returns the current screen.
func (a *App) Screen() Screen { return a.screen }
// ThemeName is the theme in use.
func (a *App) ThemeName() string { return a.theme }
// Viewer exposes the viewer model (tests, stage 6.3).
func (a *App) Viewer() *viewer.Model { return &a.viewer }
// Quitting reports whether the user confirmed quitting.
func (a *App) Quitting() bool { return a.quitting }
// handle registers a handler for feature messages; it returns true when it
// consumed the message.
func (a *App) handle(fn func(*App, tea.Msg) (tea.Cmd, bool)) { a.handlers = append(a.handlers, fn) }
// onJob registers a job-event hook.
func (a *App) onJob(fn func(*App, jobEvent) tea.Cmd) { a.jobHooks = append(a.jobHooks, fn) }
// now is the clock.
func (a *App) now() time.Time { return a.deps.Clock() }