Skip to content

Commit 4a789f3

Browse files
committed
[windows] Implement destroy method
1 parent d8363bd commit 4a789f3

File tree

12 files changed

+73
-11
lines changed

12 files changed

+73
-11
lines changed
66.1 KB
Binary file not shown.
1.41 KB
Loading

example/lib/pages/home.dart

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:bot_toast/bot_toast.dart';
44
import 'package:flutter/cupertino.dart';
55
import 'package:flutter/material.dart';
66
import 'package:preference_list/preference_list.dart';
7+
import 'package:tray_manager/tray_manager.dart';
78
import 'package:window_manager/window_manager.dart';
89

910
import '../utilities/utilities.dart';
@@ -29,7 +30,7 @@ class HomePage extends StatefulWidget {
2930
_HomePageState createState() => _HomePageState();
3031
}
3132

32-
class _HomePageState extends State<HomePage> with WindowListener {
33+
class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
3334
bool _isPreventClose = false;
3435
Size _size = _kSizes.first;
3536
Size? _minSize;
@@ -47,18 +48,37 @@ class _HomePageState extends State<HomePage> with WindowListener {
4748

4849
@override
4950
void initState() {
51+
trayManager.addListener(this);
5052
windowManager.addListener(this);
5153
_init();
5254
super.initState();
5355
}
5456

5557
@override
5658
void dispose() {
59+
trayManager.removeListener(this);
5760
windowManager.removeListener(this);
5861
super.dispose();
5962
}
6063

6164
void _init() async {
65+
await trayManager.setIcon(
66+
Platform.isWindows
67+
? 'images/tray_icon_original.ico'
68+
: 'images/tray_icon_original.png',
69+
);
70+
List<MenuItem> items = [
71+
MenuItem(
72+
key: 'show_window',
73+
title: 'Show Window',
74+
),
75+
MenuItem.separator,
76+
MenuItem(
77+
key: 'exit_app',
78+
title: 'Exit App',
79+
),
80+
];
81+
await trayManager.setContextMenu(items);
6282
setState(() {});
6383
}
6484

@@ -747,6 +767,11 @@ class _HomePageState extends State<HomePage> with WindowListener {
747767
);
748768
}
749769

770+
@override
771+
void onTrayIconMouseDown() {
772+
windowManager.show();
773+
}
774+
750775
@override
751776
void onWindowFocus() {
752777
setState(() {});
@@ -771,7 +796,7 @@ class _HomePageState extends State<HomePage> with WindowListener {
771796
child: Text('Yes'),
772797
onPressed: () {
773798
Navigator.of(context).pop();
774-
exit(0);
799+
windowManager.destroy();
775800
},
776801
),
777802
],

example/pubspec.lock

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ packages:
5050
url: "https://pub.dartlang.org"
5151
source: hosted
5252
version: "1.15.0"
53+
crypto:
54+
dependency: transitive
55+
description:
56+
name: crypto
57+
url: "https://pub.dartlang.org"
58+
source: hosted
59+
version: "3.0.1"
5360
cupertino_icons:
5461
dependency: "direct main"
5562
description:
@@ -156,13 +163,27 @@ packages:
156163
url: "https://pub.dartlang.org"
157164
source: hosted
158165
version: "0.4.8"
166+
tray_manager:
167+
dependency: "direct main"
168+
description:
169+
name: tray_manager
170+
url: "https://pub.dartlang.org"
171+
source: hosted
172+
version: "0.1.3"
159173
typed_data:
160174
dependency: transitive
161175
description:
162176
name: typed_data
163177
url: "https://pub.dartlang.org"
164178
source: hosted
165179
version: "1.3.0"
180+
uuid:
181+
dependency: transitive
182+
description:
183+
name: uuid
184+
url: "https://pub.dartlang.org"
185+
source: hosted
186+
version: "3.0.6"
166187
vector_math:
167188
dependency: transitive
168189
description:

example/pubspec.yaml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ dependencies:
2020
# the parent directory to use the current plugin's version.
2121
path: ../
2222

23+
bot_toast: ^4.0.1
24+
preference_list: ^0.0.1
25+
tray_manager: ^0.1.2
26+
2327
# The following adds the Cupertino Icons font to your application.
2428
# Use with the CupertinoIcons class for iOS style icons.
2529
cupertino_icons: ^1.0.2
26-
bot_toast: ^4.0.1
27-
preference_list: ^0.0.1
2830

2931
dev_dependencies:
3032
flutter_test:
@@ -42,9 +44,8 @@ flutter:
4244
uses-material-design: true
4345

4446
# To add assets to your application, add an assets section, like this:
45-
# assets:
46-
# - images/a_dot_burr.jpeg
47-
# - images/a_dot_ham.jpeg
47+
assets:
48+
- images/
4849

4950
# An image asset can refer to one or more resolution-specific "variants", see
5051
# https://flutter.dev/assets-and-images/#resolution-aware.

example/windows/flutter/generated_plugin_registrant.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
#include "generated_plugin_registrant.h"
88

9+
#include <tray_manager/tray_manager_plugin.h>
910
#include <window_manager/window_manager_plugin.h>
1011

1112
void RegisterPlugins(flutter::PluginRegistry* registry) {
13+
TrayManagerPluginRegisterWithRegistrar(
14+
registry->GetRegistrarForPlugin("TrayManagerPlugin"));
1215
WindowManagerPluginRegisterWithRegistrar(
1316
registry->GetRegistrarForPlugin("WindowManagerPlugin"));
1417
}

example/windows/flutter/generated_plugins.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#
44

55
list(APPEND FLUTTER_PLUGIN_LIST
6+
tray_manager
67
window_manager
78
)
89

example/windows/runner/Runner.rc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ BEGIN
9090
BLOCK "040904e4"
9191
BEGIN
9292
VALUE "CompanyName", "org.leanflutter.plugins" "\0"
93-
VALUE "FileDescription", "Demonstrates how to use the window_manager plugin." "\0"
93+
VALUE "FileDescription", "window_manager_example" "\0"
9494
VALUE "FileVersion", VERSION_AS_STRING "\0"
9595
VALUE "InternalName", "window_manager_example" "\0"
9696
VALUE "LegalCopyright", "Copyright (C) 2021 org.leanflutter.plugins. All rights reserved." "\0"

example/windows/runner/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
3333
if (!window.CreateAndShow(L"window_manager_example", origin, size)) {
3434
return EXIT_FAILURE;
3535
}
36-
window.SetQuitOnClose(false);
36+
window.SetQuitOnClose(true);
3737

3838
run_loop.Run();
3939

lib/src/window_manager.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ class WindowManager {
9292
await _channel.invokeMethod('waitUntilReadyToShow');
9393
}
9494

95+
/// Force closing the window.
96+
Future<void> destroy() async {
97+
await _channel.invokeMethod('destroy');
98+
}
99+
95100
/// Try to close the window.
96101
Future<void> close() async {
97102
await _channel.invokeMethod('close');

0 commit comments

Comments
 (0)