mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-16 21:33:41 +00:00
feat: add desktop system tray support (#120)
This commit is contained in:
1 parent
0100f0131d
commit
b3e372326f
17 files changed
+203
-7
No files matched your search
@@ -7,7 +7,9 @@ import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:tray_manager/tray_manager.dart';
|
||||
import 'package:uri_to_file/uri_to_file.dart';
|
||||
import 'package:window_manager/window_manager.dart';
|
||||
|
||||
import '../../../../api/api.dart';
|
||||
import '../../../../api/model/downloader_config.dart';
|
||||
@@ -15,6 +17,7 @@ import '../../../../core/common/start_config.dart';
|
||||
import '../../../../generated/locales.g.dart';
|
||||
import '../../../../util/localeManager.dart';
|
||||
import '../../../../util/log_util.dart';
|
||||
import '../../../../util/package_info.dart';
|
||||
import '../../../../util/util.dart';
|
||||
import '../../../routes/app_pages.dart';
|
||||
|
||||
@@ -59,7 +62,7 @@ final allTrackerSubscribeUrlCdns = Map.fromIterable(allTrackerSubscribeUrls,
|
||||
return ret;
|
||||
});
|
||||
|
||||
class AppController extends GetxController {
|
||||
class AppController extends GetxController with WindowListener, TrayListener {
|
||||
static StartConfig? _defaultStartConfig;
|
||||
|
||||
final startConfig = StartConfig().obs;
|
||||
@@ -72,12 +75,45 @@ class AppController extends GetxController {
|
||||
@override
|
||||
void onReady() {
|
||||
super.onReady();
|
||||
_initDeepLinks();
|
||||
try {
|
||||
_initDeepLinks();
|
||||
} catch (e) {
|
||||
logger.w("initDeepLinks error", e);
|
||||
}
|
||||
try {
|
||||
_initWinodws();
|
||||
} catch (e) {
|
||||
logger.w("_initWinodws error", e);
|
||||
}
|
||||
try {
|
||||
_initTray();
|
||||
} catch (e) {
|
||||
logger.w("initTray error", e);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
_linkSubscription?.cancel();
|
||||
trayManager.removeListener(this);
|
||||
}
|
||||
|
||||
@override
|
||||
void onWindowClose() async {
|
||||
final isPreventClose = await windowManager.isPreventClose();
|
||||
if (isPreventClose) {
|
||||
windowManager.hide();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void onTrayIconMouseDown() {
|
||||
windowManager.show();
|
||||
}
|
||||
|
||||
@override
|
||||
void onTrayIconRightMouseDown() {
|
||||
trayManager.popUpContextMenu();
|
||||
}
|
||||
|
||||
Future<void> _initDeepLinks() async {
|
||||
@@ -95,6 +131,56 @@ class AppController extends GetxController {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _initWinodws() async {
|
||||
if (!Util.isDesktop()) {
|
||||
return;
|
||||
}
|
||||
windowManager.addListener(this);
|
||||
}
|
||||
|
||||
Future<void> _initTray() async {
|
||||
if (!Util.isDesktop()) {
|
||||
return;
|
||||
}
|
||||
await trayManager.setIcon(
|
||||
Util.isWindows()
|
||||
? 'assets/tray_icon/icon.ico'
|
||||
: 'assets/tray_icon/icon.png',
|
||||
);
|
||||
final menu = Menu(items: [
|
||||
MenuItem(
|
||||
label: "create".tr,
|
||||
onClick: (menuItem) async => {
|
||||
await windowManager.show(),
|
||||
await Get.rootDelegate.offAndToNamed(Routes.CREATE),
|
||||
},
|
||||
),
|
||||
MenuItem(
|
||||
label: 'setting'.tr,
|
||||
onClick: (menuItem) async => {
|
||||
await windowManager.show(),
|
||||
await Get.rootDelegate.offAndToNamed(Routes.SETTING),
|
||||
},
|
||||
),
|
||||
MenuItem.separator(),
|
||||
MenuItem(
|
||||
label: 'donate'.tr,
|
||||
onClick: (menuItem) => {windowManager.destroy()},
|
||||
),
|
||||
MenuItem(
|
||||
label: '${"version".tr}(${packageInfo.version})',
|
||||
onClick: (menuItem) => {windowManager.destroy()},
|
||||
),
|
||||
MenuItem.separator(),
|
||||
MenuItem(
|
||||
label: 'exit'.tr,
|
||||
onClick: (menuItem) => {windowManager.destroy()},
|
||||
),
|
||||
]);
|
||||
await trayManager.setContextMenu(menu);
|
||||
trayManager.addListener(this);
|
||||
}
|
||||
|
||||
Future<void> _toCreate(Uri uri) async {
|
||||
final path = uri.scheme == "magnet"
|
||||
? uri.toString()
|
||||
|
||||
@@ -11,6 +11,15 @@ class HomeView extends GetView<HomeController> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GetRouterOutlet.builder(builder: (context, delegate, currentRoute) {
|
||||
switch (currentRoute?.location) {
|
||||
case Routes.SETTING:
|
||||
controller.currentIndex.value = 1;
|
||||
break;
|
||||
default:
|
||||
controller.currentIndex.value = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
// extendBody: true,
|
||||
body: Row(
|
||||
|
||||
@@ -23,6 +23,8 @@ class LocaleKeys {
|
||||
static const downloading = 'downloading';
|
||||
static const downloaded = 'downloaded';
|
||||
static const setting = 'setting';
|
||||
static const donate = 'donate';
|
||||
static const exit = 'exit';
|
||||
static const create = 'create';
|
||||
static const downloadLink = 'downloadLink';
|
||||
static const downloadLinkValid = 'downloadLinkValid';
|
||||
@@ -82,6 +84,8 @@ class Locales {
|
||||
'downloading': 'downloading',
|
||||
'downloaded': 'downloaded',
|
||||
'setting': 'Setting',
|
||||
'donate': 'Donate',
|
||||
'exit': 'Exit',
|
||||
'create': 'Create Task',
|
||||
'downloadLink': 'Download Link',
|
||||
'downloadLinkValid': 'Please enter the download link',
|
||||
@@ -139,6 +143,8 @@ class Locales {
|
||||
'selectAll': 'انتخاب همه',
|
||||
'task': 'کار',
|
||||
'setting': 'تنظیمات',
|
||||
'donate': 'کمک مالی',
|
||||
'exit': 'خروج',
|
||||
'create': 'ایجاد کار',
|
||||
'downloading': 'دانلود کردن',
|
||||
'downloaded': 'دانلود شدن',
|
||||
@@ -196,6 +202,8 @@ class Locales {
|
||||
'downloading': 'Загрузка',
|
||||
'downloaded': 'Загружено',
|
||||
'setting': 'Настройки',
|
||||
'donate': 'Пожертвовать',
|
||||
'exit': 'Выход',
|
||||
'create': 'Создать задачу',
|
||||
'downloadLink': 'Ссылка для скачивание',
|
||||
'downloadLinkValid': 'Пожалуйста, введите ссылку для скачивания',
|
||||
@@ -255,6 +263,8 @@ class Locales {
|
||||
'downloading': '下载中',
|
||||
'downloaded': '已完成',
|
||||
'setting': '设置',
|
||||
'donate': '打赏',
|
||||
'exit': '退出',
|
||||
'create': '创建任务',
|
||||
'downloadLink': '下载链接',
|
||||
'downloadLinkValid': '请输入下载链接',
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:window_manager/window_manager.dart';
|
||||
|
||||
import 'api/api.dart' as api;
|
||||
import 'app/modules/app/controllers/app_controller.dart';
|
||||
@@ -11,6 +12,7 @@ import 'util/localeManager.dart';
|
||||
import 'util/log_util.dart';
|
||||
import 'util/mac_secure_util.dart';
|
||||
import 'util/package_info.dart';
|
||||
import 'util/util.dart';
|
||||
|
||||
void main() async {
|
||||
await init();
|
||||
@@ -21,6 +23,23 @@ void main() async {
|
||||
|
||||
Future<void> init() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
if (Util.isDesktop()) {
|
||||
await windowManager.ensureInitialized();
|
||||
const windowOptions = WindowOptions(
|
||||
size: Size(1024, 768),
|
||||
center: true,
|
||||
backgroundColor: Colors.transparent,
|
||||
skipTaskbar: false,
|
||||
);
|
||||
windowManager.waitUntilReadyToShow(windowOptions, () async {
|
||||
await windowManager.setBrightness(
|
||||
Brightness.light,
|
||||
);
|
||||
await windowManager.show();
|
||||
await windowManager.focus();
|
||||
await windowManager.setPreventClose(true);
|
||||
});
|
||||
}
|
||||
|
||||
final controller = Get.put(AppController());
|
||||
try {
|
||||
|
||||
@@ -41,6 +41,10 @@ class Util {
|
||||
return Platform.isWindows || Platform.isLinux || Platform.isMacOS;
|
||||
}
|
||||
|
||||
static isWindows() {
|
||||
return !kIsWeb && Platform.isWindows;
|
||||
}
|
||||
|
||||
static isMacos() {
|
||||
return !kIsWeb && Platform.isMacOS;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user