Skip to content

how to reference widgets #9

@ktye

Description

@ktye

As I understand, all widgets that should be referenced in another one, e.g. to change the text of a label, have to be declared upfront:

label := &duit.Label{Text: "status: not logged in yet"}

further down, the UI is declared in a nested structure:

dui.Top.UI = &duit.Box{
 ...
Kids: duit.NewKids(
			status,
			&duit.Grid{...}
...

This is simple, but has the drawback, that you have to declare the widgets in a chaotic order.
lxn/walk uses a simple trick here in it's declarative package:
You can assign each widget to a pointer, and use this later as a reference.

If we implement something like that in duit, it would look like:

  var label **duit.Label
  duit.Top.UI = &duit.Box{
   ...
   Kids: duit.NewKids(
       &duit.Label{ // we declare the widget in it's usual place
            Target: &label, // This get's evaluated later.
      },
      & duit.Button{
           // here we can use the label pointer in the callback.
     },
}

The needed change would be, that each UI element, needs a Target as a pointer to itself.
The variables holding the pointer, have to be assigned when building the Top.UI.
I don't know if we need to add another function for this, or use dui.Render.
Each widget would check it's Target field and if it is not nil, set the pointer to itself.

What do you think? Is it worth it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions