Skip to content

Commit

Permalink
feat(frontend/presenter): only advance on click if focused (#577)
Browse files Browse the repository at this point in the history
Closes #388
  • Loading branch information
Harjot1Singh authored Jun 3, 2020
1 parent 7f5ced3 commit f5ec302
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
26 changes: 24 additions & 2 deletions app/frontend/src/lib/hooks.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useContext } from 'react'
import { useContext, useState, useEffect } from 'react'
import { invert } from 'lodash'
import copy from 'copy-to-clipboard'
import { useSnackbar } from 'notistack'

import { getTranslation, getTransliteration, findLineIndex } from './utils'
import { ContentContext, RecommendedSourcesContext, SettingsContext } from './contexts'
import { LANGUAGES } from './consts'
import { isMac, LANGUAGES } from './consts'

const languagesById = invert( LANGUAGES )

Expand Down Expand Up @@ -73,3 +73,25 @@ export const useCopyToClipboard = () => {
)
}
}

export const useWindowFocus = () => {
const [ focused, setFocused ] = useState( document.hasFocus() )

// Keep track of whether the window is focused
useEffect( () => {
// Use click to determine focus on non-Mac platforms
const focusEvent = isMac ? 'focus' : 'click'
const onBlur = () => setFocused( false )
const onFocus = () => setFocused( true )

window.addEventListener( 'blur', onBlur )
window.addEventListener( focusEvent, onFocus )

return () => {
window.removeEventListener( 'blur', onBlur )
window.removeEventListener( focusEvent, onFocus )
}
}, [ setFocused ] )

return focused
}
8 changes: 5 additions & 3 deletions app/frontend/src/shared/NavigatorHotkeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { mapPlatformKeys, getJumpLines, findLineIndex } from '../lib/utils'
import controller from '../lib/controller'
import { NAVIGATOR_SHORTCUTS, LINE_HOTKEYS } from '../lib/keyMap'
import { ContentContext, HistoryContext, SettingsContext } from '../lib/contexts'
import { useCurrentLines } from '../lib/hooks'
import { useCurrentLines, useWindowFocus } from '../lib/hooks'

/**
* Hotkeys for controlling the navigator.
Expand Down Expand Up @@ -135,11 +135,13 @@ const NavigatorHotKeys = ( { active, children, mouseTargetRef } ) => {

const keyMap = mapPlatformKeys( { ...hotkeys, ...numberKeyMap } )

const windowFocused = useWindowFocus()

// Register mouse shortcuts
useEffect( () => {
const { current: mouseTarget } = mouseTargetRef

if ( !active || !mouseTarget ) return () => {}
if ( !active || !mouseTarget || !windowFocused ) return noop

const events = [
[ 'click', goNextLine ],
Expand All @@ -155,7 +157,7 @@ const NavigatorHotKeys = ( { active, children, mouseTargetRef } ) => {
return () => events.forEach(
( [ event, handler ] ) => mouseTarget.removeEventListener( event, handler ),
)
}, [ mouseTargetRef, active, goNextLine, goPreviousLine, autoToggle ] )
}, [ mouseTargetRef, active, goNextLine, goPreviousLine, autoToggle, windowFocused ] )

return (
<GlobalHotKeys keyMap={keyMap} handlers={active ? hotKeyHandlers : {}}>
Expand Down

0 comments on commit f5ec302

Please sign in to comment.