pyobjc icon indicating copy to clipboard operation
pyobjc copied to clipboard

Cannot get NSWindowDelegate protocol(s) applicationDidFinishLaunching: to work

Open leifadev opened this issue 2 years ago • 5 comments

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 avatar Jul 25 '22 02:07 leifadev

bump

leifadev avatar Aug 19 '22 17:08 leifadev

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).

g0t4 avatar Aug 22 '22 03:08 g0t4

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_.

SKaplanOfficial avatar Aug 22 '22 20:08 SKaplanOfficial

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)

g0t4 avatar Sep 02 '22 17:09 g0t4

@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.

ronaldoussoren avatar Sep 15 '22 21:09 ronaldoussoren