rui_orig/colorPicker.go

201 lines
5.6 KiB
Go
Raw Normal View History

2021-09-07 17:36:50 +03:00
package rui
import (
"strings"
)
// Constants for [ColorPicker] specific properties and events.
2021-09-07 17:36:50 +03:00
const (
// ColorChangedEvent is the constant for "color-changed" property tag.
//
// Used by `ColorPicker`.
// Event generated when color picker value has been changed.
//
// General listener format:
// `func(picker rui.ColorPicker, newColor, oldColor rui.Color)`.
//
// where:
// picker - Interface of a color picker which generated this event,
// newColor - New color value,
// oldColor - Old color value.
//
// Allowed listener formats:
// `func(picker rui.ColorPicker, newColor rui.Color)`,
// `func(newColor, oldColor rui.Color)`,
// `func(newColor rui.Color)`,
// `func(picker rui.ColorPicker)`,
// `func()`.
2024-11-13 12:56:39 +03:00
ColorChangedEvent PropertyName = "color-changed"
// ColorPickerValue is the constant for "color-picker-value" property tag.
//
// Used by `ColorPicker`.
// Define current color picker value.
//
// Supported types: `Color`, `string`.
//
// Internal type is `Color`, other types converted to it during assignment.
// See `Color` description for more details.
2024-11-13 12:56:39 +03:00
ColorPickerValue PropertyName = "color-picker-value"
2021-09-07 17:36:50 +03:00
)
// ColorPicker represent a ColorPicker view
2021-09-07 17:36:50 +03:00
type ColorPicker interface {
View
}
type colorPickerData struct {
viewData
}
// NewColorPicker create new ColorPicker object and return it
func NewColorPicker(session Session, params Params) ColorPicker {
view := new(colorPickerData)
view.init(session)
2021-09-07 17:36:50 +03:00
setInitParams(view, params)
return view
}
func newColorPicker(session Session) View {
2024-11-13 12:56:39 +03:00
return new(colorPickerData)
2021-09-07 17:36:50 +03:00
}
func (picker *colorPickerData) init(session Session) {
picker.viewData.init(session)
2021-09-07 17:36:50 +03:00
picker.tag = "ColorPicker"
2024-04-23 18:24:51 +03:00
picker.hasHtmlDisabled = true
2021-09-07 17:36:50 +03:00
picker.properties[Padding] = Px(0)
2024-11-13 12:56:39 +03:00
picker.normalize = normalizeColorPickerTag
picker.set = colorPickerSet
picker.changed = colorPickerPropertyChanged
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
func normalizeColorPickerTag(tag PropertyName) PropertyName {
tag = defaultNormalize(tag)
2021-09-07 17:36:50 +03:00
switch tag {
case Value, ColorTag:
2021-09-07 17:36:50 +03:00
return ColorPickerValue
}
2024-11-13 12:56:39 +03:00
return normalizeDataListTag(tag)
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
func colorPickerSet(view View, tag PropertyName, value any) []PropertyName {
2021-09-07 17:36:50 +03:00
switch tag {
case ColorChangedEvent:
2024-11-13 12:56:39 +03:00
return setEventWithOldListener[ColorPicker, Color](view, tag, value)
2021-09-07 17:36:50 +03:00
case ColorPickerValue:
2024-11-13 12:56:39 +03:00
oldColor := GetColorPickerValue(view)
result := setColorProperty(view, ColorPickerValue, value)
if result != nil {
view.setRaw("old-color", oldColor)
2024-05-18 18:57:41 +03:00
}
2024-11-13 12:56:39 +03:00
return result
2024-05-18 18:57:41 +03:00
2024-11-13 12:56:39 +03:00
case DataList:
return setDataList(view, value, "")
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
return viewSet(view, tag, value)
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
func colorPickerPropertyChanged(view View, tag PropertyName) {
2021-09-07 17:36:50 +03:00
switch tag {
case ColorPickerValue:
2024-11-13 12:56:39 +03:00
color := GetColorPickerValue(view)
view.Session().callFunc("setInputValue", view.htmlID(), color.rgbString())
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
if listeners := GetColorChangedListeners(view); len(listeners) > 0 {
oldColor := Color(0)
if value := view.getRaw("old-color"); value != nil {
oldColor = value.(Color)
}
for _, listener := range listeners {
listener(view, color, oldColor)
}
2021-09-07 17:36:50 +03:00
}
2024-05-18 18:57:41 +03:00
2021-09-07 17:36:50 +03:00
default:
2024-11-13 12:56:39 +03:00
viewPropertyChanged(view, tag)
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
2021-09-07 17:36:50 +03:00
}
func (picker *colorPickerData) htmlTag() string {
return "input"
}
2024-05-18 18:57:41 +03:00
func (picker *colorPickerData) htmlSubviews(self View, buffer *strings.Builder) {
2024-11-13 12:56:39 +03:00
dataListHtmlSubviews(self, buffer, func(text string, session Session) string {
text, _ = session.resolveConstants(text)
return text
})
2024-05-18 18:57:41 +03:00
}
2021-09-07 17:36:50 +03:00
func (picker *colorPickerData) htmlProperties(self View, buffer *strings.Builder) {
picker.viewData.htmlProperties(self, buffer)
buffer.WriteString(` type="color" value="`)
buffer.WriteString(GetColorPickerValue(picker).rgbString())
2021-09-07 17:36:50 +03:00
buffer.WriteByte('"')
buffer.WriteString(` oninput="editViewInputEvent(this)"`)
2022-04-15 15:41:44 +03:00
if picker.getRaw(ClickEvent) == nil {
buffer.WriteString(` onclick="stopEventPropagation(this, event)"`)
}
2024-05-18 18:57:41 +03:00
2024-11-13 12:56:39 +03:00
dataListHtmlProperties(picker, buffer)
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
func (picker *colorPickerData) handleCommand(self View, command PropertyName, data DataObject) bool {
2021-09-07 17:36:50 +03:00
switch command {
case "textChanged":
if text, ok := data.PropertyValue("text"); ok {
if color, ok := StringToColor(text); ok {
2024-11-13 12:56:39 +03:00
oldColor := GetColorPickerValue(picker)
2021-09-07 17:36:50 +03:00
picker.properties[ColorPickerValue] = color
if color != oldColor {
2024-11-13 12:56:39 +03:00
for _, listener := range GetColorChangedListeners(picker) {
2023-04-23 18:27:04 +03:00
listener(picker, color, oldColor)
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
if listener, ok := picker.changeListener[ColorPickerValue]; ok {
listener(picker, ColorPickerValue)
}
2021-09-07 17:36:50 +03:00
}
}
}
return true
}
return picker.viewData.handleCommand(self, command, data)
}
// GetColorPickerValue returns the value of ColorPicker subview.
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
func GetColorPickerValue(view View, subviewID ...string) Color {
if len(subviewID) > 0 && subviewID[0] != "" {
view = ViewByID(view, subviewID[0])
2021-09-07 17:36:50 +03:00
}
if view != nil {
2022-07-28 12:11:27 +03:00
if value, ok := colorProperty(view, ColorPickerValue, view.Session()); ok {
return value
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
for _, tag := range []PropertyName{ColorPickerValue, Value, ColorTag} {
2022-05-23 15:22:14 +03:00
if value := valueFromStyle(view, tag); value != nil {
2021-09-07 17:36:50 +03:00
if result, ok := valueToColor(value, view.Session()); ok {
return result
}
}
}
}
return 0
}
// GetColorChangedListeners returns the ColorChangedListener list of an ColorPicker subview.
// If there are no listeners then the empty list is returned
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
2023-04-23 18:27:04 +03:00
func GetColorChangedListeners(view View, subviewID ...string) []func(ColorPicker, Color, Color) {
return getEventWithOldListeners[ColorPicker, Color](view, subviewID, ColorChangedEvent)
2021-09-07 17:36:50 +03:00
}