macosrunloop

CFRunLoopRunInMode used recursively freezes the main window


For a cross-platform project with much legacy code, my Cocoa app uses only one NSWindow with a custom NSView that captures all the mouse events and draws all my app graphic details.

I need to implement a local modal behavior and so I use inside the main thread:

CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.01, false);

inside a locale loop. The function returns after 0.01 sec as expected with a value of kCFRunLoopRunTimedOut. However, the mouse events are during that time no more received by the custom NSView and so my app looks like it is frozen (with the color mouse cursor going round and round).

Timer events still arrive to the custom NSView, but not mouse events.

Apple specifies that this function can be called recursively. Is there anything else I should do so that the NSView still receives the user mouse inputs ?


Solution

  • The event loop is built on top of a run loop, but it's more than that. Merely running the run loop is not enough to receive and handle events.

    You can perhaps achieve some of what you're trying to do by building a loop around -[NSWindow nextEventMatchingMask:untilDate:inMode:dequeue:] and -sendEvent:, but really it's not clear what your objective is. If you explain that, there are probably superior approaches.

    For example, if you want to present a modal dialog, you should use -[NSApplication runModalForWindow:].