Skip to content

Commit

Permalink
🎯 其他:优化代码结构
Browse files Browse the repository at this point in the history
  • Loading branch information
gvenusleo committed Nov 19, 2023
1 parent 6b45d19 commit 4b7c629
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 79 deletions.
74 changes: 5 additions & 69 deletions lib/global.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,80 +8,19 @@ import "package:lex/modules/ocr_item.dart";
import "package:lex/modules/translation_item.dart";
import "package:lex/utils/dir_utils.dart";
import "package:lex/utils/font_utils.dart";
import "package:lex/utils/tray_help.dart";
import "package:local_notifier/local_notifier.dart";
import "package:package_info_plus/package_info_plus.dart";
import "package:shared_preferences/shared_preferences.dart";
import "package:tray_manager/tray_manager.dart";
import "package:window_manager/window_manager.dart";

/// 应用版本号
String version = "1.0.0-beta.1";
const String version = "1.0.0-beta.1";

/// 持久化存储
late SharedPreferences prefs;
late Isar isar;

/// 系统托盘菜单
Menu menu = Menu(
items: [
MenuItem(
key: "show_translate",
label: "输入翻译",
),
// MenuItem(
// key: "ocr",
// label: "文字识别",
// ),
MenuItem.separator(),
MenuItem.submenu(
key: "autoCopy",
label: "自动复制",
submenu: Menu(
items: [
MenuItem(
key: "closeAutoCopy",
label: "关闭",
onClick: (_) async {
await prefs.setString("autoCopy", "close");
},
),
MenuItem.separator(),
MenuItem(
key: "autoCopySource",
label: "原文",
onClick: (_) async {
await prefs.setString("autoCopy", "source");
},
),
MenuItem(
key: "autoCopyResult",
label: "译文",
onClick: (_) async {
await prefs.setString("autoCopy", "result");
},
),
MenuItem(
key: "autoCopyBoth",
label: "原文+译文",
onClick: (_) async {
await prefs.setString("autoCopy", "both");
},
),
],
),
),
MenuItem(
key: "show_settings",
label: "应用设置",
),
MenuItem.separator(),
MenuItem(
key: "exit_app",
label: "退出应用",
),
],
);

/// 全局初始化
Future<void> init() async {
WidgetsFlutterBinding.ensureInitialized();
Expand All @@ -108,7 +47,7 @@ Future<void> init() async {
minimumSize: const Size(280, 300),
center: true,
backgroundColor: Colors.transparent,
title: "质感翻译",
title: "Lex",
titleBarStyle: TitleBarStyle.hidden,
windowButtonVisibility: false,
);
Expand All @@ -120,6 +59,7 @@ Future<void> init() async {
await windowManager.show();
await windowManager.focus();
}
await windowManager.setPreventClose(true);
});

// 开机自启动
Expand All @@ -130,11 +70,7 @@ Future<void> init() async {
);

// 注册系统托盘
await trayManager.destroy();
await trayManager.setIcon(
Platform.isWindows ? "assets/logo.ico" : "assets/logo.png",
);
await trayManager.setContextMenu(menu);
await initTray();

