Skip to content

Commit

Permalink
Move copyToClipboard and isEmbedded helpers to `devtools_app_shar…
Browse files Browse the repository at this point in the history
…ed` (#8118)
  • Loading branch information
kenzieschmoll authored Jul 30, 2024
1 parent 205176e commit 1862692
Show file tree
Hide file tree
Showing 19 changed files with 42 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:flutter/material.dart';
import '../../shared/analytics/analytics.dart' as ga;
import '../../shared/analytics/constants.dart' as gac;
import '../../shared/globals.dart';
import '../../shared/utils.dart';
import '_view_desktop.dart' if (dart.library.js_interop) '_view_web.dart';
import 'controller.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import '../shared/analytics/constants.dart' as gac;
import '../shared/common_widgets.dart';
import '../shared/globals.dart';
import '../shared/screen.dart';
import '../shared/utils.dart';
import 'embedded/controller.dart';
import 'embedded/view.dart';
import 'extension_screen_controls.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import '../shared/connection_info.dart';
import '../shared/globals.dart';
import '../shared/query_parameters.dart';
import '../shared/routing.dart';
import '../shared/utils.dart';

class DisconnectObserver extends StatefulWidget {
const DisconnectObserver({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:async';
import 'dart:convert';

import 'package:devtools_app_shared/ui.dart';
import 'package:devtools_app_shared/utils.dart';
import 'package:devtools_shared/devtools_shared.dart';
import 'package:flutter/material.dart';
Expand Down
1 change: 0 additions & 1 deletion packages/devtools_app/lib/src/framework/scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import '../shared/query_parameters.dart';
import '../shared/routing.dart';
import '../shared/screen.dart';
import '../shared/title.dart';
import '../shared/utils.dart';
import 'about_dialog.dart';
import 'app_bar.dart';
import 'report_feedback_button.dart';
Expand Down
6 changes: 3 additions & 3 deletions packages/devtools_app/lib/src/framework/settings_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
import 'dart:async';

import 'package:devtools_app_shared/ui.dart';
import 'package:devtools_app_shared/utils.dart';
import 'package:flutter/material.dart';
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/globals.dart';
import '../shared/log_storage.dart';
import '../shared/server/server.dart';
import '../shared/utils.dart';

class OpenSettingsAction extends ScaffoldAction {
OpenSettingsAction({super.key, super.color})
Expand Down Expand Up @@ -113,7 +112,8 @@ class _VerboseLoggingSetting extends StatelessWidget {
_minScreenWidthForTextBeforeScaling,
onPressed: () async => await copyToClipboard(
LogStorage.root.toString(),
'Successfully copied logs',
onSuccess: () =>
notificationService.push('Successfully copied logs'),
),
),
const SizedBox(width: denseSpacing),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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 Expand Up @@ -437,7 +436,8 @@ class ActionsColumn extends ColumnData<NetworkRequest>
unawaited(
copyToClipboard(
data.uri,
'Copied the URL to the clipboard',
onSuccess: () =>
notificationService.push('Copied the URL to the clipboard'),
),
);
},
Expand All @@ -448,7 +448,8 @@ class ActionsColumn extends ColumnData<NetworkRequest>
unawaited(
copyToClipboard(
CurlCommand.from(data).toString(),
'Copied the cURL command to the clipboard',
onSuccess: () => notificationService
.push('Copied the cURL command to the clipboard'),
),
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ library;
import 'dart:async';
import 'dart:js_interop';

import 'package:devtools_app_shared/ui.dart';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:unified_analytics/unified_analytics.dart' as ua;
Expand Down
11 changes: 8 additions & 3 deletions packages/devtools_app/lib/src/shared/common_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import 'package:vm_service/vm_service.dart';
import '../screens/debugger/debugger_controller.dart';
import 'analytics/analytics.dart' as ga;
import 'analytics/constants.dart' as gac;
import 'config_specific/copy_to_clipboard/copy_to_clipboard.dart';
import 'console/widgets/expandable_variable.dart';
import 'diagnostics/dart_object_node.dart';
import 'diagnostics/tree_builder.dart';
Expand Down Expand Up @@ -1178,7 +1177,8 @@ class _JsonViewerState extends State<JsonViewer>
.serviceManager.service!.fakeServiceCache
.instanceToJson(copiedVariable.value as Instance),
),
'JSON copied to clipboard',
onSuccess: () =>
notificationService.push('JSON copied to clipboard'),
),
);
},
Expand Down Expand Up @@ -1414,7 +1414,12 @@ class CopyToClipboardControl extends StatelessWidget {
ga.select(gaScreen!, gaItem!);
}
unawaited(
copyToClipboard(dataProvider!() ?? '', successMessage),
copyToClipboard(
dataProvider!() ?? '',
onSuccess: successMessage != null
? () => notificationService.push(successMessage!)
: null,
),
);
};
final size = this.size ?? defaultIconSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'dart:developer';

