A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
refactor(app): move the popout window from voice to shared ui
Hampus6f47a1bde825
diff --git a/fluxer_app/src/features/voice/components/popout/PopoutTitlebar.module.css b/fluxer_app/src/features/ui/popout_window/PopoutTitlebar.module.csssimilarity index 85%rename from fluxer_app/src/features/voice/components/popout/PopoutTitlebar.module.cssrename to fluxer_app/src/features/ui/popout_window/PopoutTitlebar.module.cssindex c3a972828..1b6e27a12 100644--- a/fluxer_app/src/features/voice/components/popout/PopoutTitlebar.module.css+++ b/fluxer_app/src/features/ui/popout_window/PopoutTitlebar.module.css@@ -5,14 +5,14 @@align-items: center;flex: 0 0 auto;height: 32px;- padding-left: var(--spacing-3);- gap: var(--spacing-2);+ padding-inline-start: 12px;+ gap: 8px;background: var(--background-secondary);-webkit-app-region: drag;user-select: none;}-.titlebarMac {+.titlebar.titlebarMac {padding-left: calc(var(--macos-traffic-light-inset) + env(safe-area-inset-left));}@@ -22,7 +22,7 @@overflow: hidden;text-overflow: ellipsis;white-space: nowrap;- font-size: 0.8125rem;+ font-size: 13px;font-weight: 600;color: var(--text-primary-muted);}@@ -32,7 +32,7 @@align-items: stretch;height: 100%;gap: 0;- margin-left: auto;+ margin-inline-start: auto;-webkit-app-region: no-drag;}diff --git a/fluxer_app/src/features/voice/components/popout/PopoutTitlebar.tsx b/fluxer_app/src/features/ui/popout_window/PopoutTitlebar.tsxsimilarity index 84%rename from fluxer_app/src/features/voice/components/popout/PopoutTitlebar.tsxrename to fluxer_app/src/features/ui/popout_window/PopoutTitlebar.tsxindex d25d7a4e7..515902fab 100644--- a/fluxer_app/src/features/voice/components/popout/PopoutTitlebar.tsx+++ b/fluxer_app/src/features/ui/popout_window/PopoutTitlebar.tsx@@ -1,8 +1,8 @@// SPDX-License-Identifier: AGPL-3.0-or-laterimport FocusRing from '@app/features/ui/focus_ring/FocusRing';+import styles from '@app/features/ui/popout_window/PopoutTitlebar.module.css';import {getNativePlatformSync} from '@app/features/ui/utils/NativeUtils';-import styles from '@app/features/voice/components/popout/PopoutTitlebar.module.css';import {POP_BACK_IN_DESCRIPTOR} from '@app/features/voice/utils/VoiceSharedMessageDescriptors';import {NATIVE_TITLEBAR_CLASS} from '@fluxer/desktop_ipc/src/NativeTitlebarShell';import {msg} from '@lingui/core/macro';@@ -33,7 +33,7 @@ interface PopoutTitlebarProps {showTitle?: boolean;isAlwaysOnTop: boolean;onToggleAlwaysOnTop?: () => void;- onRestore: () => void;+ onRestore?: () => void;onClose: () => void;}@@ -79,18 +79,20 @@ export const PopoutTitlebar: React.FC<PopoutTitlebarProps> = ({</button></FocusRing>)}- <FocusRing offset={-2} data-flx="voice.popout-titlebar.focus-ring.restore">- <button- type="button"- className={NATIVE_TITLEBAR_CLASS.control}- onClick={onRestore}- aria-label={i18n._(POP_BACK_IN_DESCRIPTOR)}- title={i18n._(POP_BACK_IN_DESCRIPTOR)}- data-flx="voice.popout-titlebar.control-button.restore"- >- <ArrowSquareInIcon weight="bold" data-flx="voice.popout-titlebar.arrow-square-in-icon" />- </button>- </FocusRing>+ {onRestore && (+ <FocusRing offset={-2} data-flx="voice.popout-titlebar.focus-ring.restore">+ <button+ type="button"+ className={NATIVE_TITLEBAR_CLASS.control}+ onClick={onRestore}+ aria-label={i18n._(POP_BACK_IN_DESCRIPTOR)}+ title={i18n._(POP_BACK_IN_DESCRIPTOR)}+ data-flx="voice.popout-titlebar.control-button.restore"+ >+ <ArrowSquareInIcon weight="bold" data-flx="voice.popout-titlebar.arrow-square-in-icon" />+ </button>+ </FocusRing>+ )}{!usesSystemChrome && (<FocusRing offset={-2} data-flx="voice.popout-titlebar.focus-ring.close"><buttondiff --git a/fluxer_app/src/features/voice/components/popout/PopoutWindow.module.css b/fluxer_app/src/features/ui/popout_window/PopoutWindow.module.csssimilarity index 100%rename from fluxer_app/src/features/voice/components/popout/PopoutWindow.module.cssrename to fluxer_app/src/features/ui/popout_window/PopoutWindow.module.cssdiff --git a/fluxer_app/src/features/voice/components/popout/PopoutWindow.tsx b/fluxer_app/src/features/ui/popout_window/PopoutWindow.tsxsimilarity index 85%rename from fluxer_app/src/features/voice/components/popout/PopoutWindow.tsxrename to fluxer_app/src/features/ui/popout_window/PopoutWindow.tsxindex 45f7d7879..4c47a32c8 100644--- a/fluxer_app/src/features/voice/components/popout/PopoutWindow.tsx+++ b/fluxer_app/src/features/ui/popout_window/PopoutWindow.tsx@@ -5,19 +5,21 @@ import {Logger} from '@app/features/platform/utils/AppLogger';import {ContextMenu} from '@app/features/ui/action_menu/ContextMenu';import {SVGMasks} from '@app/features/ui/components/SVGMasks';import {getActivePortalHost, PortalHostContext, setActivePortalHost} from '@app/features/ui/overlay/PortalHostContext';-import {Popouts} from '@app/features/ui/popover/PopoverPopouts';-import {- createWindowFocusInteractionGuard,- UNFOCUSED_FULLY_INTERACTIVE_CLASS,-} from '@app/features/ui/utils/WindowFocusInteractionGuard';-import {PopoutTitlebar} from '@app/features/voice/components/popout/PopoutTitlebar';-import styles from '@app/features/voice/components/popout/PopoutWindow.module.css';+import {PopoutTitlebar} from '@app/features/ui/popout_window/PopoutTitlebar';+import styles from '@app/features/ui/popout_window/PopoutWindow.module.css';import {copyStylesheetsIntoDocument,observeDocumentStylesheets,observeDocumentThemeAttributes,+ type StylesheetMirrorOptions,syncDocumentThemeAttributes,-} from '@app/features/voice/components/popout/PopoutWindowDocument';+} from '@app/features/ui/popout_window/PopoutWindowDocument';+import {Popouts} from '@app/features/ui/popover/PopoverPopouts';+import {Toasts} from '@app/features/ui/toast/Toasts';+import {+ createWindowFocusInteractionGuard,+ UNFOCUSED_FULLY_INTERACTIVE_CLASS,+} from '@app/features/ui/utils/WindowFocusInteractionGuard';import type React from 'react';import {useCallback, useEffect, useRef, useState} from 'react';import {createPortal} from 'react-dom';@@ -33,10 +35,12 @@ interface PopoutWindowProps {height: number;isAlwaysOnTop: boolean;onToggleAlwaysOnTop?: () => void;- onRestore: () => void;+ onRestore?: () => void;onClosed: (windowGeneration: number) => void;+ onCloseRequest?: (close: () => void) => void;onWindowOpened?: (childWindow: Window, windowGeneration: number) => void;existingWindow?: Window | null;+ stylesheetMirror?: StylesheetMirrorOptions;children: React.ReactNode;}@@ -55,15 +59,20 @@ const PopoutWindowOverlayTree: React.FC<PopoutWindowOverlayTreeProps> = ({ownerD<ModalStack ownerDocument={ownerDocument} data-flx="voice.popout-window.modal-stack" /><Popouts ownerDocument={ownerDocument} data-flx="voice.popout-window.popouts" /><ContextMenu ownerDocument={ownerDocument} data-flx="voice.popout-window.context-menu" />+ <Toasts ownerDocument={ownerDocument} data-flx="ui.popout-window.toasts" /></>);-function prepareChildDocument(childWindow: Window, title: string): HTMLElement {+function prepareChildDocument(+ childWindow: Window,+ title: string,+ stylesheetMirror: StylesheetMirrorOptions | undefined,+): HTMLElement {const childDocument = childWindow.document;childDocument.title = title;syncDocumentThemeAttributes(document, childDocument);childDocument.documentElement.classList.add(UNFOCUSED_FULLY_INTERACTIVE_CLASS);- copyStylesheetsIntoDocument(document, childDocument);+ copyStylesheetsIntoDocument(document, childDocument, stylesheetMirror);childDocument.body.style.margin = '0';const container = childDocument.createElement('div');container.className = styles.root;@@ -82,8 +91,10 @@ export const PopoutWindow: React.FC<PopoutWindowProps> = ({onToggleAlwaysOnTop,onRestore,onClosed,+ onCloseRequest,onWindowOpened,existingWindow,+ stylesheetMirror,children,}) => {const [childState, setChildState] = useState<PopoutChildState | null>(null);@@ -92,6 +103,7 @@ export const PopoutWindow: React.FC<PopoutWindowProps> = ({const initialSizeRef = useRef({width, height});const initialTitleRef = useRef(title);const initialWindowRef = useRef(existingWindow);+ const stylesheetMirrorRef = useRef(stylesheetMirror);onClosedRef.current = onClosed;onWindowOpenedRef.current = onWindowOpened;useEffect(() => {@@ -104,9 +116,13 @@ export const PopoutWindow: React.FC<PopoutWindowProps> = ({onClosedRef.current(windowGeneration);return;}- const container = prepareChildDocument(childWindow, initialTitleRef.current);+ const container = prepareChildDocument(childWindow, initialTitleRef.current, stylesheetMirrorRef.current);const disconnectThemeObserver = observeDocumentThemeAttributes(document, childWindow.document);- const disconnectStylesheetObserver = observeDocumentStylesheets(document, childWindow.document);+ const disconnectStylesheetObserver = observeDocumentStylesheets(+ document,+ childWindow.document,+ stylesheetMirrorRef.current,+ );let closed = false;const handlePageHide = (): void => {if (closed) return;@@ -174,8 +190,14 @@ export const PopoutWindow: React.FC<PopoutWindowProps> = ({onClosedRef.current(childState?.windowGeneration ?? windowGeneration);return;}- childState.childWindow.close();- }, [childState, windowGeneration]);+ const {childWindow} = childState;+ const close = (): void => childWindow.close();+ if (onCloseRequest) {+ onCloseRequest(close);+ return;+ }+ close();+ }, [childState, onCloseRequest, windowGeneration]);if (!childState) {return null;}diff --git a/fluxer_app/src/features/voice/components/popout/PopoutWindowDocument.ts b/fluxer_app/src/features/ui/popout_window/PopoutWindowDocument.tssimilarity index 83%rename from fluxer_app/src/features/voice/components/popout/PopoutWindowDocument.tsrename to fluxer_app/src/features/ui/popout_window/PopoutWindowDocument.tsindex cc51d0079..55eccc987 100644--- a/fluxer_app/src/features/voice/components/popout/PopoutWindowDocument.ts+++ b/fluxer_app/src/features/ui/popout_window/PopoutWindowDocument.ts@@ -45,6 +45,10 @@ interface DocumentStylesheetMirror {mirroredStyles: WeakMap<Node, HTMLStyleElement>;}+export interface StylesheetMirrorOptions {+ exclude?: (node: Element) => boolean;+}+const documentMirrors = new WeakMap<Document, DocumentStylesheetMirror>();function getDocumentMirror(targetDocument: Document): DocumentStylesheetMirror {@@ -102,11 +106,31 @@ function copyStyleElement(sourceStyle: HTMLStyleElement, targetDocument: DocumentargetDocument.head.appendChild(styleElement);}-export function copyStylesheetsIntoDocument(sourceDocument: Document, targetDocument: Document): void {+function removeMirroredStylesheet(stylesheetMirror: DocumentStylesheetMirror, node: Node): void {+ const mirroredStyle = stylesheetMirror.mirroredStyles.get(node);+ if (mirroredStyle) {+ mirroredStyle.remove();+ stylesheetMirror.mirroredStyles.delete(node);+ }+ const mirroredLink = stylesheetMirror.mirroredLinks.get(node);+ if (mirroredLink) {+ mirroredLink.remove();+ stylesheetMirror.mirroredLinks.delete(node);+ }+}++export function copyStylesheetsIntoDocument(+ sourceDocument: Document,+ targetDocument: Document,+ {exclude}: StylesheetMirrorOptions = {},+): void {const styleNodes = sourceDocument.querySelectorAll('link[rel="stylesheet"], style');const copyCount = Math.min(styleNodes.length, POPOUT_STYLESHEET_COPY_MAX);for (let index = 0; index < copyCount; index += 1) {const node = styleNodes[index];+ if (exclude?.(node)) {+ continue;+ }if (node instanceof HTMLLinkElement) {copyLinkStylesheet(node, targetDocument);continue;@@ -117,7 +141,11 @@ export function copyStylesheetsIntoDocument(sourceDocument: Document, targetDocu}}-export function observeDocumentStylesheets(sourceDocument: Document, targetDocument: Document): () => void {+export function observeDocumentStylesheets(+ sourceDocument: Document,+ targetDocument: Document,+ {exclude}: StylesheetMirrorOptions = {},+): () => void {if (typeof MutationObserver === 'undefined') {return () => undefined;}@@ -128,13 +156,20 @@ export function observeDocumentStylesheets(sourceDocument: Document, targetDocumconst sourceStyle =mutation.target instanceof HTMLStyleElement ? mutation.target : mutation.target.parentElement;if (sourceStyle instanceof HTMLStyleElement) {- const mirroredStyle = stylesheetMirror.mirroredStyles.get(sourceStyle);- if (mirroredStyle) {- mirroredStyle.textContent = sourceStyle.textContent;+ if (exclude?.(sourceStyle)) {+ removeMirroredStylesheet(stylesheetMirror, sourceStyle);+ } else {+ const mirroredStyle = stylesheetMirror.mirroredStyles.get(sourceStyle);+ if (mirroredStyle) {+ mirroredStyle.textContent = sourceStyle.textContent;+ }}}}for (const node of Array.from(mutation.addedNodes)) {+ if (node instanceof Element && exclude?.(node)) {+ continue;+ }if (node instanceof HTMLLinkElement && node.rel === 'stylesheet') {copyLinkStylesheet(node, targetDocument);continue;@@ -144,16 +179,7 @@ export function observeDocumentStylesheets(sourceDocument: Document, targetDocum}}for (const node of Array.from(mutation.removedNodes)) {- const mirroredStyle = stylesheetMirror.mirroredStyles.get(node);- if (mirroredStyle) {- mirroredStyle.remove();- stylesheetMirror.mirroredStyles.delete(node);- }- const mirroredLink = stylesheetMirror.mirroredLinks.get(node);- if (mirroredLink) {- mirroredLink.remove();- stylesheetMirror.mirroredLinks.delete(node);- }+ removeMirroredStylesheet(stylesheetMirror, node);}}});diff --git a/fluxer_app/src/features/voice/components/popout/VoicePopoutHost.tsx b/fluxer_app/src/features/voice/components/popout/VoicePopoutHost.tsxindex f9751fd71..1ab30dcd2 100644--- a/fluxer_app/src/features/voice/components/popout/VoicePopoutHost.tsx+++ b/fluxer_app/src/features/voice/components/popout/VoicePopoutHost.tsx@@ -2,7 +2,7 @@import Channels from '@app/features/channel/state/Channels';import {createNamedLoadableComponent} from '@app/features/platform/components/loadable/LoadableComponent';-import {PopoutWindow} from '@app/features/voice/components/popout/PopoutWindow';+import {PopoutWindow} from '@app/features/ui/popout_window/PopoutWindow';import styles from '@app/features/voice/components/popout/VoicePopoutHost.module.css';import {VoicePopoutScopeContext} from '@app/features/voice/components/popout/VoicePopoutScopeContext';import {useVoiceEngineConnectionState} from '@app/features/voice/components/useVoiceEngineConnectionState';