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

[feature request]: Create a method on WidgetExt that allows the user to run some code when the widget is added. #1480

Closed
richard-uk1 opened this issue Dec 21, 2020 · 6 comments · Fixed by #1485
Labels
D-Easy just needs to be implemented enhancement adds or requests a new feature good first issue requires little knowledge of druid's internals widget concerns a particular widget

Comments

@richard-uk1
Copy link
Collaborator

richard-uk1 commented Dec 21, 2020

Follows on from #1478.

Sometimes it's necessary to do something when a widget is created. One example would be to set the cursor when the mouse is hovered over the widget. Currently this would require creating a custom controller. This issue proposes the following function:

pub trait WidgetExt<T: Data>: Widget<T> + Sized + 'static {
  // .... after on_click
  fn on_added(self, f: impl Fn(&mut LifeCycleCtx, &T, &Env) + 'static) -> ControllerHost<Self, Added<T>>;
  // ...
}

(I might have got the function signature wrong). The function would require a new type be created (Added<T>) and Controller implemented for it. The controller would react to the LifeCycle::WidgetAdded lifecycle event by running the closure.

EDIT I did get the function signature wrong. Corrected now.

@richard-uk1 richard-uk1 added D-Easy just needs to be implemented enhancement adds or requests a new feature good first issue requires little knowledge of druid's internals help wanted has no one working on it yet widget concerns a particular widget labels Dec 21, 2020
@richard-uk1
Copy link
Collaborator Author

If anyone without much experience with druid wants to have a go, I'm happy to answer and questions you might have.

@arthmis
Copy link
Collaborator

arthmis commented Dec 22, 2020

I would like to do this.

@richard-uk1
Copy link
Collaborator Author

@arthmis go for it! :)

@richard-uk1 richard-uk1 removed the help wanted has no one working on it yet label Dec 22, 2020
@arthmis
Copy link
Collaborator

arthmis commented Dec 24, 2020

I have run into an issue here.

The function parameter for on_added

f: impl Fn(&mut LifeCycleCtx, &mut T, &Env)

requires a &mut T for data, which is incompatible with Controller's lifecycle's function definition which is

fn lifecycle(
        &mut self,
        child: &mut W,
        ctx: &mut LifeCycleCtx,
        event: &crate::LifeCycle,
        data: &T,
        env: &Env,
    )

Does the closure have to receive a &mut T?

@cmyr
Copy link
Member

cmyr commented Dec 24, 2020

No, on_added doesn't get mutable access to the data. We could also have a on_window_connected method that does get mutable data, if we wanted; the potential gotcha there is that not all widgets will see the WindowConnected event (it's only sent once, so widgets created after the window is created won't get it).

@richard-uk1
Copy link
Collaborator Author

@arthmis sorry my bad: on_added should get an immutable reference to T. I will update the issue accordingly.

The on_added method will be for setting things up inside the widget. It can use the data, but can't alter it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
D-Easy just needs to be implemented enhancement adds or requests a new feature good first issue requires little knowledge of druid's internals widget concerns a particular widget
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants