File: vscroll.go

package info (click to toggle)
golang-github-gcla-gowid 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,456 kB
  • sloc: makefile: 4
file content (377 lines) | stat: -rw-r--r-- 9,770 bytes parent folder | download
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright 2019-2022 Graham Clark. All rights reserved.  Use of this source
// code is governed by the MIT license that can be found in the LICENSE
// file.

// Package vscroll provides a vertical scrollbar widget with mouse support. See the editor
// demo for more.
package vscroll

import (
	"fmt"

	"github.com/gcla/gowid"
	"github.com/gcla/gowid/gwutil"
	tcell "github.com/gdamore/tcell/v2"
)

//======================================================================

type VerticalScrollbarRunes struct {
	Up, Down, Space, Handle rune
}

var (
	VerticalScrollbarAsciiRunes   = VerticalScrollbarRunes{'^', 'v', ' ', '#'}
	VerticalScrollbarUnicodeRunes = VerticalScrollbarRunes{'▲', '▼', ' ', '█'}
)

//======================================================================

type ClickUp struct{}
type ClickDown struct{}
type ClickAbove struct{}
type ClickBelow struct{}
type RightClick struct{}

//======================================================================

type IVerticalScrollbar interface {
	GetTop() int
	GetMiddle() int
	GetBottom() int
	ClickUp(app gowid.IApp)
	ClickDown(app gowid.IApp)
	ClickAbove(app gowid.IApp)
	ClickBelow(app gowid.IApp)
	GetRunes() VerticalScrollbarRunes
}

type IRightMouseClick interface {
	RightClick(frac float32, app gowid.IApp)
}

type IWidget interface {
	gowid.IWidget
	IVerticalScrollbar
}

type Widget struct {
	Top       int
	Middle    int
	Bottom    int
	Runes     VerticalScrollbarRunes
	Callbacks *gowid.Callbacks
	gowid.IsSelectable
}

func New() *Widget {
	return NewWithChars(VerticalScrollbarAsciiRunes)
}

func NewUnicode() *Widget {
	return NewWithChars(VerticalScrollbarUnicodeRunes)
}

func NewWithChars(runes VerticalScrollbarRunes) *Widget {
	return NewExt(runes)
}

func NewExt(runes VerticalScrollbarRunes) *Widget {
	return &Widget{
		Top:       -1,
		Middle:    -1,
		Bottom:    -1,
		Runes:     runes,
		Callbacks: gowid.NewCallbacks(),
	}
}

func (w *Widget) String() string {
	return fmt.Sprintf("vscroll[t=%d,m=%d,b=%d]", w.GetTop(), w.GetMiddle(), w.GetBottom())
}

func (w *Widget) GetTop() int {
	return w.Top
}

func (w *Widget) GetMiddle() int {
	return w.Middle
}

func (w *Widget) GetBottom() int {
	return w.Bottom
}

func (w *Widget) OnClickBelow(f gowid.IWidgetChangedCallback) {
	gowid.AddWidgetCallback(w.Callbacks, ClickBelow{}, f)
}

func (w *Widget) RemoveOnClickBelow(f gowid.IIdentity) {
	gowid.RemoveWidgetCallback(w.Callbacks, ClickBelow{}, f)
}

func (w *Widget) OnClickAbove(f gowid.IWidgetChangedCallback) {
	gowid.AddWidgetCallback(w.Callbacks, ClickAbove{}, f)
}

func (w *Widget) RemoveOnClickAbove(f gowid.IIdentity) {
	gowid.RemoveWidgetCallback(w.Callbacks, ClickAbove{}, f)
}

func (w *Widget) OnRightClick(f gowid.IWidgetChangedCallback) {
	gowid.AddWidgetCallback(w.Callbacks, RightClick{}, f)
}

func (w *Widget) RemoveOnRightClick(f gowid.IIdentity) {
	gowid.RemoveWidgetCallback(w.Callbacks, RightClick{}, f)
}

func (w *Widget) OnClickDownArrow(f gowid.IWidgetChangedCallback) {
	gowid.AddWidgetCallback(w.Callbacks, ClickDown{}, f)
}

