Chrome 创建窗口时设置大小和位置

bweufnob  于 2023-02-06  发布在  Go
关注(0)|答案(2)|浏览(793)

我试着做一个小应用程序,我有一个小问题,创建窗口时,我启动我的应用程序。
在开始时,我有一个固定位置和大小的页面,这就是我在background.js文件中所做的(我在这个文件中没有其他内容):

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

这个方法很好用,但这只是第一次。如果用户自己调整窗口大小(他可以这么做,这不是问题),之后当他重新启动应用时,窗口的初始化大小不是我在background.js文件中指定的大小,而是他最后一次使用时关闭窗口之前的大小。
所以我想要的是当应用程序启动时,大小总是700*650。

xoefb8l8

xoefb8l81#

感谢您的回答,我已通过执行以下操作解决了问题:

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

因此,当应用程序启动时,大小为700*650,用户可以自己调整窗口大小。但当用户关闭它时,大小将重置为默认值,以备下次使用。

ffvjumwh

ffvjumwh2#

我认为在启动应用程序时,您可以通过chrome.app.window.current()方法检查窗口的当前大小,然后如果setSize方法需要,则可以更改大小:
另一种方法是通过setMinimumSize方法设置窗口的最小要求大小(例如700*650)或在关闭应用程序时重置属性。从用户的Angular 来看,如果用户可以改变窗口的大小将是很好的。

相关问题