Skip to content

Commit

Permalink
perf: remove passive: false wheel listener
Browse files Browse the repository at this point in the history
...when it's unnecessary
  • Loading branch information
WofWca authored and Simon-Laux committed Apr 8, 2024
1 parent 31cd8af commit 6b7d2f2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Fixed
- fix chat audit dialog was going out of viewport on smaller screens #3736
- fix long names breaking layout of reactions dialog #3736
- improve chat scroll performance #3743

<a id="1_44_1"></a>

Expand Down
25 changes: 18 additions & 7 deletions src/renderer/components/ContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function ContextMenuLayer({

// Get required information
setCurrentItems(items)
window.__contextMenuActive = true
window.__setContextMenuActive(true)
setActive(true)

await new Promise<void>((resolve, _reject) => {
Expand Down Expand Up @@ -124,7 +124,7 @@ export function ContextMenuLayer({
}, [])

const cancel = useCallback(() => {
window.__contextMenuActive = false
window.__setContextMenuActive(false)
setActive(false)
setCurrentItems([])
endPromiseRef.current?.()
Expand Down Expand Up @@ -415,11 +415,22 @@ export function useContextMenuWithActiveState(
* for some reason removing the listeners doesn't work for those
*/
function preventDefault(e: Event) {
if (window.__contextMenuActive) {
e.preventDefault()
}
e.preventDefault()
}
const wheelEvent: 'wheel' | 'mousewheel' =
'onwheel' in document.createElement('div') ? 'wheel' : 'mousewheel'
document.addEventListener(wheelEvent, preventDefault, { passive: false })
document.addEventListener('touchmove', preventDefault, { passive: false })

window.__setContextMenuActive = (newVal: boolean): void => {
if (newVal) {
// Adding the same listener twice in a row has no effect.
document.addEventListener(wheelEvent, preventDefault, { passive: false })
document.addEventListener('touchmove', preventDefault, { passive: false })
} else {
document.removeEventListener(wheelEvent, preventDefault)
document.removeEventListener('touchmove', preventDefault)
}
type Writable<T> = {
-readonly [P in keyof T]: T[P]
}
;(window as Writable<typeof window>).__contextMenuActive = newVal
}
3 changes: 2 additions & 1 deletion src/renderer/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ declare global {
__selectAccount: (accountId: number) => Promise<void>
readonly __selectedAccountId: number | undefined
__screen: Screens
__contextMenuActive: boolean
readonly __contextMenuActive: boolean
__setContextMenuActive: (newVal: boolean) => void
__settingsOpened: boolean
__keybindingsDialogOpened: boolean
__setQuoteInDraft: ((msgId: number) => void) | null
Expand Down

0 comments on commit 6b7d2f2

Please sign in to comment.