-
Notifications
You must be signed in to change notification settings - Fork 569
Add closures to AppDelegate #268
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
Conversation
I've added two optional closures to AppDelegate, which can be used to track window creation and deletion. I was unable to pass WinCtx along from `connect` and `remove_window`, and after consulting with @cmyr we decided to separate DelegateCtx from WinCtx. Other calls have been refactored to reflect this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks more or less exactly how I would've done it. As discussed, I think we should move away from this fancy closure stuff and just use a trait; let me know if you'd like to try that change or if I should. 👌
@@ -306,14 +306,41 @@ impl<T: Data + 'static> AppState<T> { | |||
|
|||
fn connect(&mut self, id: WindowId, handle: WindowHandle) { | |||
self.windows.connect(id, handle); | |||
if let Some(delegate) = &mut self.delegate { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So these 10 lines or so are the main place here where I wonder if we can't reduce boilerplate. What I am imagining is a function on self
with the signature,
fn with_delegate(&mut self, f: impl Fn(&mut AppDelegate, &mut DelegateCtx));
which internally does this whole dance of destructing self and creating the DelegateCtx
, and then calls the passed closure with them.
If we had this, we could replace these 12 lines with,
self.with_delegate(|del, ctx| del.window_added(id, data, env, ctx));
Does that make sense?
@@ -56,13 +48,24 @@ impl<'a, 'b> DelegateCtx<'a, 'b> { | |||
/// | |||
/// You customize the `AppDelegate` by passing closures during creation. | |||
pub struct AppDelegate<T> { | |||
event_fn: Option<Box<dyn Fn(Event, &mut T, &Env, &mut DelegateCtx) -> Option<Event> + 'static>>, | |||
event_fn: Option< |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that this complexity isn't worth it, and we should just back up and make AppDelegate
a trait.
If you'd like to try that, it's probably a separate PR, and then we could redo this on top of that? Or I could make that change, whichever is easiest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'd like to try my hand at that. I'll leave this as-is for now then.
{ | ||
self.event_fn = Some(Box::new(f)); | ||
self | ||
} | ||
|
||
pub fn window_added_handler<F>(mut self, f: F) -> Self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The one thing missing in this PR is docs for the public API.
closed in favor of #286 |
closes #242
I've added two optional closures to AppDelegate, which can be used to track window creation and deletion.
I was unable to pass WinCtx along from
connect
andremove_window
, and after consulting with @cmyr we decided to separate DelegateCtx from WinCtx. Other calls have been refactored to reflect this.In my testing, I noticed that closing the window normally in
multiwin
does not emit the println, whereas using the close menu command does.Any help/comments appreciated!