简体   繁体   English

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

[英]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. 我有一个用FlashDevelop创建的窗口AIR应用程序,希望它记住用户关闭窗口时的大小和位置。 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. 找到了一个可以执行此操作的AS3脚本,但是我的应用程序没有AS3。 There is only an html and js file. 只有一个html和js文件。

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 这是我在$(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: 当窗口关闭时,我希望函数保存窗口的状态,但是我尝试执行的函数永远不会被调用:$(window).on(“ close” ...或window.onclose =或<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>
...

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? 加载时如何设置当前窗口x和y?
  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). 使用window.nativeWindow解决了它(请注意nativeWindow的小写字母n,在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: 在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. 设置visible false将使其平滑显示在用户关闭它的最后位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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