|
| 1 | +import 'dart:io'; |
| 2 | +import 'package:flutter/gestures.dart'; |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:tray_manager/tray_manager.dart'; |
| 5 | +import 'package:url_launcher/url_launcher.dart'; |
| 6 | +import 'package:window_manager/window_manager.dart'; |
| 7 | +import 'pages/home_page.dart'; |
| 8 | +import 'services/config_service.dart'; |
| 9 | + |
| 10 | +final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>(); |
| 11 | + |
| 12 | +void main() async { |
| 13 | + WidgetsFlutterBinding.ensureInitialized(); |
| 14 | + await ConfigService.instance.initialize(); |
| 15 | + await windowManager.ensureInitialized(); |
| 16 | + |
| 17 | + WindowOptions windowOptions = const WindowOptions( |
| 18 | + size: Size(800, 600), |
| 19 | + center: true, |
| 20 | + backgroundColor: Colors.transparent, |
| 21 | + skipTaskbar: false, |
| 22 | + titleBarStyle: TitleBarStyle.normal, |
| 23 | + ); |
| 24 | + |
| 25 | + windowManager.waitUntilReadyToShow(windowOptions, () async { |
| 26 | + await windowManager.show(); |
| 27 | + await windowManager.focus(); |
| 28 | + runApp(const GitSwitcherApp()); |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +class GitSwitcherApp extends StatefulWidget { |
| 33 | + const GitSwitcherApp({super.key}); |
| 34 | + |
| 35 | + @override |
| 36 | + State<GitSwitcherApp> createState() => _GitSwitcherAppState(); |
| 37 | +} |
| 38 | + |
| 39 | +class _GitSwitcherAppState extends State<GitSwitcherApp> |
| 40 | + with WindowListener, TrayListener { |
| 41 | + @override |
| 42 | + void initState() { |
| 43 | + super.initState(); |
| 44 | + windowManager.addListener(this); |
| 45 | + trayManager.addListener(this); |
| 46 | + windowManager.setPreventClose(true); |
| 47 | + _initTray(); |
| 48 | + } |
| 49 | + |
| 50 | + @override |
| 51 | + void dispose() { |
| 52 | + windowManager.removeListener(this); |
| 53 | + trayManager.removeListener(this); |
| 54 | + super.dispose(); |
| 55 | + } |
| 56 | + |
| 57 | + void _initTray() async { |
| 58 | + String iconPath = Platform.isWindows |
| 59 | + ? 'assets/img/app_icon.ico' |
| 60 | + : 'assets/img/app_icon.png'; |
| 61 | + await trayManager.setIcon(iconPath); |
| 62 | + |
| 63 | + List<MenuItem> items = [ |
| 64 | + MenuItem(key: 'about_app', label: '关于'), |
| 65 | + MenuItem.separator(), |
| 66 | + MenuItem(key: 'exit_app', label: '退出'), |
| 67 | + ]; |
| 68 | + |
| 69 | + await trayManager.setContextMenu(Menu(items: items)); |
| 70 | + trayManager.setToolTip('Git Switcher'); |
| 71 | + |
| 72 | + await trayManager.setContextMenu(Menu(items: items)); |
| 73 | + trayManager.setToolTip('Git Switcher'); |
| 74 | + } |
| 75 | + |
| 76 | + void _showAboutDialog() { |
| 77 | + final context = navigatorKey.currentContext; |
| 78 | + if (context == null) return; |
| 79 | + |
| 80 | + final uriGithub = Uri.parse('https://github.com/voidbytes/git-switcher'); |
| 81 | + final uriHomepage = Uri.parse('http://voidbytes.com/'); |
| 82 | + |
| 83 | + showDialog( |
| 84 | + context: context, |
| 85 | + builder: (dialogContext) => AlertDialog( |
| 86 | + title: const Text('关于 Git Switcher'), |
| 87 | + content: RichText( |
| 88 | + text: TextSpan( |
| 89 | + style: Theme.of(context).textTheme.bodyMedium, |
| 90 | + children: [ |
| 91 | + const TextSpan(text: '作者: voidbytes\n'), |
| 92 | + const TextSpan(text: '作者主页:'), |
| 93 | + TextSpan( |
| 94 | + text: 'https://voidbytes.com\n', |
| 95 | + style: const TextStyle( |
| 96 | + color: Colors.blue, |
| 97 | + decoration: TextDecoration.underline, |
| 98 | + ), |
| 99 | + recognizer: TapGestureRecognizer() |
| 100 | + ..onTap = () { |
| 101 | + launchUrl(uriHomepage); |
| 102 | + }, |
| 103 | + ), |
| 104 | + const TextSpan(text: '项目地址:'), |
| 105 | + TextSpan( |
| 106 | + text: 'https://github.com/voidbytes/git-switcher', |
| 107 | + style: const TextStyle( |
| 108 | + color: Colors.blue, |
| 109 | + decoration: TextDecoration.underline, |
| 110 | + ), |
| 111 | + recognizer: TapGestureRecognizer() |
| 112 | + ..onTap = () { |
| 113 | + launchUrl(uriGithub); |
| 114 | + }, |
| 115 | + ), |
| 116 | + ], |
| 117 | + ), |
| 118 | + ), |
| 119 | + actions: [ |
| 120 | + TextButton( |
| 121 | + onPressed: () => Navigator.of(dialogContext).pop(), |
| 122 | + child: const Text('关闭'), |
| 123 | + ), |
| 124 | + ], |
| 125 | + ), |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + @override |
| 130 | + Widget build(BuildContext context) { |
| 131 | + return MaterialApp( |
| 132 | + navigatorKey: navigatorKey, |
| 133 | + title: 'Git账号切换器', |
| 134 | + theme: ThemeData( |
| 135 | + primarySwatch: Colors.blue, |
| 136 | + visualDensity: VisualDensity.comfortable, |
| 137 | + fontFamily: "AlibabaPuHuiTi", |
| 138 | + fontFamilyFallback: ["AlibabaPuHuiTi"], |
| 139 | + colorScheme: ColorScheme.fromSeed( |
| 140 | + brightness: Brightness.light, |
| 141 | + seedColor: Colors.lightBlueAccent, |
| 142 | + ), |
| 143 | + useMaterial3: true, |
| 144 | + ), |
| 145 | + home: const HomePage(), |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + @override |
| 150 | + void onWindowClose() { |
| 151 | + if (ConfigService.instance.appConfig.minimizeToTray) { |
| 152 | + windowManager.hide(); |
| 153 | + } else { |
| 154 | + windowManager.destroy(); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + @override |
| 159 | + void onTrayIconMouseDown() { |
| 160 | + if (Platform.isWindows) { |
| 161 | + windowManager.show(); |
| 162 | + } else { |
| 163 | + trayManager.popUpContextMenu(); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + @override |
| 168 | + void onTrayIconRightMouseDown() { |
| 169 | + if (Platform.isWindows) { |
| 170 | + trayManager.popUpContextMenu(); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + @override |
| 175 | + void onTrayMenuItemClick(MenuItem menuItem) { |
| 176 | + switch (menuItem.key) { |
| 177 | + case 'show_window': |
| 178 | + windowManager.show(); |
| 179 | + break; |
| 180 | + case 'about_app': |
| 181 | + windowManager.show(); |
| 182 | + _showAboutDialog(); |
| 183 | + break; |
| 184 | + case 'exit_app': |
| 185 | + windowManager.destroy(); |
| 186 | + break; |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments