【问题标题】:Creating windows in Chrome App ignores bounds when launching?启动时在 Chrome 应用程序中创建窗口会忽略边界?
【发布时间】:2016-08-31 18:22:50
【问题描述】:

我使用background.js 脚本创建新窗口,但是当我在使用chrome.app.window.create 方法时设置边界并将其传递给CreateWindowOptions 对象的参数时,第一次使用,但每当我重新启动时都会忽略应用程序。

在 OSX 上,似乎在窗口的 outerBounds 对象上创建回调之后调用方法 setPosition 和 setSize 应用程序将响应我尝试设置的边界。但是,这似乎根本不适用于 Windows 10。

当应用程序关闭时,我创建的窗口似乎以某种方式持续存在。我不确定如何清除它们,特别是如果用户关闭应用程序使用菜单或操作系统退出。有什么方法可以确保销毁或清除这些窗口,或者强制 Chrome 应用设置我设置的边界而不恢复到以前的状态?

如果这有什么不同的话,我创建的窗口正在运行 PixiJS。触发关闭事件时,我确实在 Windows 上调用了 destroy 方法,但这没有任何影响。

这是我的后台脚本:

(function( global ) {

    "use strict";


    /* ----- VARS ----- */
    var displayInfo;
    var isMultiDisplay;

    var controlDisplayIndex;
    var controlBounds;
    var controlLoaded;
    var controlWindow;

    var gameDisplayIndex;
    var gameBounds;
    var gameLoaded;
    var gameWindow;


    /* ----- CONSTANTS ----- */
    var CONTROL_WIDTH = 1920;
    var CONTROL_HEIGHT = 1080;

    var GAME_WIDTH = 2160;
    var GAME_HEIGHT = 3840;

    var FRAME_OPTIONS = {
        type: "none"
    };


    /* ----- FUNCTIONS ----- */
    function init() {
        console.log( "background.js: init" );
        console.info( "background.js: ", chrome.app.window.getAll() );

        isMultiDisplay = false;

        controlLoaded = false;
        gameLoaded = false;

        loadDisplayInfo();

    };

    function loadDisplayInfo() {
        console.log( "background.js: loadDisplayInfo" );

        chrome.system.display.getInfo( onDisplayInfo );

    };

    function createWindows() {
        console.log( "background.js: createWindows" );

        isMultiDisplay = ( displayInfo.length > 0 );

        if ( isMultiDisplay ) {

            var i = 0;
            var length = 2;

            for ( i; i < length; i++ ) {

                if ( displayInfo[i].isPrimary )
                    controlDisplayIndex = i;
                else 
                    gameDisplayIndex = i;

            }

            gameBounds = {
                height: displayInfo[ gameDisplayIndex ].workArea.height,
                left: displayInfo[ gameDisplayIndex ].workArea.left,
                top: displayInfo[ gameDisplayIndex ].workArea.top,
                width: displayInfo[ gameDisplayIndex ].workArea.width
            };

            controlBounds = {
                height: displayInfo[ controlDisplayIndex ].workArea.height,
                left: displayInfo[ controlDisplayIndex ].workArea.left,
                top: displayInfo[ controlDisplayIndex ].workArea.top,
                width: displayInfo[ controlDisplayIndex ].workArea.width
            };

        } else {

            // This assumes single monitor is in landscape.
            gameBounds = {
                top: displayInfo[0].workArea.top,
                left: displayInfo[0].workArea.left,
                height: displayInfo[0].workArea.height,
                width: Math.floor( displayInfo[0].workArea.height * GAME_WIDTH / GAME_HEIGHT )
            };

            controlBounds = {
                top: displayInfo[0].workArea.top,
                left: displayInfo[0].workArea.left + gameBounds.width,
                width: gameBounds.width,
                height: Math.floor( gameBounds.width * CONTROL_HEIGHT / CONTROL_WIDTH )
            };

        }

        console.info( "Game Bounds:", gameDisplayIndex, gameBounds.left, gameBounds.top, gameBounds.width, gameBounds.height );
        console.info( "Control Bounds:", controlDisplayIndex, controlBounds.left, controlBounds.top, controlBounds.width, controlBounds.height );

        var state = ( isMultiDisplay ) ? "fullscreen" : "normal";

        // Game
        chrome.app.window.create( "window-game.html", {
            id: "gameWindow",
            bounds: gameBounds,
            frame: FRAME_OPTIONS,
            resizable: true,
            state: state
        }, onGameWindowCreated );

        // Control
        chrome.app.window.create( "window-control.html", {
            id: "controlWindow",
            bounds: controlBounds,
            frame: FRAME_OPTIONS,
            resizable: true,
            state: state
        }, onControlWindowCreated );

    };


    /* ----- EVENT LISTENERS ----- */
    function onLaunched() {
        console.log( "background.js: onLaunched" );

        init();

    };

    function onDisplayInfo( info ) {
        console.log( "background.js: onDisplayInfo" );

        displayInfo = info;
        createWindows();

    };

    function onControlWindowCreated( obj ) {
        console.log( "background.js: onControlWindowCreated", obj.id );

        controlLoaded = true;
        controlWindow = obj;

        controlWindow.outerBounds.setPosition( controlBounds.left, controlBounds.top );
        controlWindow.outerBounds.setSize( controlBounds.width, controlBounds.height );

        if ( isMultiDisplay ) controlWindow.fullscreen();

        //console.info( controlWindow.innerBounds.width, controlWindow.innerBounds.height );
        //console.info( controlWindow.outerBounds.width, controlWindow.outerBounds.height );

        controlWindow.onClosed.addListener( onControlWindowClosed );

    };

    function onControlWindowClosed() {
        console.log( "background.js: onControlWindowClosed" );

        controlWindow.onClosed.removeListener( onControlWindowClosed );
        controlWindow.destroy();
        controlWindow = undefined;

    };

    function onGameWindowCreated( obj ) {
        console.log( "background.js: onGameWindowCreated", obj.id );

        gameLoaded = true;
        gameWindow = obj;

        gameWindow.outerBounds.setPosition( gameBounds.left, gameBounds.top );
        gameWindow.outerBounds.setSize( gameBounds.width, gameBounds.height );

        if ( isMultiDisplay ) gameWindow.fullscreen();

        //console.info( gameWindow.innerBounds.width, gameWindow.innerBounds.height );
        //console.info( gameWindow.outerBounds.width, gameWindow.outerBounds.height );

        gameWindow.onClosed.addListener( onGameWindowClosed );

    };

    function onGameWindowClosed() {
        console.log( "background.js: onGameWindowClosed" );

        gameWindow.onClosed.removeListener( onGameWindowClosed );
        gameWindow.destroy();
        gameWindow = undefined;

    };


    /* ----- CALL ----- */
    chrome.app.runtime.onLaunched.addListener( onLaunched );

}( this ));

