简体   繁体   中英

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.

I think that on the start your application you can check current size of window by chrome.app.window.current() method and next to change the size if is it required by setSize method :

The other approach is set minimal required size (eg 700*650) of window by setMinimumSize method or reset properties on close application. From user's point of view it would be nice if user can change size of window.

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.

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