简体   繁体   中英

Adobe AIR application remember and set window state in JavaScript

I have a single window AIR application created with FlashDevelop and would like it to remember the size and position of the window when the user closes it. When it re opens it would resize and go to that position.

Found an AS3 script that will do that but my application has no AS3. There is only an html and js file.

When the window is closed it should save the state of the window and when it's opened it should load the saved state and resize/move to the saved state. Here is what I have on $(document).ready

    var width = screen.width;
    var height = screen.height;
    var p=new DOMParser();
    var xml,x,y,windowWidth,windowHeight;
    var f = window.runtime.flash.filesystem.File
      .applicationDirectory.resolvePath("appPosition.xml");
    if (f.exists){
        var s = new window.runtime.flash.filesystem.FileStream();
        try {
            s.open(f,window.runtime.flash.filesystem.FileMode.READ);
            xml=p.parseFromString(s.readUTFBytes(s.bytesAvailable),"text/xml");
            x = parseInt(xml.childNodes[0].getAttribute("x"),10);
            y = parseInt(xml.childNodes[0].getAttribute("y"),10); 
            windowWidth = parseInt(xml.childNodes[0].getAttribute("width"),10);
            windowHeight = 
                parseInt(xml.childNodes[0].getAttribute("height"),10);
            x = (x >= width) ? 20 : x;
            y = (y >= height) ? 0 : y;
            x = (x <= (width*-1)) ? 20 : x;
            y = (y <= (height*-1)) ? 0 : y;
            if (windowWidth >= (width-60) && windowHeight >= (height-60)) {
//the x and y are not set
                window.runtime.flash.display.NativeWindow.x =20;
                window.runtime.flash.display.NativeWindow.y = 0;
                window.resizeTo(900,600);
// not yet sure if the following works
                window.runtime.flash.display.NativeWindow.maximize();
            }else{
// x and y don't do anything here either
                window.runtime.flash.display.NativeWindow.x = x;
                window.runtime.trace("sitting window x:",x);
                window.runtime.flash.display.NativeWindow.y = y;
                window.resizeTo(windowWidth,windowHeight);
            }
        }catch (e) {
            window.runtime.trace(e.message);
        }finally{
            s.close();
        }
        return;

When the window is closed I want my function to save the state of the window but whatever I try the function is never called: $(window).on("close"... or window.onclose= or <document onunluoad=... The lengthy guide doesn't seem to have anything on how to get the current window: http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/scripting/pdfs/javascript_tools_guide.pdf Creating windows is covered and once having it you can manipulate it but I never create a window, the application.xml looks like this:

...
  <initialWindow>
    <title>App Title</title>
    <content>main.html</content>
...

So I have the following questions:

  1. How do I add event listeners to the current window?
  2. How do I set current window x and y when it loads?
  3. Can I size and move the window before it is visible as right now it seems to resize but looks kind of flaky as it opens initially at default size and then reiszes to the previously stored values.

Solved it using window.nativeWindow (note the lower case n of nativeWindow, it's upper case in AS3). Here is the code:

$(document).ready(function(){
    var width = screen.width;
    var height = screen.height;
    var p=new DOMParser();
    var xml,x,y,windowWidth,windowHeight;
    var f = window.runtime.flash.filesystem.File
      .applicationDirectory.resolvePath("appPosition.xml");
    if (f.exists){
        var s = new window.runtime.flash.filesystem.FileStream();
        try {
            s.open(f,window.runtime.flash.filesystem.FileMode.READ);
            xml=p.parseFromString(s.readUTFBytes(s.bytesAvailable),"text/xml");
            x = parseInt(xml.childNodes[0].getAttribute("x"),10);
            y = parseInt(xml.childNodes[0].getAttribute("y"),10); 
            windowWidth = parseInt(xml.childNodes[0]
              .getAttribute("width"),10);
            windowHeight = parseInt(xml.childNodes[0]
             .getAttribute("height"),10);
            x = (x >= width) ? 20 : x;
            y = (y >= height) ? 0 : y;
            x = (x <= (width*-1)) ? 20 : x;
            y = (y <= (height*-1)) ? 0 : y;
            if (windowWidth >= (width-60) && windowHeight >= (height-60)) {
                window.nativeWindow.x =20;
                window.nativeWindow.y = 0;
                window.resizeTo(866,600);
                window.nativeWindow.height = height-60;
                window.nativeWindow.maximize();
            }else{
                window.nativeWindow.x = x;
                window.nativeWindow.y = y;
                window.resizeTo(windowWidth,windowHeight);
            }
        }catch (e) {
            window.runtime.trace(e.message);
        }finally{
            s.close();
            window.nativeWindow.visible=true;
        }
        return;
    }
    try{
        window.nativeWindow.x = 20;
        window.nativeWindow.y = 0;
        window.nativeWindow.width = 866;
        window.nativeWindow.height = height-60;
        window.nativeWindow.visible=true;
    }catch(e){
        window.runtime.trace(e.message);
    }

    window.nativeWindow.addEventListener
      (window.runtime.flash.events.Event.CLOSING, function(e){
        var xml = '<position x="'+window.nativeWindow.x 
          +'" y="'+window.nativeWindow.y+'" width="'
          + window.nativeWindow.width + '" height="' 
          + window.nativeWindow.height + '"/>';
            var f = window.runtime.flash.filesystem.File.
          applicationDirectory.resolvePath("appPosition.xml");
        f = new window.runtime.flash.filesystem.File(f.nativePath);
        var s = new window.runtime.flash.filesystem.FileStream();
        try{
            s.open(f,window.runtime.flash.filesystem.FileMode.WRITE);
            s.writeUTFBytes(xml);
        }catch (e){
            window.runtime.trace(e.message);
        }finally{
            s.close();
        }
    });

});

In the application.xml:

  <initialWindow>
    <title>app title</title>
    <content>main.html</content>
    <systemChrome>standard</systemChrome>
    <transparent>false</transparent>
    <visible>false</visible>

Setting visible false makes it smoothly appear in the last position the user closed it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM