| import 'package:jaspr/jaspr.dart'; |
| |
| import '../../../utils/provider.dart'; |
| import '../../shared/components/icon_button.dart'; |
| import '../../shared/components/material_icon.dart'; |
| import '../view_models/preview_view_model.dart'; |
| |
| enum RuntimeButtonType { |
| start, |
| restart, |
| hotReload, |
| stop; |
| |
| String get title => switch (this) { |
| RuntimeButtonType.start => 'Start', |
| RuntimeButtonType.restart => 'Restart', |
| RuntimeButtonType.hotReload => 'Hot Reload', |
| RuntimeButtonType.stop => 'Stop', |
| }; |
| |
| String get icon => switch (this) { |
| RuntimeButtonType.start => MyMaterialIcons.playArrow, |
| RuntimeButtonType.restart => MyMaterialIcons.restartAlt, |
| RuntimeButtonType.hotReload => MyMaterialIcons.bolt, |
| RuntimeButtonType.stop => MyMaterialIcons.stop, |
| }; |
| |
| bool isEnabled(PreviewViewModel previewViewModel) => switch (this) { |
| RuntimeButtonType.start => previewViewModel.canStart, |
| RuntimeButtonType.restart => previewViewModel.canRestart, |
| RuntimeButtonType.hotReload => previewViewModel.canHotReload, |
| RuntimeButtonType.stop => previewViewModel.canStop, |
| }; |
| |
| Future<void> execute(PreviewViewModel previewViewModel) async { |
| switch (this) { |
| case RuntimeButtonType.start: |
| await previewViewModel.runCode('lib/main.dart'); |
| case RuntimeButtonType.restart: |
| await previewViewModel.runCode('lib/main.dart', skipRecompilation: true); |
| case RuntimeButtonType.hotReload: |
| await previewViewModel.hotReloadCode('lib/main.dart'); |
| case RuntimeButtonType.stop: |
| await previewViewModel.stopCode(); |
| } |
| } |
| } |
| |
| class RuntimeButton extends StatelessComponent { |
| final RuntimeButtonType type; |
| |
| const RuntimeButton({required this.type, super.key}); |
| |
| @override |
| Component build(BuildContext context) { |
| final previewViewModel = context.watch<PreviewViewModel>(); |
| final enabled = type.isEnabled(previewViewModel); |
| |
| return IconButton( |
| label: type.title, |
| icon: type.icon, |
| disabled: !enabled, |
| tooltip: type.title, |
| onClick: (_) async { |
| await type.execute(previewViewModel); |
| }, |
| ); |
| } |
| } |