| 1 |
# RadioButton.nas : radio button, and group helper |
| 2 |
# to manage updating checked state conherently |
| 3 |
# SPDX-FileCopyrightText: (C) 2022 James Turner <[email protected]>, Frederic Croix <[email protected]> |
| 4 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 5 |
|
| 6 |
gui.widgets.RadioButton = { |
| 7 |
new: func(parent, style = nil, cfg = nil) { |
| 8 |
style = style or canvas.style; |
| 9 |
cfg = Config.new(cfg); |
| 10 |
var m = gui.Widget.new(gui.widgets.RadioButton, cfg); |
| 11 |
m._focus_policy = m.StrongFocus; |
| 12 |
m._checked = 0; |
| 13 |
m._text = ""; |
| 14 |
m.radioGroup = nil; |
| 15 |
m._data = cfg.get("data", {}); |
| 16 |
|
| 17 |
m._setView( style.createWidget(parent, cfg.get("type", "radio-button"), cfg) ); |
| 18 |
|
| 19 |
m.setChecked(m._cfg.get("checked", 0)); |
| 20 |
var radioButtonGroup = cfg.get("radio-button-group"); |
| 21 |
var parentRadio = cfg.get("parent-radio", nil); |
| 22 |
var radioButtonGroupClass = cfg.get("radio-button-group-class"); |
| 23 |
if (radioButtonGroup != nil) { |
| 24 |
m.radioGroup = radioButtonGroup; |
| 25 |
} elsif (parentRadio != nil) { |
| 26 |
m.radioGroup = parentRadio.radioGroup; |
| 27 |
} elsif (radioButtonGroupClass != nil) { |
| 28 |
m.radioGroup = radioButtonGroupClass.new(); |
| 29 |
} else { |
| 30 |
m.radioGroup = gui.widgets.RadioButtonsGroup.new(); |
| 31 |
} |
| 32 |
m.radioGroup.addRadio(m); |
| 33 |
|
| 34 |
m.setText(m._cfg.get("text", "")); |
| 35 |
|
| 36 |
return m; |
| 37 |
}, |
| 38 |
# @description Get the radio button group this radio button is in |
| 39 |
# @return gui.widgets.RadioButtonsGroup |
| 40 |
getRadioButtonsGroup: func { |
| 41 |
return me.radioGroup; |
| 42 |
}, |
| 43 |
# @description Set the data for this radio button. |
| 44 |
# @param key Union[scalar, hash] required If @param key is a hash, this item's data is replaced with that hash. |
| 45 |
# If @param key is a scalar, the data field with that name will be set to value. |
| 46 |
# If @param key is anything else, an error will be raised. |
| 47 |
# @param value Any optional The value to set the data field with key @param key to, if @param key is a scalar; |
| 48 |
# otherwise, this argument is ignored. |
| 49 |
# @return canvas.gui.widgets.RadioButton This radio button to support method chaining. |
| 50 |
setData: func(key, value = nil) { |
| 51 |
if (isscalar(key)) { |
| 52 |
me._data[key] = value; |
| 53 |
} elsif (ishash(key)) { |
| 54 |
me._data = key; |
| 55 |
} else { |
| 56 |
die("cannot set data field with non-scalar key !"); |
| 57 |
} |
| 58 |
return me; |
| 59 |
}, |
| 60 |
|
| 61 |
# @description Get the data of this radio button. |
| 62 |
# @param key Union[scalar, nil] The scalar key of the data field to return the value of, or nil to return the whole data. |
| 63 |
# @return Any If @param key is a scalar, the value of the field with key @param key, else the whole data as a hash. |
| 64 |
getData: func(key = nil) { |
| 65 |
if (key != nil) { |
| 66 |
if (!isscalar(key)) { |
| 67 |
die("cannot get data field with non-scalar key !") |
| 68 |
} |
| 69 |
return me._data[key]; |
| 70 |
} else { |
| 71 |
return me._data; |
| 72 |
} |
| 73 |
}, |
| 74 |
|
| 75 |
# @description Clear data |
| 76 |
# @return canvas.gui.widgets.RadioButton This radio button to support method chaining. |
| 77 |
clearData: func { |
| 78 |
me._data = {}; |
| 79 |
return me; |
| 80 |
}, |
| 81 |
|
| 82 |
getText: func { |
| 83 |
return me._text; |
| 84 |
}, |
| 85 |
|
| 86 |
setText: func(text) { |
| 87 |
me._text = text; |
| 88 |
me._view.setText(me, text); |
| 89 |
return me; |
| 90 |
}, |
| 91 |
|
| 92 |
getChecked: func { |
| 93 |
return me._checked; |
| 94 |
}, |
| 95 |
|
| 96 |
setChecked: func(checked = 1) { |
| 97 |
if (me._checked == checked) { |
| 98 |
return me; |
| 99 |
} |
| 100 |
|
| 101 |
me._setRadioGroupSiblingsUnchecked(); |
| 102 |
me._setChecked(checked); |
| 103 |
me._trigger("toggled", {checked: checked}); |
| 104 |
if (checked) { |
| 105 |
me._trigger("checked"); |
| 106 |
} else { |
| 107 |
me._trigger("unchecked"); |
| 108 |
} |
| 109 |
}, |
| 110 |
|
| 111 |
_setChecked: func(checked = 1) { |
| 112 |
me._checked = checked; |
| 113 |
me._onStateChange(); |
| 114 |
return me; |
| 115 |
}, |
| 116 |
|
| 117 |
_setRadioGroupSiblingsUnchecked: func { |
| 118 |
me.radioGroup._updateChecked(me); |
| 119 |
}, |
| 120 |
|
| 121 |
toggle: func { |
| 122 |
me.setChecked(!me._checked); |
| 123 |
return me; |
| 124 |
}, |
| 125 |
|
| 126 |
_setView: func(view) { |
| 127 |
call(gui.Widget._setView, [view], me); |
| 128 |
|
| 129 |
var el = view._root; |
| 130 |
el.addEventListener("click", func { |
| 131 |
if (me._enabled) { |
| 132 |
me.setChecked() |
| 133 |
} |
| 134 |
}); |
| 135 |
|
| 136 |
el.addEventListener("drag", func(e) e.stopPropagation()); |
| 137 |
}, |
| 138 |
}; |
| 139 |
|
| 140 |
# auto manage radio-button checked state |
| 141 |
gui.widgets.RadioButtonsGroup = { |
| 142 |
new: func(name = "unnamed") |
| 143 |
{ |
| 144 |
var m = { |
| 145 |
parents: [gui.widgets.RadioButtonsGroup], |
| 146 |
name: name, |
| 147 |
radios: [], |
| 148 |
}; |
| 149 |
return m; |
| 150 |
}, |
| 151 |
|
| 152 |
_onRadioToggled: func { |
| 153 |
var checked = me.getCheckedRadio(); |
| 154 |
foreach (var radio; me.radios) { |
| 155 |
radio._trigger("group-checked-radio-changed", {checkedRadio: checked}); |
| 156 |
} |
| 157 |
}, |
| 158 |
|
| 159 |
addRadio: func(r) |
| 160 |
{ |
| 161 |
r.listen("toggled", func me._onRadioToggled()); |
| 162 |
if (size(me.radios) == 0) { |
| 163 |
r.setChecked(); |
| 164 |
} |
| 165 |
append(me.radios, r); |
| 166 |
}, |
| 167 |
|
| 168 |
removeRadio: func(r) |
| 169 |
{ |
| 170 |
if (contains(me.radios, r)) { |
| 171 |
var index = find(r, me.radios); |
| 172 |
radios[index].del(); |
| 173 |
me.radios = subvec(me.radios, 0, index) ~ subvec(me.radios, index + 1); |
| 174 |
if (index < size(me.radios)) { |
| 175 |
me.radios[index].setChecked(); |
| 176 |
} elsif (size(me.radios) > 0) { |
| 177 |
me.radios[-1].setChecked(); |
| 178 |
} |
| 179 |
} |
| 180 |
}, |
| 181 |
|
| 182 |
setEnabled: func(enabled = 1) |
| 183 |
{ |
| 184 |
foreach (var r; me.radios) { |
| 185 |
r.setEnabled(enabled); |
| 186 |
} |
| 187 |
return me; |
| 188 |
}, |
| 189 |
|
| 190 |
getCheckedRadio: func { |
| 191 |
foreach (var radio; me.radios) { |
| 192 |
if (radio._checked) { |
| 193 |
return radio; |
| 194 |
} |
| 195 |
} |
| 196 |
return nil; |
| 197 |
}, |
| 198 |
|
| 199 |
# update check state of all radios in the group |
| 200 |
_updateChecked: func(active) |
| 201 |
{ |
| 202 |
foreach (var r; me.radios) { |
| 203 |
if (r != active) { |
| 204 |
r._setChecked(0); |
| 205 |
} |
| 206 |
} |
| 207 |
return me; |
| 208 |
}, |
| 209 |
}; |