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

Commit

Permalink
move BrowserWindow creation out of the dispatch call
Browse files Browse the repository at this point in the history
  • Loading branch information
bridiver committed Mar 2, 2017
1 parent 84129f8 commit 6d72210
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 112 deletions.
79 changes: 76 additions & 3 deletions app/browser/windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

const {app, BrowserWindow} = require('electron')
const {app, BrowserWindow, ipcMain} = require('electron')
const appActions = require('../../js/actions/appActions')
const debounce = require('../../js/lib/debounce.js')
const appUrlUtil = require('../../js/lib/appUrlUtil')
const debounce = require('../../js/lib/debounce')
const {getSetting} = require('../../js/settings')
const locale = require('../locale')
const LocalShortcuts = require('../localShortcuts')
const { makeImmutable } = require('../common/state/immutableUtil')
const messages = require('../../js/constants/messages')
const settings = require('../../js/constants/settings')
const windowState = require('../common/state/windowState')

// TODO(bridiver) - set window uuid
Expand Down Expand Up @@ -55,15 +61,82 @@ const api = {
init: (state, action) => {
app.on('browser-window-created', function (event, win) {
let windowId = -1
const updateWindowMove = debounce(updateWindow, 1000)
const updateWindowMove = debounce(updateWindow, 100)
const updateWindowDebounce = debounce(updateWindow, 5)

win.once('initialized', () => {
windowId = win.id
currentWindows[windowId] = win
let windowValue = getWindowValue(windowId)

win.setMenuBarVisibility(true)
win.webContents.once('will-destroy', () => {
LocalShortcuts.unregister(win)
})
win.webContents.once('close', () => {
LocalShortcuts.unregister(win)
})
win.once('close', () => {
LocalShortcuts.unregister(win)
})
win.on('scroll-touch-begin', function (e) {
win.webContents.send('scroll-touch-begin')
})
win.on('scroll-touch-end', function (e) {
win.webContents.send('scroll-touch-end')
})
win.on('scroll-touch-edge', function (e) {
win.webContents.send('scroll-touch-edge')
})
win.on('enter-full-screen', function () {
if (win.isMenuBarVisible()) {
win.setMenuBarVisibility(false)
}
})
win.on('leave-full-screen', function () {
win.webContents.send(messages.LEAVE_FULL_SCREEN)

if (getSetting(settings.AUTO_HIDE_MENU) === false) {
win.setMenuBarVisibility(true)
}
})
win.on('app-command', function (e, cmd) {
switch (cmd) {
case 'browser-backward':
win.webContents.send(messages.SHORTCUT_ACTIVE_FRAME_BACK)
return
case 'browser-forward':
win.webContents.send(messages.SHORTCUT_ACTIVE_FRAME_FORWARD)
return
}
})
win.webContents.on('crashed', (e) => {
console.error('Window crashed. Reloading...')
win.loadURL(appUrlUtil.getBraveExtIndexHTML())

ipcMain.on(messages.NOTIFICATION_RESPONSE, function notificationResponseCallback (e, message, buttonIndex, persist) {
if (message === locale.translation('unexpectedErrorWindowReload')) {
appActions.hideMessageBox(message)
ipcMain.removeListener(messages.NOTIFICATION_RESPONSE, notificationResponseCallback)
}
})

appActions.showMessageBox({
buttons: [
{text: locale.translation('ok')}
],
options: {
persist: false
},
message: locale.translation('unexpectedErrorWindowReload')
})
})

appActions.windowCreated(windowValue)
})
win.once('show', () => {
LocalShortcuts.register(win)
})
win.once('closed', () => {
cleanupWindow(windowId)
})
Expand Down
152 changes: 43 additions & 109 deletions js/stores/appStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ const siteSettings = require('../state/siteSettings')
const appUrlUtil = require('../lib/appUrlUtil')
const electron = require('electron')
const app = electron.app
const ipcMain = electron.ipcMain
const messages = require('../constants/messages')
const UpdateStatus = require('../constants/updateStatus')
const BrowserWindow = electron.BrowserWindow
const LocalShortcuts = require('../../app/localShortcuts')
const appActions = require('../actions/appActions')
const syncActions = require('../actions/syncActions')
const firstDefinedValue = require('../lib/functional').firstDefinedValue
const dates = require('../../app/dates')
Expand All @@ -30,7 +27,6 @@ const EventEmitter = require('events').EventEmitter
const Immutable = require('immutable')
const diff = require('immutablediff')
const debounce = require('../lib/debounce')
const locale = require('../../app/locale')
const path = require('path')
const autofill = require('../../app/autofill')
const nativeImage = require('../../app/nativeImage')
Expand Down Expand Up @@ -111,7 +107,12 @@ const setWindowPosition = (browserOpts, defaults, windowState) => {
return browserOpts
}

const createWindow = (browserOpts, defaults, frameOpts, windowState) => {
const createWindow = (action) => {
const frameOpts = (action.frameOpts && action.frameOpts.toJS()) || {}
let browserOpts = (action.browserOpts && action.browserOpts.toJS()) || {}
const windowState = action.restoredState || {}
const defaults = windowDefaults()

browserOpts = setWindowDimensions(browserOpts, defaults, windowState)
browserOpts = setWindowPosition(browserOpts, defaults, windowState)

Expand Down Expand Up @@ -185,65 +186,50 @@ const createWindow = (browserOpts, defaults, frameOpts, windowState) => {
windowProps.icon = path.join(__dirname, '..', '..', 'res', 'app.png')
}

let mainWindow = new BrowserWindow(Object.assign(windowProps, browserOpts))

mainWindow.setMenuBarVisibility(true)

if (windowState.ui && windowState.ui.isMaximized) {
mainWindow.maximize()
}

if (windowState.ui && windowState.ui.isFullScreen) {
mainWindow.setFullScreen(true)
}
const homepageSetting = getSetting(settings.HOMEPAGE)
const startupSetting = getSetting(settings.STARTUP_MODE)

mainWindow.on('close', function () {
LocalShortcuts.unregister(mainWindow)
})

mainWindow.on('closed', function () {
mainWindow = null
})

mainWindow.on('scroll-touch-begin', function (e) {
mainWindow.webContents.send('scroll-touch-begin')
})
setImmediate(() => {
let mainWindow = new BrowserWindow(Object.assign(windowProps, browserOpts))

mainWindow.on('scroll-touch-end', function (e) {
mainWindow.webContents.send('scroll-touch-end')
})
// initialize frames state
let frames = []
if (frameOpts && Object.keys(frameOpts).length > 0) {
if (frameOpts.forEach) {
frames = frameOpts
} else {
frames.push(frameOpts)
}
} else if (startupSetting === 'homePage' && homepageSetting) {
frames = homepageSetting.split('|').map((homepage) => {
return {
location: homepage
}
})
}

mainWindow.on('scroll-touch-edge', function (e) {
mainWindow.webContents.send('scroll-touch-edge')
})
mainWindow.webContents.on('did-finish-load', (e) => {
lastEmittedState = appState
e.sender.send(messages.INITIALIZE_WINDOW, frameOpts.disposition, appState.toJS(), frames, action.restoredState)
if (action.cb) {
action.cb()
}
})

mainWindow.on('enter-full-screen', function () {
if (mainWindow.isMenuBarVisible()) {
mainWindow.setMenuBarVisibility(false)
}
})
mainWindow.on('ready-to-show', () => {
mainWindow.show()

mainWindow.on('leave-full-screen', function () {
mainWindow.webContents.send(messages.LEAVE_FULL_SCREEN)
if (windowState.ui && windowState.ui.isMaximized) {
mainWindow.maximize()
}

if (getSetting(settings.AUTO_HIDE_MENU) === false) {
mainWindow.setMenuBarVisibility(true)
}
})
if (windowState.ui && windowState.ui.isFullScreen) {
mainWindow.setFullScreen(true)
}
})

mainWindow.on('app-command', function (e, cmd) {
switch (cmd) {
case 'browser-backward':
mainWindow.webContents.send(messages.SHORTCUT_ACTIVE_FRAME_BACK)
return
case 'browser-forward':
mainWindow.webContents.send(messages.SHORTCUT_ACTIVE_FRAME_FORWARD)
return
}
mainWindow.loadURL(appUrlUtil.getBraveExtIndexHTML())
})

LocalShortcuts.register(mainWindow)
return mainWindow
}

class AppStore extends EventEmitter {
Expand Down Expand Up @@ -402,59 +388,7 @@ const handleAppAction = (action) => {
app.quit()
break
case appConstants.APP_NEW_WINDOW:
const frameOpts = (action.frameOpts && action.frameOpts.toJS()) || {}
const browserOpts = (action.browserOpts && action.browserOpts.toJS()) || {}
const newWindowState = action.restoredState || {}

const mainWindow = createWindow(browserOpts, windowDefaults(), frameOpts, newWindowState)
const homepageSetting = getSetting(settings.HOMEPAGE)

// initialize frames state
let frames = []
if (frameOpts && Object.keys(frameOpts).length > 0) {
if (frameOpts.forEach) {
frames = frameOpts
} else {
frames.push(frameOpts)
}
} else if (getSetting(settings.STARTUP_MODE) === 'homePage' && homepageSetting) {
frames = homepageSetting.split('|').map((homepage) => {
return {
location: homepage
}
})
}

mainWindow.webContents.on('did-finish-load', (e) => {
lastEmittedState = appState
e.sender.send(messages.INITIALIZE_WINDOW, frameOpts.disposition, appState.toJS(), frames, action.restoredState)
if (action.cb) {
action.cb()
}
})
mainWindow.webContents.on('crashed', (e) => {
console.error('Window crashed. Reloading...')
mainWindow.loadURL(appUrlUtil.getBraveExtIndexHTML())

ipcMain.on(messages.NOTIFICATION_RESPONSE, function notificationResponseCallback (e, message, buttonIndex, persist) {
if (message === locale.translation('unexpectedErrorWindowReload')) {
appActions.hideNotification(message)
ipcMain.removeListener(messages.NOTIFICATION_RESPONSE, notificationResponseCallback)
}
})

appActions.showNotification({
buttons: [
{text: locale.translation('ok')}
],
options: {
persist: false
},
message: locale.translation('unexpectedErrorWindowReload')
})
})
mainWindow.loadURL(appUrlUtil.getBraveExtIndexHTML())
mainWindow.show()
createWindow(action)
break
case appConstants.APP_CLOSE_WINDOW:
appState = windows.closeWindow(appState, action)
Expand Down

0 comments on commit 6d72210

Please sign in to comment.