Skip to content

Commit

Permalink
feat: add close to tray option
Browse files Browse the repository at this point in the history
  • Loading branch information
Merrit committed Jun 19, 2024
1 parent f380661 commit ecc0968
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
6 changes: 2 additions & 4 deletions lib/src/localization/strings.i18n.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"appTitle": "Feeling Finder",
"@AppTitle": "The title of the application",

"emojiCategories": {
"recent": "Recent",
"all": "All",
Expand All @@ -16,9 +15,9 @@
"flags": "Flags",
"custom": "Custom"
},

"settings": {
"title": "Settings",
"closeToTray": "Close to system tray",
"theme": "Theme",
"systemTheme": "System",
"lightTheme": "Light",
Expand All @@ -33,6 +32,5 @@
"hotkeyToggle": "Toggle visibility with a keyboard shortcut",
"shortcutUsage": "Press {modifierKey} + {actionKey} to use the shortcut"
},

"searchHintText": "Type to search"
}
}
12 changes: 12 additions & 0 deletions lib/src/settings/cubit/settings_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ class SettingsCubit extends Cubit<SettingsState> {
SettingsCubit._(
this._settingsService,
this._systemTray, {
required bool closeToTray,
required ThemeMode userThemePreference,
}) : super(
SettingsState(
closeToTray: closeToTray,
exitOnCopy: _settingsService.exitOnCopy(),
hotKeyEnabled: _settingsService.hotKeyEnabled(),
showSystemTrayIcon: _settingsService.showSystemTrayIcon(),
Expand All @@ -40,9 +42,12 @@ class SettingsCubit extends Cubit<SettingsState> {
SettingsService settingsService,
SystemTray? systemTray,
) async {
final bool closeToTray = await settingsService.closeToTray();

return SettingsCubit._(
settingsService,
systemTray,
closeToTray: closeToTray,
userThemePreference: await settingsService.themeMode(),
);
}
Expand Down Expand Up @@ -73,6 +78,13 @@ class SettingsCubit extends Cubit<SettingsState> {
});
}

Future<void> updateCloseToTray([bool? closeToTray]) async {
if (closeToTray == null) return;

await _settingsService.saveCloseToTray(closeToTray);
emit(state.copyWith(closeToTray: closeToTray));
}

/// Update and persist whether the app should exit after copy.
Future<void> updateExitOnCopy(bool value) async {
emit(state.copyWith(exitOnCopy: value));
Expand Down
3 changes: 3 additions & 0 deletions lib/src/settings/cubit/settings_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ part of 'settings_cubit.dart';
@freezed
class SettingsState with _$SettingsState {
const factory SettingsState({
/// Whether the app should continue running in the tray when closed.
required bool closeToTray,

/// Whether the app should exit automatically after copying an emoji.
required bool exitOnCopy,

Expand Down
12 changes: 12 additions & 0 deletions lib/src/settings/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import '../core/core.dart';
import '../helpers/helpers.dart';
import '../localization/strings.g.dart';
import '../shortcuts/app_hotkey.dart';
import '../window/app_window.dart';
import 'cubit/settings_cubit.dart';

/// Displays the various settings that can be customized by the user.
Expand Down Expand Up @@ -79,6 +80,16 @@ class SettingsPage extends StatelessWidget {
},
);

final Widget closeToTrayTile = BlocBuilder<SettingsCubit, SettingsState>(
builder: (context, state) {
return SwitchListTile(
title: Text(translations.settings.closeToTray),
value: state.closeToTray,
onChanged: (value) => settingsCubit.updateCloseToTray(value),
);
},
);

final Widget hotkeyEnabledTile = BlocBuilder<SettingsCubit, SettingsState>(
builder: (context, state) {
return SwitchListTile(
Expand Down Expand Up @@ -174,6 +185,7 @@ class SettingsPage extends StatelessWidget {
const Divider(),
exitAfterCopyTile,
if (defaultTargetPlatform.isDesktop) showTrayTile,
if (defaultTargetPlatform.isDesktop) closeToTrayTile,
if (runsX11) hotkeyEnabledTile,
if (runsX11) hotkeyConfigurationTile,
const Divider(),
Expand Down
10 changes: 10 additions & 0 deletions lib/src/settings/settings_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ class SettingsService {
/// Singleton instance of the SettingsService.
static late SettingsService instance;

/// Whether the app should continue running in the tray when closed.
Future<bool> closeToTray() async {
final closeToTray = _storageService.getValue('closeToTray') as bool?;
return closeToTray ?? false;
}

Future<void> saveCloseToTray(bool value) async {
await _storageService.saveValue(key: 'closeToTray', value: value);
}

bool exitOnCopy() {
final shouldExit = _storageService.getValue('exitOnCopy') as bool?;
return shouldExit ?? false;
Expand Down
1 change: 1 addition & 0 deletions test/src/emoji/cubit/emoji_cubit_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ void main() {

settingsCubit = MockSettingsCubit();
when(settingsCubit.state).thenReturn(const SettingsState(
closeToTray: false,
exitOnCopy: false,
hotKeyEnabled: false,
showSystemTrayIcon: false,
Expand Down

0 comments on commit ecc0968

Please sign in to comment.