/
image.go
155 lines (137 loc) · 3.21 KB
/
image.go
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
package tcellimg
import (
"context"
"image"
"image/draw"
"log/slog"
"time"
"github.com/gdamore/tcell/v2"
"github.com/gdamore/tcell/v2/views"
"github.com/srlehn/termimg/internal/errors"
"github.com/srlehn/termimg/internal/logx"
"github.com/srlehn/termimg/term"
)
// TODO handle events
var _ image.Image = (*Image)(nil)
var _ draw.Image = (*Image)(nil)
var _ views.Widget = (*Image)(nil)
type Image struct {
*views.CellView
*term.Canvas
term *term.Terminal
scr tcell.Screen
mdl linesModel
}
func NewImage(img image.Image, bounds image.Rectangle, tm *term.Terminal, scr tcell.Screen) (*Image, error) {
// TODO accept
if img == nil || tm == nil || scr == nil {
return nil, errors.NilParam()
}
canvas, err := tm.NewCanvas(bounds)
if err != nil {
return nil, err
}
if logx.IsErr(canvas.SetImage(img), tm, slog.LevelError) {
return nil, err
}
m := &Image{
CellView: views.NewCellView(),
Canvas: canvas,
term: tm,
scr: scr,
}
view := views.NewViewPort(scr, bounds.Min.X, bounds.Min.Y, bounds.Dx(), bounds.Dy())
m.Init()
m.SetView(view)
mdl := &linesModel{
width: bounds.Dx(),
height: bounds.Dy(),
x: bounds.Min.X,
y: bounds.Min.Y,
}
m.SetModel(mdl)
return m, nil
}
func (m *Image) Draw() {
// m.CellView.Draw() // area not drawn until tcell.Screen.Sync() is called
mdl := m.CellView.GetModel()
x, y, _, _ := mdl.GetCursor()
w, h := mdl.GetBounds()
bounds := image.Rect(x, y, x+w, y+h)
if !bounds.Eq(m.Canvas.CellArea()) {
logx.Error(`bounds changed`, m.term, "canvas-bounds", m.Canvas.CellArea(), "tcell-bounds", bounds)
}
m.CellView.Lock()
defer m.CellView.Unlock()
m.scr.LockRegion(x, y, w, h, true)
defer m.scr.LockRegion(x, y, w, h, false)
err := m.Canvas.Draw(nil)
_ = logx.IsErr(err, m.term, slog.LevelError)
}
func (m *Image) Video(ctx context.Context, vid <-chan image.Image, frameDur time.Duration) error {
if m == nil || m.Canvas == nil || ctx == nil {
return nil
}
return m.Canvas.Video(ctx, vid, frameDur)
}
func (m *Image) Close() error {
if m == nil {
return nil
}
if m.Canvas == nil {
m = nil
return nil
}
err := m.Canvas.Close()
m = nil
return logx.Err(err, m.term, slog.LevelError)
}
// copied from github.com/gdamore/tcell/v2/views/textarea.go (Apache-2.0 license)
type linesModel struct {
runes [][]rune
width int
height int
x int
y int
hide bool
cursor bool
style tcell.Style
}
func (m *linesModel) GetCell(x, y int) (rune, tcell.Style, []rune, int) {
// if x < 0 || y < 0 || y >= m.height || x >= len(m.runes[y]) {
if x < 0 || y < 0 || y >= m.height || x >= m.width {
return 0, m.style, nil, 1
}
// XXX: extend this to support combining and full width chars
return 0, tcell.Style{}, nil, 1
}
func (m *linesModel) GetBounds() (int, int) {
return m.width, m.height
}
func (m *linesModel) limitCursor() {
if m.x > m.width-1 {
m.x = m.width - 1
}
if m.y > m.height-1 {
m.y = m.height - 1
}
if m.x < 0 {
m.x = 0
}
if m.y < 0 {
m.y = 0
}
}
func (m *linesModel) SetCursor(x, y int) {
m.x = x
m.y = y
m.limitCursor()
}
func (m *linesModel) MoveCursor(x, y int) {
m.x += x
m.y += y
m.limitCursor()
}
func (m *linesModel) GetCursor() (int, int, bool, bool) {
return m.x, m.y, m.cursor, !m.hide
}