Fix copy to clipboard for Wayland.

This commit is contained in:
Dain Nilsson 2023-02-24 14:23:17 +01:00
parent 15e310ca38
commit 68cbea833a
No known key found for this signature in database
GPG Key ID: F04367096FBA95E8

View File

@ -159,7 +159,29 @@ class _DesktopClipboard extends AppClipboard {
@override
Future<void> 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);
}
}
}
}