63 lines
1.4 KiB
Dart
63 lines
1.4 KiB
Dart
import 'dart:io';
|
|
import 'dart:async';
|
|
|
|
import 'package:window_manager/window_manager.dart';
|
|
|
|
import '../domain/models.dart';
|
|
import 'tray_service.dart';
|
|
|
|
class WindowLifecycleService with WindowListener {
|
|
WindowLifecycleService({
|
|
required this.trayService,
|
|
required this.readConfig,
|
|
});
|
|
|
|
final TrayService trayService;
|
|
final AppConfig Function() readConfig;
|
|
Future<bool> Function()? onBeforeClose;
|
|
|
|
bool _initialized = false;
|
|
bool _isClosing = false;
|
|
|
|
Future<void> init() async {
|
|
if (_initialized || !(Platform.isLinux || Platform.isWindows)) {
|
|
return;
|
|
}
|
|
await windowManager.ensureInitialized();
|
|
windowManager.addListener(this);
|
|
await windowManager.setPreventClose(true);
|
|
_initialized = true;
|
|
}
|
|
|
|
@override
|
|
void onWindowClose() {
|
|
unawaited(_handleWindowClose());
|
|
}
|
|
|
|
Future<void> _handleWindowClose() async {
|
|
if (_isClosing) {
|
|
return;
|
|
}
|
|
_isClosing = true;
|
|
if (onBeforeClose != null) {
|
|
final shouldContinue = await onBeforeClose!();
|
|
if (!shouldContinue) {
|
|
_isClosing = false;
|
|
return;
|
|
}
|
|
}
|
|
final config = readConfig();
|
|
if (config.minimizeToTray && trayService.isAvailable) {
|
|
await trayService.hideWindow();
|
|
_isClosing = false;
|
|
return;
|
|
}
|
|
await windowManager.setPreventClose(false);
|
|
await windowManager.destroy();
|
|
}
|
|
|
|
Future<void> dispose() async {
|
|
windowManager.removeListener(this);
|
|
}
|
|
}
|