Skip to content

Commit

Permalink
feat: add system_tray
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Dec 3, 2023
1 parent bb5501b commit 6117762
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
42 changes: 42 additions & 0 deletions lib/init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Future<MyApp> init() async {

WidgetsFlutterBinding.ensureInitialized();
final packageInfo = await PackageInfo.fromPlatform();
await initSystemTray();
final mainBloc = MainBloc(
clientSettingBloc,
tipherethBloc,
Expand All @@ -43,3 +44,44 @@ Future<MyApp> init() async {

return MyApp(router, mainBloc);
}

Future<void> initSystemTray() async {
if (!PlatformHelper.isWindowsApp()) {
return null;
}
const String path = 'windows/runner/resources/app_icon.ico';

final AppWindow appWindow = AppWindow();
final SystemTray systemTray = SystemTray();

// We first init the systray menu
await systemTray.initSystemTray(
title: 'system tray',
iconPath: path,
);

// create context menu
final Menu menu = Menu();
await menu.buildFrom([
MenuItemLabel(label: 'Show', onClicked: (menuItem) => appWindow.show()),
MenuItemLabel(label: 'Hide', onClicked: (menuItem) => appWindow.hide()),
MenuItemLabel(label: 'Exit', onClicked: (menuItem) => appWindow.close()),
]);

// set context menu
await systemTray.setContextMenu(menu);

// handle system tray event
systemTray.registerSystemTrayEventHandler((eventName) {
debugPrint('eventName: $eventName');
if (eventName == kSystemTrayEventClick) {
PlatformHelper.isWindows()
? appWindow.show()
: systemTray.popUpContextMenu();
} else if (eventName == kSystemTrayEventRightClick) {
PlatformHelper.isWindows()
? systemTray.popUpContextMenu()
: appWindow.show();
}
});
}
1 change: 1 addition & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:go_router/go_router.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:path_provider/path_provider.dart';
import 'package:system_tray/system_tray.dart';

import 'bloc/chesed/chesed_bloc.dart';
import 'bloc/client_setting/client_setting_bloc.dart';
Expand Down
40 changes: 38 additions & 2 deletions lib/view/specialized/title_bar.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import 'dart:async';

import 'package:bitsdojo_window/bitsdojo_window.dart';
import 'package:flutter/material.dart';

import '../../common/platform.dart';
import 'window_button.dart';

class TitleBar extends StatelessWidget {
const TitleBar({super.key, this.actions = const []});

final List<Widget> actions;

void hideWindow() {}

@override
Widget build(BuildContext context) {
if (PlatformHelper.isAndroidApp()) {
Expand Down Expand Up @@ -49,7 +52,40 @@ class TitleBar extends StatelessWidget {
const SizedBox(
width: 8,
),
if (PlatformHelper.isWindowsApp()) const WindowButtons()
if (PlatformHelper.isWindowsApp())
Row(
children: [
MinimizeWindowButton(),
MaximizeWindowButton(),
CloseWindowButton(onPressed: () {
unawaited(showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Exit Program?'),
content: const Text(
'Press Hide to hide window in tray, or press yes to exit.'),
actions: <Widget>[
TextButton(
child: const Text('Hide'),
onPressed: () {
Navigator.of(context).pop();
appWindow.hide();
},
),
TextButton(
child: const Text('Yes'),
onPressed: () {
appWindow.close();
},
),
],
);
},
));
}),
],
),
],
);
}
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ dependencies:

# window manage util
bitsdojo_window: ^0.1.5
system_tray: ^2.0.3

# api connection
grpc: ^3.1.0
Expand Down Expand Up @@ -140,6 +141,7 @@ flutter:
# To add assets to your application, add an assets section, like this:
assets:
- web/icons/Icon-512.png
- windows/runner/resources/app_icon.ico

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

0 comments on commit 6117762

Please sign in to comment.