import 'package:devtools_app_shared/service.dart';
import 'package:devtools_app_shared/service_extensions.dart';
import 'package:devtools_app_shared/ui.dart';
import 'package:devtools_app_shared/utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
Expand Down
5 changes: 3 additions & 2 deletions packages/devtools_app/lib/src/shared/dialogs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import 'dart:async';

import 'package:devtools_app_shared/ui.dart';
import 'package:devtools_app_shared/utils.dart';
import 'package:flutter/material.dart';

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

Expand Down Expand Up @@ -36,7 +36,8 @@ class UnexpectedErrorDialog extends StatelessWidget {
onPressed: () => unawaited(
copyToClipboard(
additionalInfo,
'Error details copied to clipboard',
onSuccess: () =>
notificationService.push('Error details copied to clipboard'),
),
),
),
Expand Down
9 changes: 7 additions & 2 deletions 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 'config_specific/copy_to_clipboard/copy_to_clipboard.dart';
import 'globals.dart';

/// A widget that displays the contents of [entries].
///
Expand Down Expand Up @@ -276,7 +276,12 @@ class EditableListCopyDirectoryButton extends StatelessWidget {
icon: Icons.copy_outlined,
outlined: false,
onPressed: () {
unawaited(copyToClipboard(value, 'Copied to clipboard.'));
unawaited(
copyToClipboard(
value,
onSuccess: () => notificationService.push('Copied to clipboard.'),
),
);
},
);
}
Expand Down
4 changes: 0 additions & 4 deletions packages/devtools_app/lib/src/shared/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ void debugLogger(String message) {
);
}

/// Whether DevTools is in embedded mode, as determined by the [ideTheme] parsed
/// from query parameters.
bool isEmbedded() => ideTheme.embedded;

/// Whether DevTools is using a dark theme.
///
/// When DevTools is in embedded mode, we first check if the [ideTheme] has
Expand Down
1 change: 1 addition & 0 deletions packages/devtools_app_shared/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ can be disabled to prevent exceptions from being logged to console.
* Add `caseInsensitiveFuzzyMatch` extension method on `String`.
* Add common widgets `DevToolsClearableTextField`, `InputDecorationSuffixButton`,
and `RoundedDropDownButton`.
* Add `copyToClipboard` and `isEmbedded` utility methods.
* Deprecate `ServiceManager.hasConnection` in favor of
`ServiceManager.connectedState.value.connected`.
* Correct the dartdoc for the `ListValueNotifier` class.
Expand Down
4 changes: 4 additions & 0 deletions packages/devtools_app_shared/lib/src/ui/ui_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import '../utils/globals.dart';
import 'theme/ide_theme.dart';

/// Whether DevTools is in embedded mode, as determined by the [ideTheme] parsed
/// from query parameters.
bool isEmbedded() => ideTheme.embedded;

IdeTheme get ideTheme {
final theme = globals[IdeTheme];
if (theme == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import 'package:flutter/services.dart';
import 'package:logging/logging.dart';

import '../../globals.dart';
import '../../utils.dart';
import '../../ui/ui_utils.dart';
import '_copy_to_clipboard_desktop.dart'
if (dart.library.js_interop) '_copy_to_clipboard_web.dart';

Expand All @@ -19,24 +18,23 @@ final _log = Logger('copy_to_clipboard');
/// 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 {
String data, {
void Function()? onSuccess,
}) async {
try {
await Clipboard.setData(
ClipboardData(
text: data,
),
);

if (successMessage != null) notificationService.push(successMessage);
onSuccess?.call();
} catch (e) {
if (isEmbedded()) {
_log.warning(
'DevTools copy failed. This may be as a result of a known bug in VSCode. '
'Copy failed. This may be as a result of a known bug in VS Code. '
'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.',
'information. Now attempting to use a fallback method of copying the '
'that is a workaround for VS Code only.',
);
// 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
Expand Down
1 change: 1 addition & 0 deletions packages/devtools_app_shared/lib/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

export 'src/utils/auto_dispose.dart';
export 'src/utils/copy_to_clipboard/copy_to_clipboard.dart';
export 'src/utils/globals.dart';
export 'src/utils/list.dart';
export 'src/utils/url/url.dart';
Expand Down

0 comments on commit 1862692

Please sign in to comment.