免责声明:我知道 Chrome 应用程序正在停产,但由于项目时间的原因,我必须完成此应用程序,然后寻求移植到 Electron 或替代方案。

【问题讨论】:

  • 请阅读:“如果还指定了 id 并且之前显示了具有匹配 id 的窗口,则将使用记住的边界。”因此,除非我删除 CreateWindowOptions 对象的 id 属性,否则似乎无法覆盖边界,对吗?

标签: javascript google-chrome-app pixi.js


【解决方案1】:

当您创建一个窗口并在CreateWindowOptions 对象中设置id 属性时,Chrome 应用平台将保存该窗口的最后一个已知边界,并在下一个创建具有相同ID 的窗口时使用。

这是来自id 属性的文档的描述:

标识窗口的 ID。这将用于记住大小和 窗口的位置并在窗口具有 稍后打开相同的ID。如果创建了具有给定 id 的窗口 虽然另一个具有相同 id 的窗口已经存在,但当前 打开的窗口将获得焦点,而不是创建一个新窗口。

并在outerBounds属性的描述下:

用于指定初始位置、初始大小和约束 窗口(包括窗口装饰,如标题栏和 框架)。如果还指定了 id 并且具有匹配 id 的窗口具有 之前已显示,将使用记住的边界。

因此,解决方案是省略 id 属性。如果您和我一样需要在脚本中对该窗口进行进一步处理,请使用创建窗口回调将变量分配给该函数的返回参数。

例如:

var myWindow;

chrome.app.window.create( "window.html", options, function( obj ) {

    myWindow = obj;

    // Do Stuff with myWindow

} );

我想即使在回调后尝试设置窗口的大小和位置也不是完全证明的,当我尝试在 Windows 10 下做一些事情时很明显。

我唯一不知道的是,当您给 Windows 一个 ID 时,Google Chrome 应用程序会将所有这些窗口数据存储在哪里?有没有办法清除它(除了卸载应用程序)?

【讨论】:

  • “有没有办法清除它” 不幸的是,不是以编程方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-16
  • 2019-10-08
  • 2011-02-25
  • 1970-01-01
  • 2011-07-27
  • 2016-08-05
相关资源
最近更新 更多