windows notifications: switch from desktoats to win_toats

This commit is contained in:
Dan Ballard 2022-02-02 12:18:08 -08:00
parent ca03ddbc53
commit 715b2c6876
5 changed files with 44 additions and 45 deletions

View File

@ -1,7 +1,8 @@
import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:cwtch/main.dart'; 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:desktop_notifications/desktop_notifications.dart';
import 'package:path/path.dart' as path; import 'package:path/path.dart' as path;
@ -30,51 +31,43 @@ class LinuxNotificationsManager implements NotificationsManager {
Future<void> notify(String message) async { Future<void> notify(String message) async {
var iconPath = Uri.file(path.join(path.current, "cwtch.png")); 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 Notification Manager uses https://pub.dev/packages/desktoasts to implement
// windows notifications // windows notifications
class WindowsNotificationManager implements NotificationsManager { class WindowsNotificationManager implements NotificationsManager {
late ToastService service;
bool active = false; bool active = false;
bool initialized = false;
WindowsNotificationManager() { WindowsNotificationManager() {
service = new ToastService( scheduleMicrotask(() async {
appName: 'cwtch', initialized = await WinToast.instance().initialize(
companyName: 'Open Privacy Research Society', appName: 'cwtch',
productName: 'Cwtch', productName: 'Cwtch',
); companyName: 'Open Privacy Research Society');
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;
}
}); });
} }
Future<void> notify(String message) async { Future<void> notify(String message) async {
if (!globalAppState.focus) { if (initialized && !globalAppState.focus) {
if (!active) { if (!active) {
// One string of bold text on the first line (title), WinToast.instance().clear();
// one string (subtitle) of regular text wrapped across the second and third lines. final toast = await WinToast.instance()
Toast toast = new Toast( .showToast(type: ToastType.text01, title: message);
type: ToastType.text02,
title: 'Cwtch',
subtitle: message,
);
service.show(toast);
active = true; active = true;
toast?.eventStream.listen((event) {
if (event is ActivatedEvent) {
WinToast.instance().bringWindowToFront();
}
active = false;
});
} }
} }
} }
@ -89,13 +82,15 @@ NotificationsManager newDesktopNotificationsManager() {
client.getCapabilities(); client.getCapabilities();
return LinuxNotificationsManager(client); return LinuxNotificationsManager(client);
} catch (e) { } 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) { } else if (Platform.isWindows) {
try { try {
return WindowsNotificationManager(); return WindowsNotificationManager();
} catch (e) { } catch (e) {
EnvironmentConfig.debugLog("Failed to create Windows desktoasts notification manager"); EnvironmentConfig.debugLog(
"Failed to create Windows desktoasts notification manager");
} }
} }
return NullNotificationsManager(); return NullNotificationsManager();

View File

@ -78,13 +78,6 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.5.2" version: "0.5.2"
desktoasts:
dependency: "direct main"
description:
name: desktoasts
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.2"
desktop_notifications: desktop_notifications:
dependency: "direct main" dependency: "direct main"
description: description:
@ -474,6 +467,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.2.4" 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: window_manager:
dependency: "direct main" dependency: "direct main"
description: description:

View File

@ -44,7 +44,7 @@ dependencies:
file_picker: ^4.3.2 file_picker: ^4.3.2
file_picker_desktop: ^1.1.0 file_picker_desktop: ^1.1.0
url_launcher: ^6.0.12 url_launcher: ^6.0.12
desktoasts: ^0.0.2 win_toast: ^0.0.2
window_manager: ^0.1.4 window_manager: ^0.1.4
dev_dependencies: dev_dependencies:

View File

@ -6,12 +6,15 @@
#include "generated_plugin_registrant.h" #include "generated_plugin_registrant.h"
#include <desktoasts/desktoasts_plugin.h>
#include <url_launcher_windows/url_launcher_windows.h> #include <url_launcher_windows/url_launcher_windows.h>
#include <win_toast/win_toast_plugin.h>
#include <window_manager/window_manager_plugin.h>
void RegisterPlugins(flutter::PluginRegistry* registry) { void RegisterPlugins(flutter::PluginRegistry* registry) {
DesktoastsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("DesktoastsPlugin"));
UrlLauncherWindowsRegisterWithRegistrar( UrlLauncherWindowsRegisterWithRegistrar(
registry->GetRegistrarForPlugin("UrlLauncherWindows")); registry->GetRegistrarForPlugin("UrlLauncherWindows"));
WinToastPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("WinToastPlugin"));
WindowManagerPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("WindowManagerPlugin"));
} }

View File

@ -3,8 +3,9 @@
# #
list(APPEND FLUTTER_PLUGIN_LIST list(APPEND FLUTTER_PLUGIN_LIST
desktoasts
url_launcher_windows url_launcher_windows
win_toast
window_manager
) )
set(PLUGIN_BUNDLED_LIBRARIES) set(PLUGIN_BUNDLED_LIBRARIES)