Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(desktop): stop middle-click paste while autoscrolling (#3662)

Hampus26b961b4612b
diff --git a/fluxer_app/src/app/App.tsx b/fluxer_app/src/app/App.tsx
index cd1802a12..9b33ca44c 100644
--- a/fluxer_app/src/app/App.tsx
+++ b/fluxer_app/src/app/App.tsx
@@ -55,7 +55,10 @@ import {getActivePortalHost, setActivePortalHost} from '@app/features/ui/overlay
import MobileLayout from '@app/features/ui/state/MobileLayout';
import Modal from '@app/features/ui/state/Modal';
import Popout from '@app/features/ui/state/Popout';
-import {getDesktopWindowBehaviorSettings} from '@app/features/ui/utils/DesktopWindowBehaviorUtils';
+import {
+ attachMiddleClickAutoscrollPasteGuard,
+ getDesktopWindowBehaviorSettings,
+} from '@app/features/ui/utils/DesktopWindowBehaviorUtils';
import {isDesktop} from '@app/features/ui/utils/NativeUtils';
import {UNFOCUSED_FULLY_INTERACTIVE_CLASS} from '@app/features/ui/utils/WindowFocusInteractionGuard';
import UserSettings from '@app/features/user/state/UserSettings';
@@ -266,6 +269,7 @@ export const App = observer((): React.ReactElement => {
if (!isDesktop()) return;
void ensureAutostartDefaultEnabled();
void getDesktopWindowBehaviorSettings();
+ return attachMiddleClickAutoscrollPasteGuard(window);
}, []);
useEffect(() => {
void startDeepLinkHandling();
diff --git a/fluxer_app/src/features/ui/popout_window/MirroredDocumentPortal.tsx b/fluxer_app/src/features/ui/popout_window/MirroredDocumentPortal.tsx
index 22d8ea31b..8d5bc2209 100644
--- a/fluxer_app/src/features/ui/popout_window/MirroredDocumentPortal.tsx
+++ b/fluxer_app/src/features/ui/popout_window/MirroredDocumentPortal.tsx
@@ -7,6 +7,7 @@ import {getActivePortalHost, PortalHostContext, setActivePortalHost} from '@app/
import {Popouts} from '@app/features/ui/popover/PopoverPopouts';
import LayerManager from '@app/features/ui/state/LayerManager';
import {Toasts} from '@app/features/ui/toast/Toasts';
+import {attachMiddleClickAutoscrollPasteGuard} from '@app/features/ui/utils/DesktopWindowBehaviorUtils';
import {createWindowFocusInteractionGuard} from '@app/features/ui/utils/WindowFocusInteractionGuard';
import type React from 'react';
import {useEffect} from 'react';
@@ -37,6 +38,7 @@ function useMirroredDocumentPortalHost({childWindow, container}: MirroredDocumen
};
if (childDocument.hasFocus()) activatePortalHost();
const stopObservingLayers = LayerManager.observeDocument(childDocument);
+ const detachPasteGuard = attachMiddleClickAutoscrollPasteGuard(childWindow);
childWindow.addEventListener('focus', handleFocus);
childWindow.addEventListener('blur', handleBlur);
childDocument.addEventListener('pointerdown', activatePortalHost, true);
@@ -49,6 +51,7 @@ function useMirroredDocumentPortalHost({childWindow, container}: MirroredDocumen
childDocument.removeEventListener('focusin', activatePortalHost, true);
childDocument.removeEventListener('keydown', activatePortalHost, true);
stopObservingLayers();
+ detachPasteGuard();
focusGuard.destroy();
if (getActivePortalHost() === container) {
setActivePortalHost(null);
diff --git a/fluxer_app/src/features/ui/utils/DesktopWindowBehaviorUtils.ts b/fluxer_app/src/features/ui/utils/DesktopWindowBehaviorUtils.ts
index 70fab5d2a..6c73c6017 100644
--- a/fluxer_app/src/features/ui/utils/DesktopWindowBehaviorUtils.ts
+++ b/fluxer_app/src/features/ui/utils/DesktopWindowBehaviorUtils.ts
@@ -6,12 +6,29 @@ import type {DesktopWindowBehaviorSettings} from '@app/types/electron.d';
const logger = new Logger('DesktopWindowBehaviorUtils');
+const MIDDLE_MOUSE_BUTTON = 1;
+const MIDDLE_MOUSE_BUTTONS_FLAG = 4;
+
let cachedSettings: DesktopWindowBehaviorSettings | null = null;
export function getCachedDesktopWindowBehaviorSettings(): DesktopWindowBehaviorSettings | null {
return cachedSettings;
}
+export function attachMiddleClickAutoscrollPasteGuard(target: Window): () => void {
+ const handler = (event: PointerEvent) => {
+ if (event.button !== MIDDLE_MOUSE_BUTTON || (event.buttons & MIDDLE_MOUSE_BUTTONS_FLAG) !== 0) return;
+ if (!cachedSettings?.activeMiddleClickAutoscroll) return;
+ event.preventDefault();
+ };
+ target.addEventListener('pointerup', handler, true);
+ target.addEventListener('pointermove', handler, true);
+ return () => {
+ target.removeEventListener('pointerup', handler, true);
+ target.removeEventListener('pointermove', handler, true);
+ };
+}
+
export async function getDesktopWindowBehaviorSettings(): Promise<DesktopWindowBehaviorSettings | null> {
const electronApi = getElectronAPI();
if (!electronApi?.getDesktopWindowBehaviorSettings) return null;