rui_orig/imageView.go

381 lines
11 KiB
Go
Raw Normal View History

2021-09-07 17:36:50 +03:00
package rui
import (
"fmt"
"strings"
)
// Constants which represent [ImageView] specific properties and events
2021-09-07 17:36:50 +03:00
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()`.
2024-11-13 12:56:39 +03:00
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()`.
2024-11-13 12:56:39 +03:00
ErrorEvent PropertyName = "error-event"
2021-09-07 17:36:50 +03:00
// NoneFit - value of the "object-fit" property of an ImageView. The replaced content is not resized
NoneFit = 0
2021-09-07 17:36:50 +03:00
// ContainFit - value of the "object-fit" property of an ImageView. The replaced content
// is scaled to maintain its aspect ratio while fitting within the elements 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
2021-09-07 17:36:50 +03:00
// CoverFit - value of the "object-fit" property of an ImageView. The replaced content
// is sized to maintain its aspect ratio while filling the elements 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
2021-09-07 17:36:50 +03:00
// FillFit - value of the "object-fit" property of an ImageView. The replaced content is sized
// to fill the elements 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
2021-09-07 17:36:50 +03:00
// 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
2021-09-07 17:36:50 +03:00
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
2021-09-07 17:36:50 +03:00
}
type imageViewData struct {
viewData
naturalWidth float64
naturalHeight float64
currentSrc string
2021-09-07 17:36:50 +03:00
}
// NewImageView create new ImageView object and return it
func NewImageView(session Session, params Params) ImageView {
view := new(imageViewData)
view.init(session)
2021-09-07 17:36:50 +03:00
setInitParams(view, params)
return view
}
func newImageView(session Session) View {
2024-11-13 12:56:39 +03:00
return new(imageViewData)
2021-09-07 17:36:50 +03:00
}
// Init initialize fields of imageView by default values
func (imageView *imageViewData) init(session Session) {
imageView.viewData.init(session)
2021-09-07 17:36:50 +03:00
imageView.tag = "ImageView"
2023-04-23 12:32:03 +03:00
imageView.systemClass = "ruiImageView"
2024-11-13 12:56:39 +03:00
imageView.normalize = normalizeImageViewTag
imageView.set = imageView.setFunc
imageView.changed = imageView.propertyChanged
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
func normalizeImageViewTag(tag PropertyName) PropertyName {
tag = defaultNormalize(tag)
2021-09-07 17:36:50 +03:00
switch tag {
case "source":
tag = Source
2022-11-11 12:55:58 +03:00
case "src-set", "source-set":
tag = SrcSet
2021-09-07 17:36:50 +03:00
case VerticalAlign:
tag = ImageVerticalAlign
case HorizontalAlign:
tag = ImageHorizontalAlign
case altTag:
2021-09-07 17:36:50 +03:00
tag = AltText
}
return tag
}
func (imageView *imageViewData) setFunc(tag PropertyName, value any) []PropertyName {
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
switch tag {
case Source, SrcSet, AltText:
if text, ok := value.(string); ok {
return setStringPropertyValue(imageView, tag, text)
}
2024-11-13 12:56:39 +03:00
notCompatibleType(tag, value)
return nil
case LoadedEvent, ErrorEvent:
return setNoArgEventListener[ImageView](imageView, tag, value)
2021-09-07 17:36:50 +03:00
}
return imageView.viewData.setFunc(tag, value)
2021-09-07 17:36:50 +03:00
}
func (imageView *imageViewData) propertyChanged(tag PropertyName) {
session := imageView.Session()
htmlID := imageView.htmlID()
2021-09-07 17:36:50 +03:00
switch tag {
case Source:
src, srcset := imageViewSrc(imageView, GetImageViewSource(imageView))
2024-11-13 12:56:39 +03:00
session.updateProperty(htmlID, "src", src)
if srcset != "" {
session.updateProperty(htmlID, "srcset", srcset)
} else {
session.removeProperty(htmlID, "srcset")
2021-09-07 17:36:50 +03:00
}
2022-11-11 12:55:58 +03:00
case SrcSet:
_, srcset := imageViewSrc(imageView, GetImageViewSource(imageView))
2024-11-13 12:56:39 +03:00
if srcset != "" {
session.updateProperty(htmlID, "srcset", srcset)
} else {
session.removeProperty(htmlID, "srcset")
2022-11-11 12:55:58 +03:00
}
2021-09-07 17:36:50 +03:00
case AltText:
2024-11-13 12:56:39 +03:00
updateInnerHTML(htmlID, session)
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
case ImageVerticalAlign, ImageHorizontalAlign:
updateCSSStyle(htmlID, session)
2021-09-07 17:36:50 +03:00
default:
imageView.viewData.propertyChanged(tag)
}
}
2024-11-13 12:56:39 +03:00
func imageViewSrcSet(view View, path string) string {
if value := view.getRaw(SrcSet); value != nil {
2022-11-11 12:55:58 +03:00
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()
}
}
2021-09-07 17:36:50 +03:00
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"
}
2024-11-13 12:56:39 +03:00
func imageViewSrc(view View, src string) (string, string) {
2022-11-02 20:08:02 +03:00
if src != "" && src[0] == '@' {
2024-11-13 12:56:39 +03:00
if image, ok := view.Session().ImageConstant(src[1:]); ok {
2022-11-02 20:08:02 +03:00
src = image
} else {
src = ""
}
}
if src != "" {
2024-11-13 12:56:39 +03:00
return src, imageViewSrcSet(view, src)
2022-11-02 20:08:02 +03:00
}
return "", ""
2021-09-07 17:36:50 +03:00
}
func (imageView *imageViewData) htmlProperties(self View, buffer *strings.Builder) {
2022-11-02 20:08:02 +03:00
2021-09-07 17:36:50 +03:00
imageView.viewData.htmlProperties(self, buffer)
2022-04-23 18:13:35 +03:00
if imageResource, ok := imageProperty(imageView, Source, imageView.Session()); ok && imageResource != "" {
2024-11-13 12:56:39 +03:00
if src, srcset := imageViewSrc(imageView, imageResource); src != "" {
2022-04-23 18:13:35 +03:00
buffer.WriteString(` src="`)
2022-11-02 20:08:02 +03:00
buffer.WriteString(src)
2021-09-07 17:36:50 +03:00
buffer.WriteString(`"`)
2022-11-02 20:08:02 +03:00
if srcset != "" {
2022-04-23 18:13:35 +03:00
buffer.WriteString(` srcset="`)
buffer.WriteString(srcset)
buffer.WriteString(`"`)
}
2021-09-07 17:36:50 +03:00
}
}
2022-04-17 11:21:01 +03:00
if text := GetImageViewAltText(imageView); text != "" {
2022-04-17 11:21:01 +03:00
buffer.WriteString(` alt="`)
2022-10-29 21:08:51 +03:00
buffer.WriteString(text)
2022-04-17 11:21:01 +03:00
buffer.WriteString(`"`)
}
buffer.WriteString(` onload="imageLoaded(this, event)"`)
if len(getNoArgEventListeners[ImageView](imageView, nil, ErrorEvent)) > 0 {
buffer.WriteString(` onerror="imageError(this, event)"`)
}
2021-09-07 17:36:50 +03:00
}
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)
2021-09-07 17:36:50 +03:00
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)
}
}
2024-11-13 12:56:39 +03:00
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
}
2021-09-07 17:36:50 +03:00
// 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 len(subviewID) > 0 && subviewID[0] != "" {
view = ViewByID(view, subviewID[0])
2021-09-07 17:36:50 +03:00
}
2022-04-23 18:13:35 +03:00
if view != nil {
if image, ok := imageProperty(view, Source, view.Session()); ok {
return image
}
}
2021-09-07 17:36:50 +03:00
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 len(subviewID) > 0 && subviewID[0] != "" {
view = ViewByID(view, subviewID[0])
2022-05-17 10:46:00 +03:00
}
if view != nil {
if value := view.getRaw(AltText); value != nil {
if text, ok := value.(string); ok {
text, _ = view.Session().GetString(text)
return text
}
}
2021-09-07 17:36:50 +03:00
}
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 {
2022-07-28 12:41:50 +03:00
return enumStyledProperty(view, subviewID, Fit, NoneFit, false)
2021-09-07 17:36:50 +03:00
}
// 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 {
2022-07-28 12:41:50 +03:00
return enumStyledProperty(view, subviewID, ImageVerticalAlign, LeftAlign, false)
2021-09-07 17:36:50 +03:00
}
// 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 {
2022-07-28 12:41:50 +03:00
return enumStyledProperty(view, subviewID, ImageHorizontalAlign, LeftAlign, false)
2021-09-07 17:36:50 +03:00
}