Skip to content
This repository was archived by the owner on Dec 11, 2019. It is now read-only.

Commit 9d1d278

Browse files
committed
Merge pull request #13161 from brave/fix/window-restore-focus
windows restore in the correct order to ensure correct focused window
1 parent da68356 commit 9d1d278

File tree

5 files changed

+52
-19
lines changed

5 files changed

+52
-19
lines changed

app/browser/windows.js

+32-3
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,37 @@ const updatePinnedTabs = (win, appState) => {
147147
}
148148
}
149149

150+
function refocusFocusedWindow () {
151+
const focusedWindow = BrowserWindow.getFocusedWindow()
152+
if (focusedWindow) {
153+
if (shouldDebugWindowEvents) {
154+
console.log('focusing on window', focusedWindow.id)
155+
}
156+
focusedWindow.focus()
157+
}
158+
}
159+
150160
function showDeferredShowWindow (win) {
151-
if (shouldDebugWindowEvents) {
152-
console.log(`Window [${win.id}] showDeferredShowWindow`)
161+
// were we asked to make the window active / foreground?
162+
const shouldShowInactive = win.webContents.browserWindowOptions.inactive
163+
if (shouldShowInactive) {
164+
// we were asked NOT to show the window active.
165+
// we should maintain focus on the window which already has it
166+
if (shouldDebugWindowEvents) {
167+
console.log('showing deferred window inactive', win.id)
168+
}
169+
win.showInactive()
170+
// Whilst the window will not have focus, it will potentially be
171+
// on top of the window which already had focus,
172+
// so re-focus the focused window.
173+
setImmediate(refocusFocusedWindow)
174+
} else {
175+
// we were asked to show the window active
176+
if (shouldDebugWindowEvents) {
177+
console.log('showing deferred window active', win.id)
178+
}
179+
win.show()
153180
}
154-
win.show()
155181
if (win.__shouldFullscreen) {
156182
// this timeout helps with an issue that
157183
// when a user is loading from state, and
@@ -160,6 +186,9 @@ function showDeferredShowWindow (win) {
160186
// spaces because macOS has switched away from the desktop space
161187
setTimeout(() => {
162188
win.setFullScreen(true)
189+
if (shouldShowInactive) {
190+
setImmediate(refocusFocusedWindow)
191+
}
163192
}, 100)
164193
} else if (win.__shouldMaximize) {
165194
win.maximize()

app/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ app.on('ready', () => {
208208
appActions.newWindow()
209209
}
210210
} else {
211+
const lastIndex = loadedPerWindowImmutableState.size - 1
211212
loadedPerWindowImmutableState
212213
.sort((a, b) => {
213214
let comparison = 0
@@ -222,8 +223,12 @@ app.on('ready', () => {
222223

223224
return comparison
224225
})
225-
.forEach((wndState) => {
226-
appActions.newWindow(undefined, undefined, wndState)
226+
.forEach((wndState, i) => {
227+
const isLastWindow = i === lastIndex
228+
if (CmdLine.shouldDebugWindowEvents && isLastWindow) {
229+
console.log(`The restored window which should get focus has ${wndState.get('frames').size} frames`)
230+
}
231+
appActions.newWindow(undefined, isLastWindow ? undefined : { inactive: true }, wndState, true)
227232
})
228233
}
229234
process.emit(messages.APP_INITIALIZED)

js/entry.js

+3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ ipc.on(messages.INITIALIZE_WINDOW, (e, mem) => {
8181
const windowValue = message.windowValue
8282

8383
currentWindow.setWindowId(windowValue.id)
84+
if (process.env.NODE_ENV === 'development') {
85+
console.debug(`This Window's ID is:`, windowValue.id)
86+
}
8487
const newState = Immutable.fromJS(message.windowState) || windowStore.getState()
8588

8689
appStoreRenderer.state = Immutable.fromJS(message.appState)

js/stores/windowStore.js

+9-14
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
33
* You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
/* global performance */
6-
75
const appDispatcher = require('../dispatcher/appDispatcher')
86
const EventEmitter = require('events').EventEmitter
97
const appActions = require('../actions/appActions')
@@ -745,21 +743,18 @@ const doAction = (action) => {
745743
}
746744
break
747745
}
748-
case windowConstants.WINDOW_ON_WINDOW_UPDATE:
749746
case appConstants.APP_WINDOW_READY:
750-
{
751-
const oldInfo = windowState.get('windowInfo', Immutable.Map())
752-
let windowValue = makeImmutable(action.windowValue)
753-
754-
if (windowValue.get('focused')) {
755-
windowValue = windowValue.set('focusTime', performance.timing.navigationStart + performance.now())
756-
}
757-
windowState = windowState.set('windowInfo', oldInfo.merge(windowValue))
758-
break
759-
}
760747
case appConstants.APP_WINDOW_UPDATED:
761748
case appConstants.APP_WINDOW_RESIZED:
762-
windowState = windowState.set('windowInfo', action.windowValue)
749+
let windowValue = makeImmutable(action.windowValue)
750+
const oldInfo = windowState.get('windowInfo', Immutable.Map())
751+
// detect if window is newly focused
752+
if (windowValue.get('focused') && !oldInfo.get('focused')) {
753+
// record time of focus so we can make sure the window
754+
// z-index is restored on app-restart
755+
windowValue = windowValue.set('focusTime', new Date().getTime())
756+
}
757+
windowState = windowState.set('windowInfo', oldInfo.merge(windowValue))
763758
break
764759
case windowConstants.WINDOW_TAB_MOVE_INCREMENTAL_REQUESTED:
765760
const sourceFrame = frameStateUtil.getActiveFrame(windowState)

test/unit/lib/fakeWindow.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function FakeWindow (id) {
1212
this.webContents = Object.assign(new EventEmitter())
1313
this.webContents.send = this.webContents.emit
1414
this._isVisible = false
15+
this.webContents.browserWindowOptions = { }
1516
}
1617

1718
util.inherits(FakeWindow, EventEmitter)

0 commit comments

Comments
 (0)