A user of Focus Booster sent through an interesting request, which is to make Focus Booster restore the window position upon subsequent loads. I can’t believe I hadn’t thought of that, and have now added it to my list of required standard functionality for AIR applications.
So, I set out to implement this feature and got to thinking about multi-screen support. Everything screen related takes place in the flash.display.Screen class. I thought there might be a simple method or property to return the current screen index a particular nativeWindow is on; but there isn’t really anything quite that useful there.
The closest thing you can get is Screen.screens which is an array containing individual Screen objects, one for each screen plugged into the viewing computer. From there, you have to implement your own routines to determine which screen a nativeWindow is on.
I wrote up a quick example that demonstrates how to determine which screen the window is on, and also how to store the window position and restore it next time the application is loaded. It’s called TwoScreens, and you can right-click to view the source (once you’ve unzipped the AIR file and installed it).
There may be a better way to do this, so post a comment if you know better ways to achieve this.
Filed under: ActionScript, air, flex
Cool – I’ve done something similar for my AIR applications (I just wish there was a standard library for powering this functionality).
One thing to be aware of is validating that window dimensions actually exist, and that your sizes and positions are correct. If you left your application open in a 2nd screen, and then lost that screen (for example) your application would be trying to open itself off-screen, without a method for repositioning it.
And co-ordinates can go screwy if you have your application open over the boundaries of more than one screen!
Yeah, I actually tested that, thinking I would definitely have to check window dimensions before restoring. I was almost positive it would restore off-screen. To my surprise though, it didn’t! It always restored on the remaining screen. Have you noticed this behaviour? Perhaps I need to do more testing on different operating systems (I’m using a Mac OS 10.5).
I tried to listen to updates to the Screen.screens array, thinking it would update if I hot un-plugged a screen. But that didn’t work. Nothing I bound to, or listened to would fire when I hot plugged, or hot un-plugged a screen. It would only update Screen.screens when I restarted the app. Have you been able to achieve that?