Skip to content

Commit

Permalink
[gui] add basiscs for autostart and implement for linux
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-toterman committed Jul 3, 2024
1 parent 0a8234f commit 3fa513a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
1 change: 1 addition & 0 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ apps:
- network
gui:
desktop: usr/share/applications/multipass.gui.desktop
autostart: multipass.gui.autostart.desktop
extensions: [ gnome ]
environment: &_client-environment
XDG_DATA_HOME: $SNAP_USER_DATA
Expand Down
6 changes: 6 additions & 0 deletions src/client/gui/assets/multipass.gui.autostart.desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[Desktop Entry]
Name=Multipass
Exec=multipass.gui
Type=Application
Terminal=false
Categories=Utility;
67 changes: 67 additions & 0 deletions src/client/gui/lib/settings/autostart_notifiers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import 'dart:async';
import 'dart:io';

import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

final autostartProvider =
AsyncNotifierProvider.autoDispose<AutostartNotifier, bool>(() {
if (Platform.isLinux) return LinuxAutostartNotifier();
if (Platform.isWindows) return WindowsAutostartNotifier();
if (Platform.isMacOS) return MacOSAutostartNotifier();
throw UnimplementedError('no autostart notifier for this platform');
});

abstract class AutostartNotifier extends AutoDisposeAsyncNotifier<bool> {
Future<void> set(bool value);
}

class LinuxAutostartNotifier extends AutostartNotifier {
static const autostartFile = 'multipass.gui.autostart.desktop';
final file = File(
'${Platform.environment['HOME']}/.config/autostart/$autostartFile',
);

@override
Future<bool> build() => file.exists();

@override
Future<void> set(bool value) async {
if (value) {
final data = await rootBundle.load('assets/$autostartFile');
await file.writeAsBytes(data.buffer.asUint8List());
} else {
if (await file.exists()) await file.delete();
}

ref.invalidateSelf();
}
}

class WindowsAutostartNotifier extends AutostartNotifier {
@override
FutureOr<bool> build() {
// TODO: implement build
throw UnimplementedError();
}

@override
Future<void> set(bool value) {
// TODO: implement set
throw UnimplementedError();
}
}

class MacOSAutostartNotifier extends AutostartNotifier {
@override
FutureOr<bool> build() {
// TODO: implement build
throw UnimplementedError();
}

@override
Future<void> set(bool value) {
// TODO: implement set
throw UnimplementedError();
}
}
6 changes: 4 additions & 2 deletions src/client/gui/lib/settings/general_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:url_launcher/url_launcher.dart';
import '../dropdown.dart';
import '../providers.dart';
import '../switch.dart';
import 'autostart_notifiers.dart';

final updateProvider = Provider.autoDispose((ref) {
ref
Expand All @@ -25,6 +26,7 @@ class GeneralSettings extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final update = ref.watch(updateProvider);
final autostart = ref.watch(autostartProvider).valueOrNull ?? false;
final onAppClose = ref.watch(onAppCloseProvider);

return Column(
Expand All @@ -38,10 +40,10 @@ class GeneralSettings extends ConsumerWidget {
if (update.version.isNotBlank) UpdateAvailable(update),
Switch(
label: 'Open the Multipass GUI on startup',
value: false,
value: autostart,
trailingSwitch: true,
size: 30,
onChanged: (value) {},
onChanged: (value) => ref.read(autostartProvider.notifier).set(value),
),
const SizedBox(height: 20),
Dropdown(
Expand Down

0 comments on commit 3fa513a

Please sign in to comment.