Adobe AIR应用程序记住并在JavaScript中设置窗口状态

时间:2013-03-04 01:33:55

标签: javascript air

我有一个使用FlashDevelop创建的单一窗口AIR应用程序,并希望它在用户关闭时记住窗口的大小和位置。当它重新打开时,它会调整大小并转到该位置。

找到一个AS3脚本,但是我的应用程序没有AS3。只有一个html和js文件。

当窗口关闭时,它应该保存窗口的状态,当它打开时,它应该加载保存的状态并调整大小/移动到保存的状态。这是我在$(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;

当窗口关闭时,我希望我的函数能够保存窗口的状态,但是我尝试的函数永远不会被调用:$(window).on(“close”...或window.onclose =或&lt; document onunluoad = ...冗长的指南似乎没有关于如何获取当前窗口的任何内容: http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/scripting/pdfs/javascript_tools_guide.pdf 创建窗口已经被覆盖,一旦拥有它就可以操作它但我从不创建窗口,application.xml看起来像这样:

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

所以我有以下问题:

  1. 如何将事件侦听器添加到当前窗口?
  2. 如何在加载时设置当前窗口x和y?
  3. 我可以调整窗口大小并移动窗口,然后立即显示它 似乎调整大小,但看起来有点片状,因为它最初打开 默认大小,然后重新分配到以前存储的值。

1 个答案:

答案 0 :(得分:1)

使用window.nativeWindow解决它(注意nativeWindow的小写n,它是AS3中的大写字母)。这是代码:

$(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();
        }
    });

});

在application.xml中:

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

设置可见的false会使其平滑地显示在用户关闭它的最后位置。