From 715b2c6876c9191cb5f60468bdbf4cc63f53b442 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Wed, 2 Feb 2022 12:18:08 -0800 Subject: [PATCH] windows notifications: switch from desktoats to win_toats --- lib/notification_manager.dart | 61 +++++++++---------- pubspec.lock | 14 ++--- pubspec.yaml | 2 +- .../flutter/generated_plugin_registrant.cc | 9 ++- windows/flutter/generated_plugins.cmake | 3 +- 5 files changed, 44 insertions(+), 45 deletions(-) diff --git a/lib/notification_manager.dart b/lib/notification_manager.dart index 91f1cb52..68f56f3e 100644 --- a/lib/notification_manager.dart +++ b/lib/notification_manager.dart @@ -1,7 +1,8 @@ +import 'dart:async'; import 'dart:io'; import 'package:cwtch/main.dart'; -import 'package:desktoasts/desktoasts.dart'; +import 'package:win_toast/win_toast.dart'; import 'package:desktop_notifications/desktop_notifications.dart'; import 'package:path/path.dart' as path; @@ -30,51 +31,43 @@ class LinuxNotificationsManager implements NotificationsManager { Future notify(String message) async { var iconPath = Uri.file(path.join(path.current, "cwtch.png")); - client.notify(message, appName: "cwtch", appIcon: iconPath.toString(), replacesId: this.previous_id).then((Notification value) => previous_id = value.id); + client + .notify(message, + appName: "cwtch", + appIcon: iconPath.toString(), + replacesId: this.previous_id) + .then((Notification value) => previous_id = value.id); } } // Windows Notification Manager uses https://pub.dev/packages/desktoasts to implement // windows notifications class WindowsNotificationManager implements NotificationsManager { - late ToastService service; bool active = false; + bool initialized = false; WindowsNotificationManager() { - service = new ToastService( - appName: 'cwtch', - companyName: 'Open Privacy Research Society', - productName: 'Cwtch', - ); - - service.stream.listen((event) { - // the user closed the notification of the OS timed it out - if (event is ToastDismissed) { - active = false; - } - // clicked - if (event is ToastActivated) { - active = false; - } - // if a supplied action was clicked - if (event is ToastInteracted) { - active = false; - } + scheduleMicrotask(() async { + initialized = await WinToast.instance().initialize( + appName: 'cwtch', + productName: 'Cwtch', + companyName: 'Open Privacy Research Society'); }); } Future notify(String message) async { - if (!globalAppState.focus) { + if (initialized && !globalAppState.focus) { if (!active) { - // One string of bold text on the first line (title), - // one string (subtitle) of regular text wrapped across the second and third lines. - Toast toast = new Toast( - type: ToastType.text02, - title: 'Cwtch', - subtitle: message, - ); - service.show(toast); + WinToast.instance().clear(); + final toast = await WinToast.instance() + .showToast(type: ToastType.text01, title: message); active = true; + toast?.eventStream.listen((event) { + if (event is ActivatedEvent) { + WinToast.instance().bringWindowToFront(); + } + active = false; + }); } } } @@ -89,13 +82,15 @@ NotificationsManager newDesktopNotificationsManager() { client.getCapabilities(); return LinuxNotificationsManager(client); } catch (e) { - EnvironmentConfig.debugLog("Attempted to access DBUS for notifications but failed. Switching off notifications."); + EnvironmentConfig.debugLog( + "Attempted to access DBUS for notifications but failed. Switching off notifications."); } } else if (Platform.isWindows) { try { return WindowsNotificationManager(); } catch (e) { - EnvironmentConfig.debugLog("Failed to create Windows desktoasts notification manager"); + EnvironmentConfig.debugLog( + "Failed to create Windows desktoasts notification manager"); } } return NullNotificationsManager(); diff --git a/pubspec.lock b/pubspec.lock index 1e1a4bec..0fa7e9d9 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -78,13 +78,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.5.2" - desktoasts: - dependency: "direct main" - description: - name: desktoasts - url: "https://pub.dartlang.org" - source: hosted - version: "0.0.2" desktop_notifications: dependency: "direct main" description: @@ -474,6 +467,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.2.4" + win_toast: + dependency: "direct main" + description: + name: win_toast + url: "https://pub.dartlang.org" + source: hosted + version: "0.0.2" window_manager: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index c4dc007b..e13164d9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -44,7 +44,7 @@ dependencies: file_picker: ^4.3.2 file_picker_desktop: ^1.1.0 url_launcher: ^6.0.12 - desktoasts: ^0.0.2 + win_toast: ^0.0.2 window_manager: ^0.1.4 dev_dependencies: diff --git a/windows/flutter/generated_plugin_registrant.cc b/windows/flutter/generated_plugin_registrant.cc index 6859cc41..0177b517 100644 --- a/windows/flutter/generated_plugin_registrant.cc +++ b/windows/flutter/generated_plugin_registrant.cc @@ -6,12 +6,15 @@ #include "generated_plugin_registrant.h" -#include #include +#include +#include void RegisterPlugins(flutter::PluginRegistry* registry) { - DesktoastsPluginRegisterWithRegistrar( - registry->GetRegistrarForPlugin("DesktoastsPlugin")); UrlLauncherWindowsRegisterWithRegistrar( registry->GetRegistrarForPlugin("UrlLauncherWindows")); + WinToastPluginRegisterWithRegistrar( + registry->GetRegistrarForPlugin("WinToastPlugin")); + WindowManagerPluginRegisterWithRegistrar( + registry->GetRegistrarForPlugin("WindowManagerPlugin")); } diff --git a/windows/flutter/generated_plugins.cmake b/windows/flutter/generated_plugins.cmake index cb499b22..21bba102 100644 --- a/windows/flutter/generated_plugins.cmake +++ b/windows/flutter/generated_plugins.cmake @@ -3,8 +3,9 @@ # list(APPEND FLUTTER_PLUGIN_LIST - desktoasts url_launcher_windows + win_toast + window_manager ) set(PLUGIN_BUNDLED_LIBRARIES)