~adnano/astronaut

ref: 5b2ba7d7bffd153f57a7eac22f172a5d4d56c6a9 astronaut/ui/view.go -rw-r--r-- 7.4 KiB
5b2ba7d7Adnan Maolood ui/view: Remove focus variable 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright 2016 The Tcell Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ui

import (
	"sync/atomic"
	"unicode"

	"github.com/gdamore/tcell/v2"
	"github.com/mattn/go-runewidth"
)

// View provides a smaller logical view of a larger content area.
type View struct {
	ScrollState
	x      int  // Anchor to the real world
	y      int  // Anchor to the real world
	limx   int  // Content limits -- can't right scroll past this
	limy   int  // Content limits -- can't down scroll past this
	width  int  // View width
	height int  // View height
	locked bool // if true, don't autogrow

	v       tcell.Screen
	invalid *int32
	exit    *int32
}

// ScrollState contains the scroll state of the view.
type ScrollState struct {
	scrollx int // Logical offset of the view
	scrolly int // Logical offset of the view
}

// Clear clears the View.
func (v *View) Clear() {
	v.Fill(' ', tcell.StyleDefault)
}

// Fill fills the View with the given character and style.
func (v *View) Fill(ch rune, style tcell.Style) {
	for y := 0; y < v.height; y++ {
		for x := 0; x < v.width; x++ {
			v.v.SetContent(x+v.x, y+v.y, ch, nil, style)
		}
	}
}

// Size returns the visible size of the View in character cells.
func (v *View) Size() (int, int) {
	return v.width, v.height
}

// Reset resets the record of content, and also resets the offset back
// to the origin.  It doesn't alter the dimensions of the view port, nor
// the physical location relative to its parent.
func (v *View) Reset() {
	v.limx = 0
	v.limy = 0
	v.scrollx = 0
	v.scrolly = 0
}

// SetContent places data at the given cell location.  Note that
// since the View doesn't retain this data, if the location is outside
// of the visible area, it is simply discarded.
func (v *View) SetContent(x, y int, ch rune, comb []rune, s tcell.Style) {
	if x > v.limx && !v.locked {
		v.limx = x
	}
	if y > v.limy && !v.locked {
		v.limy = y
	}
	if x < v.scrollx || y < v.scrolly {
		return
	}
	if x >= (v.scrollx + v.width) {
		return
	}
	if y >= (v.scrolly + v.height) {
		return
	}
	v.v.SetContent(x-v.scrollx+v.x, y-v.scrolly+v.y, ch, comb, s)
}

// MakeVisible moves the View the minimum amount necessary to make the given
// point visible. This should be called before any content is changed with
// SetContent, since otherwise it may be possible to move the location onto
// a region whose contents have been discarded.
func (v *View) MakeVisible(x, y int) {
	if x < v.limx && x >= v.scrollx+v.width {
		v.scrollx = x - (v.width - 1)
	}
	if x >= 0 && x < v.scrollx {
		v.scrollx = x
	}
	if y < v.limy && y >= v.scrolly+v.height {
		v.scrolly = y - (v.height - 1)
	}
	if y >= 0 && y < v.scrolly {
		v.scrolly = y
	}
	v.Validate()
}

// Validate ensures that the X and Y offsets of the View are valid.
func (v *View) Validate() {
	if v.scrollx >= v.limx-v.width {
		v.scrollx = (v.limx - v.width)
	}
	if v.scrollx < 0 {
		v.scrollx = 0
	}
	if v.scrolly >= v.limy-v.height {
		v.scrolly = (v.limy - v.height)
	}
	if v.scrolly < 0 {
		v.scrolly = 0
	}
}

// Center centers the point, if possible, in the View.
func (v *View) Center(x, y int) {
	if x < 0 || y < 0 || x >= v.limx || y >= v.limy || v.v == nil {
		return
	}
	v.scrollx = x - (v.width / 2)
	v.scrolly = y - (v.height / 2)
	v.Validate()
}

// ScrollUp moves the view up, showing lower numbered rows of content.
func (v *View) ScrollUp(rows int) {
	v.scrolly -= rows
	v.Validate()
}

// ScrollDown moves the View down, showing higher numbered rows of content.
func (v *View) ScrollDown(rows int) {
	v.scrolly += rows
	v.Validate()
}

