forked from brave/browser-laptop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moves bookmark text calculation into the state
Resolves brave#9517 Auditors: Test Plan:
- Loading branch information
Showing
11 changed files
with
268 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* 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 assert = require('assert') | ||
const Immutable = require('immutable') | ||
|
||
// Utils | ||
const bookmarkUtil = require('../lib/bookmarkUtil') | ||
const bookmarkFoldersUtil = require('../lib/bookmarkFoldersUtil') | ||
const {makeImmutable, isMap} = require('./immutableUtil') | ||
|
||
// Styles | ||
const globalStyles = require('../../renderer/components/styles/global') | ||
const {iconSize} = require('../../../js/constants/config') | ||
|
||
const validateState = function (state) { | ||
state = makeImmutable(state) | ||
assert.ok(isMap(state), 'state must be an Immutable.Map') | ||
assert.ok(isMap(state.get('bookmarkToolbar')), 'state must contain an Immutable.Map of bookmarkToolbar') | ||
return state | ||
} | ||
|
||
const bookmarkToolbarState = { | ||
// TODO trigger this function when BOOKMARKS_TOOLBAR_MODE changes | ||
setToolbar: (state, bookmarks) => { | ||
validateState(state) | ||
let widthAccountedFor = 0 | ||
|
||
const onlyText = bookmarkUtil.showOnlyText() | ||
const textAndFavicon = bookmarkUtil.showTextAndFavicon() | ||
const onlyFavicon = bookmarkUtil.showOnlyFavicon() | ||
|
||
const maxWidth = parseInt(globalStyles.spacing.bookmarksItemMaxWidth, 10) | ||
const padding = parseInt(globalStyles.spacing.bookmarksItemPadding, 10) * 2 | ||
const itemMargin = parseInt(globalStyles.spacing.bookmarksItemMargin, 10) * 2 | ||
|
||
// No margin for show only fav icons | ||
const margin = onlyFavicon ? 0 : itemMargin | ||
|
||
// Toolbar padding is only on the left | ||
const toolbarPadding = parseInt(globalStyles.spacing.bookmarksToolbarPadding) | ||
|
||
const overflowButtonWidth = parseInt(globalStyles.spacing.bookmarksToolbarOverflowButtonWidth, 10) | ||
const maximumBookmarksToolbarWidth = state.getIn(['defaultWindowParams', 'width']) - overflowButtonWidth | ||
|
||
widthAccountedFor += toolbarPadding | ||
|
||
// Loop through until we fill up the entire bookmark toolbar width | ||
let i = 0 | ||
for (let item of bookmarks) { | ||
let iconWidth | ||
|
||
if (onlyText) { | ||
iconWidth = 0 | ||
} else if (textAndFavicon || bookmarkFoldersUtil.isFolder(item)) { | ||
iconWidth = iconSize + parseInt(globalStyles.spacing.itemsItemMargin, 10) | ||
} else if (onlyFavicon) { | ||
iconWidth = iconSize | ||
} | ||
|
||
let extraWidth | ||
|
||
if (onlyText) { | ||
extraWidth = padding + item.get('width') | ||
} else if (textAndFavicon) { | ||
extraWidth = padding + iconWidth + item.get('width') | ||
} else if (onlyFavicon) { | ||
extraWidth = padding + iconWidth | ||
|
||
if (bookmarkFoldersUtil.isFolder(item)) { | ||
extraWidth += item.get('width') | ||
} | ||
} | ||
|
||
extraWidth = Math.min(extraWidth, maxWidth) | ||
widthAccountedFor += extraWidth + margin | ||
|
||
if (widthAccountedFor >= maximumBookmarksToolbarWidth) { | ||
widthAccountedFor -= extraWidth + margin | ||
i-- | ||
|
||
break | ||
} | ||
|
||
i++ | ||
} | ||
|
||
return state | ||
.setIn(['bookmarkToolbar', 'toolbar'], bookmarks.take(i).map((item) => item.get('key')).toList()) | ||
.setIn(['bookmarkToolbar', 'other'], bookmarks.skip(i).take(100).map((item) => item.get('key')).toList()) | ||
}, | ||
|
||
getToolbar: (state) => { | ||
validateState(state) | ||
|
||
return state.getIn(['bookmarkToolbar', 'toolbar'], Immutable.List()) | ||
}, | ||
|
||
getOther: (state) => { | ||
validateState(state) | ||
|
||
return state.getIn(['bookmarkToolbar', 'toolbar'], Immutable.List()) | ||
} | ||
} | ||
|
||
module.exports = bookmarkToolbarState |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.