Search code examples
javascriptgoogle-chromegoogle-chrome-app

Set size and position when create window


I try to do a little application and I have a little problem with the creation of the window when I start my application.

At the beginning, I have a page with a fixed position and size, that what I do in the background.js file (I have nothing else in this file):

chrome.app.runtime.onLaunched.addListener(function() {
   var screenWidth = screen.availWidth;
   var screenHeight = screen.availHeight;
   var width = 700;
   var height = 650;
   var left = Math.round((screenWidth-width)/2);
   var top = Math.round((screenHeight-height)/2);

   chrome.app.window.create('./index.html', {
       id: 'main',
       icon: 'icon.png',
       outerBounds: {
           height:height,
           width:width,
           left: left,
           top: top
       }
   });
});

This work good but only for the first time. If the user resize himself the window (that he can do, it's not the problem), after that when he restart the app, the init size of the window is not what I have specified in the background.js file, but it's the size of the window just before he close it at the last use.

So what I want is when the app start, the size is always 700*650.


Solution

  • Thanks to answer, I've resolved my problem by to do the following :

    chrome.app.runtime.onLaunched.addListener(function() {
        var screenWidth = screen.availWidth;
        var screenHeight = screen.availHeight;
        var width = 700;
        var height = 650;
        var left = Math.round((screenWidth-width)/2);
        var top = Math.round((screenHeight-height)/2);
    
        chrome.app.window.create('./index.html', 
            {
                id: 'main',
                outerBounds: {
                    height:height,
                    width:width,
                    left: left,
                    top: top
                }
            }, function(win){
                win.onClosed.addListener(function(){
                    var appWindow = chrome.app.window.current();
                    appWindow.outerBounds.height = height;
                    appWindow.outerBounds.width = width;
                    appWindow.outerBounds.top = top;
                    appWindow.outerBounds.left = left;
                });
            });
    });
    

    So when the app is lauch, the size is 700*650 and the user can resize himself the window. But when user close it, the size is reset with the default value for the next time.