-
Notifications
You must be signed in to change notification settings - Fork 321
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
Changes from 3 commits
e68fde7
c565fc6
a34fcef
b59d134
74771df
7344822
84ce7d5
8bd5bf0
a360909
ea26099
c06f41b
7d1f090
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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,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. | ||
|
||
// ignore: avoid_web_libraries_in_flutter, as designed | ||
import 'dart:html'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use package:web here. See #6607 |
||
|
||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can probably use the existing Edit: Actually looks like the postMessage function there maybe isn't used... maybe it was only partly done? 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. interesting, yeah it doesn't look like it exposed 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue created :) |
||
{ | ||
'command': 'clipboard-write', | ||
'data': data, | ||
}, | ||
'*', | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// 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:flutter/services.dart'; | ||
import 'package:logging/logging.dart'; | ||
|
||
import '../../globals.dart'; | ||
import '_copy_to_clipboard_stub.dart' | ||
if (dart.library.js_interop) '_copy_to_clipboard_web.dart' | ||
if (dart.library.io) '_copy_to_clipboard_desktop.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) { | ||
_log.warning( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check if we are embedded and in VS code before doing any of this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can check if we are embedded by using Although, I think we may want to modify the IdeTheme class to include the IDE name so we can look it up from there instead of having to look at our analytics file. |
||
'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. | ||
// See https://github.com/Dart-Code/Dart-Code/issues/4540 for more | ||
// information. | ||
copyToClipboardVSCode(data); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove the stub file and just use the desktop file in the conditional import