Difference between revisions of "Cocoa Internals/Application"

From Free Pascal wiki
Jump to navigationJump to search
Line 21: Line 21:
 
The sub-classing option is used, introducing TCocoaApplication class.
 
The sub-classing option is used, introducing TCocoaApplication class.
 
It overrides two methods:
 
It overrides two methods:
* '''run'' - the code is actually LCL Loop procedure, which would process ObjC events loop manually
+
* '''run''' - the code is actually LCL Loop procedure, which would process ObjC events loop manually
 
* '''isRunning''' - returns true, if '''run''' method was actually called.
 
* '''isRunning''' - returns true, if '''run''' method was actually called.
  

Revision as of 05:00, 31 December 2017

Cocoa's Application API pretty much matches what LCL provides. It could be possible to simply call NSApp apis, that correspond to LCL APIs. However, there's a major difference, that makes a simple solution not possible. LCL Application provides a methods HandleMessages and ProcessMessagess allowing a manual control over the event-loop.

NSApp doesn't provide any corresponding counter parts, thus it's necessary to process events loops manually.

Manual Event Loop

Cocoa Widgetset implements even loops manually. Apple documentation advises that it's possible, though could be complicated. The actual loop implementation could be found at AppWaitMessage and AppProcessMessage.

...todo...

Sub-classing NSApplication

With a manual event loop implementation, there's another problem showed up.

NSWindows are behaving differently depending if NSApplication has started or not. (running property).

If NSApplication is not running, then NSWindows would not pass the focus to another Window with the application. Instead they would keep the focus (while hidden) or make NO window a focused window (if closed). (see #32177)

There are two options to make NSApplication "running" flag to set to true:

  • use NSApp default "run" method (which could not be used, due to the need of manual event processing)
  • subclass NSApplication, reimplement "isRunning" (getter of "running" property) and possible "run" method.

The sub-classing option is used, introducing TCocoaApplication class. It overrides two methods:

  • run - the code is actually LCL Loop procedure, which would process ObjC events loop manually
  • isRunning - returns true, if run method was actually called.

See Also