func (w *Widget) RemoveOnClickDownArrow(f gowid.IIdentity) {
	gowid.RemoveWidgetCallback(w.Callbacks, ClickDown{}, f)
}

func (w *Widget) OnClickUpArrow(f gowid.IWidgetChangedCallback) {
	gowid.AddWidgetCallback(w.Callbacks, ClickUp{}, f)
}

func (w *Widget) RemoveOnClickUpArrow(f gowid.IIdentity) {
	gowid.RemoveWidgetCallback(w.Callbacks, ClickUp{}, f)
}

func (w *Widget) RenderSize(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderBox {
	return RenderSize(w, size, focus, app)
}

func (w *Widget) UserInput(ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool {
	return UserInput(w, ev, size, focus, app)
}

func (w *Widget) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
	return Render(w, size, focus, app)
}

func (w *Widget) GetRunes() VerticalScrollbarRunes {
	return w.Runes
}

func (w *Widget) ClickUp(app gowid.IApp) {
	gowid.RunWidgetCallbacks(w.Callbacks, ClickUp{}, app, w)
}

func (w *Widget) ClickDown(app gowid.IApp) {
	gowid.RunWidgetCallbacks(w.Callbacks, ClickDown{}, app, w)
}

func (w *Widget) ClickAbove(app gowid.IApp) {
	gowid.RunWidgetCallbacks(w.Callbacks, ClickAbove{}, app, w)
}

func (w *Widget) ClickBelow(app gowid.IApp) {
	gowid.RunWidgetCallbacks(w.Callbacks, ClickBelow{}, app, w)
}

func (w *Widget) RightClick(frac float32, app gowid.IApp) {
	gowid.RunWidgetCallbacks(w.Callbacks, RightClick{}, app, w, frac)
}

//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

func RenderSize(w interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderBox {
	cols, haveCols := size.(gowid.IColumns)
	rows, haveRows := size.(gowid.IRows)
	switch {
	case haveCols && haveRows:
		return gowid.RenderBox{C: cols.Columns(), R: rows.Rows()}
	case haveCols:
		return gowid.RenderBox{C: cols.Columns(), R: 1}
	default:
		panic(gowid.WidgetSizeError{Widget: w, Size: size})
	}
}

func UserInput(w IVerticalScrollbar, ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool {
	if ev2, ok := ev.(*tcell.EventMouse); ok {
		switch ev2.Buttons() {
		case tcell.Button1, tcell.Button3:
			b3 := (ev2.Buttons() == tcell.Button3)
			t, m, b := w.GetTop(), w.GetMiddle(), w.GetBottom()
			rows := 1
			if box, ok := size.(gowid.IRenderBox); ok {
				rows = box.BoxRows()
			}
			//t, m, b = granularizeSplits(t, m, b, gwutil.Max(0, rows-2))
			splits := gwutil.HamiltonAllocation([]int{t, m, b}, gwutil.Max(0, rows-2))
			// Make sure that the "handle" in the middle is always at least 1 row tall
			if splits[1] == 0 {
				fixSplit(1, 0, 2, &splits)
			}
			// Make sure that unless we're at the top, there is a space to click to go closer to the top
			// and vice-versa for the bottom
			if t != 0 && splits[0] == 0 {
				fixSplit(0, 1, 2, &splits)
			}
			if b != 0 && splits[2] == 0 {
				fixSplit(2, 0, 1, &splits)
			}
			_, y := ev2.Position()
			res := false
			switch b3 {
			case true:
				if w, ok := w.(IRightMouseClick); ok {
					var frac float32
					switch {
					case y == 0:
						frac = 0.0
					case y <= splits[2]+splits[1]+splits[0]:
						if rows > 2 {
							frac = float32(y-1) / float32(rows-2)
						}
					default:
						frac = 1.0
					}
					w.RightClick(frac, app)
					res = true
				}
			case false:
				switch {
				case y == 0:
					w.ClickUp(app)
					res = true
				case y <= splits[0]:
					w.ClickAbove(app)
					res = true
				case y <= splits[1]+splits[0]:
				case y <= splits[2]+splits[1]+splits[0]:
					w.ClickBelow(app)
					res = true
				default:
					w.ClickDown(app)
					res = true
				}
			}
			return res
		default:
			return false
		}
	} else {
		return false
	}
}

func fixSplit(i int, o1, o2 int, splits *[]int) {
	if (*splits)[o1] > (*splits)[o2] {
		if (*splits)[o1] > 2 {
			(*splits)[i]++
			(*splits)[o1]--
		}
	} else {
		if (*splits)[o2] > 2 {
			(*splits)[i]++
			(*splits)[o2]--
		}
	}
}

func Render(w IWidget, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {

	// If a col and row not provided, what can I choose??
	cl, isCols := size.(gowid.IColumns)
	if !isCols {
		panic(gowid.WidgetSizeError{Widget: w, Size: size, Required: "gowid.IColumns"})
	}

	cols := cl.Columns()
	rows := 1
	if rs, isRows := size.(gowid.IRows); isRows {
		rows = rs.Rows()
	}

	t, m, b := w.GetTop(), w.GetMiddle(), w.GetBottom()
	splits := gwutil.HamiltonAllocation([]int{t, m, b}, gwutil.Max(0, rows-2))
	// Make sure that the "handle" in the middle is always at least 1 row tall
	if splits[1] == 0 {
		fixSplit(1, 0, 2, &splits)
	}
	// Make sure that unless we're at the top, there is a space to click to go closer to the top
	// and vice-versa for the bottom
	if t != 0 && splits[0] == 0 {
		fixSplit(0, 1, 2, &splits)
	}
	if b != 0 && splits[2] == 0 {
		fixSplit(2, 0, 1, &splits)
	}

	fill := gowid.CellFromRune(w.GetRunes().Handle)
	fillArr := make([]gowid.Cell, 0)
	blank := gowid.CellFromRune(w.GetRunes().Space)
	blankArr := make([]gowid.Cell, 0)
	for i := 0; i < cols; i++ {
		fillArr = append(fillArr, fill)
		blankArr = append(blankArr, blank)
	}

	resblankabove := gowid.NewCanvas()
	resblankbelow := gowid.NewCanvas()
	resfill := gowid.NewCanvas()

	// [2, 4, 7]
	dup1 := make([]gowid.Cell, cols)
	copy(dup1, blankArr)
	resblankabove.Lines = append(resblankabove.Lines, dup1)

	var dup []gowid.Cell
	for i := 0; i < rows-2; i++ {
		if i < splits[0] {
			dup = make([]gowid.Cell, cols)
			copy(dup, blankArr)
			resblankabove.Lines = append(resblankabove.Lines, dup)
		} else if i < splits[1]+splits[0] {
			dup = make([]gowid.Cell, cols)
			copy(dup, fillArr)
			resfill.Lines = append(resfill.Lines, dup)
		} else {
			dup = make([]gowid.Cell, cols)
			copy(dup, blankArr)
			resblankbelow.Lines = append(resblankbelow.Lines, dup)
		}
	}

	dup2 := make([]gowid.Cell, cols)
	copy(dup2, blankArr)
	resblankbelow.Lines = append(resblankbelow.Lines, dup2)

	resblankabove.AlignRight()
	resfill.AlignRightWith(gowid.MakeCell(
		'#',
		gowid.MakeTCellColorExt(tcell.ColorDefault),
		gowid.MakeTCellColorExt(tcell.ColorDefault),
		gowid.StyleNone))
	resblankbelow.AlignRight()

	res := gowid.NewCanvas()
	res.AppendBelow(resblankabove, false, false)
	res.AppendBelow(resfill, false, false)
	res.AppendBelow(resblankbelow, false, false)

	l := len(res.Lines)
	if l > 0 {
		for i := 0; i < len(res.Lines[0]); i++ {
			res.Lines[0][i] = res.Lines[0][i].WithRune(w.GetRunes().Up)
		}
		for i := 0; i < len(res.Lines[0]); i++ {
			res.Lines[l-1][i] = res.Lines[l-1][i].WithRune(w.GetRunes().Down)
		}
	}

	return res
}

//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End: