From 4c532d110cd80480f2b82df628396dfdebe92efa Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 9 May 2017 02:24:04 +0300 Subject: [PATCH 1/2] Add maximumDuration --- DesktopToast.Wpf/MainWindow.xaml.cs | 4 ++-- DesktopToast/ToastManager.cs | 23 ++++++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/DesktopToast.Wpf/MainWindow.xaml.cs b/DesktopToast.Wpf/MainWindow.xaml.cs index a2c0482..7400cb2 100644 --- a/DesktopToast.Wpf/MainWindow.xaml.cs +++ b/DesktopToast.Wpf/MainWindow.xaml.cs @@ -142,7 +142,7 @@ private async Task ShowToastAsync() ActivatorId = typeof(NotificationActivator).GUID // For Action Center of Windows 10 }; - var result = await ToastManager.ShowAsync(request); + var result = await ToastManager.ShowAsync(request, maximumDuration: TimeSpan.FromSeconds(10)); return result.ToString(); } @@ -165,7 +165,7 @@ private async Task ShowInteractiveToastAsync() ActivatorId = typeof(NotificationActivator).GUID }; - var result = await ToastManager.ShowAsync(request); + var result = await ToastManager.ShowAsync(request, maximumDuration: TimeSpan.FromSeconds(10)); return result.ToString(); } diff --git a/DesktopToast/ToastManager.cs b/DesktopToast/ToastManager.cs index feb9b20..9345876 100644 --- a/DesktopToast/ToastManager.cs +++ b/DesktopToast/ToastManager.cs @@ -19,8 +19,9 @@ public class ToastManager /// Shows a toast. /// /// Toast request + /// Optional maximum duration /// Result of showing a toast - public static async Task ShowAsync(ToastRequest request) + public static async Task ShowAsync(ToastRequest request, TimeSpan? maximumDuration = null) { if (request == null) throw new ArgumentNullException(nameof(request)); @@ -38,15 +39,16 @@ public static async Task ShowAsync(ToastRequest request) if (document == null) return ToastResult.Invalid; - return await ShowBaseAsync(document, request.AppId); + return await ShowBaseAsync(document, request.AppId, maximumDuration); } /// /// Shows a toast using JSON format. /// /// Toast request in JSON format + /// Optional maximum duration /// Result of showing a toast - public static async Task ShowAsync(string requestJson) + public static async Task ShowAsync(string requestJson, TimeSpan? maximumDuration = null) { ToastRequest request; try @@ -58,7 +60,7 @@ public static async Task ShowAsync(string requestJson) return ToastResult.Invalid; } - return await ShowAsync(request); + return await ShowAsync(request, maximumDuration); } /// @@ -66,8 +68,9 @@ public static async Task ShowAsync(string requestJson) /// /// Toast document /// AppUserModelID + /// Optional maximum duration /// Result of showing a toast - public static async Task ShowAsync(XmlDocument document, string appId) + public static async Task ShowAsync(XmlDocument document, string appId, TimeSpan? maximumDuration = null) { if (document == null) throw new ArgumentNullException(nameof(document)); @@ -78,7 +81,7 @@ public static async Task ShowAsync(XmlDocument document, string app if (!OsVersion.IsEightOrNewer) return ToastResult.Unavailable; - return await ShowBaseAsync(document, appId); + return await ShowBaseAsync(document, appId, maximumDuration); } #region Document @@ -331,11 +334,17 @@ private static async Task CheckInstallShortcut(ToastRequest request) /// /// Toast document /// AppUserModelID + /// Optional maximum duration /// Result of showing a toast - private static async Task ShowBaseAsync(XmlDocument document, string appId) + private static async Task ShowBaseAsync(XmlDocument document, string appId, TimeSpan? maximumDuration) { // Create a toast and prepare to handle toast events. var toast = new ToastNotification(document); + if (maximumDuration != null) + { + toast.ExpirationTime = DateTime.Now + maximumDuration; + } + var tcs = new TaskCompletionSource(); TypedEventHandler activated = (sender, e) => From bfb512e6c2359b2834f10e0f976761de4aea4c09 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Wed, 10 May 2017 22:43:16 +0300 Subject: [PATCH 2/2] Make MaximumDuration consistent with WatingDuration --- DesktopToast.Wpf/MainWindow.xaml.cs | 6 ++++-- DesktopToast/ToastManager.cs | 19 ++++++++----------- DesktopToast/ToastRequest.cs | 5 +++++ 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/DesktopToast.Wpf/MainWindow.xaml.cs b/DesktopToast.Wpf/MainWindow.xaml.cs index 7400cb2..647ddc3 100644 --- a/DesktopToast.Wpf/MainWindow.xaml.cs +++ b/DesktopToast.Wpf/MainWindow.xaml.cs @@ -138,11 +138,12 @@ private async Task ShowToastAsync() ToastLogoFilePath = string.Format("file:///{0}", Path.GetFullPath("Resources/toast128.png")), ShortcutFileName = "DesktopToast.Wpf.lnk", ShortcutTargetFilePath = Assembly.GetExecutingAssembly().Location, + MaximumDuration = TimeSpan.FromSeconds(10), AppId = "DesktopToast.Wpf", ActivatorId = typeof(NotificationActivator).GUID // For Action Center of Windows 10 }; - var result = await ToastManager.ShowAsync(request, maximumDuration: TimeSpan.FromSeconds(10)); + var result = await ToastManager.ShowAsync(request); return result.ToString(); } @@ -161,11 +162,12 @@ private async Task ShowInteractiveToastAsync() ToastXml = ComposeInteractiveToast(), ShortcutFileName = "DesktopToast.Wpf.lnk", ShortcutTargetFilePath = Assembly.GetExecutingAssembly().Location, + MaximumDuration = TimeSpan.FromSeconds(10), AppId = "DesktopToast.Wpf", ActivatorId = typeof(NotificationActivator).GUID }; - var result = await ToastManager.ShowAsync(request, maximumDuration: TimeSpan.FromSeconds(10)); + var result = await ToastManager.ShowAsync(request); return result.ToString(); } diff --git a/DesktopToast/ToastManager.cs b/DesktopToast/ToastManager.cs index 9345876..3ff04aa 100644 --- a/DesktopToast/ToastManager.cs +++ b/DesktopToast/ToastManager.cs @@ -19,9 +19,8 @@ public class ToastManager /// Shows a toast. /// /// Toast request - /// Optional maximum duration /// Result of showing a toast - public static async Task ShowAsync(ToastRequest request, TimeSpan? maximumDuration = null) + public static async Task ShowAsync(ToastRequest request) { if (request == null) throw new ArgumentNullException(nameof(request)); @@ -39,16 +38,15 @@ public static async Task ShowAsync(ToastRequest request, TimeSpan? if (document == null) return ToastResult.Invalid; - return await ShowBaseAsync(document, request.AppId, maximumDuration); + return await ShowBaseAsync(document, request.AppId, request.MaximumDuration); } /// /// Shows a toast using JSON format. /// /// Toast request in JSON format - /// Optional maximum duration /// Result of showing a toast - public static async Task ShowAsync(string requestJson, TimeSpan? maximumDuration = null) + public static async Task ShowAsync(string requestJson) { ToastRequest request; try @@ -60,7 +58,7 @@ public static async Task ShowAsync(string requestJson, TimeSpan? ma return ToastResult.Invalid; } - return await ShowAsync(request, maximumDuration); + return await ShowAsync(request); } /// @@ -68,9 +66,8 @@ public static async Task ShowAsync(string requestJson, TimeSpan? ma /// /// Toast document /// AppUserModelID - /// Optional maximum duration /// Result of showing a toast - public static async Task ShowAsync(XmlDocument document, string appId, TimeSpan? maximumDuration = null) + public static async Task ShowAsync(XmlDocument document, string appId) { if (document == null) throw new ArgumentNullException(nameof(document)); @@ -81,7 +78,7 @@ public static async Task ShowAsync(XmlDocument document, string app if (!OsVersion.IsEightOrNewer) return ToastResult.Unavailable; - return await ShowBaseAsync(document, appId, maximumDuration); + return await ShowBaseAsync(document, appId); } #region Document @@ -336,11 +333,11 @@ private static async Task CheckInstallShortcut(ToastRequest request) /// AppUserModelID /// Optional maximum duration /// Result of showing a toast - private static async Task ShowBaseAsync(XmlDocument document, string appId, TimeSpan? maximumDuration) + private static async Task ShowBaseAsync(XmlDocument document, string appId, TimeSpan maximumDuration = default(TimeSpan)) { // Create a toast and prepare to handle toast events. var toast = new ToastNotification(document); - if (maximumDuration != null) + if (maximumDuration != default(TimeSpan)) { toast.ExpirationTime = DateTime.Now + maximumDuration; } diff --git a/DesktopToast/ToastRequest.cs b/DesktopToast/ToastRequest.cs index 4ededbb..1aeae3a 100644 --- a/DesktopToast/ToastRequest.cs +++ b/DesktopToast/ToastRequest.cs @@ -148,6 +148,11 @@ public string ShortcutIconFilePath [DataMember] public TimeSpan WaitingDuration { get; set; } + /// + /// Maximum toast display duration (optional) + /// + public TimeSpan MaximumDuration { get; set; } + #endregion #region Internal Property