2013-03-04 37 views
1

我有一個使用FlashDevelop創建的單個窗口AIR應用程序,並希望它在用戶關閉時記住窗口的大小和位置。當它重新打開時,它會調整大小並轉到該位置。Adob​​e AIR應用程序記住並在JavaScript中設置窗口狀態

找到了一個AS3腳本,可以做到這一點,但我的應用程序沒有AS3。只有一個html和js文件。

當窗口關閉時,它應該保存窗口的狀態,當它打開時,它應該加載保存的狀態並調整大小/移動到保存的狀態。以下是我對$(文件)。就緒

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; 

當窗口關閉我希望我的函數保存窗口,但無論我嘗試的功能是從來沒有所謂的狀態:$(窗口)。 on(「close」...或window.onclose =或<文件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> 
... 

所以我hav E以下問題:

  1. 如何事件偵聽器添加到當前窗口?
  2. 如何在加載時設置當前窗口x和y?
  3. 我可以調整大小並移動窗口,因爲它現在看起來像是 似乎調整大小,但看起來有點片狀,因爲它最初以 默認大小打開,然後重新顯示爲以前存儲的值。
+0

window.nativeWindow.addEventListener(window.runtime.flash.events.Event.CLOSING,onCloseCommand); 這將在窗口關閉時運行onCloseCommand,現在要弄清楚如何保存和設置x和y。 – HMR 2013-03-04 01:58:13

+0

解決了它,文檔沒有提到window.nativeWindow,但在腳本/ samples/window下的優秀空氣樣本http://jsairsamples.googlecode.com/files/air-samples.zip中找到它。太糟糕了,Adobe在JavaScript方面有這麼過時和不完整的文檔。 – HMR 2013-03-04 02:36:38

回答

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> 

設置上可見的僞使其順利出現在最後一個位置的用戶關閉它。

相關問題