Skip to content

Commit

Permalink
Saves computed styles in the state
Browse files Browse the repository at this point in the history
Resolves brave#9149

Auditors:

Test Plan:
  • Loading branch information
NejcZdovc committed May 30, 2017
1 parent 033b122 commit 573784d
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/browser/reducers/windowsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ const windowsReducer = (state, action, immutableAction) => {
case windowConstants.WINDOW_SHOULD_OPEN_DEV_TOOLS:
windows.openDevTools(action.get('windowId'))
break
case appConstants.APP_WINDOW_COMPUTED_STYLES:
state = windowState.updateComputedStyles(state, action)
break
}
return state
}
Expand Down
35 changes: 35 additions & 0 deletions app/common/constants/computedStyles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 computedStyles = {
'bookmark-item': [
'max-width',
'padding',
'margin',
'font-size'
],
'bookmark-item-chevron': [
'margin'
],
'bookmarks-toolbar': [
'padding'
],
'default': [
'font-family'
],
'download-bar': [
'buttons',
'height',
'padding'
],
'download-item': [
'margin',
'width'
],
'navbar': [
'height'
]
}

module.exports = computedStyles
14 changes: 14 additions & 0 deletions app/common/lib/windowsUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

const Immutable = require('immutable')
const computedStyles = require('../constants/computedStyles')

module.exports.getPinnedSiteProps = site => {
return Immutable.fromJS({
Expand All @@ -11,3 +12,16 @@ module.exports.getPinnedSiteProps = site => {
partitionNumber: site.get('partitionNumber') || 0
})
}

module.exports.getComputedStyleData = (root) => {
const result = {}

Object.keys(computedStyles).forEach(group => {
computedStyles[group].forEach(prop => {
const value = `--${group}-${prop}`
result[value] = root.getPropertyValue(value).trim()
})
})

return result
}
7 changes: 7 additions & 0 deletions app/common/state/windowState.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ const api = {
return state.set('windows', windows.delete(index).insert(index, windowValue))
},

updateComputedStyles: (state, action) => {
const windowIndex = api.getWindowIndexByWindowId(state, action.get('windowId'))
return state.mergeDeepIn(['windows', windowIndex], {
computedStyles: action.get('values')
})
},

getWindows: (state) => {
state = validateState(state)
return state.get('windows')
Expand Down
5 changes: 5 additions & 0 deletions app/renderer/components/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const appActions = require('../../../js/actions/appActions')

// Utils
const cx = require('../../../js/lib/classSet')
const windowsUtils = require('../../common/lib/windowsUtil')
const {getPlatformStyles} = require('../../common/lib/platformUtil')
const {getCurrentWindowId} = require('../currentWindow')

Expand Down Expand Up @@ -93,6 +94,10 @@ class Window extends React.Component {

componentDidMount () {
appActions.windowReady(getCurrentWindowId())

const root = window.getComputedStyle(document.querySelector(':root'))
const computedValues = windowsUtils.getComputedStyleData(root)
appActions.updateComputedStyles(getCurrentWindowId(), computedValues)
}

componentWillUnmount () {
Expand Down
8 changes: 8 additions & 0 deletions js/actions/appActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,14 @@ const appActions = {
dispatch({
actionType: appConstants.APP_UPDATE_LOG_OPENED
})
},

updateComputedStyles: function (windowId, values) {
dispatch({
actionType: appConstants.APP_WINDOW_COMPUTED_STYLES,
windowId,
values
})
}

}
Expand Down
3 changes: 2 additions & 1 deletion js/constants/appConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ const appConstants = {
APP_REMOVE_PASSWORD: _, /** @param {Object} passwordDetail */
APP_REMOVE_PASSWORD_SITE: _, /** @param {Object} passwordDetail */
APP_CLEAR_PASSWORDS: _,
APP_UPDATE_LOG_OPENED: _
APP_UPDATE_LOG_OPENED: _,
APP_WINDOW_COMPUTED_STYLES: _
}

module.exports = mapValuesByKeys(appConstants)

0 comments on commit 573784d

Please sign in to comment.