2021-09-07 17:36:50 +03:00
|
|
|
package rui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// Constants for [VideoPlayer] specific properties and events
|
2021-09-07 17:36:50 +03:00
|
|
|
const (
|
2024-09-18 13:50:06 +03:00
|
|
|
// 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.
|
2021-09-07 17:36:50 +03:00
|
|
|
VideoWidth = "video-width"
|
2024-04-23 19:34:36 +03:00
|
|
|
|
2024-09-18 13:50:06 +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.
|
2021-09-07 17:36:50 +03:00
|
|
|
VideoHeight = "video-height"
|
2024-04-23 19:34:36 +03:00
|
|
|
|
2024-09-18 13:50:06 +03:00
|
|
|
// Poster is the constant for "poster" property tag.
|
|
|
|
//
|
|
|
|
// Used by `VideoPlayer`.
|
|
|
|
// 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`.
|
2021-09-07 17:36:50 +03:00
|
|
|
Poster = "poster"
|
|
|
|
)
|
|
|
|
|
2024-09-12 14:05:11 +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)
|
2022-09-01 11:04:50 +03:00
|
|
|
view.init(session)
|
2021-09-07 17:36:50 +03:00
|
|
|
view.tag = "VideoPlayer"
|
|
|
|
setInitParams(view, params)
|
|
|
|
return view
|
|
|
|
}
|
|
|
|
|
|
|
|
func newVideoPlayer(session Session) View {
|
|
|
|
return NewVideoPlayer(session, nil)
|
|
|
|
}
|
|
|
|
|
2022-09-01 11:04:50 +03:00
|
|
|
func (player *videoPlayerData) init(session Session) {
|
|
|
|
player.mediaPlayerData.init(session)
|
2021-09-07 17:36:50 +03:00
|
|
|
player.tag = "VideoPlayer"
|
|
|
|
}
|
|
|
|
|
2022-05-22 12:54:02 +03:00
|
|
|
func (player *videoPlayerData) String() string {
|
2024-05-02 15:07:57 +03:00
|
|
|
return getViewString(player, nil)
|
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) Remove(tag string) {
|
|
|
|
player.remove(strings.ToLower(tag))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (player *videoPlayerData) remove(tag string) {
|
|
|
|
switch tag {
|
|
|
|
|
|
|
|
case VideoWidth:
|
|
|
|
delete(player.properties, tag)
|
2022-10-30 17:22:33 +03:00
|
|
|
player.session.removeProperty(player.htmlID(), "width")
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
case VideoHeight:
|
|
|
|
delete(player.properties, tag)
|
2022-10-30 17:22:33 +03:00
|
|
|
player.session.removeProperty(player.htmlID(), "height")
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
case Poster:
|
|
|
|
delete(player.properties, tag)
|
2022-10-30 17:22:33 +03:00
|
|
|
player.session.removeProperty(player.htmlID(), Poster)
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
player.mediaPlayerData.remove(tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (player *videoPlayerData) Set(tag string, value any) bool {
|
2021-09-07 17:36:50 +03:00
|
|
|
return player.set(strings.ToLower(tag), value)
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (player *videoPlayerData) set(tag string, value any) bool {
|
2021-09-07 17:36:50 +03:00
|
|
|
if value == nil {
|
|
|
|
player.remove(tag)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if player.mediaPlayerData.set(tag, value) {
|
|
|
|
session := player.Session()
|
|
|
|
updateSize := func(cssTag string) {
|
2022-08-18 18:18:36 +03:00
|
|
|
if size, ok := floatTextProperty(player, tag, session, 0); ok {
|
|
|
|
if size != "0" {
|
2022-10-30 17:22:33 +03:00
|
|
|
session.updateProperty(player.htmlID(), cssTag, size)
|
2021-09-07 17:36:50 +03:00
|
|
|
} else {
|
2022-10-30 17:22:33 +03:00
|
|
|
session.removeProperty(player.htmlID(), cssTag)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch tag {
|
|
|
|
case VideoWidth:
|
|
|
|
updateSize("width")
|
|
|
|
|
|
|
|
case VideoHeight:
|
|
|
|
updateSize("height")
|
|
|
|
|
|
|
|
case Poster:
|
|
|
|
if url, ok := stringProperty(player, Poster, session); ok {
|
2022-10-30 17:22:33 +03:00
|
|
|
session.updateProperty(player.htmlID(), Poster, url)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
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(`"`)
|
|
|
|
}
|
|
|
|
}
|