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

Cannot get NSWindowDelegate protocol(s) applicationDidFinishLaunching: to work #485

Open
leifadev opened this issue Jul 25, 2022 · 5 comments
Labels
bug Something isn't working

Comments

@leifadev
Copy link

Describe the bug
I wouldn't want to put this completely as a bug per se, but I have modeled after the pyobj-c examples exactly to the best of my ability, and I am not sure what to make of this situation looking at the PyObj-C protcols support page either.

When I am creating my AppDelegate.py I add the NSWindowDelegate protocols methods such as applicationDidFinishLaunching: and applicationShouldHandleReopen:hasVisibleWindows:, but I they don't seem to trigger or anything when they are supposed too. I have the correct named parameters they take in the docs as well, I have looked around the internet for a bit but I can't find any good answers specifically in PyObj-C contexts. Do I need to be doing something else? A decorator?

Platform information

  • Python version: 3.9.2
  • How was python installed: Homebrew/Pythons Installer
  • macOS version: 10.15.7

To Reproduce
Use my AppDelegate.py, WindowController.py, my main.py, any anything else from my repo to see what I have fully setup.

Expected behavior
Have applicationDidFinishLaunching: print my message when the app launches, and have applicationShouldHandleReopen:hasVisibleWindows: trigger my print statement as well when my dock icon is clicked on, at least according to here.

Additional context
Add any other context about the problem here.

@leifadev leifadev added the bug Something isn't working label Jul 25, 2022
@leifadev
Copy link
Author

bump

@g0t4
Copy link

g0t4 commented Aug 22, 2022

I have what may be the same problem with SCStreamOutput and SCStreamDelegate protocols. I defined everything by the books and neither seems to be called (using print() just to test they're invoked).

@SKaplanOfficial
Copy link

You need to specifically call NSApplication's setDelegate_ at some point, or you can directly subclass NSApplication. Adding the following to the bottom of AppDelegate.py causes "Finished loading!" to appear correctly:

delegate = AppDelegate.alloc().init().retain()
NSApplication.sharedApplication().setDelegate_(delegate)

If you go that route, you also need to add a _ to the end of applicationDidFinishLaunching.

You could also instead change the AppDelegate to a subclass of NSApplication, then define finishLaunching instead of applicationDidFinishLaunching_.

@g0t4
Copy link

g0t4 commented Sep 2, 2022

My problem turned out to be a matter of scope for the SCStream instance I created. IIAC the stream was autoreleased and thus never would call back to my SCStreamOutput (protocol) instance. Moving the SCStream instance to the global scope fixed things. Thanks @SKaplanOfficial for the explanation, you reminded me that I needed to RTFM because I missed a few pages that directly addressed scoping (and pseudo weak refs in objc)

@ronaldoussoren
Copy link
Owner

@SKaplanOfficial 's explanations looks like the most likely fix to me. I'd advise against subclassing NSApplicaition unless you really have to, the preferred pattern for this in Cocoa is to use the application delegate for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants