From 5ebcc58ae31b4ad3c7ead44d51e2be82d19a32cf Mon Sep 17 00:00:00 2001 From: Dmitry Mandrichenko Date: Wed, 21 Dec 2016 08:32:50 +0200 Subject: [PATCH 1/2] Window state restored with previous saved window state (fullscreen or normal). Window startup size changed to 16:9 aspect ratio = 960x540. After closing the game, window restored in previous state. --- static/js/background.js | 47 ++++++++++++++++++++++++++++++++++++----- static/js/messages.js | 1 - 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/static/js/background.js b/static/js/background.js index 02c2dd6..ed880e1 100644 --- a/static/js/background.js +++ b/static/js/background.js @@ -1,8 +1,45 @@ +var windowState = 'normal'; + chrome.app.runtime.onLaunched.addListener(function() { + if (chrome.storage) { + // load stored window settings + chrome.storage.sync.get('windowState', function(item) { + windowState = (item && item.windowState) + ? item.windowState + : windowState; + createWindow(windowState); + }); + } else { + createWindow(windowState); + } +}); + +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) { + window.onFullscreened.addListener(onFullscreened); + window.onBoundsChanged.addListener(onBoundsChanged); }); -}); \ No newline at end of file +} + + +function onFullscreened() { + // save windowState: 'fullscreen' + windowState != 'fullscreen' && saveItem('windowState', 'fullscreen', null); +} + +function onBoundsChanged() { + // save windowState: 'normal' + windowState != 'normal' && saveItem('windowState', 'normal', null); +} + +function saveItem(key, value, callback) { + var item = { }; + item[key] = value; + chrome.storage && chrome.storage.sync.set(item, callback); +} \ No newline at end of file diff --git a/static/js/messages.js b/static/js/messages.js index 5cfcea6..d524a62 100644 --- a/static/js/messages.js +++ b/static/js/messages.js @@ -30,7 +30,6 @@ function handleMessage(msg) { }); }); showApps(api); - chrome.app.window.current().restore(); }); } else if(msg.data === 'Connection Established') { From e9ccbb08d30ca3a2371fff18934bed9711620700 Mon Sep 17 00:00:00 2001 From: Dmitry Mandrichenko Date: Sat, 21 Jan 2017 04:04:58 +0200 Subject: [PATCH 2/2] Bugfix: window state not restored in normal mode after streaming. Bugfix: window always run in fullscreen mode when app launched from chrome://apps or chrome://extensions --- static/js/background.js | 51 +++++++++++++++-------------------------- static/js/index.js | 38 ++++++++++++++++++++++++++++++ static/js/messages.js | 5 ++++ 3 files changed, 62 insertions(+), 32 deletions(-) diff --git a/static/js/background.js b/static/js/background.js index ed880e1..027af33 100644 --- a/static/js/background.js +++ b/static/js/background.js @@ -1,8 +1,25 @@ -var windowState = 'normal'; +function createWindow(state) { + chrome.app.window.create('index.html', { + 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); + } + }); +} chrome.app.runtime.onLaunched.addListener(function() { + var windowState = 'normal'; + if (chrome.storage) { - // load stored window settings + // load stored window state chrome.storage.sync.get('windowState', function(item) { windowState = (item && item.windowState) ? item.windowState @@ -13,33 +30,3 @@ chrome.app.runtime.onLaunched.addListener(function() { createWindow(windowState); } }); - -function createWindow(state) { - chrome.app.window.create('index.html', { - state: state, - bounds: { - width: 960, - height: 540 - } - }, function(window) { - window.onFullscreened.addListener(onFullscreened); - window.onBoundsChanged.addListener(onBoundsChanged); - }); -} - - -function onFullscreened() { - // save windowState: 'fullscreen' - windowState != 'fullscreen' && saveItem('windowState', 'fullscreen', null); -} - -function onBoundsChanged() { - // save windowState: 'normal' - windowState != 'normal' && saveItem('windowState', 'normal', null); -} - -function saveItem(key, value, callback) { - var item = { }; - item[key] = value; - chrome.storage && chrome.storage.sync.set(item, callback); -} \ No newline at end of file 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 d524a62..e2ab954 100644 --- a/static/js/messages.js +++ b/static/js/messages.js @@ -30,6 +30,11 @@ function handleMessage(msg) { }); }); showApps(api); + + 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') {