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