From 68cbea833a4fdec29522a990cd339706a873cce2 Mon Sep 17 00:00:00 2001 From: Dain Nilsson Date: Fri, 24 Feb 2023 14:23:17 +0100 Subject: [PATCH] Fix copy to clipboard for Wayland. --- lib/desktop/state.dart | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/desktop/state.dart b/lib/desktop/state.dart index 1a7963e4..4b332cc5 100755 --- a/lib/desktop/state.dart +++ b/lib/desktop/state.dart @@ -159,7 +159,29 @@ class _DesktopClipboard extends AppClipboard { @override Future setText(String toClipboard, {bool isSensitive = false}) async { - await Clipboard.setData(ClipboardData(text: toClipboard)); + // Wayland requires the window to be focused to copy to clipboard + final needsFocus = Platform.isLinux && + Platform.environment['XDG_SESSION_TYPE'] == 'wayland'; + var hidden = false; + try { + if (needsFocus && !await windowManager.isFocused()) { + if (!await windowManager.isVisible()) { + hidden = true; + await windowManager.setOpacity(0.0); + await windowManager.show(); + } + await windowManager.focus(); + // Window focus isn't immediate, wait until focused with 10s timeout + await Future.doWhile(() async => !await windowManager.isFocused()) + .timeout(const Duration(seconds: 10)); + } + await Clipboard.setData(ClipboardData(text: toClipboard)); + } finally { + if (hidden) { + await windowManager.hide(); + await windowManager.setOpacity(1.0); + } + } } }