1package vt
2
3import renderer "github.com/bnema/vev-vt/core"
4
5func (s *Screen) Resize(width, height int) {
6 if width == s.Frame.Width && height == s.Frame.Height {
7 s.geometry.Cols, s.geometry.Rows = width, height
8 return
9 }
10 if width <= 0 || height <= 0 {
11 // renderer.Frame permits a collapsed geometry. There is no layout to
12 // preserve until a usable viewport is supplied again.
13 width, height = max(width, 0), max(height, 0)
14 s.buffer = s.newBuffer(width, height)
15 s.Row, s.Col = 0, 0
16 if state := s.alternate; state != nil {
17 state.buffer = s.newBuffer(width, height)
18 state.frame = state.buffer.frame
19 state.row, state.col = 0, 0
20 state.scrollTop, state.scrollBottom = 0, height-1
21 }
22 } else {
23 resize := func(b *buffer, row, col int, saved *cursorState, evict bool) (int, int) {
24 active := &bufferCursor{row: row, col: col}
25 var savedPoint *bufferCursor
26 if saved != nil && saved.saved {
27 savedPoint = &bufferCursor{row: saved.row, col: saved.col}
28 }
29 evicted, evictedBounds, evictedIDs := b.resize(width, height, active, savedPoint)
30 for i := range evictedIDs {
31 if evictedIDs[i] == 0 {
32 evictedIDs[i] = s.nextRowIDValue()
33 }
34 }
35 s.fillMissingRowIDs(b)
36 if evict {
37 for i, line := range evicted {
38 s.recordEvicted(line, evictedBounds[i], evictedIDs[i])
39 }
40 }
41 if savedPoint != nil {
42 saved.row, saved.col = savedPoint.row, clamp(savedPoint.col, 0, width-1)
43 }
44 return active.row, clamp(active.col, 0, width-1)
45 }
46
47 if s.alternate != nil {
48 // Both screen states are reflowed independently, but active and saved
49 // cursors for each state are mapped by their single buffer layout pass.
50 s.Row, s.Col = resize(s.buffer, s.Row, s.Col, &s.savedCursor, false)
51 state := s.alternate
52 if state.buffer == nil {
53 state.buffer = bufferFromFrame(state.frame)
54 s.fillMissingRowIDs(state.buffer)
55 }
56 state.row, state.col = resize(state.buffer, state.row, state.col, &state.savedCursor, true)
57 state.frame = state.buffer.frame
58 state.scrollTop, state.scrollBottom = 0, height-1
59 } else {
60 s.Row, s.Col = resize(s.buffer, s.Row, s.Col, &s.savedCursor, true)
61 }
62 }
63 s.Frame = s.buffer.frame
64 s.geometry.Cols, s.geometry.Rows = width, height
65 // A resize can split an in-flight escape sequence from the terminal state it
66 // was meant to mutate; keep the durable child state but discard partial bytes.
67 s.escapeBuf = s.escapeBuf[:0]
68 s.kittyDiscard = false
69 s.kittyDiscardEscaped = false
70 s.abortAllKittyPending()
71 s.resetScrollRegion()
72 s.fullRedraw()
73}
74
75// cloneFrame produces an independent copy in canonical layout: it copies the
76// source's logical rows (via Row) into a fresh frame whose offsets are already
77// canonical, so a rotated source is normalized in the clone.
78func cloneFrame(frame renderer.Frame) renderer.Frame {
79 out := renderer.NewFrame(frame.Width, frame.Height)
80 for y := range frame.Height {
81 copy(out.Row(y), frame.Row(y))
82 }
83 return out
84}
85
86func clamp(v, lo, hi int) int {
87 if hi < lo {
88 return lo
89 }
90 if v < lo {
91 return lo
92 }
93 if v > hi {
94 return hi
95 }
96 return v
97}