Skip to content

Commit

Permalink
Reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
pr8x committed Jun 22, 2023
1 parent c9440df commit 28e0eee
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 77 deletions.
8 changes: 4 additions & 4 deletions DesktopNotifications.Apple/AppleNotificationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ namespace DesktopNotifications.Apple
{
public class AppleNotificationManager : INotificationManager
{
[DllImport("DesktopNotifications.Apple.Native.dylib")]
private static extern void ShowNotification();

public void Dispose()
{
}
Expand Down Expand Up @@ -42,5 +39,8 @@ public Task HideNotification(Notification notification)
{
return Task.CompletedTask;
}

[DllImport("DesktopNotifications.Apple.Native.dylib")]
private static extern void ShowNotification();
}
}
}
11 changes: 4 additions & 7 deletions DesktopNotifications.Avalonia/AppBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
namespace DesktopNotifications.Avalonia
{
/// <summary>
/// Extensions for <see cref="AppBuilderBase{TAppBuilder}"/>
/// Extensions for <see cref="AppBuilderBase{TAppBuilder}" />
/// </summary>
public static class AppBuilderExtensions
{
/// <summary>
/// Setups the <see cref="INotificationManager"/> for the current platform and
/// binds it to the service locator (<see cref="AvaloniaLocator"/>).
/// Setups the <see cref="INotificationManager" /> for the current platform and
/// binds it to the service locator (<see cref="AvaloniaLocator" />).
/// </summary>
/// <typeparam name="TAppBuilder"></typeparam>
/// <param name="builder"></param>
Expand Down Expand Up @@ -52,10 +52,7 @@ public static TAppBuilder SetupDesktopNotifications<TAppBuilder>(this TAppBuilde
{
if (b.Instance.ApplicationLifetime is IControlledApplicationLifetime lifetime)
{
lifetime.Exit += (s, e) =>
{
manager.Dispose();
};
lifetime.Exit += (s, e) => { manager.Dispose(); };
}
});

Expand Down
22 changes: 10 additions & 12 deletions DesktopNotifications.FreeDesktop/FreeDesktopNotificationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@
using System.Linq;
using System.Threading.Tasks;
using Tmds.DBus;
using static DesktopNotifications.Extensions;

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");
private readonly Dictionary<uint, Notification> _activeNotifications;
private readonly FreeDesktopApplicationContext _appContext;
private Connection? _connection;
private IDisposable? _notificationActionSubscription;
private IDisposable? _notificationCloseSubscription;

private IFreeDesktopNotificationsProxy? _proxy;

/// <summary>
///
/// </summary>
/// <param name="appContext"></param>
public FreeDesktopNotificationManager(FreeDesktopApplicationContext? appContext = null)
Expand Down Expand Up @@ -88,14 +86,6 @@ public async Task ShowNotification(Notification notification, DateTimeOffset? ex
_activeNotifications[id] = notification;
}

private void CheckConnection()
{
if (_connection == null || _proxy == null)
{
throw new InvalidOperationException("Not connected. Call Initialize() first.");
}
}

public async Task HideNotification(Notification notification)
{
CheckConnection();
Expand Down Expand Up @@ -125,6 +115,14 @@ public async Task ScheduleNotification(
await ShowNotification(notification, expirationTime);
}

private void CheckConnection()
{
if (_connection == null || _proxy == null)
{
throw new InvalidOperationException("Not connected. Call Initialize() first.");
}
}

private static IEnumerable<string> GenerateActions(Notification notification)
{
foreach (var (title, actionId) in notification.Buttons)
Expand Down Expand Up @@ -182,4 +180,4 @@ private void OnNotificationActionInvoked((uint id, string actionKey) @event)
new NotificationActivatedEventArgs(notification, @event.actionKey));
}
}
}
}
10 changes: 6 additions & 4 deletions DesktopNotifications.FreeDesktop/FreeDesktopNotificationProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ namespace DesktopNotifications.FreeDesktop
[DBusInterface("org.freedesktop.Notifications")]
internal interface IFreeDesktopNotificationsProxy : IDBusObject
{
Task<uint> NotifyAsync(string appName, uint replacesId, string appIcon, string summary, string body, string[] actions, IDictionary<string, object> hints, int expireTimeout);
Task<uint> NotifyAsync(string appName, uint replacesId, string appIcon, string summary, string body,
string[] actions, IDictionary<string, object> hints, int expireTimeout);

Task CloseNotificationAsync(uint id);

Task<string[]> GetCapabilitiesAsync();

Task<(string name, string vendor, string version, string spec_version)> GetServerInformationAsync();

Task<IDisposable> WatchNotificationClosedAsync(Action<(uint id, uint reason)> handler, Action<Exception> onError = null);
Task<IDisposable> WatchNotificationClosedAsync(Action<(uint id, uint reason)> handler,
Action<Exception> onError = null);

Task<IDisposable> WatchActionInvokedAsync(Action<(uint id, string action_key)> handler, Action<Exception> onError = null);
Task<IDisposable> WatchActionInvokedAsync(Action<(uint id, string action_key)> handler,
Action<Exception> onError = null);
}

}
37 changes: 16 additions & 21 deletions DesktopNotifications.Windows/ShellLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,23 @@ public class ShellLink : IDisposable
[Guid("000214F9-0000-0000-C000-000000000046")]
private interface IShellLinkW
{
uint GetPath([Out] [MarshalAs(UnmanagedType.LPWStr)]
StringBuilder pszFile,
uint GetPath([Out] [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile,
int cchMaxPath, ref WIN32_FIND_DATAW pfd, uint fFlags);

uint GetIDList(out IntPtr ppidl);
uint SetIDList(IntPtr pidl);

uint GetDescription([Out] [MarshalAs(UnmanagedType.LPWStr)]
StringBuilder pszName,
uint GetDescription([Out] [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName,
int cchMaxName);

uint SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);

uint GetWorkingDirectory([Out] [MarshalAs(UnmanagedType.LPWStr)]
StringBuilder pszDir,
uint GetWorkingDirectory([Out] [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir,
int cchMaxPath);

uint SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);

uint GetArguments([Out] [MarshalAs(UnmanagedType.LPWStr)]
StringBuilder pszArgs,
uint GetArguments([Out] [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs,
int cchMaxPath);

uint SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
Expand All @@ -49,8 +45,7 @@ uint GetArguments([Out] [MarshalAs(UnmanagedType.LPWStr)]
uint GetShowCmd(out int piShowCmd);
uint SetShowCmd(int iShowCmd);

uint GetIconLocation([Out] [MarshalAs(UnmanagedType.LPWStr)]
StringBuilder pszIconPath,
uint GetIconLocation([Out] [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath,
int cchIconPath, out int piIcon);

uint SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
Expand Down Expand Up @@ -164,14 +159,14 @@ private sealed class PropVariant : IDisposable
// Value type (System.Runtime.InteropServices.VarEnum)
public VarEnum VarType
{
get => (VarEnum) valueType;
set => valueType = (ushort) value;
get => (VarEnum)valueType;
set => valueType = (ushort)value;
}

// Whether value is empty or null
public bool IsNullOrEmpty =>
valueType == (ushort) VarEnum.VT_EMPTY ||
valueType == (ushort) VarEnum.VT_NULL;
valueType == (ushort)VarEnum.VT_EMPTY ||
valueType == (ushort)VarEnum.VT_NULL;

// Value (only for string value)
public string? Value => Marshal.PtrToStringUni(ptr);
Expand All @@ -192,7 +187,7 @@ public PropVariant(string value)
throw new ArgumentException("Failed to set value.");
}

valueType = (ushort) VarEnum.VT_LPWSTR;
valueType = (ushort)VarEnum.VT_LPWSTR;
ptr = Marshal.StringToCoTaskMemUni(value);
}

Expand Down Expand Up @@ -290,7 +285,7 @@ public string TargetPath
get
{
// No limitation to length of buffer string in the case of Unicode though.
StringBuilder targetPath = new StringBuilder(MAX_PATH);
var targetPath = new StringBuilder(MAX_PATH);

var data = new WIN32_FIND_DATAW();

Expand All @@ -307,7 +302,7 @@ public string Arguments
get
{
// No limitation to length of buffer string in the case of Unicode though.
StringBuilder arguments = new StringBuilder(INFOTIPSIZE);
var arguments = new StringBuilder(INFOTIPSIZE);

VerifySucceeded(shellLinkW!.GetArguments(arguments, arguments.Capacity));

Expand All @@ -321,7 +316,7 @@ public string AppUserModelID
{
get
{
using (PropVariant pv = new PropVariant())
using (var pv = new PropVariant())
{
VerifySucceeded(PropertyStore.GetValue(AppUserModelIDKey, pv));

Expand All @@ -335,7 +330,7 @@ public string AppUserModelID
}
set
{
using (PropVariant pv = new PropVariant(value))
using (var pv = new PropVariant(value))
{
VerifySucceeded(PropertyStore.SetValue(AppUserModelIDKey, pv));
VerifySucceeded(PropertyStore.Commit());
Expand All @@ -357,7 +352,7 @@ public ShellLink(string? file)
{
try
{
shellLinkW = (IShellLinkW) new CShellLink();
shellLinkW = (IShellLinkW)new CShellLink();
}
catch
{
Expand Down Expand Up @@ -402,7 +397,7 @@ protected virtual void Dispose(bool disposing)
// Save shortcut file.
public void Save()
{
string file = ShortcutFile;
var file = ShortcutFile;

if (file == null)
{
Expand Down
14 changes: 6 additions & 8 deletions DesktopNotifications.Windows/WindowsNotificationManager.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.UI.Notifications;
using XmlDocument = Windows.Data.Xml.Dom.XmlDocument;
using System.Linq;

#if NETSTANDARD
#if NETSTANDARD
using System.IO;
using System.Xml;
#else
using System.Diagnostics;
using Microsoft.Toolkit.Uwp.Notifications;
#endif
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;

namespace DesktopNotifications.Windows
{
Expand Down
4 changes: 2 additions & 2 deletions DesktopNotifications/INotificationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public interface INotificationManager : IDisposable
Task ShowNotification(Notification notification, DateTimeOffset? expirationTime = null);

/// <summary>
/// Hides an already delivered notification (if possible).
/// Hides an already delivered notification (if possible).
/// If the notification is scheduled for delivery the schedule will be cancelled.
/// </summary>
/// <param name="notification">The notification to hide</param>
Expand All @@ -55,7 +55,7 @@ public interface INotificationManager : IDisposable
/// <param name="expirationTime"></param>
/// <returns></returns>
Task ScheduleNotification(
Notification notification,
Notification notification,
DateTimeOffset deliveryTime,
DateTimeOffset? expirationTime = null);
}
Expand Down
1 change: 0 additions & 1 deletion DesktopNotifications/NotificationActivatedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace DesktopNotifications
{
/// <summary>
///
/// </summary>
public class NotificationActivatedEventArgs : NotificationEventArgs
{
Expand Down
4 changes: 2 additions & 2 deletions Example.Avalonia/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Example.Avalonia.App">
<Application.Styles>
<FluentTheme Mode="Light"/>
<FluentTheme Mode="Light" />
</Application.Styles>
</Application>
</Application>
2 changes: 1 addition & 1 deletion Example.Avalonia/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ public override void OnFrameworkInitializationCompleted()
base.OnFrameworkInitializationCompleted();
}
}
}
}
2 changes: 1 addition & 1 deletion Example.Avalonia/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<Button Click="Show_OnClick" Content="Show Notification" />
<Button Click="Schedule_OnClick" Content="Schedule notification (in 5 Seconds)" />
<Button Click="HideLast_OnClick" Content="Hide last notification" />
<Button Click="HideLast_OnClick" Content="Hide last notification" />

<TextBlock Foreground="Gray">Events:</TextBlock>
<ListBox Name="EventsListBox" Height="200" />
Expand Down
18 changes: 8 additions & 10 deletions Example.Avalonia/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using DesktopNotifications;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace Example.Avalonia
{
public class MainWindow : Window
{
private readonly TextBox _bodyTextBox;
private readonly ListBox _eventsListBox;
private readonly TextBox _titleTextBox;
private readonly INotificationManager _notificationManager;
private readonly TextBox _titleTextBox;

private Notification? _lastNotification;

Expand Down Expand Up @@ -73,9 +73,9 @@ public async void Show_OnClick(object? sender, RoutedEventArgs e)
Title = _titleTextBox.Text ?? _titleTextBox.Watermark,
Body = _bodyTextBox.Text ?? _bodyTextBox.Watermark,
Buttons =
{
("This is awesome!", "awesome")
}
{
("This is awesome!", "awesome")
}
};

await _notificationManager.ShowNotification(nf);
Expand All @@ -96,7 +96,6 @@ private async void Schedule_OnClick(object? sender, RoutedEventArgs e)
{
Title = _titleTextBox.Text ?? _titleTextBox.Watermark,
Body = _bodyTextBox.Text ?? _bodyTextBox.Watermark

};

await _notificationManager.ScheduleNotification(
Expand All @@ -119,7 +118,6 @@ private async void HideLast_OnClick(object? sender, RoutedEventArgs e)
{
await _notificationManager.HideNotification(_lastNotification);
}

}
catch (Exception ex)
{
Expand Down
Loading

0 comments on commit 28e0eee

Please sign in to comment.