Skip to content

Commit

Permalink
feat(app): add system tray functionality
Browse files Browse the repository at this point in the history
- Update pubspec.yaml with tray_manager dependency.
- Initialize system tray in app.dart and handle menu item clicks.
  • Loading branch information
dongfengweixiao committed Aug 4, 2024
1 parent 1140d1c commit 5e3c96b
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/appimage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
channel: 'stable'
flutter-version: ${{env.FLUTTER_VERSION}}
- run: sudo apt update
- run: sudo apt install -y clang cmake curl libgtk-3-dev ninja-build pkg-config unzip libunwind-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libmpv-dev
- run: sudo apt install -y clang cmake curl libgtk-3-dev ninja-build pkg-config unzip libunwind-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libmpv-dev libappindicator3-1 libappindicator3-dev
- run: flutter --disable-analytics
- run: flutter pub get
- run: flutter build linux --release
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ jobs:
channel: 'stable'
flutter-version: ${{env.FLUTTER_VERSION}}
- run: sudo apt update
- run: sudo apt install -y clang cmake curl libgtk-3-dev ninja-build pkg-config unzip libunwind-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libmpv-dev
- run: sudo apt install -y clang cmake curl libgtk-3-dev ninja-build pkg-config unzip libunwind-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libmpv-dev libappindicator3-1 libappindicator3-dev
- run: flutter pub get
- run: flutter build linux -v
2 changes: 1 addition & 1 deletion .github/workflows/snap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
channel: 'stable'
flutter-version: ${{env.FLUTTER_VERSION}}
- run: sudo apt update
- run: sudo apt install -y clang cmake curl libgtk-3-dev ninja-build pkg-config unzip libunwind-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libmpv-dev
- run: sudo apt install -y clang cmake curl libgtk-3-dev ninja-build pkg-config unzip libunwind-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libmpv-dev libappindicator3-1 libappindicator3-dev
- run: flutter pub get
- uses: snapcore/action-build@v1
id: build
Expand Down
Binary file added assets/images/tray_icon.ico
Binary file not shown.
Binary file added assets/images/tray_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions lib/app/view/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import '../../local_audio/local_audio_model.dart';
import '../../settings/settings_model.dart';
import 'scaffold.dart';
import 'splash_screen.dart';
import 'system_tray.dart';

