-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimageView.go
372 lines (316 loc) · 10.9 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
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
package rui
import (
"fmt"
"strings"
)
// Constants which represent [ImageView] specific properties and events
const (
// LoadedEvent is the constant for "loaded-event" property tag.
//
// Used by ImageView.
// Occur when the image has been loaded.
//
// General listener format:
// func(image rui.ImageView)
//
// where:
// image - Interface of an image view which generated this event.
//
// Allowed listener formats:
// func()
LoadedEvent PropertyName = "loaded-event"
// ErrorEvent is the constant for "error-event" property tag.
//
// Used by ImageView.
// Occur when the image loading has been failed.
//
// General listener format:
// func(image rui.ImageView)
//
// where:
// image - Interface of an image view which generated this event.
//
// Allowed listener formats:
// func()
ErrorEvent PropertyName = "error-event"
// NoneFit - value of the "object-fit" property of an ImageView. The replaced content is not resized
NoneFit = 0
// ContainFit - value of the "object-fit" property of an ImageView. The replaced content
// is scaled to maintain its aspect ratio while fitting within the element’s content box.
// The entire object is made to fill the box, while preserving its aspect ratio, so the object
// will be "letterboxed" if its aspect ratio does not match the aspect ratio of the box.
ContainFit = 1
// CoverFit - value of the "object-fit" property of an ImageView. The replaced content
// is sized to maintain its aspect ratio while filling the element’s entire content box.
// If the object's aspect ratio does not match the aspect ratio of its box, then the object will be clipped to fit.
CoverFit = 2
// FillFit - value of the "object-fit" property of an ImageView. The replaced content is sized
// to fill the element’s content box. The entire object will completely fill the box.
// If the object's aspect ratio does not match the aspect ratio of its box, then the object will be stretched to fit.
FillFit = 3
// ScaleDownFit - value of the "object-fit" property of an ImageView. The content is sized as
// if NoneFit or ContainFit were specified, whichever would result in a smaller concrete object size.
ScaleDownFit = 4
)
// ImageView represents an ImageView view
type ImageView interface {
View
// NaturalSize returns the intrinsic, density-corrected size (width, height) of the image in pixels.
// If the image hasn't been loaded yet or an load error has occurred, then (0, 0) is returned.
NaturalSize() (float64, float64)
// CurrentSource() return the full URL of the image currently visible in the ImageView.
// If the image hasn't been loaded yet or an load error has occurred, then "" is returned.
CurrentSource() string
}
type imageViewData struct {
viewData
naturalWidth float64
naturalHeight float64
currentSrc string
}
// NewImageView create new ImageView object and return it
func NewImageView(session Session, params Params) ImageView {
view := new(imageViewData)
view.init(session)
setInitParams(view, params)
return view
}
func newImageView(session Session) View {
return new(imageViewData)
}
// Init initialize fields of imageView by default values
func (imageView *imageViewData) init(session Session) {
imageView.viewData.init(session)
imageView.tag = "ImageView"
imageView.systemClass = "ruiImageView"
imageView.normalize = normalizeImageViewTag
imageView.set = imageView.setFunc
imageView.changed = imageView.propertyChanged
}
func normalizeImageViewTag(tag PropertyName) PropertyName {
tag = defaultNormalize(tag)
switch tag {
case "source":
tag = Source
case "src-set", "source-set":
tag = SrcSet
case VerticalAlign:
tag = ImageVerticalAlign
case HorizontalAlign:
tag = ImageHorizontalAlign
case altTag:
tag = AltText
}
return tag
}
func (imageView *imageViewData) setFunc(tag PropertyName, value any) []PropertyName {
switch tag {
case Source, SrcSet, AltText:
if text, ok := value.(string); ok {
return setStringPropertyValue(imageView, tag, text)
}
notCompatibleType(tag, value)
return nil
case LoadedEvent, ErrorEvent:
return setNoArgEventListener[ImageView](imageView, tag, value)
}
return imageView.viewData.setFunc(tag, value)
}
func (imageView *imageViewData) propertyChanged(tag PropertyName) {
session := imageView.Session()
htmlID := imageView.htmlID()
switch tag {
case Source:
src, srcset := imageViewSrc(imageView, GetImageViewSource(imageView))
session.updateProperty(htmlID, "src", src)
if srcset != "" {
session.updateProperty(htmlID, "srcset", srcset)
} else {
session.removeProperty(htmlID, "srcset")
}
case SrcSet:
_, srcset := imageViewSrc(imageView, GetImageViewSource(imageView))
if srcset != "" {
session.updateProperty(htmlID, "srcset", srcset)
} else {
session.removeProperty(htmlID, "srcset")
}
case AltText:
updateInnerHTML(htmlID, session)
case ImageVerticalAlign, ImageHorizontalAlign:
updateCSSStyle(htmlID, session)
default:
imageView.viewData.propertyChanged(tag)
}
}
func imageViewSrcSet(view View, path string) string {
if value := view.getRaw(SrcSet); value != nil {
if text, ok := value.(string); ok {
srcset := strings.Split(text, ",")
buffer := allocStringBuilder()
defer freeStringBuilder(buffer)
for i, src := range srcset {
if i > 0 {
buffer.WriteString(", ")
}
src = strings.Trim(src, " \t\n")
buffer.WriteString(src)
if index := strings.LastIndex(src, "@"); index > 0 {
if ext := strings.LastIndex(src, "."); ext > index {
buffer.WriteRune(' ')
buffer.WriteString(src[index+1 : ext])
}
} else {
buffer.WriteString(" 1x")
}
}
return buffer.String()
}
}
if srcset, ok := resources.imageSrcSets[path]; ok {
buffer := allocStringBuilder()
defer freeStringBuilder(buffer)
for i, src := range srcset {
if i > 0 {
buffer.WriteString(", ")
}
buffer.WriteString(src.path)
buffer.WriteString(fmt.Sprintf(" %gx", src.scale))
}
return buffer.String()
}
return ""
}
func (imageView *imageViewData) htmlTag() string {
return "img"
}
func imageViewSrc(view View, src string) (string, string) {
if src != "" && src[0] == '@' {
if image, ok := view.Session().ImageConstant(src[1:]); ok {
src = image
} else {
src = ""
}
}
if src != "" {
return src, imageViewSrcSet(view, src)
}
return "", ""
}
func (imageView *imageViewData) htmlProperties(self View, buffer *strings.Builder) {
imageView.viewData.htmlProperties(self, buffer)
if imageResource, ok := imageProperty(imageView, Source, imageView.Session()); ok && imageResource != "" {
if src, srcset := imageViewSrc(imageView, imageResource); src != "" {
buffer.WriteString(` src="`)
buffer.WriteString(src)
buffer.WriteString(`"`)
if srcset != "" {
buffer.WriteString(` srcset="`)
buffer.WriteString(srcset)
buffer.WriteString(`"`)
}
}
}
if text := GetImageViewAltText(imageView); text != "" {
buffer.WriteString(` alt="`)
buffer.WriteString(text)
buffer.WriteString(`"`)
}
buffer.WriteString(` onload="imageLoaded(this, event)"`)
if len(getNoArgEventListeners[ImageView](imageView, nil, ErrorEvent)) > 0 {
buffer.WriteString(` onerror="imageError(this, event)"`)
}
}
func (imageView *imageViewData) cssStyle(self View, builder cssBuilder) {
imageView.viewData.cssStyle(self, builder)
if value, ok := enumProperty(imageView, Fit, imageView.session, 0); ok {
builder.add("object-fit", enumProperties[Fit].cssValues[value])
} else {
builder.add("object-fit", "none")
}
vAlign := GetImageViewVerticalAlign(imageView)
hAlign := GetImageViewHorizontalAlign(imageView)
if vAlign != CenterAlign || hAlign != CenterAlign {
var position string
switch hAlign {
case LeftAlign:
position = "left"
case RightAlign:
position = "right"
default:
position = "center"
}
switch vAlign {
case TopAlign:
position += " top"
case BottomAlign:
position += " bottom"
default:
position += " center"
}
builder.add("object-position", position)
}
}
func (imageView *imageViewData) handleCommand(self View, command PropertyName, data DataObject) bool {
switch command {
case "imageViewError":
for _, listener := range getNoArgEventListeners[ImageView](imageView, nil, ErrorEvent) {
listener(imageView)
}
case "imageViewLoaded":
imageView.naturalWidth = dataFloatProperty(data, "natural-width")
imageView.naturalHeight = dataFloatProperty(data, "natural-height")
imageView.currentSrc, _ = data.PropertyValue("current-src")
for _, listener := range getNoArgEventListeners[ImageView](imageView, nil, LoadedEvent) {
listener(imageView)
}
default:
return imageView.viewData.handleCommand(self, command, data)
}
return true
}
func (imageView *imageViewData) NaturalSize() (float64, float64) {
return imageView.naturalWidth, imageView.naturalHeight
}
func (imageView *imageViewData) CurrentSource() string {
return imageView.currentSrc
}
// GetImageViewSource returns the image URL of an ImageView subview.
// If the second argument (subviewID) is not specified or it is "" then a left position of the first argument (view) is returned
func GetImageViewSource(view View, subviewID ...string) string {
if view = getSubview(view, subviewID); view != nil {
if image, ok := imageProperty(view, Source, view.Session()); ok {
return image
}
}
return ""
}
// GetImageViewAltText returns an alternative text description of an ImageView subview.
// If the second argument (subviewID) is not specified or it is "" then a left position of the first argument (view) is returned
func GetImageViewAltText(view View, subviewID ...string) string {
if view = getSubview(view, subviewID); view != nil {
if value := view.getRaw(AltText); value != nil {
if text, ok := value.(string); ok {
text, _ = view.Session().GetString(text)
return text
}
}
}
return ""
}
// GetImageViewFit returns how the content of a replaced ImageView subview:
// NoneFit (0), ContainFit (1), CoverFit (2), FillFit (3), or ScaleDownFit (4).
// If the second argument (subviewID) is not specified or it is "" then a left position of the first argument (view) is returned
func GetImageViewFit(view View, subviewID ...string) int {
return enumStyledProperty(view, subviewID, Fit, NoneFit, false)
}
// GetImageViewVerticalAlign return the vertical align of an ImageView subview: TopAlign (0), BottomAlign (1), CenterAlign (2)
// If the second argument (subviewID) is not specified or it is "" then a left position of the first argument (view) is returned
func GetImageViewVerticalAlign(view View, subviewID ...string) int {
return enumStyledProperty(view, subviewID, ImageVerticalAlign, LeftAlign, false)
}
// GetImageViewHorizontalAlign return the vertical align of an ImageView subview: LeftAlign (0), RightAlign (1), CenterAlign (2)
// If the second argument (subviewID) is not specified or it is "" then a left position of the first argument (view) is returned
func GetImageViewHorizontalAlign(view View, subviewID ...string) int {
return enumStyledProperty(view, subviewID, ImageHorizontalAlign, LeftAlign, false)
}