-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New VSCode fallback behaviour for copyToClipboard (#6598)
![](https://media.giphy.com/media/4URfklUToxk6A/giphy.gif) Adds a fallback behaviour for copying which will allow copy buttons to actually work in VSCode. NOTE: success toasts will no longer show in VSCode though. I've also added some logging just in case a user is looking at their console when a copy fails. Hopefully this provides enough information that the unhide-able error doesn't look suspicious. ![Screenshot 2023-10-26 at 10 53 39 AM](https://github.com/flutter/devtools/assets/1386322/b4ebd139-f3f5-4dd6-8c62-794f7d6df80d) Related to Dart-Code/Dart-Code#4814 Fixes #5775 Mirror package:web version from Flutter SDK
- Loading branch information
1 parent
b23f579
commit 7d0a194
Showing
10 changed files
with
84 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...ools_app/lib/src/shared/config_specific/copy_to_clipboard/_copy_to_clipboard_desktop.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Copyright 2023 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be found | ||
// in the LICENSE file. | ||
|
||
void copyToClipboardVSCode(String _) { | ||
// Do nothing | ||
} |
19 changes: 19 additions & 0 deletions
19
...devtools_app/lib/src/shared/config_specific/copy_to_clipboard/_copy_to_clipboard_web.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2023 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be found | ||
// in the LICENSE file. | ||
|
||
import 'dart:js_interop'; | ||
import 'package:web/web.dart'; | ||
|
||
void copyToClipboardVSCode(String data) { | ||
// This post message is only relevant in VSCode. If the parent frame is | ||
// listening for this command, then it will attempt to copy the contents | ||
// to the clipboard in the context of the parent frame. | ||
window.parent?.postMessage( | ||
{ | ||
'command': 'clipboard-write', | ||
'data': data, | ||
}.jsify(), | ||
'*'.toJS, | ||
); | ||
} |
52 changes: 52 additions & 0 deletions
52
...ages/devtools_app/lib/src/shared/config_specific/copy_to_clipboard/copy_to_clipboard.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2023 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be found | ||
// in the LICENSE file. | ||
|
||
import 'package:devtools_app_shared/ui.dart'; | ||
import 'package:flutter/services.dart'; | ||
import 'package:logging/logging.dart'; | ||
|
||
import '../../globals.dart'; | ||
import '_copy_to_clipboard_desktop.dart' | ||
if (dart.library.js_interop) '_copy_to_clipboard_web.dart'; | ||
|
||
final _log = Logger('copy_to_clipboard'); | ||
|
||
/// Attempts to copy a String of `data` to the clipboard. | ||
/// | ||
/// Shows a `successMessage` [Notification] on the passed in `context`, if the | ||
/// copy is successfully done using the [Clipboard.setData] api. Otherwise it | ||
/// attempts to post the [data] to the parent frame where the parent frame will | ||
/// try to complete the copy (this fallback will only work in VSCode). | ||
Future<void> copyToClipboard( | ||
String data, | ||
String? successMessage, | ||
) async { | ||
try { | ||
await Clipboard.setData( | ||
ClipboardData( | ||
text: data, | ||
), | ||
); | ||
|
||
if (successMessage != null) notificationService.push(successMessage); | ||
} catch (e) { | ||
if (ideTheme.embed) { | ||
_log.warning( | ||
'DevTools copy failed. This may be as a result of a known bug in VSCode. ' | ||
'See https://github.com/Dart-Code/Dart-Code/issues/4540 for more ' | ||
'information. DevTools will now attempt to use a fallback method of ' | ||
'copying the contents.', | ||
); | ||
// Trying to use Clipboard.setData to copy in vscode will not work as a | ||
// result of a bug. So we should fallback to `copyToClipboardVSCode` which | ||
// delegates the copy to the frame above DevTools. | ||
// Sending this message in other environments will just have no effect. | ||
// See https://github.com/Dart-Code/Dart-Code/issues/4540 for more | ||
// information. | ||
copyToClipboardVSCode(data); | ||
} else { | ||
rethrow; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters