How to listen to desktop display state change events?

Hi,

I’m moderately new to Haxe/Openfl.

I’m trying to figure a few things…

Firstly, how to listen to native application’s display-change-events, i.e. minimise, restore etc.

Secondly, how I can minimize or restore the application (?)

and last but not least, is there any way to prevent the application from closing?
I tried to use lime.app.Application.current.onExit.cancel() - but for some reason this put the application in busy state (on macOS).

Thanks in-advance for help.

Regards

Hi

Well, you can extend the lime.app.Application class and modify your Lime Haxe template accordingly to override Application.onWindowMinimize() and other relevant functions, but you’re better off listening to the main window’s (or any other created windows’) events instead. Also, when you click the minimize button, you don’t really minimize the application, but its window(s) instead.

function setupEventListeners() {
  // listen to the main window's onMinimize event
  lime.app.Application.current.window.onMinimize.add( mainWindowMinimized );

  // this also works
  // lime.app.Application.current.windows[0].onMinimize.add( mainWindowMinimized );
}

function mainWindowMinimized() {
  // Wait for 2 seconds, then restore window
  Sys.sleep( 2 );
  lime.app.Application.current.window.minimized = false;
}

There’s a handful of properties, methods and events you can play around with.

As for catching the exit event: Someone will correct me if I’m wrong, but I believe you have to extend Lime’s main Application class, override the onExit method, and use this subclass as your main application class, but this is not a beginner-friendly approach, as it requires custom app templates etc.

Again, you’re better off catching the main window’s onClose event, and canceling that one instead.

function setupEventListeners() {
  // listen to the main window's onClose event
  lime.app.Application.current.window.onClose.add( mainWindowClosed );
}

function mainWindowClosed() {
  // The window will never close, therefore the app keeps running
  lime.app.Application.current.window.onClose.cancel();
}
1 Like

Thank you @Igazine , that was very helpful.

Incidentally earlier, while I was trying to similar ideas I encountered with different problems. I can’t recall now exact troubles I noticed, but one I do remember clearly that I didn’t seen onWindowRestore method that I can override while using Feathers.Application.

I even now amazed that how I can use your suggestion correctly:
lime.app.Application.current.window.onRestore.add($method)

I vaguely remember I had trouble having the add property, but I’m not sure why. I confirm I can see the add property populates against onRestore using VSCode. Thank you.

Some portion still confuse me from the APIs - is Lib.application.window and lime.app.Application.current.window are same?

I’d like to confirm that preventing-application-close solution worked great. I see how the usage is different than my original one (that I mentioned in the issue).

Yeah, the naming is confusing, because a FeathersUI Application is NOT your application, it’s only a root display object, that’s why you’ll never find lime.app.Application or openfl.Application related API in a Feathers component.

It’s actually pretty easy to check if the 2 window objects are the same

trace( 'Equals? ${ Lib.application.window == lime.app.Application.current.window }');

Also it is very important to understand the difference between preventing the main window from closing, and preventing your app from exiting. You can’t just prevent the latter with a single line of code in your logic.

1 Like

It’s not an official release yet, but in OpenFL’s 9.3.0-Dev branch, you can listen for Event.EXITING from NativeApplication.nativeApplication, and call preventDefault(). Just like Adobe AIR. This won’t stop any windows from closing, though.

This requires Lime’s 8.1.0-Dev branch (or 8.2.0-Dev… either one should be fine).

1 Like

Wow! That is great @joshtynjala . Just an old days :slightly_smiling_face: Incidentally, I’m on Openfl and Lime dev-branch to address other different problems. It’s good to know that we’re going to get a new NativeApplication API, soon. Thank you.

1 Like