Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to reference widgets #9

Open
ktye opened this issue May 28, 2018 · 1 comment
Open

how to reference widgets #9

ktye opened this issue May 28, 2018 · 1 comment

Comments

@ktye
Copy link

ktye commented May 28, 2018

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?

ktye referenced this issue in ktye/duit Jun 1, 2018
This allows to define widgets inside the main UI in the
natural order.
Widgets that are referenced from other widgets can do so
by Target pointers.

This is an example implementation for mjl-/duit#9
@ktye
Copy link
Author

ktye commented Jun 1, 2018

I implemented this for some widgets as an example in the fork ktye/duit in
ktye/duit@164b760
The assignments take place within the widget's Layout method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant