MCPcopy Create free account
hub / github.com/cogentcore/core / bindMapValue

Function bindMapValue

core/keyedlist.go:271–296  ·  view source on GitHub ↗

bindMapValue is a version of [Bind] that works for values in a map.

(mapv reflect.Value, key reflect.Value, vw T)

Source from the content-addressed store, hash-verified

269
270// bindMapValue is a version of [Bind] that works for values in a map.
271func bindMapValue[T Value](mapv reflect.Value, key reflect.Value, vw T) T {
272 wb := vw.AsWidget()
273 alreadyBound := wb.ValueUpdate != nil
274 wb.ValueUpdate = func() {
275 value := mapv.MapIndex(key).Interface()
276 if vws, ok := any(vw).(ValueSetter); ok {
277 ErrorSnackbar(vw, vws.SetWidgetValue(value))
278 } else {
279 ErrorSnackbar(vw, reflectx.SetRobust(vw.WidgetValue(), value))
280 }
281 }
282 wb.ValueOnChange = func() {
283 value := reflect.New(mapv.Type().Elem())
284 ErrorSnackbar(vw, reflectx.SetRobust(value.Interface(), vw.WidgetValue()))
285 mapv.SetMapIndex(key, value.Elem())
286 }
287 if alreadyBound {
288 ResetWidgetValue(vw)
289 }
290 if ob, ok := any(vw).(OnBinder); ok {
291 value := mapv.MapIndex(key).Interface()
292 ob.OnBind(value, "")
293 }
294 wb.ValueUpdate() // we update it with the initial value immediately
295 return vw
296}

Callers 1

InitMethod · 0.85

Calls 11

SetRobustFunction · 0.92
ErrorSnackbarFunction · 0.85
ResetWidgetValueFunction · 0.85
MapIndexMethod · 0.80
ElemMethod · 0.80
AsWidgetMethod · 0.65
SetWidgetValueMethod · 0.65
WidgetValueMethod · 0.65
NewMethod · 0.65
TypeMethod · 0.65
OnBindMethod · 0.65

Tested by

no test coverage detected