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

Commit

Permalink
preserve last closed window state for session restore and undo
Browse files Browse the repository at this point in the history
Fix #892.  Fix #881. Fix #758

Auditors: @diracdeltas
  • Loading branch information
bbondy committed Feb 24, 2016
1 parent 0b44d9a commit 46d5896
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
17 changes: 17 additions & 0 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ let loadAppStatePromise = SessionStore.loadAppState().catch(() => {
// Used to collect the per window state when shutting down the application
let perWindowState = []
let sessionStateStoreAttempted = false
let lastWindowState

// URLs to accept bad certs for.
let acceptCertUrls = {}
Expand All @@ -49,6 +50,9 @@ const saveIfAllCollected = () => {
if (perWindowState.length === BrowserWindow.getAllWindows().length) {
const appState = AppStore.getState().toJS()
appState.perWindowState = perWindowState
if (perWindowState.length === 0 && lastWindowState) {
appState.perWindowState.push(lastWindowState)
}
const ignoreCatch = () => {}

// If the status is still UPDATE_AVAILABLE then the user wants to quit
Expand Down Expand Up @@ -122,6 +126,19 @@ app.on('ready', function () {
saveIfAllCollected()
})

ipcMain.on(messages.LAST_WINDOW_STATE, (wnd, data) => {
if (data) {
lastWindowState = data
}
})

process.on(messages.UNDO_CLOSED_WINDOW, () => {
if (lastWindowState) {
appActions.newWindow(undefined, undefined, lastWindowState)
lastWindowState = undefined
}
})

loadAppStatePromise.then(initialState => {
// For tests we always want to load default app state
const perWindowState = initialState.perWindowState
Expand Down
5 changes: 4 additions & 1 deletion app/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,10 @@ const init = (settingsState, args) => {
CommonMenu.separatorMenuItem,
CommonMenu.reopenLastClosedTabItem, {
label: 'Reopen Last Closed Window',
enabled: false

This comment has been minimized.

Copy link
@diracdeltas

diracdeltas Feb 27, 2016

Member

at some point, these items should be disabled if there is no last window/tab available to reopen.

accelerator: 'Alt+Shift+CmdOrCtrl+T',
click: function () {
process.emit(messages.UNDO_CLOSED_WINDOW)
}
},
CommonMenu.separatorMenuItem,
{
Expand Down
4 changes: 2 additions & 2 deletions js/components/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ class Main extends ImmutableComponent {
ipc.on(messages.CERT_ERROR, (e, details) => {
const frames = self.props.windowState.get('frames').filter(frame => frame.get('location') === details.url)
frames.forEach(frame => {
WindowActions.setSecurityState(frame, {
windowActions.setSecurityState(frame, {
certDetails: details
})
WindowActions.loadUrl(frame, 'about:certerror')
windowActions.loadUrl(frame, 'about:certerror')
})
})

Expand Down
2 changes: 2 additions & 0 deletions js/constants/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ const messages = {
// Session restore
REQUEST_WINDOW_STATE: _,
RESPONSE_WINDOW_STATE: _,
LAST_WINDOW_STATE: _,
UNDO_CLOSED_WINDOW: _,
// Ad block and tracking protection
BLOCKED_RESOURCE: _,
// About pages to contentScripts
Expand Down
4 changes: 4 additions & 0 deletions js/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ ipc.on(messages.INITIALIZE_WINDOW, (e, appState, frames, initWindowState) => {
ipc.on(messages.REQUEST_WINDOW_STATE, () => {
ipc.send(messages.RESPONSE_WINDOW_STATE, WindowStore.getState().toJS())
})

window.addEventListener('beforeunload', function () {
ipc.send(messages.LAST_WINDOW_STATE, WindowStore.getState().toJS())
})

1 comment on commit 46d5896

@diracdeltas
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

Please sign in to comment.