2021-09-07 17:36:50 +03:00
|
|
|
package rui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// Constants for [ProgressBar] specific properties and events
|
2021-09-07 17:36:50 +03:00
|
|
|
const (
|
2024-09-12 14:05:11 +03:00
|
|
|
// ProgressBarMax is the constant for "progress-max" property tag.
|
2024-09-18 13:50:06 +03:00
|
|
|
//
|
|
|
|
// Used by `ProgressBar`.
|
|
|
|
// Maximum value, default is 1.
|
|
|
|
//
|
|
|
|
// Supported types: `float`, `int`, `string`.
|
|
|
|
//
|
|
|
|
// Internal type is `float`, other types converted to it during assignment.
|
2024-09-12 14:05:11 +03:00
|
|
|
ProgressBarMax = "progress-max"
|
|
|
|
|
|
|
|
// ProgressBarValue is the constant for "progress-value" property tag.
|
2024-09-18 13:50:06 +03:00
|
|
|
//
|
|
|
|
// Used by `ProgressBar`.
|
|
|
|
// Current value, default is 0.
|
|
|
|
//
|
|
|
|
// Supported types: `float`, `int`, `string`.
|
|
|
|
//
|
|
|
|
// Internal type is `float`, other types converted to it during assignment.
|
2021-09-07 17:36:50 +03:00
|
|
|
ProgressBarValue = "progress-value"
|
|
|
|
)
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// ProgressBar represents a ProgressBar view
|
2021-09-07 17:36:50 +03:00
|
|
|
type ProgressBar interface {
|
|
|
|
View
|
|
|
|
}
|
|
|
|
|
|
|
|
type progressBarData struct {
|
|
|
|
viewData
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewProgressBar create new ProgressBar object and return it
|
|
|
|
func NewProgressBar(session Session, params Params) ProgressBar {
|
|
|
|
view := new(progressBarData)
|
2022-09-01 11:04:50 +03:00
|
|
|
view.init(session)
|
2021-09-07 17:36:50 +03:00
|
|
|
setInitParams(view, params)
|
|
|
|
return view
|
|
|
|
}
|
|
|
|
|
|
|
|
func newProgressBar(session Session) View {
|
|
|
|
return NewProgressBar(session, nil)
|
|
|
|
}
|
|
|
|
|
2022-09-01 11:04:50 +03:00
|
|
|
func (progress *progressBarData) init(session Session) {
|
|
|
|
progress.viewData.init(session)
|
2021-09-07 17:36:50 +03:00
|
|
|
progress.tag = "ProgressBar"
|
|
|
|
}
|
|
|
|
|
2022-05-22 12:54:02 +03:00
|
|
|
func (progress *progressBarData) String() string {
|
2024-05-02 15:07:57 +03:00
|
|
|
return getViewString(progress, nil)
|
2022-05-22 12:54:02 +03:00
|
|
|
}
|
|
|
|
|
2021-09-07 17:36:50 +03:00
|
|
|
func (progress *progressBarData) normalizeTag(tag string) string {
|
|
|
|
tag = strings.ToLower(tag)
|
|
|
|
switch tag {
|
|
|
|
case Max, "progress-bar-max", "progressbar-max":
|
|
|
|
return ProgressBarMax
|
|
|
|
|
|
|
|
case Value, "progress-bar-value", "progressbar-value":
|
|
|
|
return ProgressBarValue
|
|
|
|
}
|
|
|
|
return tag
|
|
|
|
}
|
|
|
|
|
|
|
|
func (progress *progressBarData) Remove(tag string) {
|
|
|
|
progress.remove(progress.normalizeTag(tag))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (progress *progressBarData) remove(tag string) {
|
|
|
|
progress.viewData.remove(tag)
|
|
|
|
progress.propertyChanged(tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (progress *progressBarData) propertyChanged(tag string) {
|
2021-11-20 11:15:28 +03:00
|
|
|
if progress.created {
|
|
|
|
switch tag {
|
|
|
|
case ProgressBarMax:
|
2022-10-30 17:22:33 +03:00
|
|
|
progress.session.updateProperty(progress.htmlID(), Max,
|
|
|
|
strconv.FormatFloat(GetProgressBarMax(progress), 'f', -1, 32))
|
2021-11-20 11:15:28 +03:00
|
|
|
|
|
|
|
case ProgressBarValue:
|
2022-10-30 17:22:33 +03:00
|
|
|
progress.session.updateProperty(progress.htmlID(), Value,
|
|
|
|
strconv.FormatFloat(GetProgressBarValue(progress), 'f', -1, 32))
|
2021-11-20 11:15:28 +03:00
|
|
|
}
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (progress *progressBarData) Set(tag string, value any) bool {
|
2021-09-07 17:36:50 +03:00
|
|
|
return progress.set(progress.normalizeTag(tag), value)
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (progress *progressBarData) set(tag string, value any) bool {
|
2021-09-07 17:36:50 +03:00
|
|
|
if progress.viewData.set(tag, value) {
|
|
|
|
progress.propertyChanged(tag)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (progress *progressBarData) Get(tag string) any {
|
2021-09-07 17:36:50 +03:00
|
|
|
return progress.get(progress.normalizeTag(tag))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (progress *progressBarData) htmlTag() string {
|
|
|
|
return "progress"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (progress *progressBarData) htmlProperties(self View, buffer *strings.Builder) {
|
|
|
|
progress.viewData.htmlProperties(self, buffer)
|
|
|
|
|
|
|
|
buffer.WriteString(` max="`)
|
2022-08-31 22:17:46 +03:00
|
|
|
buffer.WriteString(strconv.FormatFloat(GetProgressBarMax(progress), 'f', -1, 64))
|
2021-09-07 17:36:50 +03:00
|
|
|
buffer.WriteByte('"')
|
|
|
|
|
|
|
|
buffer.WriteString(` value="`)
|
2022-08-31 22:17:46 +03:00
|
|
|
buffer.WriteString(strconv.FormatFloat(GetProgressBarValue(progress), 'f', -1, 64))
|
2021-09-07 17:36:50 +03:00
|
|
|
buffer.WriteByte('"')
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetProgressBarMax returns the max value of ProgressBar subview.
|
2022-08-31 22:17:46 +03:00
|
|
|
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
|
|
|
|
func GetProgressBarMax(view View, subviewID ...string) float64 {
|
2022-07-28 12:11:27 +03:00
|
|
|
return floatStyledProperty(view, subviewID, ProgressBarMax, 1)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetProgressBarValue returns the value of ProgressBar subview.
|
2022-08-31 22:17:46 +03:00
|
|
|
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
|
|
|
|
func GetProgressBarValue(view View, subviewID ...string) float64 {
|
2022-07-28 12:11:27 +03:00
|
|
|
return floatStyledProperty(view, subviewID, ProgressBarValue, 0)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|