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

Commit 2d77f3c

Browse files
committed
refactor preview handling to make the view dumber
1 parent 8ead2c8 commit 2d77f3c

File tree

8 files changed

+70
-89
lines changed

8 files changed

+70
-89
lines changed

app/renderer/components/tabs/tab.js

-18
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ const tabContentState = require('../../../common/state/tabContentState')
2727

2828
// Constants
2929
const dragTypes = require('../../../../js/constants/dragTypes')
30-
const settings = require('../../../../js/constants/settings')
3130

3231
// Styles
3332
const styles = require('../styles/tab')
@@ -43,7 +42,6 @@ const frameStateUtil = require('../../../../js/state/frameStateUtil')
4342
const {getTabBreakpoint, tabUpdateFrameRate} = require('../../lib/tabUtil')
4443
const {isWindows} = require('../../../common/lib/platformUtil')
4544
const {getCurrentWindowId} = require('../../currentWindow')
46-
const {getSetting} = require('../../../../js/settings')
4745
const UrlUtil = require('../../../../js/lib/urlutil')
4846
const {hasBreakpoint} = require('../../lib/tabUtil')
4947

@@ -130,25 +128,10 @@ class Tab extends React.Component {
130128
}
131129

132130
onMouseLeave () {
133-
if (this.props.previewTabs) {
134-
window.clearTimeout(this.hoverTimeout)
135-
windowActions.setPreviewFrame(null)
136-
}
137131
windowActions.setTabHoverState(this.props.frameKey, false)
138132
}
139133

140134
onMouseEnter (e) {
141-
// relatedTarget inside mouseenter checks which element before this event was the pointer on
142-
// if this element has a tab-like class, then it's likely that the user was previewing
143-
// a sequency of tabs. Called here as previewMode.
144-
const previewMode = /tab(?!pages)/i.test(e.relatedTarget.classList)
145-
146-
// If user isn't in previewMode, we add a bit of delay to avoid tab from flashing out
147-
// as reported here: https://github.com/brave/browser-laptop/issues/1434
148-
if (this.props.previewTabs) {
149-
this.hoverTimeout =
150-
window.setTimeout(windowActions.setPreviewFrame.bind(null, this.props.frameKey), previewMode ? 0 : 200)
151-
}
152135
windowActions.setTabHoverState(this.props.frameKey, true)
153136
}
154137

