Skip to content

Commit

Permalink
New VSCode fallback behaviour for copyToClipboard (#6598)
Browse files Browse the repository at this point in the history
![](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
  • Loading branch information
CoderDake authored and kenzieschmoll committed Nov 16, 2023
1 parent cb92f63 commit ebf8297
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import 'package:provider/provider.dart';
import '../shared/analytics/analytics_controller.dart';
import '../shared/analytics/constants.dart' as gac;
import '../shared/common_widgets.dart';
import '../shared/config_specific/copy_to_clipboard/copy_to_clipboard.dart';
import '../shared/config_specific/server/server.dart';
import '../shared/globals.dart';
import '../shared/log_storage.dart';
import '../shared/utils.dart';

class OpenSettingsAction extends ScaffoldAction {
OpenSettingsAction({super.key, Color? color})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:provider/provider.dart';
import '../../shared/analytics/analytics.dart' as ga;
import '../../shared/analytics/constants.dart' as gac;
import '../../shared/common_widgets.dart';
import '../../shared/config_specific/copy_to_clipboard/copy_to_clipboard.dart';
import '../../shared/globals.dart';
import '../../shared/http/curl_command.dart';
import '../../shared/http/http_request_data.dart';
Expand Down
1 change: 1 addition & 0 deletions packages/devtools_app/lib/src/shared/common_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import '../screens/debugger/debugger_controller.dart';
import '../screens/inspector/layout_explorer/ui/theme.dart';
import 'analytics/analytics.dart' as ga;
import 'analytics/constants.dart' as gac;
import 'config_specific/copy_to_clipboard/copy_to_clipboard.dart';
import 'config_specific/launch_url/launch_url.dart';
import 'console/widgets/expandable_variable.dart';
import 'diagnostics/dart_object_node.dart';
Expand Down
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
}
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,
);
}
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;
}
}
}
2 changes: 1 addition & 1 deletion packages/devtools_app/lib/src/shared/dialogs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import 'package:devtools_app_shared/ui.dart';
import 'package:flutter/material.dart';

import '../shared/config_specific/launch_url/launch_url.dart';
import 'config_specific/copy_to_clipboard/copy_to_clipboard.dart';
import 'globals.dart';
import 'utils.dart';

/// A dialog, that reports unexpected error and allows to copy details and create issue.
class UnexpectedErrorDialog extends StatelessWidget {
Expand Down
2 changes: 1 addition & 1 deletion packages/devtools_app/lib/src/shared/editable_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'common_widgets.dart';
import 'utils.dart';
import 'config_specific/copy_to_clipboard/copy_to_clipboard.dart';

/// A widget that displays the contents of [entries].
///
Expand Down
19 changes: 0 additions & 19 deletions packages/devtools_app/lib/src/shared/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
// other libraries in this package.
// Utils, that do not have dependencies, should go to primitives/utils.dart.

import 'dart:async';

import 'package:devtools_app_shared/service.dart';
import 'package:devtools_app_shared/ui.dart';
import 'package:devtools_app_shared/utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:logging/logging.dart';
import 'package:provider/provider.dart';
Expand All @@ -25,22 +22,6 @@ import 'globals.dart';

final _log = Logger('lib/src/shared/utils');

/// Attempts to copy a String of `data` to the clipboard.
///
/// Shows a `successMessage` [Notification] on the passed in `context`.
Future<void> copyToClipboard(
String data,
String? successMessage,
) async {
await Clipboard.setData(
ClipboardData(
text: data,
),
);

if (successMessage != null) notificationService.push(successMessage);
}

/// Logging to debug console only in debug runs.
void debugLogger(String message) {
assert(
Expand Down
5 changes: 0 additions & 5 deletions packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ To learn more about DevTools, check out the

## General updates

* Enabled DevTools extensions when debugging a Dart entry point that is not
under `lib` (e.g. a unit test or integration test). Thanks to
[@bartekpacia](https://github.com/bartekpacia) for this change! -
[#6644](https://github.com/flutter/devtools/pull/6644)

## Inspector updates

TODO: Remove this section if there are not any general updates.
Expand Down

0 comments on commit ebf8297

Please sign in to comment.