Skip to content

Commit

Permalink
Merge pull request #3830 from canonical/gui-open-shell-start-vm
Browse files Browse the repository at this point in the history
`Open shell` button starts the VM if needed
  • Loading branch information
andrei-toterman authored Dec 20, 2024
2 parents 2d23e10 + 859eef1 commit 4d27d5c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
23 changes: 19 additions & 4 deletions src/client/gui/lib/providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,24 @@ final daemonAvailableProvider = Provider((ref) {
return false;
});

class AllVmInfosNotifier extends Notifier<List<DetailedInfoItem>> {
@override
List<DetailedInfoItem> build() {
return ref.watch(vmInfosStreamProvider).valueOrNull ?? const [];
}

Future<void> update() async {
state = await ref.read(grpcClientProvider).info();
}
}

final allVmInfosProvider =
NotifierProvider<AllVmInfosNotifier, List<DetailedInfoItem>>(
AllVmInfosNotifier.new);

final vmInfosProvider = Provider((ref) {
final vmInfos = ref.watch(vmInfosStreamProvider).valueOrNull ?? const [];
final existingVms = vmInfos
final existingVms = ref
.watch(allVmInfosProvider)
.where((info) => info.instanceStatus.status != Status.DELETED)
.toBuiltList();
final existingVmNames = existingVms.map((i) => i.name).toSet();
Expand Down Expand Up @@ -112,8 +127,8 @@ final vmNamesProvider = Provider((ref) {
});

final deletedVmsProvider = Provider((ref) {
final vmInfos = ref.watch(vmInfosStreamProvider).valueOrNull ?? const [];
return vmInfos
return ref
.watch(allVmInfosProvider)
.where((info) => info.instanceStatus.status == Status.DELETED)
.map((info) => info.name)
.toBuiltSet();
Expand Down
37 changes: 30 additions & 7 deletions src/client/gui/lib/vm_details/terminal.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:convert';
import 'dart:isolate';
import 'dart:math';
Expand All @@ -14,6 +15,7 @@ import '../logger.dart';
import '../notifications.dart';
import '../platform/platform.dart';
import '../providers.dart';
import '../vm_action.dart';

final runningShellsProvider =
StateProvider.autoDispose.family<int, String>((_, __) {
Expand Down Expand Up @@ -182,6 +184,7 @@ class _VmTerminalState extends ConsumerState<VmTerminal> {
final scrollController = ScrollController();
final focusNode = FocusNode();
var fontSize = defaultFontSize;
late final terminalIdentifier = (vmName: widget.name, shellId: widget.id);

@override
void initState() {
Expand All @@ -202,13 +205,35 @@ class _VmTerminalState extends ConsumerState<VmTerminal> {
if (widget.isCurrent) focusNode.requestFocus();
}

Future<void> startVmIfNeeded(final bool vmRunning) async {
if (vmRunning) return;
final name = widget.name;
final action = VmAction.start;
final operation = ref.read(grpcClientProvider).start([name]);
ref.read(notificationsProvider.notifier).addOperation(
operation,
loading: '${action.continuousTense} $name',
onSuccess: (_) => '${action.pastTense} $name',
onError: (error) {
return 'Failed to ${action.name.toLowerCase()} $name: $error';
},
);
await operation;
await ref.read(allVmInfosProvider.notifier).update();
}

void openShell() {
ref.read(terminalProvider(terminalIdentifier).notifier).start();
}

@override
Widget build(BuildContext context) {
final terminalIdentifier = (vmName: widget.name, shellId: widget.id);
final terminal = ref.watch(terminalProvider(terminalIdentifier));
final vmRunning = ref.watch(vmInfoProvider(widget.name).select((info) {
return info.instanceStatus.status == Status.RUNNING;
final vmStatus = ref.watch(vmInfoProvider(widget.name).select((info) {
return info.instanceStatus.status;
}));
final vmRunning = vmStatus == Status.RUNNING;
final canStartVm = [Status.STOPPED, Status.SUSPENDED].contains(vmStatus);

final buttonStyle = ButtonStyle(
foregroundColor: WidgetStateColor.resolveWith((states) {
Expand All @@ -234,10 +259,8 @@ class _VmTerminalState extends ConsumerState<VmTerminal> {
const SizedBox(height: 12),
OutlinedButton(
style: buttonStyle,
onPressed: vmRunning
? () => ref
.read(terminalProvider(terminalIdentifier).notifier)
.start()
onPressed: canStartVm || vmRunning
? () => startVmIfNeeded(vmRunning).then((_) => openShell())
: null,
child: const Text('Open shell'),
),
Expand Down

0 comments on commit 4d27d5c

Please sign in to comment.