Skip to content

Commit

Permalink
use name for tray icon when setting badge
Browse files Browse the repository at this point in the history
  • Loading branch information
Merrit committed Apr 9, 2024
1 parent 6b7b53d commit bb838f3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 19 deletions.
37 changes: 37 additions & 0 deletions lib/src/core/constants.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'package:flutter/foundation.dart';
import 'package:helpers/helpers.dart';

const String kDonateUrl = 'https://merritt.codes/support/';
const String kPackageId = 'codes.merritt.adventurelist';
const String kRepoUrl = 'https://github.com/Merrit/adventure_list';
Expand Down Expand Up @@ -35,4 +38,38 @@ abstract class AppIcons {
/// Symbolic icon with a red dot indicating a notification, as an ICO.
static const String windowsSymbolicWithNotificationBadge =
'$path/$kPackageId-symbolic-with-notification-badge.ico';

/// Returns the appropriate icon path (or icon name) based on the current platform.
static String platformSpecific({
required bool symbolic,
bool withNotificationBadge = false,
}) {
if (runningInFlatpak() || runningInSnap()) {
// When running in a sandboxed environment the icon must be specified by
// the icon's name, not the path.
return kPackageId;
}

return defaultTargetPlatform.isWindows
? getWindowsIcon(symbolic, withNotificationBadge)
: getLinuxIcon(symbolic, withNotificationBadge);
}

static String getWindowsIcon(bool symbolic, bool withNotificationBadge) {
if (symbolic) {
return withNotificationBadge
? windowsSymbolicWithNotificationBadge
: windowsSymbolic;
} else {
return withNotificationBadge ? windowsWithNotificationBadge : windows;
}
}

static String getLinuxIcon(bool symbolic, bool withNotificationBadge) {
if (symbolic) {
return withNotificationBadge ? linuxSymbolicWithNotificationBadge : linuxSymbolic;
} else {
return withNotificationBadge ? linuxWithNotificationBadge : linux;
}
}
}
26 changes: 18 additions & 8 deletions lib/src/notifications/cubit/notifications_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,16 @@ class NotificationsCubit extends Cubit<NotificationsState> {

/// Set the notification badge on the Linux system tray.
Future<void> _setNotificationBadgeLinuxSystemTray(int count) async {
final icon = (count > 0)
? AppIcons.linuxSymbolicWithNotificationBadge
: AppIcons.linuxSymbolic;
// TODO: This is now identical to the Windows method. Refactor to share code.

await SystemTrayManager.instance.setIcon(icon);
final bool withNotificationBadge = count > 0;

final String iconPath = AppIcons.platformSpecific(
symbolic: true,
withNotificationBadge: withNotificationBadge,
);

await SystemTrayManager.instance.setIcon(iconPath);
}

/// Set the notification badge on Windows.
Expand All @@ -560,11 +565,16 @@ class NotificationsCubit extends Cubit<NotificationsState> {

/// Set the notification badge on the Windows system tray.
Future<void> _setNotificationBadgeWindowsSystemTray(int count) async {
final icon = (count > 0)
? AppIcons.windowsSymbolicWithNotificationBadge
: AppIcons.windowsSymbolic;
// TODO: This is now identical to the Linux method. Refactor to share code.

final bool withNotificationBadge = count > 0;

final String iconPath = AppIcons.platformSpecific(
symbolic: true,
withNotificationBadge: withNotificationBadge,
);

await SystemTrayManager.instance.setIcon(icon);
await SystemTrayManager.instance.setIcon(iconPath);
}
}

Expand Down
16 changes: 5 additions & 11 deletions lib/src/system_tray/system_tray_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,11 @@ class SystemTrayManager {
}

Future<void> initialize() async {
final String iconPath;

if (runningInFlatpak() || runningInSnap()) {
// When running in Flatpak the icon must be specified by the icon's name, not the path.
iconPath = kPackageId;
} else {
iconPath = (defaultTargetPlatform.isWindows) //
? AppIcons.windowsSymbolic
: AppIcons.linuxSymbolic;
}
final String iconPath = AppIcons.platformSpecific(
symbolic: true,
withNotificationBadge: false,
);

log.t('Setting system tray icon to $iconPath');
await trayManager.setIcon(iconPath);

final Menu menu = Menu(
Expand All @@ -45,6 +38,7 @@ class SystemTrayManager {

/// Sets the system tray icon.
Future<void> setIcon(String iconPath) async {
log.t('Setting system tray icon to $iconPath');
await trayManager.setIcon(iconPath);
}

Expand Down

0 comments on commit bb838f3

Please sign in to comment.