-
Notifications
You must be signed in to change notification settings - Fork 31
feat: support preference for tray icon color schema #287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,65 @@ | ||||||||||
| import 'package:fluent_ui/fluent_ui.dart'; | ||||||||||
|
|
||||||||||
| import '../../../utils/l10n.dart'; | ||||||||||
| import '../../../utils/tray_manager.dart'; | ||||||||||
| import '../../../widgets/settings/settings_box_combo_box.dart'; | ||||||||||
| import '../../../constants/configurations.dart'; | ||||||||||
| import '../../../constants/settings_manager.dart'; | ||||||||||
|
|
||||||||||
| class TrayIconColorModeSetting extends StatefulWidget { | ||||||||||
| const TrayIconColorModeSetting({super.key}); | ||||||||||
|
|
||||||||||
| @override | ||||||||||
| TrayIconColorModeSettingState createState() => | ||||||||||
| TrayIconColorModeSettingState(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| class TrayIconColorModeSettingState extends State<TrayIconColorModeSetting> { | ||||||||||
| String trayIconColorMode = "auto"; | ||||||||||
|
|
||||||||||
| @override | ||||||||||
| void initState() { | ||||||||||
| super.initState(); | ||||||||||
| _loadTrayIconColorMode(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| Future<void> _loadTrayIconColorMode() async { | ||||||||||
| final storedMode = | ||||||||||
| await $settingsManager.getValue<String>(kTrayIconColorModeKey); | ||||||||||
| setState(() { | ||||||||||
| trayIconColorMode = storedMode ?? "auto"; | ||||||||||
| }); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| Future<void> _updateTrayIconColorMode(String newMode) async { | ||||||||||
| setState(() { | ||||||||||
| trayIconColorMode = newMode; | ||||||||||
| }); | ||||||||||
| await $settingsManager.setValue(kTrayIconColorModeKey, newMode); | ||||||||||
|
|
||||||||||
| // Update the tray icon immediately | ||||||||||
| $tray.updateTrayIcon(); | ||||||||||
|
Comment on lines
+40
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Consider awaiting updateTrayIcon for consistency. Awaiting updateTrayIcon will ensure the tray icon is updated before proceeding and allow proper error handling.
Suggested change
|
||||||||||
| } | ||||||||||
|
|
||||||||||
| @override | ||||||||||
| Widget build(BuildContext context) { | ||||||||||
| final s = S.of(context); | ||||||||||
|
|
||||||||||
| return SettingsBoxComboBox( | ||||||||||
| title: s.trayIconColorMode, | ||||||||||
| subtitle: s.trayIconColorModeSubtitle, | ||||||||||
| value: trayIconColorMode, | ||||||||||
| items: [ | ||||||||||
| SettingsBoxComboBoxItem( | ||||||||||
| value: "auto", title: s.automaticTrayIconMode), | ||||||||||
| SettingsBoxComboBoxItem(value: "light", title: s.light), | ||||||||||
| SettingsBoxComboBoxItem(value: "dark", title: s.dark), | ||||||||||
| ], | ||||||||||
| onChanged: (newValue) { | ||||||||||
| if (newValue != null) { | ||||||||||
| _updateTrayIconColorMode(newValue); | ||||||||||
| } | ||||||||||
| }, | ||||||||||
| ); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,9 @@ import 'package:bitsdojo_window/bitsdojo_window.dart'; | |
| import '../providers/status.dart'; | ||
| import '../providers/router_path.dart'; | ||
|
|
||
| import '../constants/configurations.dart'; | ||
| import '../constants/settings_manager.dart'; | ||
|
|
||
| import 'api/play_next.dart'; | ||
| import 'api/play_pause.dart'; | ||
| import 'api/play_play.dart'; | ||
|
|
@@ -26,16 +29,28 @@ class TrayIcon { | |
| } | ||
|
|
||
| class TrayManager { | ||
| static TrayIcon getTrayIcon() { | ||
| static Future<TrayIcon> getTrayIcon() async { | ||
| if (Platform.isMacOS) { | ||
| return TrayIcon('assets/mac-tray.svg', false); | ||
| } | ||
|
|
||
| final brightness = | ||
| SchedulerBinding.instance.platformDispatcher.platformBrightness == | ||
| Brightness.light | ||
| ? Brightness.dark.name | ||
| : Brightness.light.name; | ||
| // Get user preference for tray icon color mode | ||
| final trayIconColorMode = | ||
| await $settingsManager.getValue<String>(kTrayIconColorModeKey) ?? | ||
| "auto"; | ||
|
Comment on lines
+38
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Defaulting to "auto" is reasonable, but consider validating the value. Validate trayIconColorMode against the allowed values before using it to prevent issues from invalid or corrupted settings. |
||
|
|
||
| String brightness; | ||
| if (trayIconColorMode == "auto") { | ||
| // Automatic mode: follow system theme | ||
| brightness = | ||
| SchedulerBinding.instance.platformDispatcher.platformBrightness == | ||
| Brightness.light | ||
| ? Brightness.dark.name | ||
| : Brightness.light.name; | ||
| } else { | ||
| // Manual mode: use user's selection directly | ||
| brightness = trayIconColorMode; | ||
| } | ||
|
|
||
| if (Platform.isWindows) { | ||
| return TrayIcon('assets/tray_icon_$brightness.ico', false); | ||
|
|
@@ -113,7 +128,7 @@ class TrayManager { | |
| } | ||
|
|
||
| Future<void> updateTrayIcon() async { | ||
| final icon = getTrayIcon(); | ||
| final icon = await TrayManager.getTrayIcon(); | ||
| await systemTray.setImage(icon.path, isTemplate: true, isInstalled: icon.isInstalled); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (bug_risk): Consider handling asynchronous setState more robustly.
Check if the widget is mounted before calling setState in _loadTrayIconColorMode to prevent errors if the widget is disposed during the async operation.