Introduce application context

This commit is contained in:
Luis von der Eltz
2021-02-06 14:52:41 +01:00
parent 97a3efc2a6
commit fd461bb9d4
7 changed files with 114 additions and 29 deletions
@@ -0,0 +1,28 @@
using System.Diagnostics;
using System.IO;
namespace DesktopNotifications.FreeDesktop
{
/// <summary>
/// </summary>
public class FreeDesktopApplicationContext : ApplicationContext
{
private FreeDesktopApplicationContext(string name, string? appIcon) : base(name)
{
AppIcon = appIcon;
}
/// <summary>
/// </summary>
public string? AppIcon { get; }
public static FreeDesktopApplicationContext FromCurrentProcess(string? appIcon = null)
{
var mainModule = Process.GetCurrentProcess().MainModule;
return new FreeDesktopApplicationContext(
Path.GetFileNameWithoutExtension(mainModule.FileName),
appIcon
);
}
}
}
@@ -9,6 +9,7 @@ namespace DesktopNotifications.FreeDesktop
{
public class FreeDesktopNotificationManager : INotificationManager, IDisposable
{
private readonly FreeDesktopApplicationContext _appContext;
private const string NotificationsService = "org.freedesktop.Notifications";
private static readonly ObjectPath NotificationsPath = new ObjectPath("/org/freedesktop/Notifications");
@@ -19,8 +20,13 @@ namespace DesktopNotifications.FreeDesktop
private IFreeDesktopNotificationsProxy? _proxy;
public FreeDesktopNotificationManager()
/// <summary>
///
/// </summary>
/// <param name="appContext"></param>
public FreeDesktopNotificationManager(FreeDesktopApplicationContext? appContext = null)
{
_appContext = appContext ?? FreeDesktopApplicationContext.FromCurrentProcess();
_activeNotifications = new Dictionary<uint, Notification>();
}
@@ -65,9 +71,9 @@ namespace DesktopNotifications.FreeDesktop
var actions = GenerateActions(notification);
var id = await _proxy.NotifyAsync(
"MyApp",
_appContext.Name,
0,
string.Empty,
_appContext.AppIcon ?? string.Empty,
notification.Title ?? throw new ArgumentException(),
notification.Body ?? throw new ArgumentException(),
actions.ToArray(),
@@ -1,11 +1,10 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
namespace Example.Win32
namespace DesktopNotifications.Windows
{
// Modified from http://smdn.jp/programming/tips/createlnk/
// Originally from http://www.vbaccelerator.com/home/NET/Code/Libraries/Shell_Projects
@@ -0,0 +1,47 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
namespace DesktopNotifications.Windows
{
public class WindowsApplicationContext : ApplicationContext
{
private WindowsApplicationContext(string name, string appUserModelId) : base(name)
{
AppUserModelId = appUserModelId;
}
public string AppUserModelId { get; }
[DllImport("shell32.dll", SetLastError = true)]
private static extern void SetCurrentProcessExplicitAppUserModelID(
[MarshalAs(UnmanagedType.LPWStr)] string appId);
public static WindowsApplicationContext FromCurrentProcess(string? customName = null,
string? appUserModelId = null)
{
var aumid = appUserModelId ?? Guid.NewGuid().ToString();
SetCurrentProcessExplicitAppUserModelID(aumid);
var mainModule = Process.GetCurrentProcess().MainModule;
using var shortcut = new ShellLink
{
TargetPath = mainModule.FileName,
Arguments = string.Empty,
AppUserModelID = aumid
};
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var startMenuPath = Path.Combine(appData, @"Microsoft\Windows\Start Menu\Programs");
var appName = customName ?? Path.GetFileNameWithoutExtension(mainModule.FileName);
var shortcutFile = Path.Combine(startMenuPath, $"{appName}.lnk");
shortcut.Save(shortcutFile);
return new WindowsApplicationContext(appName, aumid);
}
}
}
@@ -10,6 +10,7 @@ namespace DesktopNotifications.Windows
{
public class WindowsNotificationManager : INotificationManager
{
private readonly WindowsApplicationContext _applicationContext;
private readonly Dictionary<ToastNotification, Notification> _notifications;
private readonly ToastNotifier _toastNotifier;
private string? _launchAction;
@@ -18,10 +19,11 @@ namespace DesktopNotifications.Windows
/// <summary>
/// </summary>
/// <param name="appId"></param>
public WindowsNotificationManager(string appId)
/// <param name="applicationContext"></param>
public WindowsNotificationManager(WindowsApplicationContext? applicationContext = null)
{
_toastNotifier = ToastNotificationManager.CreateToastNotifier(appId);
_applicationContext = applicationContext ?? WindowsApplicationContext.FromCurrentProcess();
_toastNotifier = ToastNotificationManager.CreateToastNotifier(_applicationContext.AppUserModelId);
_notifications = new Dictionary<ToastNotification, Notification>();
}
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace DesktopNotifications
{
/// <summary>
///
/// </summary>
public class ApplicationContext
{
public ApplicationContext(string name)
{
Name = name;
}
/// <summary>
///
/// </summary>
public string Name { get; }
}
}
+1 -21
View File
@@ -1,12 +1,9 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using DesktopNotifications;
using DesktopNotifications.FreeDesktop;
using DesktopNotifications.Windows;
using Example.Win32;
namespace Example
{
@@ -25,24 +22,7 @@ namespace Example
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
const string AppName = "DesktopNotificationsExample";
SetCurrentProcessExplicitAppUserModelID(AppName);
using var shortcut = new ShellLink
{
TargetPath = Process.GetCurrentProcess().MainModule.FileName,
Arguments = string.Empty,
AppUserModelID = AppName
};
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var startMenuPath = Path.Combine(appData, @"Microsoft\Windows\Start Menu\Programs");
var shortcutFile = Path.Combine(startMenuPath, $"{AppName}.lnk");
shortcut.Save(shortcutFile);
return new WindowsNotificationManager(AppName);
return new WindowsNotificationManager();
}
throw new PlatformNotSupportedException();