Skip to content

Commit

Permalink
Add hide-on-close-support
Browse files Browse the repository at this point in the history
  • Loading branch information
gnunicorn committed Aug 11, 2024
1 parent 63acf0c commit 2311a09
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 6 deletions.
54 changes: 54 additions & 0 deletions app/lib/config/desktop.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:window_manager/window_manager.dart';

Future<void> initDesktop() async {
// Must add this line.
await windowManager.ensureInitialized();

WindowOptions windowOptions = WindowOptions(
title: 'Acter',
titleBarStyle: TitleBarStyle.normal,
);
windowManager.waitUntilReadyToShow(windowOptions, () async {
await windowManager.show();
await windowManager.focus();
await windowManager.setPreventClose(true);
});
}

class DesktopSupport extends StatefulWidget {
final Widget child;

const DesktopSupport({super.key, required this.child});

@override
State<DesktopSupport> createState() => _DesktopSupportState();
}

class _DesktopSupportState extends State<DesktopSupport> with WindowListener {
@override
void initState() {
super.initState();
windowManager.addListener(this);
}

@override
void dispose() {
windowManager.removeListener(this);
super.dispose();
}

@override
Widget build(BuildContext context) {
return widget.child;
}

@override
void onWindowClose() async {
bool _isPreventClose = await windowManager.isPreventClose();
if (_isPreventClose) {
windowManager.hide();
}
}
}
7 changes: 7 additions & 0 deletions app/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:async';

import 'package:acter/common/themes/app_theme.dart';
import 'package:acter/config/desktop.dart';
import 'package:acter/config/notifications/init.dart';
import 'package:acter/common/providers/app_state_provider.dart';
import 'package:acter/common/themes/acter_theme.dart';
Expand Down Expand Up @@ -55,6 +57,11 @@ Future<void> _startAppInner(Widget app, bool withSentry) async {
await initLogging();
final initialLocationFromNotification = await initializeNotifications();

if (isDesktop) {
app = DesktopSupport(child: app);
await initDesktop();
}

if (initialLocationFromNotification != null) {
WidgetsBinding.instance.addPostFrameCallback((Duration duration) {
// push after the next render to ensure we still have the "initial" location
Expand Down
12 changes: 12 additions & 0 deletions app/macos/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,16 @@ class AppDelegate: FlutterAppDelegate {
override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
override func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
if !flag {
for window in NSApp.windows {
if !window.isVisible {
window.setIsVisible(true)
}
window.makeKeyAndOrderFront(self)
NSApp.activate(ignoringOtherApps: true)
}
}
return true
}
}
52 changes: 46 additions & 6 deletions app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -797,26 +797,26 @@ packages:
dependency: transitive
description:
name: flutter_local_notifications
sha256: "40e6fbd2da7dcc7ed78432c5cdab1559674b4af035fddbfb2f9a8f9c2112fcef"
sha256: c500d5d9e7e553f06b61877ca6b9c8b92c570a4c8db371038702e8ce57f8a50f
url: "https://pub.dev"
source: hosted
version: "17.1.2"
version: "17.2.2"
flutter_local_notifications_linux:
dependency: transitive
description:
name: flutter_local_notifications_linux
sha256: "33f741ef47b5f63cc7f78fe75eeeac7e19f171ff3c3df054d84c1e38bedb6a03"
sha256: c49bd06165cad9beeb79090b18cd1eb0296f4bf4b23b84426e37dd7c027fc3af
url: "https://pub.dev"
source: hosted
version: "4.0.0+1"
version: "4.0.1"
flutter_local_notifications_platform_interface:
dependency: transitive
description:
name: flutter_local_notifications_platform_interface
sha256: "340abf67df238f7f0ef58f4a26d2a83e1ab74c77ab03cd2b2d5018ac64db30b7"
sha256: "85f8d07fe708c1bdcf45037f2c0109753b26ae077e9d9e899d55971711a4ea66"
url: "https://pub.dev"
source: hosted
version: "7.1.0"
version: "7.2.0"
flutter_localizations:
dependency: "direct main"
description: flutter
Expand Down Expand Up @@ -1428,6 +1428,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.2.4"
menu_base:
dependency: transitive
description:
name: menu_base
sha256: "820368014a171bd1241030278e6c2617354f492f5c703d7b7d4570a6b8b84405"
url: "https://pub.dev"
source: hosted
version: "0.1.1"
meta:
dependency: transitive
description:
Expand Down Expand Up @@ -1894,6 +1902,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.1.3"
screen_retriever:
dependency: transitive
description:
name: screen_retriever
sha256: "6ee02c8a1158e6dae7ca430da79436e3b1c9563c8cf02f524af997c201ac2b90"
url: "https://pub.dev"
source: hosted
version: "0.1.9"
screenshot:
dependency: "direct main"
description:
Expand Down Expand Up @@ -2069,6 +2085,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.0"
shortid:
dependency: transitive
description:
name: shortid
sha256: d0b40e3dbb50497dad107e19c54ca7de0d1a274eb9b4404991e443dadb9ebedb
url: "https://pub.dev"
source: hosted
version: "0.1.2"
skeletonizer:
dependency: "direct main"
description:
Expand Down Expand Up @@ -2258,6 +2282,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.0.5"
tray_manager:
dependency: "direct main"
description:
name: tray_manager
sha256: c9a63fd88bd3546287a7eb8ccc978d707eef82c775397af17dda3a4f4c039e64
url: "https://pub.dev"
source: hosted
version: "0.2.3"
tuple:
dependency: transitive
description:
Expand Down Expand Up @@ -2554,6 +2586,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.3"
window_manager:
dependency: "direct main"
description:
name: window_manager
sha256: "8699323b30da4cdbe2aa2e7c9de567a6abd8a97d9a5c850a3c86dcd0b34bbfbf"
url: "https://pub.dev"
source: hosted
version: "0.3.9"
windows_notification:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ dependencies:
scrolls_to_top: ^2.1.1
scrollable_positioned_list: ^0.3.8
diffutil_dart: ^4.0.1
tray_manager: ^0.2.3
window_manager: ^0.3.9

shake_detector:
path: ../packages/shake_detector
Expand Down
7 changes: 7 additions & 0 deletions app/windows/runner/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
_In_ wchar_t *command_line, _In_ int show_command) {

HWND hwnd = ::FindWindow(L"FLUTTER_RUNNER_WIN32_WINDOW", L"Acter");
if (hwnd != NULL) {
::ShowWindow(hwnd, SW_NORMAL);
::SetForegroundWindow(hwnd);
return EXIT_FAILURE;
}
// Attach to console when present (e.g., 'flutter run') or create a
// new console when running with a debugger.
if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
Expand Down

0 comments on commit 2311a09

Please sign in to comment.