// ScrollLeft moves the View to the left.
func (v *View) ScrollLeft(cols int) {
	v.scrollx -= cols
	v.Validate()
}

// ScrollRight moves the View to the left.
func (v *View) ScrollRight(cols int) {
	v.scrollx += cols
	v.Validate()
}

// ScrollX returns the scroll offset of the View in the x-direction.
func (v *View) ScrollX() int {
	return v.scrollx
}

// ScrollY returns the scroll offset of the View in the y-direction.
func (v *View) ScrollY() int {
	return v.scrolly
}

// SetContentSize sets the size of the content area; this is used to limit
// scrolling and view moment.  If locked is true, then the content size will
// not automatically grow even if content is placed outside of this area
// with the SetContent() method.  If false, and content is drawn outside
// of the existing size, then the size will automatically grow to include
// the new content.
func (v *View) SetContentSize(width, height int, locked bool) {
	v.limx = width
	v.limy = height
	v.locked = locked
	v.Validate()
}

// GetContentSize returns the size of content as width, height in character
// cells.
func (v *View) GetContentSize() (int, int) {
	return v.limx, v.limy
}

// Layout sets the position and size of the view, usually in response to a
// window resize event. (x, y) refer to the position of the view on the screen.
// A negative value for either width or height will cause the View to expand to
// fill to the end of the screen in the relevant dimension.
func (v *View) Layout(x, y, width, height int) {
	px, py := v.v.Size()
	if x >= 0 && x < px {
		v.x = x
	}
	if y >= 0 && y < py {
		v.y = y
	}
	if width < 0 {
		width = px - x
	}
	if height < 0 {
		height = py - y
	}
	if width <= x+px {
		v.width = width
	}
	if height <= y+py {
		v.height = height
	}
}

// ShowCursor shows the cursor at (x, y).
func (v *View) ShowCursor(x, y int) {
	v.v.ShowCursor(v.x+x, v.y+y)
}

// HideCursor hides the cursor.
func (v *View) HideCursor() {
	v.v.HideCursor()
}

// DrawText draws the provided text at (sx, sy). It returns the number of cells used.
func (v *View) DrawText(sx, sy int, text string, style tcell.Style) int {
	r := rune(0)
	w := 0
	x := 0
	var comb []rune
	for _, l := range text {
		if l == '\t' {
			if w != 0 {
				v.SetContent(sx+x, sy, r, comb, style)
			}
			w = 0
			x += 4
			continue
		}
		if !unicode.IsGraphic(l) {
			continue
		}
		if runewidth.RuneWidth(l) == 0 {
			comb = append(comb, l)
			continue
		}
		if w != 0 {
			v.SetContent(sx+x, sy, r, comb, style)
			x += w
		}
		r = l
		w = runewidth.RuneWidth(l)
		comb = nil
	}
	if w != 0 {
		v.SetContent(sx+x, sy, r, comb, style)
		x += w
	}
	return x
}

// Contains reports whether the view contains the point (x, y).
func (v *View) Contains(x, y int) bool {
	w, h := v.Size()
	return x >= v.x && x < v.x+w && y >= v.y && y < v.y+h
}

// GetLocal transforms (x, y) to local coordinates.
func (v *View) GetLocal(x, y int) (int, int) {
	return x - v.x, y - v.y
}

// PgUp scrolls up one page.
func (v *View) PgUp() {
	_, h := v.Size()
	v.ScrollUp(h - 2)
}

// PgDn scrolls down one page.
func (v *View) PgDn() {
	_, h := v.Size()
	v.ScrollDown(h - 2)
}

// Top scrolls to the top of the view.
func (v *View) Top() {
	v.MakeVisible(0, 0)
}

// Bottom scrolls to the bottom of the view.
func (v *View) Bottom() {
	_, h := v.GetContentSize()
	_, vh := v.Size()
	v.MakeVisible(0, h-vh)
}

// Invalidate invalidates the view.
func (v *View) Invalidate() {
	atomic.StoreInt32(v.invalid, 1)
}

// Exit exits the UI.
func (v *View) Exit() {
	atomic.StoreInt32(v.exit, 1)
}