Skip to content

Setting an initial value of a checkbox leads to the execution of the associated trigger function #154

@TLINDEN

Description

@TLINDEN

If you define a checkbox and assign it an initial value like so:

func (scene *SceneOptions) SetInitialValue(w *widget.LabeledCheckbox, value bool) {
    if value {
        w.SetState(
            widget.WidgetChecked,
        )
    }
}

[..]

scene.SetInitialValue(AcheckBox, true)

This triggers the execution of the associated widget.CheckboxOpts.StateChangedHandler in widget/checkbox.go:104 (version 0.5.6):

func (tw *Checkbox) SetState(state WidgetState) {
	if state == WidgetGreyed && !tw.triState {
		panic("non-tri state Checkbox cannot be in greyed state")
	}

	if state != tw.state {
		tw.state = state

		tw.StateChangedEvent.Fire(&CheckboxChangedEventArgs{ // <=== !!!!
			Active: tw,
			State:  tw.state,
		})
	}
}

Also as can be seen, this ONLY happens if the initial value is TRUE.

This behavior leads to the problem, that the checkbox StateChangedEvent is being called once on first ui rendering. The user has not clicked on anything yet.

However, inside that handler function you toggle the associated config value of course. This is how the handler looks:

func(args *widget.CheckboxChangedEventArgs) {
	scene.Config.ToggleGridlines()
})

So, scene.Config.ToggleGridlines() is being called on rendering without any user interaction, which then reverses the option to the opposite value. From now on the display of the checkbox doesn't match the realitiy in the game anymore. So, the checkbox is OFF when the option is actually enabled and vice versa.

The whole system ONLY works if the option is initially set to FALSE.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions