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:
|
2024-12-05 20:15:39 +03:00
|
|
|
// func(picker rui.ColorPicker, newColor, oldColor rui.Color)
|
2024-09-18 13:50:06 +03:00
|
|
|
//
|
|
|
|
|
// where:
|
2024-12-05 20:15:39 +03:00
|
|
|
// - picker - Interface of a color picker which generated this event,
|
|
|
|
|
// - newColor - New color value,
|
|
|
|
|
// - oldColor - Old color value.
|
2024-09-18 13:50:06 +03:00
|
|
|
//
|
|
|
|
|
// Allowed listener formats:
|
2024-12-05 20:15:39 +03:00
|
|
|
// 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 {
|
2026-11-05 13:31:38 +03:00
|
|
|
if session == nil {
|
|
|
|
|
ErrorLog(`the Session argument cannot be nil`)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-07 17:36:50 +03:00
|
|
|
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"
|
|
|
|
|
picker.properties[Padding] = Px(0)
|
2024-11-13 12:56:39 +03:00
|
|
|
picker.normalize = normalizeColorPickerTag
|
2025-07-10 12:54:04 +03:00
|
|
|
picker.get = picker.getFunc
|
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
|
|
|
}
|
|
|
|
|
|
2025-07-10 12:54:04 +03:00
|
|
|
func (picker *colorPickerData) getFunc(tag PropertyName) any {
|
|
|
|
|
switch tag {
|
|
|
|
|
case ColorChangedEvent:
|
|
|
|
|
if listeners := getTwoArgEventRawListeners[ColorPicker, Color](picker, nil, tag); len(listeners) > 0 {
|
|
|
|
|
return listeners
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return picker.viewData.getFunc(tag)
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2025-06-18 13:24:53 +03:00
|
|
|
if listeners := getTwoArgEventListeners[ColorPicker, Color](picker, nil, ColorChangedEvent); 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 {
|
2025-06-18 13:24:53 +03:00
|
|
|
listener.Run(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)
|
|
|
|
|
|
2026-09-03 12:39:07 +03:00
|
|
|
writeStrings(buffer, ` type="color" value="`, GetColorPickerValue(picker).rgbString(), `"`,
|
|
|
|
|
` 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":
|
2026-10-18 16:50:24 +03:00
|
|
|
if text, ok := data.PropertyText("text"); ok {
|
2021-09-07 17:36:50 +03:00
|
|
|
if color, ok := StringToColor(text); ok {
|
2024-11-13 12:56:39 +03:00
|
|
|
oldColor := GetColorPickerValue(picker)
|
2026-08-15 11:33:22 +03:00
|
|
|
picker.setRaw(ColorPickerValue, color)
|
2021-09-07 17:36:50 +03:00
|
|
|
if color != oldColor {
|
2025-06-18 13:24:53 +03:00
|
|
|
for _, listener := range getTwoArgEventListeners[ColorPicker, Color](picker, nil, ColorChangedEvent) {
|
|
|
|
|
listener.Run(picker, color, oldColor)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
2025-07-06 11:20:37 +03:00
|
|
|
picker.runChangeListener(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.
|
2025-06-17 21:08:16 +03:00
|
|
|
//
|
|
|
|
|
// The second argument (subviewID) specifies the path to the child element whose value needs to be returned.
|
|
|
|
|
// If it is not specified then a value from the first argument (view) is returned.
|
2022-08-31 22:17:46 +03:00
|
|
|
func GetColorPickerValue(view View, subviewID ...string) Color {
|
2024-11-24 16:43:31 +03:00
|
|
|
if view = getSubview(view, subviewID); view != nil {
|
2026-07-09 20:18:29 +03:00
|
|
|
if lightColor, darkColor, ok := colorProperty(view, ColorPickerValue, view.Session()); ok {
|
|
|
|
|
if view.Session().DarkTheme() {
|
|
|
|
|
return darkColor
|
|
|
|
|
}
|
|
|
|
|
return lightColor
|
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 {
|
2026-07-09 20:18:29 +03:00
|
|
|
if lightColor, darkColor, ok := valueToColor(value, view.Session()); ok {
|
|
|
|
|
if view.Session().DarkTheme() {
|
|
|
|
|
return darkColor
|
|
|
|
|
}
|
|
|
|
|
return lightColor
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetColorChangedListeners returns the ColorChangedListener list of an ColorPicker subview.
|
|
|
|
|
// If there are no listeners then the empty list is returned
|
2025-06-17 21:08:16 +03:00
|
|
|
//
|
2025-06-18 13:24:53 +03:00
|
|
|
// Result elements can be of the following types:
|
|
|
|
|
// - func(rui.ColorPicker, rui.Color, rui.Color),
|
|
|
|
|
// - func(rui.ColorPicker, rui.Color),
|
|
|
|
|
// - func(rui.ColorPicker),
|
|
|
|
|
// - func(rui.Color, rui.Color),
|
|
|
|
|
// - func(rui.Color),
|
|
|
|
|
// - func(),
|
|
|
|
|
// - string.
|
|
|
|
|
//
|
2025-06-17 21:08:16 +03:00
|
|
|
// The second argument (subviewID) specifies the path to the child element whose value needs to be returned.
|
|
|
|
|
// If it is not specified then a value from the first argument (view) is returned.
|
2025-06-18 13:24:53 +03:00
|
|
|
func GetColorChangedListeners(view View, subviewID ...string) []any {
|
|
|
|
|
return getTwoArgEventRawListeners[ColorPicker, Color](view, subviewID, ColorChangedEvent)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|