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

Commit

Permalink
Moves bookmark item width into the state
Browse files Browse the repository at this point in the history
Resolves #9517

Auditors: @bsclifton @bridiver @ayumi

Test Plan:
  • Loading branch information
NejcZdovc committed Jun 26, 2017
1 parent cf6e22f commit be4498e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 42 deletions.
68 changes: 40 additions & 28 deletions app/common/lib/bookmarkUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ const Immutable = require('immutable')
const dragTypes = require('../../../js/constants/dragTypes')
const {bookmarksToolbarMode} = require('../constants/settingsEnums')
const settings = require('../../../js/constants/settings')
const {iconSize} = require('../../../js/constants/config')

// Utils
const domUtil = require('../../renderer/lib/domUtil')
const siteUtil = require('../../../js/state/siteUtil')
const {calculateTextWidth} = require('../../../js/lib/textCalculator')
const {iconSize} = require('../../../js/constants/config')
const {getSetting} = require('../../../js/settings')
const {calculateTextWidth} = require('../../../js/lib/textCalculator')

const bookmarkHangerHeading = (detail, isFolder, shouldShowLocation) => {
if (isFolder) {
Expand Down Expand Up @@ -72,49 +72,30 @@ const getToolbarBookmarks = (state) => {
const sites = state.get('sites', Immutable.List())

const noParentItems = siteUtil.getBookmarks(sites)
.sort(siteUtil.siteSort)
.filter((bookmark) => !bookmark.get('parentFolderId'))
let widthAccountedFor = 0
.sort(siteUtil.siteSort)

let widthAccountedFor = domUtil.getStyleConstants('bookmarks-toolbar-padding')
const overflowButtonWidth = 25
const onlyFavicon = showOnlyFavicon()
const favicon = showFavicon()

// Dynamically calculate how many bookmark items should appear on the toolbar
// before it is actually rendered.
const maxWidth = domUtil.getStyleConstants('bookmark-item-max-width')
const padding = domUtil.getStyleConstants('bookmark-item-padding') * 2
// Toolbar padding is only on the left
const toolbarPadding = domUtil.getStyleConstants('bookmarks-toolbar-padding')
const bookmarkItemMargin = domUtil.getStyleConstants('bookmark-item-margin') * 2
// No margin for show only favicons
const chevronMargin = domUtil.getStyleConstants('bookmark-item-chevron-margin')
const fontSize = domUtil.getStyleConstants('bookmark-item-font-size')
const fontFamily = domUtil.getStyleConstants('default-font-family')
const chevronWidth = chevronMargin + fontSize
const margin = favicon && onlyFavicon ? 0 : bookmarkItemMargin
const windowWidth = window.innerWidth
widthAccountedFor += toolbarPadding

// Loop through until we fill up the entire bookmark toolbar width
let i = 0
for (let bookmark of noParentItems) {
const current = bookmark[1]
let iconWidth = favicon ? iconSize : 0
// font-awesome file icons are 3px smaller
if (favicon && !current.get('folderId') && !current.get('favicon')) {
iconWidth -= 3
}
const currentChevronWidth = favicon && current.get('folderId') ? chevronWidth : 0
if (favicon && onlyFavicon) {
widthAccountedFor += padding + iconWidth + currentChevronWidth
} else {
const text = current.get('customTitle') || current.get('title') || current.get('location')
widthAccountedFor += Math.min(calculateTextWidth(text, `${fontSize} ${fontFamily}`) + padding + iconWidth + currentChevronWidth, maxWidth)
}
widthAccountedFor += noParentItems.getIn([bookmark[1], 'bookmarkWidth'])
widthAccountedFor += margin

if (widthAccountedFor >= windowWidth - overflowButtonWidth) {
break
}

i++
}

Expand All @@ -125,12 +106,43 @@ const getToolbarBookmarks = (state) => {
}
}

const calcBookmarkWidth = (bookmark) => {
let bookmarkWidth = 0
// Dynamically calculate how many bookmark items should appear on the toolbar
// before it is actually rendered.
const maxWidth = domUtil.getStyleConstants('bookmark-item-max-width')
const padding = domUtil.getStyleConstants('bookmark-item-padding') * 2
// No margin for show only favicons
const chevronMargin = domUtil.getStyleConstants('bookmark-item-chevron-margin')
const fontSize = domUtil.getStyleConstants('bookmark-item-font-size')
const fontFamily = domUtil.getStyleConstants('default-font-family')
const onlyFavicon = showOnlyFavicon()
const favicon = showFavicon()
const chevronWidth = chevronMargin + fontSize

let iconWidth = favicon ? iconSize : 0
// font-awesome file icons are 3px smaller
if (favicon && !bookmark.get('folderId') && !bookmark.get('favicon')) {
iconWidth -= 3
}
const currentChevronWidth = favicon && bookmark.get('folderId') ? chevronWidth : 0
if (favicon && onlyFavicon) {
bookmarkWidth += padding + iconWidth + currentChevronWidth
} else {
const text = bookmark.get('customTitle') || bookmark.get('title') || bookmark.get('location')
bookmarkWidth += Math.min(calculateTextWidth(text, `${fontSize} ${fontFamily}`) + padding + iconWidth + currentChevronWidth, maxWidth)
}

return bookmarkWidth
}

module.exports = {
bookmarkHangerHeading,
displayBookmarkName,
isBookmarkNameValid,
showOnlyFavicon,
showFavicon,
getDNDBookmarkData,
getToolbarBookmarks
getToolbarBookmarks,
calcBookmarkWidth
}
1 change: 1 addition & 0 deletions docs/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ AppStore
},
sites: {
[siteKey]: {
bookmarkWidth: number, // width of a bookmark on bookmarks toolbar
creationTime: number, //creation time of bookmark
customTitle: string, // User provided title for bookmark; overrides title
favicon: string, // URL of the favicon
Expand Down
6 changes: 3 additions & 3 deletions js/lib/textCalculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* 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 ctx = document.createElement('canvas').getContext('2d')
//const ctx = document.createElement('canvas').getContext('2d')
module.exports.calculateTextWidth = (text, font = '11px Arial') => {
ctx.font = font
return ctx.measureText(text).width
//ctx.font = font
return font.length*2 //ctx.measureText(text).width
}
37 changes: 26 additions & 11 deletions js/state/siteUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@

'use strict'
const Immutable = require('immutable')

// State
const siteCache = require('../../app/common/state/siteCache')
const {makeImmutable} = require('../../app/common/state/immutableUtil')

// Constants
const siteTags = require('../constants/siteTags')
const settings = require('../constants/settings')
const getSetting = require('../settings').getSetting

// Utils
const {getSetting} = require('../settings')
const UrlUtil = require('../lib/urlutil')
const urlParse = require('../../app/common/urlParse')
const {makeImmutable} = require('../../app/common/state/immutableUtil')
const bookmarkUtil = require('../../app/common/lib/bookmarkUtil')

const defaultTags = new Immutable.List([siteTags.DEFAULT])

Expand Down Expand Up @@ -216,6 +223,7 @@ const mergeSiteDetails = (oldSiteDetail, newSiteDetail, tag, folderId, order) =>
tags,
objectId: newSiteDetail.get('objectId') || (oldSiteDetail ? oldSiteDetail.get('objectId') : undefined),
title: newSiteDetail.get('title'),
bookmarkWidth: newSiteDetail.get('bookmarkWidth'),
order
})

Expand Down Expand Up @@ -289,18 +297,25 @@ module.exports.addSite = function (state, siteDetail, tag, originalSiteDetail, s
const oldSite = oldKey !== null ? sites.get(oldKey) : null
let folderId = siteDetail.get('folderId')

if (tag === siteTags.BOOKMARK_FOLDER) {
if (!oldSite && folderId) {
// Remove duplicate folder (needed for import)
const dupFolder = sites.find((site) => isBookmarkFolder(site.get('tags')) &&
switch (tag) {
case siteTags.BOOKMARK_FOLDER: {
if (!oldSite && folderId) {
// Remove duplicate folder (needed for import)
const dupFolder = sites.find((site) => isBookmarkFolder(site.get('tags')) &&
site.get('parentFolderId') === siteDetail.get('parentFolderId') &&
site.get('customTitle') === siteDetail.get('customTitle'))
if (dupFolder) {
state = module.exports.removeSite(state, dupFolder, siteTags.BOOKMARK_FOLDER, true)
if (dupFolder) {
state = module.exports.removeSite(state, dupFolder, siteTags.BOOKMARK_FOLDER, true)
}
} else if (!folderId) {
// Assign an id if this is a new folder
folderId = module.exports.getNextFolderId(sites)
}
} else if (!folderId) {
// Assign an id if this is a new folder
folderId = module.exports.getNextFolderId(sites)
siteDetail = siteDetail.set('bookmarkWidth', bookmarkUtil.calcBookmarkWidth(siteDetail))
break
}
case siteTags.BOOKMARK: {
siteDetail = siteDetail.set('bookmarkWidth', bookmarkUtil.calcBookmarkWidth(siteDetail))
}
}

Expand Down

0 comments on commit be4498e

Please sign in to comment.