feat: add minimize to tray functionality (#711)

This commit is contained in:
wanghongenpin
2026-05-07 23:57:49 +08:00
parent 0c6acb2567
commit e0b29cf08f
4 changed files with 16 additions and 21 deletions

View File

@@ -104,10 +104,7 @@ class AppConfiguration {
double panelRatio = 0.3;
/// 关闭窗口时最小化到系统托盘
bool minimizeToTray = false;
/// 首次关闭时是否已经提示过最小化到托盘
bool? minimizeToTrayPromptShown;
bool? minimizeToTray;
AppConfiguration._();
@@ -206,12 +203,9 @@ class AppConfiguration {
_theme.color = config['themeColor'] ?? "Blue";
upgradeNoticeV27 = config['upgradeNoticeV27'] ?? true;
_language = config['language'] == null
? null
: Locale.fromSubtags(
languageCode: config['language'],
scriptCode: config['languageScript']
);
_language = config['language'] == null
? null
: Locale.fromSubtags(languageCode: config['language'], scriptCode: config['languageScript']);
pipEnabled.value = config['pipEnabled'] ?? true;
pipIcon.value = config['pipIcon'] ?? false;
headerExpanded = config['headerExpanded'] ?? true;
@@ -228,8 +222,7 @@ class AppConfiguration {
if (config['panelRatio'] != null) {
panelRatio = config['panelRatio'];
}
minimizeToTray = config['minimizeToTray'] ?? false;
minimizeToTrayPromptShown = config['minimizeToTrayPromptShown'];
minimizeToTray = config['minimizeToTray'];
} catch (e) {
logger.e(e);
}
@@ -275,7 +268,6 @@ class AppConfiguration {
"windowPosition": windowPosition == null ? null : {"dx": windowPosition?.dx, "dy": windowPosition?.dy},
if (Platforms.isDesktop()) 'panelRatio': panelRatio,
if (Platforms.isDesktop()) 'minimizeToTray': minimizeToTray,
if (Platforms.isDesktop() && minimizeToTrayPromptShown != null) 'minimizeToTrayPromptShown': minimizeToTrayPromptShown,
};
}
}

View File

@@ -127,7 +127,7 @@ class _PreferenceState extends State<Preference> {
subtitle: Text(localizations.minimizeToTraySubtitle, style: subtitleStyle),
trailing: SwitchWidget(
scale: 0.75,
value: appConfiguration.minimizeToTray,
value: appConfiguration.minimizeToTray ?? false,
onChanged: (value) {
appConfiguration.minimizeToTray = value;
appConfiguration.flushConfig();

View File

@@ -106,14 +106,14 @@ class _SocketLaunchState extends State<SocketLaunch> with WindowListener, Widget
Future<void> _handleWindowClose() async {
final appConfiguration = AppConfiguration.current;
if (Platforms.isDesktop() && appConfiguration?.minimizeToTray == true) {
if (appConfiguration?.minimizeToTrayPromptShown == null) {
if (Platforms.isDesktop() && appConfiguration?.minimizeToTray == null || appConfiguration?.minimizeToTray == true) {
if (appConfiguration?.minimizeToTray == null) {
final minimize = await _showTrayClosePrompt();
if (!mounted) {
return;
}
appConfiguration?.minimizeToTrayPromptShown = true;
appConfiguration?.minimizeToTray = minimize;
await appConfiguration?.flushConfig();
if (!minimize) {
@@ -129,6 +129,7 @@ class _SocketLaunchState extends State<SocketLaunch> with WindowListener, Widget
logger.e('show to tray failed, fallback to exit', error: e);
}
}
await appExit();
}
@@ -139,9 +140,7 @@ class _SocketLaunchState extends State<SocketLaunch> with WindowListener, Widget
builder: (ctx) {
return AlertDialog(
title: Text(localizations.minimizeToTrayTitle),
content: SizedBox(
width: 320,
child: Text(maxLines: 3, localizations.trayClosePromptContent)),
content: SizedBox(width: 320, child: Text(maxLines: 3, localizations.trayClosePromptContent)),
actions: [
TextButton(
onPressed: () => Navigator.of(ctx).pop(false),
@@ -181,7 +180,10 @@ class _SocketLaunchState extends State<SocketLaunch> with WindowListener, Widget
@override
Future<AppExitResponse> didRequestAppExit() async {
await appExit();
bool isPreventClose = await windowManager.isPreventClose();
if (!isPreventClose || Platform.isMacOS) {
await appExit();
}
return super.didRequestAppExit();
}

View File

@@ -48,6 +48,7 @@ class DesktopSupport {
await windowManager.focus();
});
windowManager.addListener(WindowChangeListener(appConfiguration));
registerMethodHandler();
} catch (e) {
logger.e("Error during desktop initialization: $e");