rui_orig/progressBar.go

114 lines
3.2 KiB
Go
Raw Normal View History

2021-09-07 17:36:50 +03:00
package rui
import (
"strconv"
"strings"
)
// Constants for [ProgressBar] specific properties and events
2021-09-07 17:36:50 +03:00
const (
// ProgressBarMax is the constant for "progress-max" property tag.
//
// 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-11-13 12:56:39 +03:00
ProgressBarMax PropertyName = "progress-max"
// ProgressBarValue is the constant for "progress-value" property tag.
//
// Used by `ProgressBar`.
// Current value, default is 0.
//
// Supported types: `float`, `int`, `string`.
//
// Internal type is `float`, other types converted to it during assignment.
2024-11-13 12:56:39 +03:00
ProgressBarValue PropertyName = "progress-value"
2021-09-07 17:36:50 +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)
view.init(session)
2021-09-07 17:36:50 +03:00
setInitParams(view, params)
return view
}
func newProgressBar(session Session) View {
2024-11-13 12:56:39 +03:00
return new(progressBarData)
2021-09-07 17:36:50 +03:00
}
func (progress *progressBarData) init(session Session) {
progress.viewData.init(session)
2021-09-07 17:36:50 +03:00
progress.tag = "ProgressBar"
2024-11-13 12:56:39 +03:00
progress.normalize = normalizeProgressBarTag
progress.changed = progress.propertyChanged
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
func normalizeProgressBarTag(tag PropertyName) PropertyName {
tag = defaultNormalize(tag)
2021-09-07 17:36:50 +03:00
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) propertyChanged(tag PropertyName) {
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
switch tag {
case ProgressBarMax:
progress.Session().updateProperty(progress.htmlID(), "max",
strconv.FormatFloat(GetProgressBarMax(progress), 'f', -1, 32))
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
case ProgressBarValue:
progress.Session().updateProperty(progress.htmlID(), "value",
strconv.FormatFloat(GetProgressBarValue(progress), 'f', -1, 32))
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
default:
progress.viewData.propertyChanged(tag)
2021-09-07 17:36:50 +03:00
}
}
func (progress *progressBarData) htmlTag() string {
return "progress"
}
func (progress *progressBarData) htmlProperties(self View, buffer *strings.Builder) {
progress.viewData.htmlProperties(self, buffer)
buffer.WriteString(` max="`)
buffer.WriteString(strconv.FormatFloat(GetProgressBarMax(progress), 'f', -1, 64))
2021-09-07 17:36:50 +03:00
buffer.WriteByte('"')
buffer.WriteString(` value="`)
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.
// 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.
// 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
}