forked from gomatcha/matcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageview.go
199 lines (175 loc) · 4.52 KB
/
imageview.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
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
package view
import (
"context"
"fmt"
"image"
"image/color"
_ "image/jpeg"
_ "image/png"
"net/http"
"gomatcha.io/matcha"
"gomatcha.io/matcha/application"
"gomatcha.io/matcha/comm"
"gomatcha.io/matcha/internal"
"gomatcha.io/matcha/layout"
"gomatcha.io/matcha/paint"
pb "gomatcha.io/matcha/proto"
pbview "gomatcha.io/matcha/proto/view"
)
type ImageResizeMode int
// TODO(KD): ResizeModeFit and ResizeModeFill behave oddly on Android.
const (
// The image is resized proportionally such that a single axis is filled.
ImageResizeModeFit ImageResizeMode = iota
// The image is resized proportionally such that the entire view is filled.
ImageResizeModeFill
// The image is stretched to fill the view.
ImageResizeModeStretch
// The image is centered in the view with its natural size.
ImageResizeModeCenter
)
func (m ImageResizeMode) MarshalProtobuf() pbview.ImageResizeMode {
return pbview.ImageResizeMode(m)
}
// ImageView implements a view that displays an image.
type ImageView struct {
Embed
Image image.Image
ResizeMode ImageResizeMode
ImageTint color.Color
PaintStyle *paint.Style
stage Stage
URL string
cancelFunc context.CancelFunc
urlImage image.Image
err error
pbImage *pb.ImageOrResource
}
// NewImageView returns a new view.
func NewImageView() *ImageView {
return &ImageView{}
}
// Build implements view.View.
func (v *ImageView) Build(ctx Context) Model {
if v.pbImage == nil {
if v.Image != nil {
v.pbImage = internal.ImageMarshalProtobuf(v.Image)
} else if v.urlImage != nil {
v.pbImage = internal.ImageMarshalProtobuf(v.urlImage)
}
}
// Default to Center if we don't have an image
bounds := image.Rect(0, 0, 0, 0)
resizeMode := ImageResizeModeCenter
scale := 1.0
if v.Image != nil {
bounds = v.Image.Bounds()
resizeMode = v.ResizeMode
if res, ok := v.Image.(*application.ImageResource); ok {
scale = res.Scale()
}
}
var painter paint.Painter
if v.PaintStyle != nil {
painter = v.PaintStyle
}
return Model{
Painter: painter,
Layouter: &imageViewLayouter{bounds: bounds, resizeMode: resizeMode, scale: scale},
NativeViewName: "gomatcha.io/matcha/view/imageview",
NativeViewState: internal.MarshalProtobuf(&pbview.ImageView{
Image: v.pbImage,
Scale: scale,
ResizeMode: v.ResizeMode.MarshalProtobuf(),
Tint: pb.ColorEncode(v.ImageTint),
}),
}
}
// Lifecycle implements view.View.
func (v *ImageView) Lifecycle(from, to Stage) {
v.stage = to
v.reload()
}
func (v *ImageView) Update(v2 View) {
if v2.(*ImageView).Image != v.Image {
v.pbImage = nil
}
if v2.(*ImageView).URL != v.URL {
v.cancel()
v.urlImage = nil
v.err = nil
}
CopyFields(v, v2)
}
func (v *ImageView) reload() {
if v.stage < StageMounted {
v.cancel()
return
}
if v.URL == "" || v.Image != nil || v.cancelFunc != nil || v.urlImage != nil || v.err != nil {
return
}
c, cancelFunc := context.WithCancel(context.Background())
v.cancelFunc = cancelFunc
go func(url string) {
image, err := loadImageURL(url)
matcha.MainLocker.Lock()
defer matcha.MainLocker.Unlock()
select {
case <-c.Done():
default:
v.cancelFunc()
v.cancelFunc = nil
v.urlImage = image
v.err = err
v.Signal()
}
}(v.URL)
}
func (v *ImageView) cancel() {
if v.cancelFunc != nil {
v.cancelFunc()
v.cancelFunc = nil
}
}
type imageViewLayouter struct {
bounds image.Rectangle
scale float64
resizeMode ImageResizeMode
}
func (l *imageViewLayouter) Layout(ctx layout.Context) (layout.Guide, []layout.Guide) {
g := layout.Guide{Frame: layout.Rect{Max: ctx.MinSize()}}
switch l.resizeMode {
case ImageResizeModeFit:
imgRatio := float64(l.bounds.Dx()) / l.scale / float64(l.bounds.Dy()) / l.scale
maxRatio := ctx.MinSize().X / ctx.MinSize().Y
if imgRatio > maxRatio {
g.Frame.Max = layout.Pt(ctx.MinSize().X, ctx.MinSize().X/imgRatio)
} else {
g.Frame.Max = layout.Pt(ctx.MinSize().Y/imgRatio, ctx.MinSize().Y)
}
case ImageResizeModeFill:
fallthrough
case ImageResizeModeStretch:
g.Frame.Max = ctx.MinSize()
case ImageResizeModeCenter:
g.Frame.Max = layout.Pt(float64(l.bounds.Dx())/l.scale, float64(l.bounds.Dy())/l.scale)
}
return g, nil
}
func (l *imageViewLayouter) Notify(f func()) comm.Id {
return 0 // no-op
}
func (l *imageViewLayouter) Unnotify(id comm.Id) {
// no-op
}
func loadImageURL(url string) (image.Image, error) {
resp, err := http.Get(url)
if err != nil {
fmt.Println("loadImageURL error", err)
return nil, err
}
defer resp.Body.Close()
img, _, err := image.Decode(resp.Body)
return img, err
}