Skip to content

Commit

Permalink
Reset source if same sample chosen
Browse files Browse the repository at this point in the history
  • Loading branch information
parlough committed Sep 17, 2024
1 parent b796f32 commit 1b46ac6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
14 changes: 12 additions & 2 deletions pkgs/dartpad_ui/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1013,8 +1013,18 @@ class ListSamplesWidget extends StatelessWidget {
for (final sample in samples)
MenuItemButton(
leadingIcon: Logo(type: sample.icon),
onPressed: () =>
GoRouter.of(context).replaceQueryParam('sample', sample.id),
onPressed: () {
// If the current route is already the current sample,
// reset to it manually.
if (GoRouterState.of(context).uri.queryParameters['sample'] ==
sample.id) {
final appServices =
Provider.of<AppServices>(context, listen: false);
appServices.resetToSample(sample);
} else {
GoRouter.of(context).replaceQueryParam('sample', sample.id);
}
},
child: Padding(
padding: const EdgeInsets.only(right: 32),
child: Text(sample.name),
Expand Down
15 changes: 11 additions & 4 deletions pkgs/dartpad_ui/lib/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,19 @@ class AppServices {
return versionResponse;
}

void resetTo({String? type}) {
type ??= 'dart';
void resetToSample(Sample sample) {
_reset(source: sample.source, title: sample.name);
}

void resetTo({String type = 'dart'}) {
final source = Samples.getDefault(type: type);

_reset(source: source);

appModel.editorStatus.showToast('Created new ${titleCase(type)} snippet');
}

void _reset({String title = '', required String source}) {
// Reset the source.
appModel.sourceCodeController.text = source;

Expand All @@ -181,8 +190,6 @@ class AppServices {

// Reset the execution area.
executionService?.reset();

appModel.editorStatus.showToast('Created new ${titleCase(type)} snippet');
}

void _handleCodeChanged() {
Expand Down

0 comments on commit 1b46ac6

Please sign in to comment.