@@ -279,7 +262,6 @@ class Tab extends React.Component {
279262

280263
// used in other functions
281264
props.totalTabs = state.get('tabs').size
282-
props.previewTabs = getSetting(settings.SHOW_TAB_PREVIEWS)
283265
props.dragData = state.getIn(['dragData', 'type']) === dragTypes.TAB && state.get('dragData')
284266
props.hasTabInFullScreen = tabContentState.hasTabInFullScreen(currentWindow)
285267
props.tabId = frame.get('tabId')

app/renderer/components/tabs/tabPage.js

-17
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,6 @@ class TabPage extends React.Component {
3232
this.onMouseLeave = this.onMouseLeave.bind(this)
3333
}
3434

35-
onMouseLeave () {
36-
window.clearTimeout(this.hoverTimeout)
37-
windowActions.setPreviewTabPageIndex()
38-
}
39-
40-
onMouseEnter (e) {
41-
// relatedTarget inside mouse enter checks which element before this event was the pointer on
42-
// if this element has a tab-like class, then it's likely that the user was previewing
43-
// a sequence of tabs. Called here as previewMode.
44-
const previewMode = /tab(?!pages)/i.test(e.relatedTarget.classList)
45-
46-
// If user isn't in previewMode, we add a bit of delay to avoid tab from flashing out
47-
// as reported here: https://github.com/brave/browser-laptop/issues/1434
48-
this.hoverTimeout =
49-
window.setTimeout(windowActions.setPreviewTabPageIndex.bind(null, this.props.index), previewMode ? 0 : 200)
50-
}
51-
5235
onDrop (e) {
5336
if (this.props.isPageEmpty) {
5437
return

app/renderer/reducers/frameReducer.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ const frameStateUtil = require('../../../js/state/frameStateUtil')
2020
const {getCurrentWindowId} = require('../currentWindow')
2121
const {getSourceAboutUrl, getSourceMagnetUrl} = require('../../../js/lib/appUrlUtil')
2222
const {isURL, isPotentialPhishingUrl, getUrlFromInput} = require('../../../js/lib/urlutil')
23-
const settings = require('../../../js/constants/settings')
24-
const {getSetting} = require('../../../js/settings')
2523

2624
const setFullScreen = (state, action) => {
2725
const index = frameStateUtil.getFrameIndex(state, action.frameProps.get('key'))
@@ -32,15 +30,13 @@ const setFullScreen = (state, action) => {
3230
}
3331

3432
const closeFrame = (state, action) => {
35-
const activeFrameIndex = frameStateUtil.getActiveFrameIndex(state)
3633
const index = frameStateUtil.getFrameIndex(state, action.frameKey)
3734
if (index === -1) {
3835
return state
3936
}
4037

4138
const frameProps = frameStateUtil.getFrameByKey(state, action.frameKey)
4239
const hoverState = state.getIn(['frames', index, 'hoverState'])
43-
const framePreviewEnabled = getSetting(settings.SHOW_TAB_PREVIEWS)
4440

4541
state = state.merge(frameStateUtil.removeFrame(
4642
state,
@@ -56,14 +52,12 @@ const closeFrame = (state, action) => {
5652

5753
const nextFrame = frameStateUtil.getFrameByIndex(state, index)
5854

59-
if (nextFrame && hoverState) {
55+
if (nextFrame) {
6056
// Copy the hover state if tab closed with mouse as long as we have a next frame
6157
// This allow us to have closeTab button visible for sequential frames closing,
6258
// until onMouseLeave event happens.
63-
windowActions.setTabHoverState(nextFrame.get('key'), hoverState)
64-
if (framePreviewEnabled && index !== activeFrameIndex) {
65-
// After closing a tab, preview the next frame as long as there is one
66-
windowActions.setPreviewFrame(nextFrame.get('key'))
59+
if (hoverState) {
60+
windowActions.setTabHoverState(nextFrame.get('key'), hoverState)
6761
}
6862
}
6963

docs/windowActions.md

+5-12
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,18 @@ Dispatches a message to the store when the frame is active and the window is foc
189189

190190

191191

192-
### setPreviewFrame(frameKey)
192+
### setPreviewFrame(frameKey, immediate)
193193

194194
Dispatches a message to the store to set a preview frame.
195-
This is done when hovering over a tab.
195+
This should only be called internally by `WINDOW_SET_TAB_HOVER_STATE`
196+
when we need to delay updating the preview frame value
196197

197198
**Parameters**
198199

199200
**frameKey**: `Object`, the frame key for the webview in question.
200201

202+
**immediate**: `Object`, set the preview frame without any delays
203+
201204

202205

203206
### setTabPageIndex(index)
@@ -234,16 +237,6 @@ Dispatches a message to the store to set the current tab hover state.
234237

235238

236239

237-
### setPreviewTabPageIndex(previewTabPageIndex)
238-
239-
Dispatches a message to the store to set the tab page index being previewed.
240-
241-
**Parameters**
242-
243-
**previewTabPageIndex**: `number`, The tab page index to preview
244-
245-
246-
247240
### setTabPageIndexByFrame(frameProps)
248241

249242
Dispatches a message to the store to set the tab page index.

js/actions/windowActions.js

+6-15
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,17 @@ const windowActions = {
229229

230230
/**
231231
* Dispatches a message to the store to set a preview frame.
232-
* This is done when hovering over a tab.
232+
* This should only be called internally by `WINDOW_SET_TAB_HOVER_STATE`
233+
* when we need to delay updating the preview frame value
233234
*
234235
* @param {Object} frameKey - the frame key for the webview in question.
236+
* @param {Object} immediate - set the preview frame without any delays
235237
*/
236-
setPreviewFrame: function (frameKey) {
238+
setPreviewFrame: function (frameKey, immediate = false) {
237239
dispatch({
238240
actionType: windowConstants.WINDOW_SET_PREVIEW_FRAME,
239-
frameKey
241+
frameKey,
242+
immediate
240243
})
241244
},
242245

@@ -280,18 +283,6 @@ const windowActions = {
280283
})
281284
},
282285

283-
/**
284-
* Dispatches a message to the store to set the tab page index being previewed.
285-
*
286-
* @param {number} previewTabPageIndex - The tab page index to preview
287-
*/
288-
setPreviewTabPageIndex: function (previewTabPageIndex) {
289-
dispatch({
290-
actionType: windowConstants.WINDOW_SET_PREVIEW_TAB_PAGE_INDEX,
291-
previewTabPageIndex
292-
})
293-
},
294-
295286
/**
296287
* Dispatches a message to the store to set the tab page index.
297288
*

js/constants/windowConstants.js

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const windowConstants = {
1111
WINDOW_CLOSE_FRAMES: _,
1212
WINDOW_SET_FOCUSED_FRAME: _,
1313
WINDOW_SET_PREVIEW_FRAME: _,
14-
WINDOW_SET_PREVIEW_TAB_PAGE_INDEX: _,
1514
WINDOW_SET_TAB_PAGE_INDEX: _,
1615
WINDOW_SET_TAB_BREAKPOINT: _,
1716
WINDOW_SET_TAB_HOVER_STATE: _,

js/state/frameStateUtil.js

+54-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const {getSetting} = require('../settings')
2121
const {isIntermediateAboutPage} = require('../lib/appUrlUtil')
2222
const urlParse = require('../../app/common/urlParse')
2323

24+
let hoverTimeout = null
25+
2426
const comparatorByKeyAsc = (a, b) => a.get('key') > b.get('key')
2527
? 1 : b.get('key') > a.get('key') ? -1 : 0
2628

@@ -397,9 +399,8 @@ function removeFrame (state, frameProps, framePropsIndex) {
397399
}
398400
}
399401

400-
function getFrameTabPageIndex (state, frameProps, tabsPerTabPage) {
401-
frameProps = makeImmutable(frameProps)
402-
const index = findNonPinnedDisplayIndexForFrameKey(state, frameProps.get('key'))
402+
function getFrameTabPageIndex (state, frameKey, tabsPerTabPage = getSetting(settings.TABS_PER_PAGE)) {
403+
const index = findNonPinnedDisplayIndexForFrameKey(state, frameKey)
403404
if (index === -1) {
404405
return -1
405406
}
@@ -451,7 +452,8 @@ function isPinned (state, frameKey) {
451452
* @param frameProps Any frame belonging to the page
452453
*/
453454
function updateTabPageIndex (state, frameProps) {
454-
const index = getFrameTabPageIndex(state, frameProps, getSetting(settings.TABS_PER_PAGE))
455+
frameProps = makeImmutable(frameProps)
456+
const index = getFrameTabPageIndex(state, frameProps.get('key'))
455457

456458
if (index === -1) {
457459
return state
@@ -532,7 +534,55 @@ const getTabPageCount = (state) => {
532534
return Math.ceil(frames.size / tabsPerPage)
533535
}
534536

537+
const getPreviewFrameKey = (state) => {
538+
return state.get('previewFrameKey')
539+
}
540+
541+
const setPreviewFrameKey = (state, frameKey, immediate = false) => {
542+
clearTimeout(hoverTimeout)
543+
const frame = getFrameByKey(state, frameKey)
544+
const isActive = isFrameKeyActive(state, frameKey)
545+
const previewTabs = getSetting(settings.SHOW_TAB_PREVIEWS)
546+
let newPreviewFrameKey = frameKey
547+
548+
if (!previewTabs || frame == null || !frame.get('hoverState') || isActive) {
549+
newPreviewFrameKey = null
550+
}
551+
552+
if (!immediate) {
553+
// if there is an existing preview frame key then we're already in preview mode
554+
// we use actions here because that is the only way to delay updating the state
555+
const previewMode = getPreviewFrameKey(state) != null
556+
if (previewMode && newPreviewFrameKey == null) {
557+
// add a small delay when we are clearing the preview frame key so we don't lose
558+
// previewMode if the user mouses over another tab - see below
559+
hoverTimeout = setTimeout(windowActions.setPreviewFrame.bind(null, null, true), 200)
560+
return state
561+
}
562+
563+
if (!previewMode) {
564+
// If user isn't in previewMode so we add a bit of delay to avoid tab from flashing out
565+
// as reported here: https://github.com/brave/browser-laptop/issues/1434
566+
// using an action here because that is the only way we can do a delayed state update
567+
hoverTimeout = setTimeout(windowActions.setPreviewFrame.bind(null, newPreviewFrameKey, true), 200)
568+
return state
569+
}
570+
}
571+
572+
const index = getFrameTabPageIndex(state, frame)
573+
if (index !== -1) {
574+
if (index !== state.getIn(['ui', 'tabs', 'tabPageIndex'])) {
575+
state = state.setIn(['ui', 'tabs', 'previewTabPageIndex'], index)
576+
} else {
577+
state = state.deleteIn(['ui', 'tabs', 'previewTabPageIndex'])
578+
}
579+
}
580+
return state.set('previewFrameKey', newPreviewFrameKey)
581+
}
582+
535583
module.exports = {
584+
setPreviewFrameKey,
585+
getPreviewFrameKey,
536586
deleteTabInternalIndex,
537587
deleteFrameInternalIndex,
538588
updateFramesInternalIndex,

js/stores/windowStore.js

+2-13
Original file line numberDiff line numberDiff line change
@@ -355,19 +355,7 @@ const doAction = (action) => {
355355
windowState = windowState.set('closedFrames', new Immutable.List())
356356
break
357357
case windowConstants.WINDOW_SET_PREVIEW_FRAME:
358-
windowState = windowState.merge({
359-
previewFrameKey:
360-
action.frameKey != null && !frameStateUtil.isFrameKeyActive(windowState, action.frameKey)
361-
? action.frameKey
362-
: null
363-
})
364-
break
365-
case windowConstants.WINDOW_SET_PREVIEW_TAB_PAGE_INDEX:
366-
if (action.previewTabPageIndex !== windowState.getIn(['ui', 'tabs', 'tabPageIndex'])) {
367-
windowState = windowState.setIn(['ui', 'tabs', 'previewTabPageIndex'], action.previewTabPageIndex)
368-
} else {
369-
windowState = windowState.deleteIn(['ui', 'tabs', 'previewTabPageIndex'])
370-
}
358+
windowState = frameStateUtil.setPreviewFrameKey(windowState, action.frameKey, action.immediate)
371359
break
372360
case windowConstants.WINDOW_SET_TAB_PAGE_INDEX:
373361
if (action.index !== undefined) {
@@ -393,6 +381,7 @@ const doAction = (action) => {
393381
const frameIndex = frameStateUtil.getFrameIndex(windowState, action.frameKey)
394382
if (frameIndex !== -1) {
395383
windowState = windowState.setIn(['frames', frameIndex, 'hoverState'], action.hoverState)
384+
windowState = frameStateUtil.setPreviewFrameKey(windowState, action.frameKey)
396385
}
397386
break
398387
}

0 commit comments

Comments
 (0)