vcom/internal/tui/layout.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

94 lines
2.8 KiB
Go

package tui
import (
"vcom/internal/tui/geom"
"vcom/internal/tui/panel"
)
// Layout holds every screen rectangle. Rendering, mouse hit-tests and the
// image overlay use the same values (design §7.3).
type Layout struct {
W, H int
Panes [2]geom.Rect // empty when the pane is hidden (Info shows only the active one)
Preview geom.Rect // Info: the preview box (with border)
PreviewBody geom.Rect // inside the preview border
Viewer geom.Rect // View screen: full content area
Status geom.Rect
Footer geom.Rect
Screen geom.Rect // whole terminal (modals center in it)
}
// computeLayout splits the terminal for the current screen.
func computeLayout(w, h int, screen Screen, active int, footer bool, previewPct, gap int) Layout {
l := Layout{W: w, H: h, Screen: geom.Rect{W: w, H: h}}
bottom := 1
if footer && h >= 8 {
bottom = 2
}
l.Status = geom.Rect{X: 0, Y: h - bottom, W: w, H: 1}
if bottom == 2 {
l.Footer = geom.Rect{X: 0, Y: h - 1, W: w, H: 1}
}
body := geom.Rect{W: w, H: max(0, h-bottom)}
switch screen {
case ScreenView:
l.Viewer = body
case ScreenInfo:
pct := previewPct
if pct < 20 || pct > 80 {
pct = 50
}
pw := body.W * pct / 100
lw := body.W - pw
l.Panes[active] = geom.Rect{X: 0, Y: 0, W: lw, H: body.H}
l.Preview = geom.Rect{X: lw, Y: 0, W: pw, H: body.H}
l.PreviewBody = geom.Rect{X: lw + 1, Y: 2, W: max(0, pw-2), H: max(0, body.H-3)}
default:
gap = max(0, min(gap, 4))
lw := (body.W - gap) / 2
l.Panes[0] = geom.Rect{X: 0, Y: 0, W: lw, H: body.H}
l.Panes[1] = geom.Rect{X: lw + gap, Y: 0, W: body.W - lw - gap, H: body.H}
}
return l
}
// paneAt returns the pane whose rectangle contains (x, y).
func (l Layout) paneAt(x, y int) (int, bool) {
for i, r := range l.Panes {
if !r.Empty() && r.Contains(x, y) {
return i, true
}
}
return 0, false
}
// listRect is the rows area of pane i given its filter state.
func (a *App) listRect(i int) geom.Rect {
return panel.ListRect(a.layout.Panes[i], a.filterRow(i))
}
func (a *App) filterRow(i int) bool {
return a.panels[i].Filter() != "" || (a.filter != nil && i == a.active)
}
// relayout recomputes the layout and tells panels and the viewer their
// sizes. Called after every Update (cheap) so View stays pure.
func (a *App) relayout() {
c := a.deps.Config.UI
a.layout = computeLayout(a.w, a.h, a.screen, a.active, c.ShowFooter, c.PreviewWidthPercent, c.PaneGap)
for i, p := range a.panels {
r := a.layout.Panes[i]
if r.Empty() {
r = a.layout.Panes[a.active]
}
p.SetRows(panel.ListRect(r, a.filterRow(i)).H)
}
switch a.screen {
case ScreenView:
a.viewer.SetRows(a.layout.Viewer.H - 1)
a.viewer.SetWidth(a.layout.Viewer.W)
case ScreenInfo:
a.viewer.SetRows(a.layout.PreviewBody.H)
a.viewer.SetWidth(a.layout.PreviewBody.W)
}
}