Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New VSCode fallback behaviour for copyToClipboard #6598

Merged
merged 12 commits into from Oct 31, 2023
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
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
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
@@ -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
}
@@ -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(
Copy link
Contributor

@DanTup DanTup Oct 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably use the existing postMessage in packages\devtools_app\lib\src\shared\config_specific\post_message\post_message_web.dart to avoid needing multiple stub files for this API? (eg. have a single copy function that just calls the shared postMessage)?

Edit: Actually looks like the postMessage function there maybe isn't used... maybe it was only partly done? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting, yeah it doesn't look like it exposed 🤔
Would you be alright with me just leaving the multi stub files for the copy behaviour?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to me (if that's what's tested and works). It seems to be what the "launch URL" functionality (that I just re-discovered) does. Maybe it's worth adding a TODO that we could possibly refactor these to reduce the number of stubs though (both copy + launch url)?

(I suspect I added some of that dead postMessage code, so I'm happy to look, and either consolidate or delete the unused bits)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue created :)
#6613

{
'command': 'clipboard-write',
'data': data,
}.jsify(),
'*'.toJS,
);
}
@@ -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
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
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
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
Expand Up @@ -20,6 +20,7 @@ video provides a brief tutorial for each DevTools screen.
* Fix a bug with service extension states not being cleared on app disconnect.
[#6547](https://github.com/flutter/devtools/pull/6547)
* Improved styling of bottom status bar when connected to an app. [#6525](https://github.com/flutter/devtools/pull/6525)
* Added a work around to fix copy button functionality in VSCode. [#6598](https://github.com/flutter/devtools/pull/6598)

## Inspector updates

Expand Down