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

Minor code cleanup to new UI #2825

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions pkgs/sketch_pad/lib/console.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ class _ConsoleWidgetState extends State<ConsoleWidget> {
color: theme.scaffoldBackgroundColor,
border: widget.showDivider
? Border(
top: Divider.createBorderSide(context,
width: 8.0, color: theme.colorScheme.surface))
top: Divider.createBorderSide(
context,
width: 8.0,
color: theme.colorScheme.surface,
))
: null,
),
padding: const EdgeInsets.all(denseSpacing),
Expand Down Expand Up @@ -99,11 +102,13 @@ class _ConsoleWidgetState extends State<ConsoleWidget> {

void _scrollToEnd() {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
scrollController?.animateTo(
scrollController!.position.maxScrollExtent,
duration: animationDelay,
curve: animationCurve,
);
if (scrollController case final scrollController?) {
scrollController.animateTo(
scrollController.position.maxScrollExtent,
duration: animationDelay,
curve: animationCurve,
);
}
});
}
}
2 changes: 1 addition & 1 deletion pkgs/sketch_pad/lib/editor/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ web.Element _codeMirrorFactory(int viewId) {

codeMirrorInstance = CodeMirror(
div,
<String, dynamic>{
<String, Object>{
'lineNumbers': true,
'lineWrapping': true,
'mode': 'dart',
Expand Down
7 changes: 4 additions & 3 deletions pkgs/sketch_pad/lib/execution/execution.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import 'frame.dart';
const String _viewType = 'dartpad-execution';

bool _viewFactoryInitialized = false;
ExecutionService? executionServiceInstance;
ExecutionService? _executionServiceInstance;

final Key _elementViewKey = UniqueKey();

Expand All @@ -37,7 +37,7 @@ web.Element _iFrameFactory(int viewId) {
..style.width = '100%'
..style.height = '100%';

executionServiceInstance = ExecutionServiceImpl(frame);
_executionServiceInstance = ExecutionServiceImpl(frame);

return frame;
}
Expand Down Expand Up @@ -74,7 +74,8 @@ class _ExecutionWidgetState extends State<ExecutionWidget> {
key: _elementViewKey,
viewType: _viewType,
onPlatformViewCreated: (int id) {
widget.appServices.registerExecutionService(executionServiceInstance);
widget.appServices
.registerExecutionService(_executionServiceInstance);
},
),
);
Expand Down
47 changes: 24 additions & 23 deletions pkgs/sketch_pad/lib/execution/frame.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ require(["dartpad_main", "dart_sdk"], function(dartpad_main, dart_sdk) {

/// Destroy and reload the iframe.
Future<void> _reset() {
if (_frame.parentElement != null) {
if (_frame.parentElement case final parentElement?) {
_readyCompleter = Completer();

final clone = _frame.clone(false) as web.HTMLIFrameElement;
clone.src = _frameSrc;

_frame.parentElement!.appendChild(clone);
_frame.parentElement!.removeChild(_frame);
parentElement.appendChild(clone);
parentElement.removeChild(_frame);
_frame = clone;
}

Expand All @@ -162,29 +162,30 @@ require(["dartpad_main", "dart_sdk"], function(dartpad_main, dart_sdk) {

void _initListener() {
web.window.addEventListener(
'message',
(web.Event event) {
if (event is web.MessageEvent) {
final data = event.data.dartify() as Map<Object?, Object?>;
if (data['sender'] != 'frame') {
return;
}
final type = data['type'] as String?;

if (type == 'stderr') {
// Ignore any exceptions before the iframe has completed
// initialization.
if (_readyCompleter.isCompleted) {
_stdoutController.add(data['message'] as String);
}
} else if (type == 'ready' && !_readyCompleter.isCompleted) {
_readyCompleter.complete();
} else if (data['message'] != null) {
'message',
(web.Event event) {
if (event is web.MessageEvent) {
final data = event.data.dartify();
if (data is! Map<Object?, Object?> || data['sender'] != 'frame') {
return;
}
final type = data['type'] as String?;

if (type == 'stderr') {
// Ignore any exceptions before the iframe has completed
// initialization.
if (_readyCompleter.isCompleted) {
_stdoutController.add(data['message'] as String);
}
} else if (type == 'ready' && !_readyCompleter.isCompleted) {
_readyCompleter.complete();
} else if (data['message'] != null) {
_stdoutController.add(data['message'] as String);
}
}.toJS,
false.toJS);
}
}.toJS,
false.toJS,
);
}
}

Expand Down
15 changes: 8 additions & 7 deletions pkgs/sketch_pad/lib/flutter_samples.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
import 'package:http/http.dart' as http;

class FlutterSampleLoader {
final http.Client client = http.Client();
final http.Client _client = http.Client();

Future<String> loadFlutterSample({
required String sampleId,
String? channel,
}) async {
// There are only two hosted versions of the docs: master/main and stable.
final sampleUrl = switch (channel) {
'master' => 'https://main-api.flutter.dev/snippets/$sampleId.dart',
'main' => 'https://main-api.flutter.dev/snippets/$sampleId.dart',
_ => 'https://api.flutter.dev/snippets/$sampleId.dart',
final sampleUrlAuthority = switch (channel) {
'master' || 'main' => 'main-api.flutter.dev',
_ => 'api.flutter.dev',
};

final response = await client.get(Uri.parse(sampleUrl));
final sampleUrl = Uri.https(sampleUrlAuthority, '/snippets/$sampleId.dart');

final response = await _client.get(sampleUrl);

if (response.statusCode != 200) {
throw Exception('Unable to load sample '
Expand All @@ -29,6 +30,6 @@ class FlutterSampleLoader {
}

void dispose() {
client.close();
_client.close();
}
}
27 changes: 12 additions & 15 deletions pkgs/sketch_pad/lib/gists.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ import 'package:collection/collection.dart';
import 'package:http/http.dart' as http;

class GistLoader {
final http.Client client = http.Client();
final http.Client _client = http.Client();

Future<Gist> load(String gistId) async {
final response =
await client.get(Uri.parse('https://api.github.com/gists/$gistId'));
await _client.get(Uri.https('api.github.com', '/gists/$gistId'));

if (response.statusCode != 200) {
throw Exception('Unable to load gist '
'(${response.statusCode} ${response.reasonPhrase}})');
}

return Gist.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
return Gist.fromJson(jsonDecode(response.body) as Map<String, Object?>);
}

void dispose() {
client.close();
_client.close();
}
}

Expand All @@ -40,7 +40,7 @@ class Gist {
required this.files,
});

factory Gist.fromJson(Map<String, dynamic> json) {
factory Gist.fromJson(Map<String, Object?> json) {
/* {
"id": "d3bd83918d21b6d5f778bdc69c3d36d6",
"description": "Fibonacci",
Expand All @@ -65,25 +65,22 @@ class Gist {
}
},
} */
final owner = json['owner'] as Map<String, dynamic>;
final files = json['files'] as Map<String, dynamic>;
final owner = json['owner'] as Map<String, Object?>;
final files = json['files'] as Map<String, Object?>;

return Gist(
id: json['id'] as String,
description: json['description'] as String?,
owner: owner['login'] as String?,
files: files.values
.cast<Map<String, dynamic>>()
.cast<Map<String, Object?>>()
.map(GistFile.fromJson)
.toList(),
.toList(growable: false),
);
}

String? get mainDartSource {
return files
.firstWhereOrNull((file) => file.fileName == 'main.dart')
?.content;
}
String? get mainDartSource =>
files.firstWhereOrNull((file) => file.fileName == 'main.dart')?.content;
}

class GistFile {
Expand All @@ -99,7 +96,7 @@ class GistFile {
required this.content,
});

factory GistFile.fromJson(Map<String, dynamic> json) {
factory GistFile.fromJson(Map<String, Object?> json) {
return GistFile(
fileName: json['filename'] as String,
truncated: json['truncated'] as bool,
Expand Down
24 changes: 12 additions & 12 deletions pkgs/sketch_pad/lib/keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ final ShortcutActivator quickFixKeyActivator = SingleActivator(
control: _nonMac,
);

// map of key activator names

final List<(String, ShortcutActivator)> keyBindings = [
('Code completion', codeCompletionKeyActivator),
('Find', findKeyActivator),
('Find next', findNextKeyActivator),
('Quick fixes', quickFixKeyActivator),
('Reload', reloadKeyActivator),
/// Key binding names and activators.
final List<({String keyName, ShortcutActivator activator})> keyBindings = [
(keyName: 'Code completion', activator: codeCompletionKeyActivator),
(keyName: 'Find', activator: findKeyActivator),
(keyName: 'Find next', activator: findNextKeyActivator),
(keyName: 'Quick fixes', activator: quickFixKeyActivator),
(keyName: 'Reload', activator: reloadKeyActivator),
];

extension SingleActivatorExtension on SingleActivator {
Expand All @@ -60,10 +59,11 @@ extension SingleActivatorExtension on SingleActivator {

return Container(
decoration: BoxDecoration(
border: Border.fromBorderSide(
Divider.createBorderSide(context, width: 1.0, color: subtleColor),
),
borderRadius: const BorderRadius.all(Radius.circular(4))),
border: Border.fromBorderSide(
Divider.createBorderSide(context, width: 1.0, color: subtleColor),
),
borderRadius: const BorderRadius.all(Radius.circular(4)),
),
padding: const EdgeInsets.symmetric(
vertical: 2,
horizontal: 6,
Expand Down
Loading
Loading