-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some simple side bar functionality with a mock editor environment
- Loading branch information
Showing
9 changed files
with
548 additions
and
6 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
packages/devtools_app/lib/src/shared/config_specific/post_message/post_message.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,15 @@ | ||
// 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. | ||
|
||
export 'post_message_stub.dart' if (dart.library.html) 'post_message_web.dart'; | ||
|
||
class PostMessageEvent { | ||
PostMessageEvent({ | ||
required this.origin, | ||
required this.data, | ||
}); | ||
|
||
final String origin; | ||
final Object? data; | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/devtools_app/lib/src/shared/config_specific/post_message/post_message_stub.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,11 @@ | ||
// 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 'post_message.dart'; | ||
|
||
Stream<PostMessageEvent> get onPostMessage => | ||
throw UnsupportedError('unsupported platform'); | ||
|
||
void postMessage(Object? _, String __) => | ||
throw UnsupportedError('unsupported platform'); |
19 changes: 19 additions & 0 deletions
19
packages/devtools_app/lib/src/shared/config_specific/post_message/post_message_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:html' as html; | ||
|
||
import 'post_message.dart'; | ||
|
||
Stream<PostMessageEvent> get onPostMessage { | ||
return html.window.onMessage.map( | ||
(message) => PostMessageEvent( | ||
origin: message.origin, | ||
data: message.data, | ||
), | ||
); | ||
} | ||
|
||
void postMessage(Object? message, String targetOrigin) => | ||
html.window.parent?.postMessage(message, targetOrigin); |
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
132 changes: 132 additions & 0 deletions
132
packages/devtools_app/lib/src/standalone_ui/vs_code/api.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,132 @@ | ||
// 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:async'; | ||
|
||
import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc_2; | ||
import 'package:stream_channel/stream_channel.dart'; | ||
import 'package:web_socket_channel/web_socket_channel.dart'; | ||
|
||
import '../../shared/config_specific/post_message/post_message.dart'; | ||
|
||
/// An API for interacting with Dart tooling. | ||
class DartApi { | ||
DartApi.rpc(this._rpc) : vsCode = VsCodeApi(_rpc) { | ||
unawaited(_rpc.listen()); | ||
} | ||
|
||
/// Connects the API using 'postMessage'. This is only available when running | ||
/// on web and embedded inside VS Code. | ||
factory DartApi.postMessage() { | ||
final postMessageController = StreamController(); | ||
postMessageController.stream.listen((message) => postMessage(message, '*')); | ||
final channel = StreamChannel( | ||
onPostMessage.map((event) => event.data), | ||
postMessageController, | ||
); | ||
return DartApi.rpc(json_rpc_2.Peer.withoutJson(channel)); | ||
} | ||
|
||
/// Connects the API over the provided WebSocket. | ||
factory DartApi.webSocket(WebSocketChannel socket) { | ||
return DartApi.rpc(json_rpc_2.Peer(socket.cast<String>())); | ||
} | ||
|
||
final json_rpc_2.Peer _rpc; | ||
|
||
/// Access to APIs related to VS Code, such as executing VS Code commands or | ||
/// interacting with the Dart/Flutter extensions. | ||
final VsCodeApi vsCode; | ||
|
||
void dispose() { | ||
unawaited(_rpc.close()); | ||
} | ||
} | ||
|
||
/// Base class for the different APIs that may be available. | ||
abstract base class ToolApi { | ||
ToolApi(this.rpc); | ||
|
||
final json_rpc_2.Peer rpc; | ||
|
||
String get apiName; | ||
|
||
/// Checks whether this API is available. | ||
/// | ||
/// Calls to any other API should only be made if and when this [Future] | ||
/// completes with `true`. | ||
late final Future<bool> isAvailable = | ||
_sendRequest<bool>('checkAvailable').catchError((_) => false); | ||
|
||
Future<T> _sendRequest<T>(String method, [Object? parameters]) async { | ||
return (await rpc.sendRequest('$apiName.$method', parameters)) as T; | ||
} | ||
|
||
/// Listens for an event '[apiName].[name]' that has a Map for parameters. | ||
Stream<Map<String, Object?>> events(String name) { | ||
final streamController = StreamController<Map<String, Object?>>.broadcast(); | ||
rpc.registerMethod('$apiName.$name', (json_rpc_2.Parameters parameters) { | ||
streamController.add(parameters.asMap.cast<String, Object?>()); | ||
}); | ||
return streamController.stream; | ||
} | ||
} | ||
|
||
final class VsCodeApi extends ToolApi { | ||
// TODO(dantup): Consider code-generation because Dart-Code and DevTools will | ||
// both have implementations of this API (although in Dart + TypeScript). | ||
VsCodeApi(super.rpc); | ||
|
||
@override | ||
final apiName = 'vsCode'; | ||
|
||
late final Stream<VsCodeDevicesEvent> devicesChanged = | ||
events('devicesChanged').map(VsCodeDevicesEvent.fromJson); | ||
|
||
Future<Object?> executeCommand(String command, [List<Object?>? arguments]) { | ||
return _sendRequest( | ||
'executeCommand', | ||
{'command': command, 'arguments': arguments}, | ||
); | ||
} | ||
|
||
Future<void> selectDevice(String id) { | ||
return _sendRequest( | ||
'selectDevice', | ||
{'id': id}, | ||
); | ||
} | ||
} | ||
|
||
class VsCodeDevice { | ||
VsCodeDevice({required this.id}); | ||
|
||
VsCodeDevice.fromJson(Map<String, Object?> json) | ||
: this(id: json['id'] as String); | ||
|
||
final String id; | ||
|
||
Map<String, Object?> toJson() => {'id': id}; | ||
} | ||
|
||
class VsCodeDevicesEvent { | ||
VsCodeDevicesEvent({required this.selectedDeviceId, required this.devices}); | ||
|
||
VsCodeDevicesEvent.fromJson(Map<String, Object?> json) | ||
: this( | ||
selectedDeviceId: json['selectedDeviceId'] as String?, | ||
devices: (json['devices'] as List) | ||
.cast<Map<String, Object?>>() | ||
.map(VsCodeDevice.fromJson) | ||
.toList(), | ||
); | ||
|
||
final String? selectedDeviceId; | ||
final List<VsCodeDevice> devices; | ||
|
||
Map<String, Object?> toJson() => { | ||
'selectedDeviceId': selectedDeviceId, | ||
'devices': devices.map((device) => device.toJson()).toList(), | ||
}; | ||
} |
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
Oops, something went wrong.