rui_orig/videoPlayer.go

126 lines
3.2 KiB
Go
Raw Permalink Normal View History

2021-09-07 17:36:50 +03:00
package rui
import (
"strings"
)
// Constants for [VideoPlayer] specific properties and events
2021-09-07 17:36:50 +03:00
const (
// VideoWidth is the constant for "video-width" property tag.
//
// Used by `VideoPlayer`.
// Defines the width of the video's display area in pixels.
//
// Supported types: `float`, `int`, `string`.
//
// Values:
// Internal type is `float`, other types converted to it during assignment.
2024-11-13 12:56:39 +03:00
VideoWidth PropertyName = "video-width"
2024-04-23 19:34:36 +03:00
// VideoHeight is the constant for "video-height" property tag.
//
// Used by `VideoPlayer`.
// Defines the height of the video's display area in pixels.
//
// Supported types: `float`, `int`, `string`.
//
// Internal type is `float`, other types converted to it during assignment.
2024-11-13 12:56:39 +03:00
VideoHeight PropertyName = "video-height"
2024-04-23 19:34:36 +03:00
// Poster is the constant for "poster" property tag.
//
// Used by `VideoPlayer`.
2024-11-13 12:56:39 +03:00
// Defines an URL for an image to be shown while the video is downloading. If this attribute isn't specified, nothing is
// displayed until the first frame is available, then the first frame is shown as the poster frame.
//
// Supported types: `string`.
2024-11-13 12:56:39 +03:00
Poster PropertyName = "poster"
2021-09-07 17:36:50 +03:00
)
// VideoPlayer is a type of a [View] which can play video files
2021-09-07 17:36:50 +03:00
type VideoPlayer interface {
MediaPlayer
}
type videoPlayerData struct {
mediaPlayerData
}
// NewVideoPlayer create new MediaPlayer object and return it
2021-11-04 21:21:42 +03:00
func NewVideoPlayer(session Session, params Params) VideoPlayer {
2021-09-07 17:36:50 +03:00
view := new(videoPlayerData)
view.init(session)
2021-09-07 17:36:50 +03:00
setInitParams(view, params)
return view
}
func newVideoPlayer(session Session) View {
2024-11-13 12:56:39 +03:00
return new(videoPlayerData) // NewVideoPlayer(session, nil)
2021-09-07 17:36:50 +03:00
}
func (player *videoPlayerData) init(session Session) {
player.mediaPlayerData.init(session)
2021-09-07 17:36:50 +03:00
player.tag = "VideoPlayer"
player.changed = player.propertyChanged
2022-05-22 12:54:02 +03:00
}
2021-09-07 17:36:50 +03:00
func (player *videoPlayerData) htmlTag() string {
return "video"
}
func (player *videoPlayerData) propertyChanged(tag PropertyName) {
2021-09-07 17:36:50 +03:00
session := player.Session()
2024-11-13 12:56:39 +03:00
updateSize := func(cssTag string) {
if size, ok := floatTextProperty(player, tag, session, 0); ok {
2024-11-13 12:56:39 +03:00
if size != "0" {
session.updateProperty(player.htmlID(), cssTag, size)
2024-11-13 12:56:39 +03:00
} else {
session.removeProperty(player.htmlID(), cssTag)
2024-11-13 12:56:39 +03:00
}
}
}
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
switch tag {
2021-09-07 17:36:50 +03:00
case VideoWidth:
2024-11-13 12:56:39 +03:00
updateSize("width")
2021-09-07 17:36:50 +03:00
case VideoHeight:
2024-11-13 12:56:39 +03:00
updateSize("height")
2021-09-07 17:36:50 +03:00
case Poster:
if url, ok := stringProperty(player, Poster, session); ok {
session.updateProperty(player.htmlID(), string(Poster), url)
2024-11-13 12:56:39 +03:00
} else {
session.removeProperty(player.htmlID(), string(Poster))
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
default:
player.mediaPlayerData.propertyChanged(tag)
2021-09-07 17:36:50 +03:00
}
}
func (player *videoPlayerData) htmlProperties(self View, buffer *strings.Builder) {
player.mediaPlayerData.htmlProperties(self, buffer)
session := player.Session()
2022-08-18 18:18:36 +03:00
if size, ok := floatTextProperty(player, VideoWidth, session, 0); ok && size != "0" {
buffer.WriteString(` width="`)
buffer.WriteString(size)
buffer.WriteString(`"`)
2021-09-07 17:36:50 +03:00
}
2022-08-18 18:18:36 +03:00
if size, ok := floatTextProperty(player, VideoHeight, session, 0); ok && size != "0" {
buffer.WriteString(` height="`)
buffer.WriteString(size)
buffer.WriteString(`"`)
2021-09-07 17:36:50 +03:00
}
if url, ok := stringProperty(player, Poster, session); ok && url != "" {
buffer.WriteString(` poster="`)
buffer.WriteString(url)
buffer.WriteString(`"`)
}
}