class YaruMusicPodApp extends StatelessWidget {
const YaruMusicPodApp({
Expand Down Expand Up @@ -106,11 +107,14 @@ class _MusicPodApp extends StatefulWidget with WatchItStatefulWidgetMixin {
class _MusicPodAppState extends State<_MusicPodApp>
with WidgetsBindingObserver {
late Future<bool> _initFuture;
late SystemTray _tray;

@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_tray = SystemTray();
_tray.init();
_initFuture = _init();
}

Expand Down
40 changes: 40 additions & 0 deletions lib/app/view/system_tray.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'package:tray_manager/tray_manager.dart';
import 'package:window_manager/window_manager.dart';

import '../../persistence_utils.dart';

class SystemTray with TrayListener {
Future<void> init() async {
trayManager.addListener(this);
await trayManager.setIcon(trayIcon());
await trayManager.setContextMenu(Menu(items: trayMenuItems));
}

// KDE@Arch Linux, this feature does not work.
// @override
// void onTrayIconMouseDown() {
// print('onTrayIconMouseDown');
// }

// KDE@Arch Linux, this feature does not work.
// @override
// void onTrayIconRightMouseDown() {
// print('onTrayIconRightMouseDown');
// }

@override
void onTrayMenuItemClick(MenuItem menuItem) {
switch (menuItem.key) {
case 'restore_window':
windowManager.isVisible().then((value) {
if (value) {
windowManager.hide();
} else {
windowManager.show();
}
});
case 'close_application':
windowManager.close();
}
}
}
22 changes: 22 additions & 0 deletions lib/persistence_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:typed_data';

import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import 'package:tray_manager/tray_manager.dart';
import 'package:xdg_directories/xdg_directories.dart';

import 'constants.dart';
Expand Down Expand Up @@ -278,3 +279,24 @@ Future<Map<String, Uint8List?>?> readUint8ListMap(String fileName) async {

return theMap;
}

/// TODO: how to l10n labels?
List<MenuItem> trayMenuItems = [
MenuItem(
key: 'restore_window',
label: 'Hide/Restore',
),
MenuItem.separator(),
MenuItem(
key: 'close_application',
label: 'Close Application',
),
];

String trayIcon() {
if (Platform.isWindows) {
return 'assets/images/tray_icon.ico';
} else {
return 'assets/images/tray_icon.png';
}
}
4 changes: 4 additions & 0 deletions linux/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <screen_retriever/screen_retriever_plugin.h>
#include <super_native_extensions/super_native_extensions_plugin.h>
#include <system_theme/system_theme_plugin.h>
#include <tray_manager/tray_manager_plugin.h>
#include <url_launcher_linux/url_launcher_plugin.h>
#include <window_manager/window_manager_plugin.h>
#include <yaru_window_linux/yaru_window_linux_plugin.h>
Expand Down Expand Up @@ -47,6 +48,9 @@ void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) system_theme_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "SystemThemePlugin");
system_theme_plugin_register_with_registrar(system_theme_registrar);
g_autoptr(FlPluginRegistrar) tray_manager_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "TrayManagerPlugin");
tray_manager_plugin_register_with_registrar(tray_manager_registrar);
g_autoptr(FlPluginRegistrar) url_launcher_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "UrlLauncherPlugin");
url_launcher_plugin_register_with_registrar(url_launcher_linux_registrar);
Expand Down
1 change: 1 addition & 0 deletions linux/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
screen_retriever
super_native_extensions
system_theme
tray_manager
url_launcher_linux
window_manager
yaru_window_linux
Expand Down
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import screen_retriever
import sqflite
import super_native_extensions
import system_theme
import tray_manager
import url_launcher_macos
import wakelock_plus
import window_manager
Expand All @@ -40,6 +41,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
SuperNativeExtensionsPlugin.register(with: registry.registrar(forPlugin: "SuperNativeExtensionsPlugin"))
SystemThemePlugin.register(with: registry.registrar(forPlugin: "SystemThemePlugin"))
TrayManagerPlugin.register(with: registry.registrar(forPlugin: "TrayManagerPlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
WakelockPlusMacosPlugin.register(with: registry.registrar(forPlugin: "WakelockPlusMacosPlugin"))
WindowManagerPlugin.register(with: registry.registrar(forPlugin: "WindowManagerPlugin"))
Expand Down
24 changes: 24 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,14 @@ packages:
url: "https://github.com/media-kit/media-kit"
source: git
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 @@ -1265,6 +1273,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.0"
shortid:
dependency: transitive
description:
name: shortid
sha256: d0b40e3dbb50497dad107e19c54ca7de0d1a274eb9b4404991e443dadb9ebedb
url: "https://pub.dev"
source: hosted
version: "0.1.2"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -1430,6 +1446,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.1"
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
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ dependencies:
smtc_windows: ^0.1.2
super_drag_and_drop: ^0.8.15
system_theme: ^2.3.1
tray_manager: ^0.2.3
url_launcher: ^6.2.5
watch_it: ^1.4.1
window_manager: ^0.3.8
Expand All @@ -92,6 +93,8 @@ flutter:
assets:
- snap/gui/musicpod.png
- assets/images/media-optical.png
- assets/images/tray_icon.ico
- assets/images/tray_icon.png

dependency_overrides:
intl: 0.19.0
Expand Down
3 changes: 3 additions & 0 deletions windows/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <screen_retriever/screen_retriever_plugin.h>
#include <super_native_extensions/super_native_extensions_plugin_c_api.h>
#include <system_theme/system_theme_plugin.h>
#include <tray_manager/tray_manager_plugin.h>
#include <url_launcher_windows/url_launcher_windows.h>
#include <window_manager/window_manager_plugin.h>

Expand All @@ -37,6 +38,8 @@ void RegisterPlugins(flutter::PluginRegistry* registry) {
registry->GetRegistrarForPlugin("SuperNativeExtensionsPluginCApi"));
SystemThemePluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("SystemThemePlugin"));
TrayManagerPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("TrayManagerPlugin"));
UrlLauncherWindowsRegisterWithRegistrar(
registry->GetRegistrarForPlugin("UrlLauncherWindows"));
WindowManagerPluginRegisterWithRegistrar(
Expand Down
1 change: 1 addition & 0 deletions windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
screen_retriever
super_native_extensions
system_theme
tray_manager
url_launcher_windows
window_manager
)
Expand Down

0 comments on commit 5e3c96b

Please sign in to comment.