diff --git a/static/js/background.js b/static/js/background.js index 02c2dd6..027af33 100644 --- a/static/js/background.js +++ b/static/js/background.js @@ -1,8 +1,32 @@ -chrome.app.runtime.onLaunched.addListener(function() { +function createWindow(state) { chrome.app.window.create('index.html', { - state: "normal", - bounds: { - width: 850, height: 500 - } + state: state, + bounds: { + width: 960, + height: 540 + } + }, function(window) { + // workaround: + // state = 'normal' in some cases not work (e.g. starting app from 'chrome://extensions' always open window in fullscreen mode) + // it requires manually restoring window state to 'normal' + if (state == 'normal') { + setTimeout(function() { window.restore(); }, 1000); + } }); -}); \ No newline at end of file +} + +chrome.app.runtime.onLaunched.addListener(function() { + var windowState = 'normal'; + + if (chrome.storage) { + // load stored window state + chrome.storage.sync.get('windowState', function(item) { + windowState = (item && item.windowState) + ? item.windowState + : windowState; + createWindow(windowState); + }); + } else { + createWindow(windowState); + } +}); diff --git a/static/js/index.js b/static/js/index.js index 64f73df..d478925 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -3,6 +3,9 @@ var activePolls = {}; // hosts currently being polled. An associated array of var pairingCert; var myUniqueid; var api; // `api` should only be set if we're in a host-specific screen. on the initial screen it should always be null. +var isInGame = false; // flag indicating whether the game stream started +var windowState = 'normal'; // chrome's windowState, possible values: 'normal' or 'fullscreen' + // Called by the common.js module. function attachListeners() { @@ -31,6 +34,35 @@ function fullscreenChromeWindow() { chrome.app.window.current().fullscreen(); } +function loadWindowState() { + if (!chrome.storage) { return; } + + chrome.storage.sync.get('windowState', function(item) { + // load stored window state + windowState = (item && item.windowState) + ? item.windowState + : windowState; + + // subscribe to chrome's windowState events + chrome.app.window.current().onFullscreened.addListener(onFullscreened); + chrome.app.window.current().onBoundsChanged.addListener(onBoundsChanged); + }); +} + +function onFullscreened() { + if (!isInGame && windowState == 'normal') { + storeData('windowState', 'fullscreen', null); + windowState = 'fullscreen'; + } +} + +function onBoundsChanged() { + if (!isInGame && windowState == 'fullscreen') { + storeData('windowState', 'normal', null); + windowState = 'normal'; + } +} + function changeUiModeForNaClLoad() { $('.mdl-layout__header').children().hide(); $("#main-content").children().not("#listener, #naclSpinner").hide(); @@ -529,6 +561,8 @@ function startGame(host, appID) { function playGameMode() { console.log("entering play game mode"); + isInGame = true; + $(".mdl-layout__header").hide(); $("#main-content").children().not("#listener, #loadingSpinner").hide(); $("#main-content").addClass("fullscreen"); @@ -585,6 +619,8 @@ function stopGameWithConfirmation() { } function stopGame(host, callbackFunction) { + isInGame = false; + if (!host.paired) { return; } @@ -695,6 +731,8 @@ function onWindowLoad(){ // don't show the game selection div $('#gameSelection').css('display', 'none'); + loadWindowState(); + if(chrome.storage) { // load stored resolution prefs chrome.storage.sync.get('resolution', function(previousValue) { diff --git a/static/js/messages.js b/static/js/messages.js index 5cfcea6..e2ab954 100644 --- a/static/js/messages.js +++ b/static/js/messages.js @@ -30,7 +30,11 @@ function handleMessage(msg) { }); }); showApps(api); - chrome.app.window.current().restore(); + + isInGame = false; + + // restore main window from 'fullscreen' to 'normal' mode (if required) + (windowState == 'normal') && chrome.app.window.current().restore(); }); } else if(msg.data === 'Connection Established') {