From 05fa7250030c9f481a075f4201352cb5151d85de Mon Sep 17 00:00:00 2001 From: anoshenko Date: Sun, 23 Apr 2023 18:39:25 +0300 Subject: [PATCH] Changed the main NumberPicker listener --- README-ru.md | 14 ++++++++++--- README.md | 54 ++++++++++++++++++++++++++++--------------------- numberPicker.go | 38 +++++++++++++++++++--------------- 3 files changed, 64 insertions(+), 42 deletions(-) diff --git a/README-ru.md b/README-ru.md index 7da4b2e..cb63587 100644 --- a/README-ru.md +++ b/README-ru.md @@ -3178,13 +3178,21 @@ NumberPicker может работать в двух режимах: редак Для отслеживания изменения вводимого значения используется событие "number-changed" (константа NumberChangedEvent). Основной слушатель события имеет следующий формат: - func(picker NumberPicker, newValue float64) + func(picker NumberPicker, newValue, oldValue float64) -где второй аргумент это новое значение +где второй аргумент это новое значение, третий аргумент - предыдущее значение. + +Дополнительные слушатели события могут иметь следующий формат + + func(picker NumberPicker, newValue string) + func(newValue, oldValue string) + func(newValue string) + func(picker NumberPicker) + func() Получить текущий список слушателей изменения значения можно с помощью функции - func GetNumberChangedListeners(view View, subviewID ...string) []func(NumberPicker, float64) + func GetNumberChangedListeners(view View, subviewID ...string) []func(NumberPicker, float64, float64) ## DatePicker diff --git a/README.md b/README.md index 6a463ff..ef7a792 100644 --- a/README.md +++ b/README.md @@ -767,9 +767,9 @@ Example equivalent to view.Set(rui.Border, NewBorder(rui.Params{ - rui.Style: rui.SolidBorder, - rui.Width: rui.Px(1), - rui.ColorProperty: rui.Black, + rui.Style : rui.SolidBorder, + rui.Width : rui.Px(1), + rui.ColorTag: rui.Black, })) The BorderProperty interface can be converted to a ViewBorders structure using the Border function. @@ -831,9 +831,9 @@ Example equivalent to view.Set(rui.Border, NewBorder(rui.Params{ - rui.Style: rui.SolidBorder, - rui.Width: rui.Px(1), - rui.ColorProperty: rui.Black, + rui.Style : rui.SolidBorder, + rui.Width : rui.Px(1), + rui.ColorTag: rui.Black, })) ### "outline" and "outline-offset" properties @@ -1012,7 +1012,7 @@ The shadow has the following properties: | Property | Constant | Type | Description | |-----------------|---------------|----------|-----------------------------------------------------------------------| -| "color" | ColorProperty | Color | Shadow color | +| "color" | ColorTag | Color | Shadow color | | "inset" | Inset | bool | true - the shadow inside the View, false - outside | | "x-offset" | XOffset | SizeUnit | Offset the shadow along the X axis | | "y-offset" | YOffset | SizeUnit | Offset the shadow along the Y axis | @@ -1031,9 +1031,9 @@ The NewShadowWithParams function is used when constants must be used as paramete For example: shadow := NewShadowWithParams(rui.Params{ - rui.ColorProperty : "@shadowColor", - rui.BlurRadius : 8.0, - rui.Dilation : 16.0, + rui.ColorTag : "@shadowColor", + rui.BlurRadius: 8.0, + rui.Dilation : 16.0, }) ViewShadow, ViewShadow array, and ViewShadow textual representation can be assigned as a value to the "shadow" property. @@ -1652,8 +1652,8 @@ To create a ViewShadow for the text shadow, the following functions are used: The NewShadowWithParams function is used when constants must be used as parameters. For example: shadow := NewShadowWithParams(rui.Params{ - rui.ColorProperty : "@shadowColor", - rui.BlurRadius : 8.0, + rui.ColorTag : "@shadowColor", + rui.BlurRadius: 8.0, }) ViewShadow, ViewShadow array, ViewShadow textual representation can be assigned as a value to the "text-shadow" property (see above, section "The 'shadow' property"). @@ -2529,11 +2529,11 @@ The separator line is described by three attributes: line style, thickness, and The value of the "column-separator" property is stored as the ColumnSeparatorProperty interface, which implements the Properties interface (see above). ColumnSeparatorProperty can contain the following properties: -| Property | Constant | Type | Description | -|----------|---------------|----------|----------------| -| "style" | Style | int | Line style | -| "width" | Width | SizeUnit | Line thickness | -| "color" | ColorProperty | Color | Line color | +| Property | Constant | Type | Description | +|----------|-----------|----------|----------------| +| "style" | Style | int | Line style | +| "width" | Width | SizeUnit | Line thickness | +| "color" | ColorTag | Color | Line color | Line style can take the following values: @@ -2587,9 +2587,9 @@ For example equivalent to view.Set(rui.ColumnSeparator, ColumnSeparatorProperty(rui.Params{ - rui.Style: rui.SolidBorder, - rui.Width: rui.Px(1), - rui.ColorProperty: rui.Black, + rui.Style : rui.SolidBorder, + rui.Width : rui.Px(1), + rui.ColorTag: rui.Black, })) ### "column-fill" property @@ -3141,13 +3141,21 @@ You can read the values of these properties using the functions: The "number-changed" event (NumberChangedEvent constant) is used to track the change in the entered value. The main event listener has the following format: - func(picker NumberPicker, newValue float64) + func(picker NumberPicker, newValue, oldValue float64) -where the second argument is the new value +where the second argument is the new value, the third argument is the previous value. + +Additional event listeners can have the following format + + func(picker NumberPicker, newValue string) + func(newValue, oldValue string) + func(newValue string) + func(picker NumberPicker) + func() You can get the current list of value change listeners using the function - func GetNumberChangedListeners(view View, subviewID ...string) []func(NumberPicker, float64) + func GetNumberChangedListeners(view View, subviewID ...string) []func(NumberPicker, float64, float64) ## DatePicker diff --git a/numberPicker.go b/numberPicker.go index 551fe7c..d7b3745 100644 --- a/numberPicker.go +++ b/numberPicker.go @@ -29,7 +29,7 @@ type NumberPicker interface { type numberPickerData struct { viewData - numberChangedListeners []func(NumberPicker, float64) + numberChangedListeners []func(NumberPicker, float64, float64) } // NewNumberPicker create new NumberPicker object and return it @@ -47,7 +47,7 @@ func newNumberPicker(session Session) View { func (picker *numberPickerData) init(session Session) { picker.viewData.init(session) picker.tag = "NumberPicker" - picker.numberChangedListeners = []func(NumberPicker, float64){} + picker.numberChangedListeners = []func(NumberPicker, float64, float64){} } func (picker *numberPickerData) String() string { @@ -76,7 +76,20 @@ func (picker *numberPickerData) remove(tag string) { switch tag { case NumberChangedEvent: if len(picker.numberChangedListeners) > 0 { - picker.numberChangedListeners = []func(NumberPicker, float64){} + picker.numberChangedListeners = []func(NumberPicker, float64, float64){} + picker.propertyChangedEvent(tag) + } + + case NumberPickerValue: + oldValue := GetNumberPickerValue(picker) + picker.viewData.remove(tag) + if oldValue != 0 { + if picker.created { + picker.session.callFunc("setInputValue", picker.htmlID(), 0) + } + for _, listener := range picker.numberChangedListeners { + listener(picker, 0, oldValue) + } picker.propertyChangedEvent(tag) } @@ -98,12 +111,12 @@ func (picker *numberPickerData) set(tag string, value any) bool { switch tag { case NumberChangedEvent: - listeners, ok := valueToEventListeners[NumberPicker, float64](value) + listeners, ok := valueToEventWithOldListeners[NumberPicker, float64](value) if !ok { notCompatibleType(tag, value) return false } else if listeners == nil { - listeners = []func(NumberPicker, float64){} + listeners = []func(NumberPicker, float64, float64){} } picker.numberChangedListeners = listeners picker.propertyChangedEvent(tag) @@ -119,7 +132,7 @@ func (picker *numberPickerData) set(tag string, value any) bool { picker.session.callFunc("setInputValue", picker.htmlID(), newValue) } for _, listener := range picker.numberChangedListeners { - listener(picker, f) + listener(picker, f, oldValue) } picker.propertyChangedEvent(tag) } @@ -159,13 +172,6 @@ func (picker *numberPickerData) propertyChanged(tag string) { } else { picker.session.updateProperty(picker.htmlID(), Step, "any") } - - case NumberPickerValue: - value := GetNumberPickerValue(picker) - picker.session.callFunc("setInputValue", picker.htmlID(), value) - for _, listener := range picker.numberChangedListeners { - listener(picker, value) - } } } } @@ -242,7 +248,7 @@ func (picker *numberPickerData) handleCommand(self View, command string, data Da picker.properties[NumberPickerValue] = text if value != oldValue { for _, listener := range picker.numberChangedListeners { - listener(picker, value) + listener(picker, value, oldValue) } } } @@ -323,6 +329,6 @@ func GetNumberPickerValue(view View, subviewID ...string) float64 { // GetNumberChangedListeners returns the NumberChangedListener list of an NumberPicker 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. -func GetNumberChangedListeners(view View, subviewID ...string) []func(NumberPicker, float64) { - return getEventListeners[NumberPicker, float64](view, subviewID, NumberChangedEvent) +func GetNumberChangedListeners(view View, subviewID ...string) []func(NumberPicker, float64, float64) { + return getEventWithOldListeners[NumberPicker, float64](view, subviewID, NumberChangedEvent) }