From 898470ed23ac5230d088367c61b28eea7ec2d377 Mon Sep 17 00:00:00 2001 From: LAGonauta Date: Wed, 24 Feb 2021 06:49:49 -0300 Subject: [PATCH] Add failed and dimissed actions --- .../api/include/gobject/winrt-enums.h | 18 ++-- .../gobject/winrt-toast-notification.h | 11 ++- .../api/src/gobject/winrt-enums.cpp | 8 +- .../src/gobject/winrt-toast-notification.cpp | 85 ++++++++++++++++++- .../vapi/winrt_windows_ui_notifications.vapi | 24 +++++- 5 files changed, 122 insertions(+), 24 deletions(-) diff --git a/plugins/windows-notification/api/include/gobject/winrt-enums.h b/plugins/windows-notification/api/include/gobject/winrt-enums.h index 2e839e50..65418b69 100644 --- a/plugins/windows-notification/api/include/gobject/winrt-enums.h +++ b/plugins/windows-notification/api/include/gobject/winrt-enums.h @@ -5,25 +5,25 @@ G_BEGIN_DECLS -#define WINRT_TYPE_DISMISSED_REASON (winrt_dismissed_reason_get_type ()) +#define WINRT_TYPE_DISMISSED_REASON (winrt_windows_ui_notifications_toast_dismissal_reason_get_type ()) /** * WinrtDismissedReason: - * @WINRT_DISMISSED_REASON_ACTIVATED: Notification was activated, clicked or through + * @WINRT_WINDOWS_UI_NOTIFICATIONS_TOAST_DISMISSAL_REASON_ACTIVATED: Notification was activated, clicked or through * a button - * @WINRT_DISMISSED_REASON_APPLICATION_HIDDEN: Application was hidden - * @WINRT_DISMISSED_REASON_TIMED_OUT: Notification timed out + * @WINRT_WINDOWS_UI_NOTIFICATIONS_TOAST_DISMISSAL_REASON_APPLICATION_HIDDEN: Application was hidden + * @WINRT_WINDOWS_UI_NOTIFICATIONS_TOAST_DISMISSAL_REASON_TIMED_OUT: Notification timed out * * Reasons for a notification dismissal * */ typedef enum { - WINRT_DISMISSED_REASON_ACTIVATED, - WINRT_DISMISSED_REASON_APPLICATION_HIDDEN, - WINRT_DISMISSED_REASON_TIMED_OUT, -} WinrtDismissedReason; + WINRT_WINDOWS_UI_NOTIFICATIONS_TOAST_DISMISSAL_REASON_ACTIVATED, + WINRT_WINDOWS_UI_NOTIFICATIONS_TOAST_DISMISSAL_REASON_APPLICATION_HIDDEN, + WINRT_WINDOWS_UI_NOTIFICATIONS_TOAST_DISMISSAL_REASON_TIMED_OUT, +} winrt_Windows_UI_Notifications_Toast_Dismissal_Reason; -GType winrt_dismissed_reason_get_type (void); +GType winrt_windows_ui_notifications_toast_dismissal_reason_get_type (void); G_END_DECLS diff --git a/plugins/windows-notification/api/include/gobject/winrt-toast-notification.h b/plugins/windows-notification/api/include/gobject/winrt-toast-notification.h index 92bf1a7f..d90bde66 100644 --- a/plugins/windows-notification/api/include/gobject/winrt-toast-notification.h +++ b/plugins/windows-notification/api/include/gobject/winrt-toast-notification.h @@ -25,9 +25,9 @@ extern "C" { #endif -typedef void(*NotificationCallbackSimple)(void* userdata); +typedef void(*NotificationCallbackFailed)(void* userdata); typedef void(*NotificationCallbackActivated)(const gchar* arguments, gchar** userInput, gint count, void* userdata); -//typedef void(*NotificationCallbackDismissed)(WinrtDismissedReason reason, void* userdata); +typedef void(*NotificationCallbackDismissed)(winrt_Windows_UI_Notifications_Toast_Dismissal_Reason reason, void* userdata); winrtWindowsUINotificationsToastNotification* winrt_windows_ui_notifications_toast_notification_new(const gchar* doc); @@ -43,8 +43,11 @@ gchar* winrt_windows_ui_notifications_toast_notification_get_Group(winrtWindowsU winrtEventToken* winrt_windows_ui_notifications_toast_notification_Activated(winrtWindowsUINotificationsToastNotification* self, NotificationCallbackActivated callback, void* context, void(*free)(void*)); void winrt_windows_ui_notifications_toast_notification_RemoveActivated(winrtWindowsUINotificationsToastNotification* self, winrtEventToken* token); -winrtEventToken* winrt_windows_ui_notifications_toast_notification_Failed(winrtWindowsUINotificationsToastNotification* self, NotificationCallbackSimple callback, void* context, void(*free)(void*)); -//winrtEventToken* winrt_windows_ui_notifications_toast_notification_Dismissed(winrtWindowsUINotificationsToastNotification* self, NotificationCallbackDismissed callback, void* context, void(*free)(void*)); +winrtEventToken* winrt_windows_ui_notifications_toast_notification_Failed(winrtWindowsUINotificationsToastNotification* self, NotificationCallbackFailed callback, void* context, void(*free)(void*)); +void winrt_windows_ui_notifications_toast_notification_RemoveFailed(winrtWindowsUINotificationsToastNotification* self, winrtEventToken* token); + +winrtEventToken* winrt_windows_ui_notifications_toast_notification_Dismissed(winrtWindowsUINotificationsToastNotification* self, NotificationCallbackDismissed callback, void* context, void(*free)(void*)); +void winrt_windows_ui_notifications_toast_notification_RemoveDismissed(winrtWindowsUINotificationsToastNotification* self, winrtEventToken* token); #ifdef __cplusplus } diff --git a/plugins/windows-notification/api/src/gobject/winrt-enums.cpp b/plugins/windows-notification/api/src/gobject/winrt-enums.cpp index 9038c2d8..88c505f3 100644 --- a/plugins/windows-notification/api/src/gobject/winrt-enums.cpp +++ b/plugins/windows-notification/api/src/gobject/winrt-enums.cpp @@ -20,7 +20,7 @@ type_name ## _get_type (void) \ return g_define_id__volatile; \ } -WINRT_GLIB_DEFINE_ENUM_TYPE (WinrtDismissedReason, winrt_dismissed_reason, - WINRT_GLIB_DEFINE_ENUM_VALUE (WINRT_DISMISSED_REASON_ACTIVATED, "activated") - WINRT_GLIB_DEFINE_ENUM_VALUE (WINRT_DISMISSED_REASON_APPLICATION_HIDDEN, "application-hidden") - WINRT_GLIB_DEFINE_ENUM_VALUE (WINRT_DISMISSED_REASON_TIMED_OUT, "timed-out")) +WINRT_GLIB_DEFINE_ENUM_TYPE (winrt_Windows_UI_Notifications_Toast_Dismissal_Reason, winrt_windows_ui_notifications_toast_dismissal_reason, + WINRT_GLIB_DEFINE_ENUM_VALUE (WINRT_WINDOWS_UI_NOTIFICATIONS_TOAST_DISMISSAL_REASON_ACTIVATED, "activated") + WINRT_GLIB_DEFINE_ENUM_VALUE (WINRT_WINDOWS_UI_NOTIFICATIONS_TOAST_DISMISSAL_REASON_APPLICATION_HIDDEN, "application-hidden") + WINRT_GLIB_DEFINE_ENUM_VALUE (WINRT_WINDOWS_UI_NOTIFICATIONS_TOAST_DISMISSAL_REASON_TIMED_OUT, "timed-out")) diff --git a/plugins/windows-notification/api/src/gobject/winrt-toast-notification.cpp b/plugins/windows-notification/api/src/gobject/winrt-toast-notification.cpp index ffaa9326..9d5173e1 100644 --- a/plugins/windows-notification/api/src/gobject/winrt-toast-notification.cpp +++ b/plugins/windows-notification/api/src/gobject/winrt-toast-notification.cpp @@ -60,8 +60,8 @@ struct _winrtWindowsUINotificationsToastNotificationPrivate winrt::Windows::UI::Notifications::ToastNotification data; std::list>> activated; - std::list>> failed; - std::list>> dismissed; + std::list>> failed; + std::list>> dismissed; }; typedef struct @@ -251,7 +251,6 @@ winrtEventToken* winrt_windows_ui_notifications_toast_notification_Activated(win } } - std::cout << "Notification activated!" << std::endl; callback(wsview_to_char(arguments), nullptr /* user_input */ , 0 /* user_input.size() */, context); }); auto new_token = winrt_event_token_new_from_token(&token); @@ -261,6 +260,46 @@ winrtEventToken* winrt_windows_ui_notifications_toast_notification_Activated(win return new_token; } +winrtEventToken* winrt_windows_ui_notifications_toast_notification_Failed(winrtWindowsUINotificationsToastNotification* self, NotificationCallbackFailed callback, void* context, void(*free)(void*)) +{ + g_return_val_if_fail (WINRT_IS_WINDOWS_UI_NOTIFICATIONS_TOAST_NOTIFICATION (self), NULL); + g_return_val_if_fail (callback != nullptr && context != nullptr && free != nullptr, NULL); + + winrtWindowsUINotificationsToastNotificationPrivate* priv = WINRT_WINDOWS_UI_NOTIFICATION_TOAST_NOTIFICATION_GET_PRIVATE(self); + + auto token = priv->notification->data.Failed([=](auto sender, auto toastFailedEventArgs) + { + callback(context); + }); + + auto new_token = winrt_event_token_new_from_token(&token); + g_object_ref(new_token); + + priv->notification->failed.push_back(std::make_shared>(callback, context, free, new_token)); + return new_token; +} + +winrtEventToken* winrt_windows_ui_notifications_toast_notification_Dismissed(winrtWindowsUINotificationsToastNotification* self, NotificationCallbackDismissed callback, void* context, void(*free)(void*)) +{ + g_return_val_if_fail (WINRT_IS_WINDOWS_UI_NOTIFICATIONS_TOAST_NOTIFICATION (self), NULL); + g_return_val_if_fail (callback != nullptr && context != nullptr && free != nullptr, NULL); + + winrtWindowsUINotificationsToastNotificationPrivate* priv = WINRT_WINDOWS_UI_NOTIFICATION_TOAST_NOTIFICATION_GET_PRIVATE(self); + + auto token = priv->notification->data.Dismissed([=](auto sender, winrt::Windows::UI::Notifications::ToastDismissedEventArgs dismissed) + { + auto reason = dismissed.Reason(); + callback(static_cast(reason), context); + }); + + auto new_token = winrt_event_token_new_from_token(&token); + g_object_ref(new_token); + + priv->notification->dismissed.push_back(std::make_shared>(callback, context, free, new_token)); + return new_token; +} + +// TODO: refactor `Remove{Activated,Failed,Dismissed}` methods into one to deduplicate code void winrt_windows_ui_notifications_toast_notification_RemoveActivated(winrtWindowsUINotificationsToastNotification* self, winrtEventToken* token) { g_return_if_fail (WINRT_IS_WINDOWS_UI_NOTIFICATIONS_TOAST_NOTIFICATION (self)); @@ -279,4 +318,44 @@ void winrt_windows_ui_notifications_toast_notification_RemoveActivated(winrtWind } return false; }); +} + +void winrt_windows_ui_notifications_toast_notification_RemoveFailed(winrtWindowsUINotificationsToastNotification* self, winrtEventToken* token) +{ + g_return_if_fail (WINRT_IS_WINDOWS_UI_NOTIFICATIONS_TOAST_NOTIFICATION (self)); + + winrtWindowsUINotificationsToastNotificationPrivate* priv = WINRT_WINDOWS_UI_NOTIFICATION_TOAST_NOTIFICATION_GET_PRIVATE(self); + + priv->notification->failed.remove_if([&](const auto& callback) { + if (winrt_event_token_get_value(token) == winrt_event_token_get_value(callback->token)) + { + if (winrt_event_token_operator_bool(callback->token)) + { + priv->notification->data.Failed(*winrt_event_token_get_internal(callback->token)); + } + g_object_unref(callback->token); + return true; + } + return false; + }); +} + +void winrt_windows_ui_notifications_toast_notification_RemoveDismissed(winrtWindowsUINotificationsToastNotification* self, winrtEventToken* token) +{ + g_return_if_fail (WINRT_IS_WINDOWS_UI_NOTIFICATIONS_TOAST_NOTIFICATION (self)); + + winrtWindowsUINotificationsToastNotificationPrivate* priv = WINRT_WINDOWS_UI_NOTIFICATION_TOAST_NOTIFICATION_GET_PRIVATE(self); + + priv->notification->dismissed.remove_if([&](const auto& callback) { + if (winrt_event_token_get_value(token) == winrt_event_token_get_value(callback->token)) + { + if (winrt_event_token_operator_bool(callback->token)) + { + priv->notification->data.Dismissed(*winrt_event_token_get_internal(callback->token)); + } + g_object_unref(callback->token); + return true; + } + return false; + }); } \ No newline at end of file diff --git a/plugins/windows-notification/vapi/winrt_windows_ui_notifications.vapi b/plugins/windows-notification/vapi/winrt_windows_ui_notifications.vapi index 7e07ea36..ccd0c70a 100644 --- a/plugins/windows-notification/vapi/winrt_windows_ui_notifications.vapi +++ b/plugins/windows-notification/vapi/winrt_windows_ui_notifications.vapi @@ -2,14 +2,23 @@ using winrt; [CCode (cheader_filename = "gobject/winrt-glib.h")] namespace winrt.Windows.UI.Notifications { - [CCode (cname = "NotificationCallbackSimple", has_target = true)] - public delegate void NotificationCallbackSimple(); + + [CCode (cname = "winrt_Windows_UI_Notifications_Toast_Dismissal_Reason", cprefix = "WINRT_WINDOWS_UI_NOTIFICATIONS_TOAST_DISMISSAL_REASON_", type_id = "winrt_windows_ui_notifications_toast_dismissal_reason_get_type ()")] + public enum ToastDismissalReason + { + Activated, + ApplicationHidden, + TimedOut + } + + [CCode (cname = "NotificationCallbackFailed", has_target = true)] + public delegate void NotificationCallbackFailed(); [CCode (cname = "NotificationCallbackActivated", has_target = true)] public delegate void NotificationCallbackActivated(string? arguments, string[]? userInput); - // [CCode (cname = "Notification_Callback_Dismissed", has_target = true)] - // public delegate void NotificationCallbackDismissed(DismissedReason reason); + [CCode (cname = "NotificationCallbackDismissed", has_target = true)] + public delegate void NotificationCallbackDismissed(ToastDismissalReason reason); [CCode (type_id = "winrt_windows_ui_notifications_toast_notification_get_type ()")] public class ToastNotification : GLib.Object { @@ -17,8 +26,15 @@ namespace winrt.Windows.UI.Notifications { public bool ExpiresOnReboot { get; set; } public string Tag { get; set; } // TODO: check if valac is cleaning this string public string Group { get; set; } + public EventToken Activated(owned NotificationCallbackActivated handler); public void RemoveActivated(EventToken token); + + public EventToken Failed(owned NotificationCallbackFailed handler); + public void RemoveFailed(EventToken token); + + public EventToken Dismissed(owned NotificationCallbackDismissed handler); + public void RemoveDismissed(EventToken token); } [CCode (type_id = "winrt_windows_ui_notifications_toast_notifier_get_type ()")]