pyobjc
pyobjc copied to clipboard
Cannot get NSWindowDelegate protocol(s) applicationDidFinishLaunching: to work
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.
bump
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).
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_
.
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)
@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.