// 初始化系统通知
await localNotifier.setup(
Expand Down
30 changes: 23 additions & 7 deletions lib/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "package:lex/global.dart";
import "package:lex/pages/setting_page/settings_page.dart";
import "package:lex/pages/translation_page.dart";
import "package:lex/providers/window_provider.dart";
import "package:lex/utils/tray_help.dart";
import "package:provider/provider.dart";
import "package:screen_retriever/screen_retriever.dart";
import "package:screen_text_extractor/screen_text_extractor.dart";
Expand Down Expand Up @@ -77,12 +78,12 @@ class _HomePageState extends State<HomePage> with WindowListener, TrayListener {
// ? "取消置顶"
// : "置顶",
),
Expanded(
const Expanded(
child: DragToMoveArea(
child: Text(
"v$version",
textAlign: TextAlign.center,
style: const TextStyle(color: Colors.transparent),
style: TextStyle(color: Colors.transparent),
),
),
),
Expand All @@ -96,8 +97,8 @@ class _HomePageState extends State<HomePage> with WindowListener, TrayListener {
// tooltip: "最小化",
),
IconButton(
onPressed: () {
windowManager.hide();
onPressed: () async {
windowManager.close();
},
icon: const Icon(Icons.close_outlined),
padding: const EdgeInsets.all(0),
Expand All @@ -117,6 +118,18 @@ class _HomePageState extends State<HomePage> with WindowListener, TrayListener {
);
}

/// 窗口关闭事件
@override
void onWindowClose() async {
await _saveTranslateWindow();
await hideToTray();
}

@override
void onWindowFocus() {
setState(() {});
}

/// 系统托盘点击事件
@override
void onTrayIconMouseDown() {
Expand All @@ -135,6 +148,7 @@ class _HomePageState extends State<HomePage> with WindowListener, TrayListener {
switch (menuItem.key) {
// 显示翻译页面
case "show_translate":
await windowManager.setSkipTaskbar(false);
await _setTranslateWindow(
() => setState(() {
_selectedPage = TranslationPage(
Expand All @@ -149,6 +163,7 @@ class _HomePageState extends State<HomePage> with WindowListener, TrayListener {
// break;
// 显示设置页面
case "show_settings":
await windowManager.setSkipTaskbar(false);
await _saveTranslateWindow();
setState(() {
_selectedPage = const SettingsPage();
Expand All @@ -157,6 +172,7 @@ class _HomePageState extends State<HomePage> with WindowListener, TrayListener {
break;
// 退出应用
case "exit_app":
await windowManager.setPreventClose(false);
windowManager.close();
break;
}
Expand Down Expand Up @@ -331,11 +347,11 @@ class _HomePageState extends State<HomePage> with WindowListener, TrayListener {
// Future<void> _setOcrWindow(Function() setPage) async {
// setPage();
// await Future.delayed(const Duration(milliseconds: 100));
// await windowManager.setSize(const Size(800, 400));
// await lexwindowManager.setSize(const Size(800, 400));
// await Future.delayed(const Duration(milliseconds: 100));
// await windowManager.center(animate: true);
// await lexwindowManager.center(animate: true);
// await Future.delayed(const Duration(milliseconds: 100));
// await windowManager.show();
// await lexwindowManager.show();
// if (!mounted) return;
// await context.read<WindowProvider>().changeAlwaysOnTop(true);
// }
Expand Down
6 changes: 3 additions & 3 deletions lib/pages/setting_page/about_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ class AboutPage extends StatelessWidget {
height: 64,
),
const SizedBox(width: 12),
Column(
const Column(
children: [
const Text(
Text(
"Lex",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 28),
),
Text(
version,
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 20),
style: TextStyle(fontSize: 20),
),
],
),
Expand Down
80 changes: 80 additions & 0 deletions lib/utils/tray_help.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import 'dart:io';

import 'package:lex/global.dart';
import 'package:tray_manager/tray_manager.dart';
import 'package:window_manager/window_manager.dart';

/// 初始化系统托盘
Future<void> initTray() async {
Menu menu = Menu(
items: [
MenuItem(
key: "show_translate",
label: "输入翻译",
),
// MenuItem(
// key: "ocr",
// label: "文字识别",
// ),
MenuItem.separator(),
MenuItem.submenu(
key: "autoCopy",
label: "自动复制",
submenu: Menu(
items: [
MenuItem(
key: "closeAutoCopy",
label: "关闭",
onClick: (_) async {
await prefs.setString("autoCopy", "close");
},
),
MenuItem.separator(),
MenuItem(
key: "autoCopySource",
label: "原文",
onClick: (_) async {
await prefs.setString("autoCopy", "source");
},
),
MenuItem(
key: "autoCopyResult",
label: "译文",
onClick: (_) async {
await prefs.setString("autoCopy", "result");
},
),
MenuItem(
key: "autoCopyBoth",
label: "原文+译文",
onClick: (_) async {
await prefs.setString("autoCopy", "both");
},
),
],
),
),
MenuItem(
key: "show_settings",
label: "应用设置",
),
MenuItem.separator(),
MenuItem(
key: "exit_app",
label: "退出应用",
),
],
);

await trayManager.destroy();
await trayManager.setIcon(
Platform.isWindows ? "assets/logo.ico" : "assets/logo.png",
);
await trayManager.setContextMenu(menu);
}

/// 隐藏窗口到系统托盘
Future<void> hideToTray() async {
await windowManager.hide();
await windowManager.setSkipTaskbar(true);
}

0 comments on commit 4b7c629

Please sign in to comment.