2021-09-07 17:36:50 +03:00
|
|
|
package rui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// Constants for [ColorPicker] specific properties and events.
|
2021-09-07 17:36:50 +03:00
|
|
|
const (
|
2024-09-12 14:05:11 +03:00
|
|
|
// ColorChangedEvent is the constant for "color-changed" property tag.
|
2024-09-18 13:50:06 +03:00
|
|
|
//
|
|
|
|
// 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"
|
2024-09-12 14:05:11 +03:00
|
|
|
|
|
|
|
// ColorPickerValue is the constant for "color-picker-value" property tag.
|
2024-09-18 13:50:06 +03:00
|
|
|
//
|
|
|
|
// 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
|
|
|
)
|
|
|
|
|
2024-09-12 14:05:11 +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)
|
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 newColorPicker(session Session) View {
|
2024-11-13 12:56:39 +03:00
|
|
|
return new(colorPickerData)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
2022-09-01 11:04: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
|
2024-11-18 17:20:25 +03:00
|
|
|
picker.set = picker.setFunc
|
|
|
|
picker.changed = picker.propertyChanged
|
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 {
|
2021-10-04 18:05:57 +03:00
|
|
|
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-18 17:20:25 +03:00
|
|
|
func (picker *colorPickerData) setFunc(tag PropertyName, value any) []PropertyName {
|
2021-09-07 17:36:50 +03:00
|
|
|
switch tag {
|
|
|
|
case ColorChangedEvent:
|
2024-11-18 17:20:25 +03:00
|
|
|
return setTwoArgEventListener[ColorPicker, Color](picker, tag, value)
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
case ColorPickerValue:
|
2024-11-18 17:20:25 +03:00
|
|
|
oldColor := GetColorPickerValue(picker)
|
|
|
|
result := setColorProperty(picker, ColorPickerValue, value)
|
2024-11-13 12:56:39 +03:00
|
|
|
if result != nil {
|
2024-11-18 17:20:25 +03:00
|
|
|
picker.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:
|
2024-11-18 17:20:25 +03:00
|
|
|
return setDataList(picker, value, "")
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
2024-11-18 17:20:25 +03:00
|
|
|
return picker.viewData.setFunc(tag, value)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
2024-11-18 17:20:25 +03:00
|
|
|
func (picker *colorPickerData) propertyChanged(tag PropertyName) {
|
2021-09-07 17:36:50 +03:00
|
|
|
switch tag {
|
|
|
|
case ColorPickerValue:
|
2024-11-18 17:20:25 +03:00
|
|
|
color := GetColorPickerValue(picker)
|
|
|
|
picker.Session().callFunc("setInputValue", picker.htmlID(), color.rgbString())
|
2021-09-07 17:36:50 +03:00
|
|
|
|
2024-11-18 17:20:25 +03:00
|
|
|
if listeners := GetColorChangedListeners(picker); len(listeners) > 0 {
|
2024-11-13 12:56:39 +03:00
|
|
|
oldColor := Color(0)
|
2024-11-18 17:20:25 +03:00
|
|
|
if value := picker.getRaw("old-color"); value != nil {
|
2024-11-13 12:56:39 +03:00
|
|
|
oldColor = value.(Color)
|
|
|
|
}
|
|
|
|
for _, listener := range listeners {
|
2024-11-18 17:20:25 +03:00
|
|
|
listener(picker, color, oldColor)
|
2024-11-13 12:56:39 +03:00
|
|
|
}
|
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-18 17:20:25 +03:00
|
|
|
picker.viewData.propertyChanged(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="`)
|
2022-08-31 22:17:46 +03:00
|
|
|
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.
|
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 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
|
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.
|
2023-04-23 18:27:04 +03:00
|
|
|
func GetColorChangedListeners(view View, subviewID ...string) []func(ColorPicker, Color, Color) {
|
2024-11-18 17:20:25 +03:00
|
|
|
return getTwoArgEventListeners[ColorPicker, Color](view, subviewID, ColorChangedEvent)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|