Skip to content

Commit

Permalink
Update to av11 (#23)
Browse files Browse the repository at this point in the history
* Bump avalonia version to 11.0.1

* update to avalonia 11

* update example to avalonia 11
  • Loading branch information
alexalok committed Oct 21, 2023
1 parent 2cafdfd commit d7a2981
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 58 deletions.
46 changes: 19 additions & 27 deletions DesktopNotifications.Avalonia/AppBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,52 @@
using Avalonia.Platform;
using DesktopNotifications.FreeDesktop;
using DesktopNotifications.Windows;
using System;

namespace DesktopNotifications.Avalonia
{
/// <summary>
/// Extensions for <see cref="AppBuilderBase{TAppBuilder}" />
/// Extensions for <see cref="AppBuilder" />
/// </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" />).
/// </summary>
/// <typeparam name="TAppBuilder"></typeparam>
/// <param name="builder"></param>
/// <returns></returns>
public static TAppBuilder SetupDesktopNotifications<TAppBuilder>(this TAppBuilder builder)
where TAppBuilder : AppBuilderBase<TAppBuilder>, new()
public static AppBuilder SetupDesktopNotifications(this AppBuilder builder, out INotificationManager? manager)
{
INotificationManager manager;
var runtimeInfo = builder.RuntimePlatform.GetRuntimeInfo();

switch (runtimeInfo.OperatingSystem)
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
var context = WindowsApplicationContext.FromCurrentProcess();
manager = new WindowsNotificationManager(context);
}
else if (Environment.OSVersion.Platform == PlatformID.Unix)
{
var context = FreeDesktopApplicationContext.FromCurrentProcess();
manager = new FreeDesktopNotificationManager(context);
}
else
{
case OperatingSystemType.WinNT:
{
var context = WindowsApplicationContext.FromCurrentProcess();
manager = new WindowsNotificationManager(context);
break;
}

case OperatingSystemType.Linux:
{
var context = FreeDesktopApplicationContext.FromCurrentProcess();
manager = new FreeDesktopNotificationManager(context);
break;
}

//TODO: OSX once implemented/stable
default: return builder;
manager = null;
return builder;
}

//TODO Any better way of doing this?
manager.Initialize().GetAwaiter().GetResult();

var manager_ = manager;
builder.AfterSetup(b =>
{
if (b.Instance.ApplicationLifetime is IControlledApplicationLifetime lifetime)
if (b.Instance?.ApplicationLifetime is IControlledApplicationLifetime lifetime)
{
lifetime.Exit += (s, e) => { manager.Dispose(); };
lifetime.Exit += (s, e) => { manager_.Dispose(); };
}
});

AvaloniaLocator.CurrentMutable.Bind<INotificationManager>().ToConstant(manager);

return builder;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Avalonia" Version="0.10.11" />
<PackageReference Include="Avalonia" Version="11.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion 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 />
</Application.Styles>
</Application>
7 changes: 4 additions & 3 deletions Example.Avalonia/Example.Avalonia.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="0.10.11" />
<PackageReference Include="Avalonia.Desktop" Version="0.10.11" />
<PackageReference Include="Avalonia.Diagnostics" Version="0.10.11" />
<PackageReference Include="Avalonia" Version="11.0.1" />
<PackageReference Include="Avalonia.Desktop" Version="11.0.1" />
<PackageReference Include="Avalonia.Diagnostics" Version="11.0.1" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DesktopNotifications.Avalonia\DesktopNotifications.Avalonia.csproj" />
Expand Down
35 changes: 10 additions & 25 deletions Example.Avalonia/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@

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

private Notification? _lastNotification;

Expand All @@ -28,23 +24,17 @@ public MainWindow()
this.AttachDevTools();
#endif

_titleTextBox = this.FindControl<TextBox>("TitleTextBox");
_bodyTextBox = this.FindControl<TextBox>("BodyTextBox");
_imagePathTextBox = this.FindControl<TextBox>("ImagePathTextBox");
_logListBox = this.FindControl<ListBox>("LogListBox");
_logListBox.Items = new ObservableCollection<string>();

_notificationManager = AvaloniaLocator.Current.GetService<INotificationManager>() ??
_notificationManager = Program.NotificationManager ??
throw new InvalidOperationException("Missing notification manager");
_notificationManager.NotificationActivated += OnNotificationActivated;
_notificationManager.NotificationDismissed += OnNotificationDismissed;

Log($"Capabilities: {FormatFlagEnum(_notificationManager.Capabilities)}");

_bodyTextBox.IsEnabled =
BodyTextBox.IsEnabled =
_notificationManager.Capabilities.HasFlag(NotificationManagerCapabilities.BodyText);

_imagePathTextBox.IsEnabled =
ImagePathTextBox.IsEnabled =
_notificationManager.Capabilities.HasFlag(NotificationManagerCapabilities.BodyImages);

if (_notificationManager.LaunchActionId != null)
Expand All @@ -65,7 +55,7 @@ public MainWindow()

private void Log(string @event)
{
((IList<string>)_logListBox.Items).Add(@event);
LogListBox.Items.Add(@event);
}

private void OnNotificationDismissed(object? sender, NotificationDismissedEventArgs e)
Expand All @@ -78,11 +68,6 @@ private void OnNotificationActivated(object? sender, NotificationActivatedEventA
Log($"Notification activated: {e.ActionId}");
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}

public async void Show_OnClick(object? sender, RoutedEventArgs e)
{
try
Expand All @@ -91,9 +76,9 @@ public async void Show_OnClick(object? sender, RoutedEventArgs e)

var nf = new Notification
{
Title = _titleTextBox.Text ?? _titleTextBox.Watermark,
Body = _bodyTextBox.Text ?? _bodyTextBox.Watermark,
BodyImagePath = _imagePathTextBox.Text,
Title = TitleTextBox.Text ?? TitleTextBox.Watermark,
Body = BodyTextBox.Text ?? BodyTextBox.Watermark,
BodyImagePath = ImagePathTextBox.Text,
Buttons =
{
("This is awesome!", "awesome")
Expand All @@ -116,8 +101,8 @@ private async void Schedule_OnClick(object? sender, RoutedEventArgs e)
{
var nf = new Notification
{
Title = _titleTextBox.Text ?? _titleTextBox.Watermark,
Body = _bodyTextBox.Text ?? _bodyTextBox.Watermark
Title = TitleTextBox.Text ?? TitleTextBox.Watermark,
Body = BodyTextBox.Text ?? BodyTextBox.Watermark
};

await _notificationManager.ScheduleNotification(
Expand Down
5 changes: 4 additions & 1 deletion Example.Avalonia/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using Avalonia;
using DesktopNotifications;
using DesktopNotifications.Avalonia;

namespace Example.Avalonia
{
internal class Program
{
public static INotificationManager NotificationManager = null!;

public static void Main(string[] args)
{
BuildAvaloniaApp()
Expand All @@ -15,7 +18,7 @@ public static AppBuilder BuildAvaloniaApp()
{
return AppBuilder.Configure<App>()
.UsePlatformDetect()
.SetupDesktopNotifications()
.SetupDesktopNotifications(out NotificationManager!)
.LogToTrace();
}
}
Expand Down

0 comments on commit d7a2981

Please sign in to comment.