Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

LiHRaM
Copy link
Collaborator

@LiHRaM LiHRaM commented Nov 6, 2019

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 and remove_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!

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.
Copy link
Member

@cmyr cmyr left a 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 {
Copy link
Member

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<
Copy link
Member

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.

Copy link
Collaborator Author

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
Copy link
Member

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.

@LiHRaM
Copy link
Collaborator Author

LiHRaM commented Nov 9, 2019

closed in favor of #286

@LiHRaM LiHRaM closed this Nov 9, 2019
@LiHRaM LiHRaM deleted the track-window-creation-and-deletion branch November 9, 2019 23:00
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

Successfully merging this pull request may close these issues.

Track window creation & deletion in AppDelegate
2 participants