Skip to content

Commit

Permalink
refactor: use window_manager for close handling on Linux
Browse files Browse the repository at this point in the history
A fix was merged that makes it work on Linux.

See: leanflutter/window_manager#343
  • Loading branch information
Merrit committed Jul 5, 2023
1 parent 7003557 commit 4d738e6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 38 deletions.
2 changes: 0 additions & 2 deletions lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ class _AppState extends State<App> with TrayListener, WindowListener {

@override
void onWindowClose() {
/// Only working on Windows for some reason.
/// Linux will use `flutter_window_close` instead.
if (settingsCubit.state.closeToTray) {
AppWindow.instance.hide();
return;
Expand Down
9 changes: 3 additions & 6 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ Future<void> main(List<String> args) async {
exit(0);
} else {}

final appWindow = AppWindow(storage);

final hotkeyService = HotkeyService(activeWindow);

final appWindow = AppWindow(storage);
appWindow.initialize();

final settingsCubit = await SettingsCubit.init(
autostartService: AutostartService(),
hotkeyService: hotkeyService,
Expand Down Expand Up @@ -107,8 +108,4 @@ Future<void> main(List<String> args) async {
child: const App(),
),
);

final bool? startHiddenInTray = await storage.getValue('startHiddenInTray');

if (startHiddenInTray != true) await appWindow.show();
}
23 changes: 7 additions & 16 deletions lib/window/app_window.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import 'dart:io';
import 'dart:ui';

import 'package:flutter_window_close/flutter_window_close.dart';
import 'package:window_manager/window_manager.dart';

import '../core/helpers/json_converters.dart';
import '../logs/logs.dart';
import '../settings/settings.dart';
import '../storage/storage_repository.dart';

/// Represents the main window of the app.
Expand All @@ -17,23 +15,16 @@ class AppWindow {

AppWindow(this._storage) {
instance = this;
_listenForWindowClose();
}

void _listenForWindowClose() {
if (!Platform.isLinux) return;
void initialize() {
windowManager.waitUntilReadyToShow().then((_) async {
final bool? startHiddenInTray =
await _storage.getValue('startHiddenInTray');

/// For now using `flutter_window_close` on Linux, because the
/// `onWindowClose` from `window_manager` is only working on Windows for
/// some reason. Probably best to switch to only using `window_manager` if
/// it starts also working on Linux in the future.
FlutterWindowClose.setWindowShouldCloseHandler(() async {
await hide();
final shouldExitProgram = (settingsCubit.state.closeToTray) //
? false
: true;

return shouldExitProgram;
if (startHiddenInTray != true) {
await show();
}
});
}

Expand Down
16 changes: 4 additions & 12 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,10 @@ packages:
dependency: "direct main"
description:
name: flutter_markdown
sha256: "9e0202b5339cd88ac0f109abae8502681bfab0b13a8e02a0e7158124610b5d98"
sha256: "86b76dbf30496024d6c816bdc13b97de9449dce1f035a73ee7b4ab7f67eab70b"
url: "https://pub.dev"
source: hosted
version: "0.6.15+1"
version: "0.6.16"
flutter_svg:
dependency: transitive
description:
Expand All @@ -315,14 +315,6 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_window_close:
dependency: "direct main"
description:
name: flutter_window_close
sha256: "63f5846efbf0974ec233e6b5f468aa604a4857157f4c6c44fd482621804a0464"
url: "https://pub.dev"
source: hosted
version: "0.2.2"
freezed:
dependency: "direct dev"
description:
Expand Down Expand Up @@ -1152,10 +1144,10 @@ packages:
dependency: "direct main"
description:
name: window_manager
sha256: "95096fede562cbb65f30d38b62d819a458f59ba9fe4a317f6cee669710f6676b"
sha256: "9eef00e393e7f9308309ce9a8b2398c9ee3ca78b50c96e8b4f9873945693ac88"
url: "https://pub.dev"
source: hosted
version: "0.3.4"
version: "0.3.5"
window_size:
dependency: "direct main"
description:
Expand Down
3 changes: 1 addition & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ dependencies:
flutter_localizations:
sdk: flutter
flutter_markdown: ^0.6.9+1
flutter_window_close: ^0.2.2
freezed_annotation: ^2.2.0
helpers:
git:
Expand All @@ -43,7 +42,7 @@ dependencies:
url_launcher: ^6.0.4
win32: ^5.0.5
win32_suspend_process: ^1.1.0
window_manager: ^0.3.0
window_manager: ^0.3.5
window_size:
git:
url: https://github.com/google/flutter-desktop-embedding.git
Expand Down

0 comments on commit 4d738e6

Please sign in to comment.