|
72 | 72 | let lastClipboardMonitorLoopError: Error | null = null;
|
73 | 73 |
|
74 | 74 | let componentDestroyed = false;
|
| 75 | + let runWhenFocusedQueue: (() => void)[] = []; |
75 | 76 |
|
76 | 77 | /* Firefox-specific BEGIN */
|
77 | 78 |
|
|
173 | 174 | }
|
174 | 175 | }
|
175 | 176 |
|
| 177 | + function runWhenWindowFocused(fn: () => void) { |
| 178 | + if (document.hasFocus()) { |
| 179 | + fn(); |
| 180 | + } else { |
| 181 | + runWhenFocusedQueue.push(fn); |
| 182 | + } |
| 183 | + } |
| 184 | +
|
176 | 185 | // This callback is required to update client clipboard state when remote side has changed.
|
177 | 186 | function onRemoteClipboardChanged(data: ClipboardData) {
|
178 | 187 | try {
|
179 | 188 | const mime_formats = clipboardDataToRecord(data);
|
180 | 189 | const clipboard_item = new ClipboardItem(mime_formats);
|
181 |
| - lastReceivedClipboardData = clipboardDataToClipboardItemsRecord(data); |
182 |
| - navigator.clipboard.write([clipboard_item]); |
| 190 | + runWhenWindowFocused(() => { |
| 191 | + lastReceivedClipboardData = clipboardDataToClipboardItemsRecord(data); |
| 192 | + navigator.clipboard.write([clipboard_item]); |
| 193 | + }); |
183 | 194 | } catch (err) {
|
184 | 195 | console.error('Failed to set client clipboard: ' + err);
|
185 | 196 | }
|
|
445 | 456 |
|
446 | 457 | window.addEventListener('keydown', captureKeys, false);
|
447 | 458 | window.addEventListener('keyup', captureKeys, false);
|
| 459 | +
|
| 460 | + window.addEventListener('focus', focusEventHandler); |
448 | 461 | }
|
449 | 462 |
|
450 | 463 | function resetHostStyle() {
|
|
717 | 730 | inner.dispatchEvent(new CustomEvent('ready', { detail: result, bubbles: true, composed: true }));
|
718 | 731 | }
|
719 | 732 |
|
| 733 | + function focusEventHandler() { |
| 734 | + while (runWhenFocusedQueue.length > 0) { |
| 735 | + const fn = runWhenFocusedQueue.shift(); |
| 736 | + fn?.(); |
| 737 | + } |
| 738 | + } |
| 739 | +
|
720 | 740 | onMount(async () => {
|
721 | 741 | loggingService.verbose = verbose === 'true';
|
722 | 742 | loggingService.info('Dom ready');
|
|
726 | 746 |
|
727 | 747 | onDestroy(() => {
|
728 | 748 | window.removeEventListener('resize', resizeHandler);
|
| 749 | + window.removeEventListener('focus', focusEventHandler); |
729 | 750 | componentDestroyed = true;
|
730 | 751 | });
|
731 | 752 | </script>
|
|
0 commit comments