2021-09-07 17:36:50 +03:00
|
|
|
package rui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CheckboxChangedEvent is the constant for "checkbox-event" property tag.
|
|
|
|
// The "checkbox-event" event occurs when the checkbox becomes checked/unchecked.
|
|
|
|
// The main listener format: func(Checkbox, bool), where the second argument is the checkbox state.
|
|
|
|
const CheckboxChangedEvent = "checkbox-event"
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// Checkbox represent a Checkbox view
|
2021-09-07 17:36:50 +03:00
|
|
|
type Checkbox interface {
|
|
|
|
ViewsContainer
|
|
|
|
}
|
|
|
|
|
|
|
|
type checkboxData struct {
|
|
|
|
viewsContainerData
|
|
|
|
checkedListeners []func(Checkbox, bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCheckbox create new Checkbox object and return it
|
|
|
|
func NewCheckbox(session Session, params Params) Checkbox {
|
|
|
|
view := new(checkboxData)
|
2022-09-01 11:04:50 +03:00
|
|
|
view.init(session)
|
2021-09-07 17:36:50 +03:00
|
|
|
setInitParams(view, Params{
|
|
|
|
ClickEvent: checkboxClickListener,
|
|
|
|
KeyDownEvent: checkboxKeyListener,
|
|
|
|
})
|
|
|
|
setInitParams(view, params)
|
|
|
|
return view
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCheckbox(session Session) View {
|
|
|
|
return NewCheckbox(session, nil)
|
|
|
|
}
|
|
|
|
|
2022-09-01 11:04:50 +03:00
|
|
|
func (button *checkboxData) init(session Session) {
|
|
|
|
button.viewsContainerData.init(session)
|
2021-09-07 17:36:50 +03:00
|
|
|
button.tag = "Checkbox"
|
|
|
|
button.systemClass = "ruiGridLayout ruiCheckbox"
|
|
|
|
button.checkedListeners = []func(Checkbox, bool){}
|
|
|
|
}
|
|
|
|
|
2022-05-22 12:54:02 +03:00
|
|
|
func (button *checkboxData) String() string {
|
2024-05-02 15:07:57 +03:00
|
|
|
return getViewString(button, nil)
|
2022-05-22 12:54:02 +03:00
|
|
|
}
|
|
|
|
|
2021-09-07 17:36:50 +03:00
|
|
|
func (button *checkboxData) Focusable() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (button *checkboxData) Get(tag string) any {
|
2021-09-07 17:36:50 +03:00
|
|
|
switch strings.ToLower(tag) {
|
|
|
|
case CheckboxChangedEvent:
|
|
|
|
return button.checkedListeners
|
|
|
|
}
|
|
|
|
|
|
|
|
return button.viewsContainerData.Get(tag)
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (button *checkboxData) Set(tag string, value any) bool {
|
2021-11-20 11:15:28 +03:00
|
|
|
return button.set(tag, value)
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (button *checkboxData) set(tag string, value any) bool {
|
2021-11-20 11:15:28 +03:00
|
|
|
switch tag {
|
2021-09-07 17:36:50 +03:00
|
|
|
case CheckboxChangedEvent:
|
2021-11-20 11:15:28 +03:00
|
|
|
if !button.setChangedListener(value) {
|
2021-09-07 17:36:50 +03:00
|
|
|
notCompatibleType(tag, value)
|
2021-11-20 11:15:28 +03:00
|
|
|
return false
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
case Checked:
|
|
|
|
oldChecked := button.checked()
|
|
|
|
if !button.setBoolProperty(Checked, value) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if button.created {
|
|
|
|
checked := button.checked()
|
|
|
|
if checked != oldChecked {
|
|
|
|
button.changedCheckboxState(checked)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case CheckboxHorizontalAlign, CheckboxVerticalAlign:
|
2021-11-20 11:15:28 +03:00
|
|
|
if !button.setEnumProperty(tag, value, enumProperties[tag].values) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if button.created {
|
|
|
|
htmlID := button.htmlID()
|
|
|
|
updateCSSStyle(htmlID, button.session)
|
|
|
|
updateInnerHTML(htmlID, button.session)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
case VerticalAlign:
|
2021-11-20 11:15:28 +03:00
|
|
|
if !button.setEnumProperty(tag, value, enumProperties[tag].values) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if button.created {
|
2022-10-30 17:22:33 +03:00
|
|
|
button.session.updateCSSProperty(button.htmlID()+"content", "align-items", button.cssVerticalAlign())
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
case HorizontalAlign:
|
2021-11-20 11:15:28 +03:00
|
|
|
if !button.setEnumProperty(tag, value, enumProperties[tag].values) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if button.created {
|
2022-10-30 17:22:33 +03:00
|
|
|
button.session.updateCSSProperty(button.htmlID()+"content", "justify-items", button.cssHorizontalAlign())
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
case CellVerticalAlign, CellHorizontalAlign, CellWidth, CellHeight:
|
|
|
|
return false
|
2021-11-20 11:15:28 +03:00
|
|
|
|
2024-09-03 15:31:11 +03:00
|
|
|
case AccentColor:
|
|
|
|
if !button.setColorProperty(AccentColor, value) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if button.created {
|
|
|
|
updateInnerHTML(button.htmlID(), button.session)
|
|
|
|
}
|
|
|
|
|
2021-11-20 11:15:28 +03:00
|
|
|
default:
|
|
|
|
return button.viewsContainerData.set(tag, value)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
2021-11-20 11:15:28 +03:00
|
|
|
button.propertyChangedEvent(tag)
|
|
|
|
return true
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (button *checkboxData) Remove(tag string) {
|
2021-11-20 11:15:28 +03:00
|
|
|
button.remove(strings.ToLower(tag))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (button *checkboxData) remove(tag string) {
|
|
|
|
switch tag {
|
|
|
|
case ClickEvent:
|
|
|
|
if !button.viewsContainerData.set(ClickEvent, checkboxClickListener) {
|
|
|
|
delete(button.properties, tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
case KeyDownEvent:
|
|
|
|
if !button.viewsContainerData.set(KeyDownEvent, checkboxKeyListener) {
|
|
|
|
delete(button.properties, tag)
|
|
|
|
}
|
|
|
|
|
2021-09-07 17:36:50 +03:00
|
|
|
case CheckboxChangedEvent:
|
|
|
|
if len(button.checkedListeners) > 0 {
|
|
|
|
button.checkedListeners = []func(Checkbox, bool){}
|
|
|
|
}
|
|
|
|
|
|
|
|
case Checked:
|
|
|
|
oldChecked := button.checked()
|
|
|
|
delete(button.properties, tag)
|
2021-11-20 11:15:28 +03:00
|
|
|
if button.created && oldChecked {
|
2021-09-07 17:36:50 +03:00
|
|
|
button.changedCheckboxState(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
case CheckboxHorizontalAlign, CheckboxVerticalAlign:
|
|
|
|
delete(button.properties, tag)
|
2021-11-20 11:15:28 +03:00
|
|
|
if button.created {
|
|
|
|
htmlID := button.htmlID()
|
|
|
|
updateCSSStyle(htmlID, button.session)
|
|
|
|
updateInnerHTML(htmlID, button.session)
|
|
|
|
}
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
case VerticalAlign:
|
|
|
|
delete(button.properties, tag)
|
2021-11-20 11:15:28 +03:00
|
|
|
if button.created {
|
2022-10-30 17:22:33 +03:00
|
|
|
button.session.updateCSSProperty(button.htmlID()+"content", "align-items", button.cssVerticalAlign())
|
2021-11-20 11:15:28 +03:00
|
|
|
}
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
case HorizontalAlign:
|
|
|
|
delete(button.properties, tag)
|
2021-11-20 11:15:28 +03:00
|
|
|
if button.created {
|
2022-10-30 17:22:33 +03:00
|
|
|
button.session.updateCSSProperty(button.htmlID()+"content", "justify-items", button.cssHorizontalAlign())
|
2021-11-20 11:15:28 +03:00
|
|
|
}
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
default:
|
2021-11-20 11:15:28 +03:00
|
|
|
button.viewsContainerData.remove(tag)
|
|
|
|
return
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
2021-11-20 11:15:28 +03:00
|
|
|
button.propertyChangedEvent(tag)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (button *checkboxData) checked() bool {
|
|
|
|
checked, _ := boolProperty(button, Checked, button.Session())
|
|
|
|
return checked
|
|
|
|
}
|
|
|
|
|
|
|
|
func (button *checkboxData) changedCheckboxState(state bool) {
|
|
|
|
for _, listener := range button.checkedListeners {
|
|
|
|
listener(button, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer := allocStringBuilder()
|
|
|
|
defer freeStringBuilder(buffer)
|
|
|
|
|
|
|
|
button.htmlCheckbox(buffer, state)
|
2022-10-30 17:22:33 +03:00
|
|
|
button.Session().updateInnerHTML(button.htmlID()+"checkbox", buffer.String())
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkboxClickListener(view View) {
|
2022-08-31 22:17:46 +03:00
|
|
|
view.Set(Checked, !IsCheckboxChecked(view))
|
Added some properties and functions
* Added "resize", "grid-auto-flow", "caret-color", and "backdrop-filter" properties
* Added BlurView, BlurViewByID, GetResize, GetGridAutoFlow, GetCaretColor, GetBackdropFilter functions
* The "warp" property for ListView and ListLayout renamed to "list-warp"
* The "warp" property for EditView renamed to "edit-warp"
* Added CertFile and KeyFile optional fields to the AppParams struct.If they are set, then an https connection is created, otherwise http.
2022-06-07 13:07:10 +03:00
|
|
|
BlurView(view)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkboxKeyListener(view View, event KeyEvent) {
|
|
|
|
switch event.Code {
|
|
|
|
case "Enter", "Space":
|
2022-08-31 22:17:46 +03:00
|
|
|
view.Set(Checked, !IsCheckboxChecked(view))
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (button *checkboxData) setChangedListener(value any) bool {
|
2022-08-24 15:00:25 +03:00
|
|
|
listeners, ok := valueToEventListeners[Checkbox, bool](value)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
} else if listeners == nil {
|
|
|
|
listeners = []func(Checkbox, bool){}
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
2022-08-24 15:00:25 +03:00
|
|
|
button.checkedListeners = listeners
|
2021-09-07 17:36:50 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (button *checkboxData) cssStyle(self View, builder cssBuilder) {
|
|
|
|
session := button.Session()
|
2022-08-31 22:17:46 +03:00
|
|
|
vAlign := GetCheckboxVerticalAlign(button)
|
|
|
|
hAlign := GetCheckboxHorizontalAlign(button)
|
2021-09-07 17:36:50 +03:00
|
|
|
switch hAlign {
|
|
|
|
case CenterAlign:
|
|
|
|
if vAlign == BottomAlign {
|
|
|
|
builder.add("grid-template-rows", "1fr auto")
|
|
|
|
} else {
|
|
|
|
builder.add("grid-template-rows", "auto 1fr")
|
|
|
|
}
|
|
|
|
|
|
|
|
case RightAlign:
|
|
|
|
builder.add("grid-template-columns", "1fr auto")
|
|
|
|
|
|
|
|
default:
|
|
|
|
builder.add("grid-template-columns", "auto 1fr")
|
|
|
|
}
|
|
|
|
|
|
|
|
if gap, ok := sizeConstant(session, "ruiCheckboxGap"); ok && gap.Type != Auto && gap.Value > 0 {
|
2022-09-05 14:00:07 +03:00
|
|
|
builder.add("gap", gap.cssString("0", session))
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
builder.add("align-items", "stretch")
|
|
|
|
builder.add("justify-items", "stretch")
|
|
|
|
|
|
|
|
button.viewsContainerData.cssStyle(self, builder)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (button *checkboxData) htmlCheckbox(buffer *strings.Builder, checked bool) (int, int) {
|
2022-08-31 22:17:46 +03:00
|
|
|
vAlign := GetCheckboxVerticalAlign(button)
|
|
|
|
hAlign := GetCheckboxHorizontalAlign(button)
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
buffer.WriteString(`<div id="`)
|
|
|
|
buffer.WriteString(button.htmlID())
|
|
|
|
buffer.WriteString(`checkbox" style="display: grid;`)
|
|
|
|
if hAlign == CenterAlign {
|
|
|
|
buffer.WriteString(" justify-items: center; grid-column-start: 1; grid-column-end: 2;")
|
|
|
|
if vAlign == BottomAlign {
|
|
|
|
buffer.WriteString(" grid-row-start: 2; grid-row-end: 3;")
|
|
|
|
} else {
|
|
|
|
buffer.WriteString(" grid-row-start: 1; grid-row-end: 2;")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if hAlign == RightAlign {
|
|
|
|
buffer.WriteString(" grid-column-start: 2; grid-column-end: 3;")
|
|
|
|
} else {
|
|
|
|
buffer.WriteString(" grid-column-start: 1; grid-column-end: 2;")
|
|
|
|
}
|
|
|
|
buffer.WriteString(" grid-row-start: 1; grid-row-end: 2;")
|
|
|
|
switch vAlign {
|
|
|
|
case BottomAlign:
|
|
|
|
buffer.WriteString(" align-items: end;")
|
|
|
|
|
|
|
|
case CenterAlign:
|
|
|
|
buffer.WriteString(" align-items: center;")
|
|
|
|
|
|
|
|
default:
|
|
|
|
buffer.WriteString(" align-items: start;")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.WriteString(`">`)
|
2024-09-03 15:31:11 +03:00
|
|
|
|
|
|
|
accentColor := Color(0)
|
|
|
|
if color := GetAccentColor(button, ""); color != 0 {
|
|
|
|
accentColor = color
|
|
|
|
}
|
|
|
|
|
2021-09-07 17:36:50 +03:00
|
|
|
if checked {
|
2024-09-03 15:31:11 +03:00
|
|
|
buffer.WriteString(button.Session().checkboxOnImage(accentColor))
|
2021-09-07 17:36:50 +03:00
|
|
|
} else {
|
2024-09-03 15:31:11 +03:00
|
|
|
buffer.WriteString(button.Session().checkboxOffImage(accentColor))
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
buffer.WriteString(`</div>`)
|
|
|
|
|
|
|
|
return vAlign, hAlign
|
|
|
|
}
|
|
|
|
|
|
|
|
func (button *checkboxData) htmlSubviews(self View, buffer *strings.Builder) {
|
|
|
|
|
2022-08-31 22:17:46 +03:00
|
|
|
vCheckboxAlign, hCheckboxAlign := button.htmlCheckbox(buffer, IsCheckboxChecked(button))
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
buffer.WriteString(`<div id="`)
|
|
|
|
buffer.WriteString(button.htmlID())
|
|
|
|
buffer.WriteString(`content" style="display: grid;`)
|
|
|
|
if hCheckboxAlign == LeftAlign {
|
|
|
|
buffer.WriteString(" grid-column-start: 2; grid-column-end: 3;")
|
|
|
|
} else {
|
|
|
|
buffer.WriteString(" grid-column-start: 1; grid-column-end: 2;")
|
|
|
|
}
|
|
|
|
|
|
|
|
if hCheckboxAlign == CenterAlign && vCheckboxAlign != BottomAlign {
|
|
|
|
buffer.WriteString(" grid-row-start: 2; grid-row-end: 3;")
|
|
|
|
} else {
|
|
|
|
buffer.WriteString(" grid-row-start: 1; grid-row-end: 2;")
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.WriteString(" align-items: ")
|
|
|
|
buffer.WriteString(button.cssVerticalAlign())
|
|
|
|
buffer.WriteRune(';')
|
|
|
|
|
|
|
|
buffer.WriteString(" justify-items: ")
|
|
|
|
buffer.WriteString(button.cssHorizontalAlign())
|
|
|
|
buffer.WriteRune(';')
|
|
|
|
|
|
|
|
buffer.WriteString(`">`)
|
|
|
|
button.viewsContainerData.htmlSubviews(self, buffer)
|
|
|
|
buffer.WriteString(`</div>`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (button *checkboxData) cssHorizontalAlign() string {
|
2022-08-31 22:17:46 +03:00
|
|
|
align := GetHorizontalAlign(button)
|
2021-09-07 17:36:50 +03:00
|
|
|
values := enumProperties[CellHorizontalAlign].cssValues
|
|
|
|
if align >= 0 && align < len(values) {
|
|
|
|
return values[align]
|
|
|
|
}
|
|
|
|
return values[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (button *checkboxData) cssVerticalAlign() string {
|
2022-08-31 22:17:46 +03:00
|
|
|
align := GetVerticalAlign(button)
|
2021-09-07 17:36:50 +03:00
|
|
|
values := enumProperties[CellVerticalAlign].cssValues
|
|
|
|
if align >= 0 && align < len(values) {
|
|
|
|
return values[align]
|
|
|
|
}
|
|
|
|
return values[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsCheckboxChecked returns true if the Checkbox is checked, false otherwise.
|
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 IsCheckboxChecked(view View, subviewID ...string) bool {
|
|
|
|
return boolStyledProperty(view, subviewID, Checked, false)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
2022-07-28 12:41:50 +03:00
|
|
|
|
|
|
|
// GetCheckboxVerticalAlign return the vertical align of a Checkbox subview: TopAlign (0), BottomAlign (1), CenterAlign (2)
|
2022-08-31 22:17:46 +03:00
|
|
|
// If the second argument (subviewID) is not specified or it is "" then a left position of the first argument (view) is returned
|
|
|
|
func GetCheckboxVerticalAlign(view View, subviewID ...string) int {
|
2022-08-16 11:40:42 +03:00
|
|
|
return enumStyledProperty(view, subviewID, CheckboxVerticalAlign, LeftAlign, false)
|
2022-07-28 12:41:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCheckboxHorizontalAlign return the vertical align of a Checkbox subview: LeftAlign (0), RightAlign (1), CenterAlign (2)
|
2022-08-31 22:17:46 +03:00
|
|
|
// If the second argument (subviewID) is not specified or it is "" then a left position of the first argument (view) is returned
|
|
|
|
func GetCheckboxHorizontalAlign(view View, subviewID ...string) int {
|
2022-08-16 11:40:42 +03:00
|
|
|
return enumStyledProperty(view, subviewID, CheckboxHorizontalAlign, TopAlign, false)
|
2022-07-28 12:41:50 +03:00
|
|
|
}
|
2024-09-03 14:51:19 +03:00
|
|
|
|
|
|
|
// GetCheckboxChangedListeners returns the CheckboxChangedListener list of an Checkbox 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 GetCheckboxChangedListeners(view View, subviewID ...string) []func(Checkbox, bool) {
|
|
|
|
return getEventListeners[Checkbox, bool](view, subviewID, CheckboxChangedEvent)
|
|
|
|
}
|