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

Commit

Permalink
remove most references to currentWindow and lazy load the rest
Browse files Browse the repository at this point in the history
  • Loading branch information
bridiver committed Apr 12, 2017
1 parent 2257864 commit 901d5db
Show file tree
Hide file tree
Showing 28 changed files with 337 additions and 256 deletions.
32 changes: 32 additions & 0 deletions app/browser/reducers/windowsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
'use strict'

const appConstants = require('../../../js/constants/appConstants')
const windowConstants = require('../../../js/constants/windowConstants')
const windowState = require('../../common/state/windowState')
const windows = require('../windows')
const {makeImmutable} = require('../../common/state/immutableUtil')

Expand All @@ -23,6 +25,36 @@ const windowsReducer = (state, action) => {
windows.pinnedTabsChanged()
})
break
case appConstants.APP_CLOSE_WINDOW:
state = windows.closeWindow(state, action)
break
case appConstants.APP_WINDOW_CLOSED:
state = windowState.removeWindow(state, action)
break
case appConstants.APP_WINDOW_CREATED:
state = windowState.maybeCreateWindow(state, action)
break
case appConstants.APP_WINDOW_UPDATED:
state = windowState.maybeCreateWindow(state, action)
break
case windowConstants.WINDOW_SHOULD_SET_TITLE:
windows.setTitle(action.get('windowId'), action.get('title'))
break
case windowConstants.WINDOW_SHOULD_MINIMIZE:
windows.minimize(action.get('windowId'))
break
case windowConstants.WINDOW_SHOULD_MAXIMIZE:
windows.maximize(action.get('windowId'))
break
case windowConstants.WINDOW_SHOULD_UNMAXIMIZE:
windows.unmaximize(action.get('windowId'))
break
case windowConstants.WINDOW_SHOULD_EXIT_FULL_SCREEN:
windows.setFullScreen(action.get('windowId'), false)
break
case windowConstants.WINDOW_SHOULD_OPEN_DEV_TOOLS:
windows.openDevTools(action.get('windowId'))
break
}
return state
}
Expand Down
51 changes: 51 additions & 0 deletions app/browser/windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ const api = {
win.on('scroll-touch-edge', function (e) {
win.webContents.send('scroll-touch-edge')
})
win.on('swipe', function (e, direction) {
win.webContents.send('swipe', direction)
})
win.on('enter-full-screen', function () {
if (win.isMenuBarVisible()) {
win.setMenuBarVisibility(false)
Expand Down Expand Up @@ -225,9 +228,15 @@ const api = {
})
win.on('resize', () => {
updateWindowDebounce(windowId)
const size = win.getSize()
// NOTE: the default window size is whatever the last window resize was
appActions.defaultWindowParamsChanged(size)
})
win.on('move', () => {
updateWindowMove(windowId)
const position = win.getPosition()
// NOTE: the default window position is whatever the last window move was
appActions.defaultWindowParamsChanged(undefined, position)
})
win.on('enter-full-screen', () => {
updateWindowDebounce(windowId)
Expand All @@ -252,6 +261,48 @@ const api = {
}
},

minimize: (windowId) => {
const win = currentWindows[windowId]
if (win && !win.isDestroyed()) {
win.minimize()
}
},

maximize: (windowId) => {
const win = currentWindows[windowId]
if (win && !win.isDestroyed()) {
win.maximize()
}
},

unmaximize: (windowId) => {
const win = currentWindows[windowId]
if (win && !win.isDestroyed()) {
win.unmaximize()
}
},

setTitle: (windowId, title) => {
const win = currentWindows[windowId]
if (win && !win.isDestroyed()) {
win.setTitle(title)
}
},

setFullScreen: (windowId, fullScreen) => {
const win = currentWindows[windowId]
if (win && !win.isDestroyed()) {
win.setFullScreen(fullScreen)
}
},

openDevTools: (windowId, fullScreen) => {
const win = currentWindows[windowId]
if (win && !win.isDestroyed()) {
win.webContents.openDevTools()
}
},

windowReady: (windowId) => {
const win = currentWindows[windowId]
if (win && !win.isDestroyed()) {
Expand Down
4 changes: 2 additions & 2 deletions app/common/commonMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const getCurrentWindowId = () => {
if (process.type === 'browser') {
return BrowserWindow.getActiveWindow().id
} else {
const {currentWindowId} = require('../renderer/currentWindow')
return currentWindowId
const currentWindow = require('../renderer/currentWindow')
return currentWindow.getCurrentWindowId()
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/common/state/windowState.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const assert = require('assert')
const validateId = function (propName, id) {
assert.ok(id, `${propName} cannot be null`)
id = parseInt(id)
assert.ok(id > 0, `${propName} must be positive`)
assert.ok(id === -1 || id >= 1, `${propName} must be positive or -1`)
return id
}

Expand Down
6 changes: 3 additions & 3 deletions app/renderer/components/bookmarksToolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const dndData = require('../../../js/dndData')
const calculateTextWidth = require('../../../js/lib/textCalculator').calculateTextWidth
const windowStore = require('../../../js/stores/windowStore')
const iconSize = require('../../common/lib/faviconUtil').iconSize
const {currentWindowId} = require('../currentWindow')
const {getCurrentWindowId} = require('../currentWindow')

const {StyleSheet, css} = require('aphrodite/no-important')
const globalStyles = require('./styles/global')
Expand Down Expand Up @@ -92,7 +92,7 @@ class BookmarkToolbarButton extends ImmutableComponent {
appActions.draggedOver({
draggingOverKey: this.props.bookmark,
draggingOverType: dragTypes.BOOKMARK,
draggingOverWindowId: currentWindowId,
draggingOverWindowId: getCurrentWindowId(),
expanded: true
})
}
Expand All @@ -105,7 +105,7 @@ class BookmarkToolbarButton extends ImmutableComponent {
appActions.draggedOver({
draggingOverKey: this.props.bookmark,
draggingOverType: dragTypes.BOOKMARK,
draggingOverWindowId: currentWindowId,
draggingOverWindowId: getCurrentWindowId(),
expanded: false
})
}
Expand Down
4 changes: 2 additions & 2 deletions app/renderer/components/navigation/navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const extensionState = require('../../../common/state/extensionState')
const siteSettingsState = require('../../../common/state/siteSettingsState')

// Util
const {currentWindow, isMaximized} = require('../../currentWindow')
const {getCurrentWindowId, isMaximized} = require('../../currentWindow')
const {makeImmutable} = require('../../../common/state/immutableUtil')
const platformUtil = require('../../../common/lib/platformUtil')
const {braveShieldsEnabled} = require('../../../common/state/shieldState')
Expand Down Expand Up @@ -169,7 +169,7 @@ class Navigator extends ImmutableComponent {
if (!e.target.className.includes('navigatorWrapper')) {
return
}
return !isMaximized() ? currentWindow.maximize() : currentWindow.unmaximize()
return !isMaximized() ? windowActions.shouldMaximize(getCurrentWindowId()) : windowActions.shouldMinimize(getCurrentWindowId())
}

render () {
Expand Down
14 changes: 8 additions & 6 deletions app/renderer/components/windowCaptionButtons.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

const React = require('react')
const ImmutableComponent = require('../../../js/components/immutableComponent')
const appActions = require('../../../js/actions/appActions')
const windowActions = require('../../../js/actions/windowActions')
const locale = require('../../../js/l10n')
const {currentWindow, isMaximized, isFullScreen} = require('../currentWindow')
const {getCurrentWindowId, isMaximized, isFullScreen} = require('../currentWindow')
const cx = require('../../../js/lib/classSet')

class WindowCaptionButtons extends ImmutableComponent {
Expand All @@ -24,21 +26,21 @@ class WindowCaptionButtons extends ImmutableComponent {
}

onMinimizeClick (e) {
currentWindow.minimize()
windowActions.shouldMinimize(getCurrentWindowId())
}

onMaximizeClick (e) {
if (isFullScreen()) {
// If full screen, toggle full screen status and restore window (make smaller)
currentWindow.setFullScreen(false)
if (isMaximized()) currentWindow.unmaximize()
windowActions.shouldExitFullScreen(getCurrentWindowId())
if (isMaximized()) windowActions.shouldUnmaximize(getCurrentWindowId())
return false
}
return (!isMaximized()) ? currentWindow.maximize() : currentWindow.unmaximize()
return (!isMaximized()) ? windowActions.shouldMaximize(getCurrentWindowId()) : windowActions.shouldUnmaximize(getCurrentWindowId())
}

onCloseClick (e) {
currentWindow.close()
appActions.closeWindow(getCurrentWindowId())
}

onDoubleClick (e) {
Expand Down
60 changes: 29 additions & 31 deletions app/renderer/currentWindow.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
const currentWindow = require('electron').remote.getCurrentWindow()
const currentWindowId = currentWindow.id
let isFocused = currentWindow.isFocused()
let isMaximized = currentWindow.isMaximized()
let isFullScreen = currentWindow.isFullScreen()
const appStoreRenderer = require('../../js/stores/appStoreRenderer')
const windowState = require('../common/state/windowState')

currentWindow.on('maximize', function (wnd) {
isMaximized = true
})
let currentWindowId = -1
let currentWindow = null

currentWindow.on('unmaximize', function (wnd) {
isMaximized = false
})

currentWindow.on('focus', function (wnd) {
isFocused = true
})

currentWindow.on('blur', function (wnd) {
isFocused = false
})
const isMaximized = () => {
const win = windowState.getByWindowId(appStoreRenderer.state, currentWindowId)
return win && win.get('state') === 'maximized'
}

currentWindow.on('enter-full-screen', function (wnd) {
isFullScreen = true
})
const isFullScreen = () => {
const win = windowState.getByWindowId(appStoreRenderer.state, currentWindowId)
return win && win.get('state') === 'fullscreen'
}

currentWindow.on('leave-full-screen', function (wnd) {
isFullScreen = false
})
const isFocused = () => {
const win = windowState.getByWindowId(appStoreRenderer.state, currentWindowId)
return (win && win.get('focused')) || false
}

module.exports = {
currentWindow,
currentWindowId,
currentWindowWebContents: currentWindow.webContents,
isMaximized: () => isMaximized,
isFocused: () => isFocused,
isFullScreen: () => isFullScreen
getCurrentWindowId: () => {
return currentWindowId
},
setWindowId: (windowId) => {
currentWindowId = windowId
},
// lazy load this
getCurrentWindow: () => {
return currentWindow || (currentWindow = require('electron').remote.getCurrentWindow())
},
isMaximized,
isFocused,
isFullScreen
}
6 changes: 3 additions & 3 deletions app/renderer/reducers/frameReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const windowActions = require('../../../js/actions/windowActions')
const settings = require('../../../js/constants/settings')
const getSetting = require('../../../js/settings').getSetting
const {updateTabPageIndex} = require('../lib/tabUtil')
const {currentWindowId} = require('../currentWindow')
const {getCurrentWindowId} = require('../currentWindow')
const messages = require('../../../js/constants/messages')

const setFullScreen = (state, action) => {
Expand Down Expand Up @@ -112,7 +112,7 @@ const frameReducer = (state, action) => {
// only has pinned frames and tried to close, so close the
// whole app.
if (nonPinnedFrames.size === 0) {
appActions.closeWindow(currentWindowId)
appActions.closeWindow(getCurrentWindowId())
return state
}

Expand All @@ -132,7 +132,7 @@ const frameReducer = (state, action) => {
if (nonPinnedFrames.size > 1 || pinnedFrames.size > 0) {
state = closeFrame(state, action)
} else {
appActions.closeWindow(currentWindowId)
appActions.closeWindow(getCurrentWindowId())
}
break

Expand Down
3 changes: 3 additions & 0 deletions app/sessionStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ module.exports.cleanPerWindowData = (perWindowData, isShutdown) => {
// Don't restore drag data and clearBrowsingDataPanel's visibility
if (perWindowData.ui) {
// This is no longer stored, we can remove this line eventually
delete perWindowData.ui.isFocused
delete perWindowData.ui.mouseInTitlebar
delete perWindowData.ui.mouseInFrame
delete perWindowData.ui.dragging
delete perWindowData.ui.isClearBrowsingDataPanelVisible
}
Expand Down
2 changes: 1 addition & 1 deletion docs/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ WindowStore
downloadsToolbar: {
isVisible: boolean // whether or not the downloads toolbar is visible
},
hasFocus: boolean, // true if window has focus
isFocused: boolean, // true if window has focus
isClearBrowsingDataPanelVisible: boolean, // true if the Clear Browsing Data panel is visible
isFullScreen: boolean, // true if window is fullscreen
isMaximized: boolean, // true if window is maximized
Expand Down
40 changes: 0 additions & 40 deletions docs/windowActions.md
Original file line number Diff line number Diff line change
Expand Up @@ -592,16 +592,6 @@ This is mainly just used to trigger updates throughout React.



### setMaximizeState(isMaximized)

Sets the maximize state of the window

**Parameters**

**isMaximized**: `boolean`, true if window is maximized



### savePosition(position)

Saves the position of the window in the window state
Expand All @@ -612,26 +602,6 @@ Saves the position of the window in the window state



### saveSize(size)

Saves the size (width, height) of the window in the window state

**Parameters**

**size**: `Array`, [x, y]



### setWindowFullScreen(isFullScreen)

Sets the fullscreen state of the window

**Parameters**

**isFullScreen**: `boolean`, true if window is fullscreen



### setMouseInTitlebar(mouseInTitlebar)

Dispatches a message to indicate if the mouse is in the titlebar
Expand Down Expand Up @@ -929,16 +899,6 @@ Fired when the mouse clicks or hovers over a bookmark folder in the bookmarks to



### onFocusChanged(hasFocus)

Fired when window receives or loses focus

**Parameters**

**hasFocus**: `boolean`, true if focused, false if blurred



### setModalDialogDetail(className, props)

Set Modal Dialog detail
Expand Down
Loading

0 comments on commit 901d5db

Please sign in to comment.