Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FreeDesktop Portal Notifications #75

Open
wants to merge 1 commit into
base: notifications
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 35 additions & 66 deletions samples/Avalonia.Labs.Catalog.Desktop/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

using Avalonia.Labs.Controls.Cache;
using Avalonia.Labs.Notifications;
using Avalonia.Labs.Notifications.Windows;
using Avalonia.Labs.Notifications.Linux;
using Avalonia.Platform;
using Avalonia.Labs.Notifications.Windows;
using Avalonia.ReactiveUI;


namespace Avalonia.Labs.Catalog.Desktop;

class Program
Expand All @@ -29,81 +30,49 @@ public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.UseReactiveUI()
.WithX11AppNotifications(new X11NotificationOptions()
.WithDBusAppNotifications(new DBusNotificationOptions
{
AppIcon = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "/avalonia-32.png",
AppName = "Avalonia.Labs",
Channels = new[]
{
new NotificationChannel("basic", "Send Notifications", Notifications.NotificationPriority.High),
new NotificationChannel("actions", "Send Notification with Predefined Actions", Notifications.NotificationPriority.High)
Channels =
[
new NotificationChannel("basic", "Send Notifications", NotificationPriority.High),
new NotificationChannel("actions", "Send Notification with Predefined Actions", NotificationPriority.High)
{
Actions = new List<NativeNotificationAction>
{
new NativeNotificationAction()
{
Tag = "hello",
Caption = "Hello"
},
new NativeNotificationAction()
{
Tag = "world",
Caption = "world"
}
}
Actions =
[
new NativeNotificationAction("hello", "Hello"),
new NativeNotificationAction("world", "world")
]
},
new NotificationChannel("custom", "Send Notification with Custom Actions", Notifications.NotificationPriority.High),
new NotificationChannel("reply", "Send Notification with Reply Action", Notifications.NotificationPriority.High)
new NotificationChannel("custom", "Send Notification with Custom Actions", NotificationPriority.High),
new NotificationChannel("reply", "Send Notification with Reply Action", NotificationPriority.High)
{
Actions = new List<NativeNotificationAction>
{
new NativeNotificationAction()
{
Tag = "reply",
Caption = "Reply"
}
}
},
}
Actions = [new NativeNotificationAction("reply", "Reply")]
}
]
})
.WithWin32AppNotifications(new Win32NotificationOptions()
.WithWin32AppNotifications(new Win32NotificationOptions
{
Channels = new[]
Channels =
[
new NotificationChannel("basic", "Send Notifications", NotificationPriority.High),
new NotificationChannel("actions", "Send Notification with Predefined Actions", NotificationPriority.High)
{
new NotificationChannel("basic", "Send Notifications", Notifications.NotificationPriority.High),
new NotificationChannel("actions", "Send Notification with Predefined Actions", Notifications.NotificationPriority.High)
{
Actions = new List<NativeNotificationAction>
{
new NativeNotificationAction()
{
Tag = "hello",
Caption = "Hello"
},
new NativeNotificationAction()
{
Tag = "world",
Caption = "world"
}
}
},
new NotificationChannel("custom", "Send Notification with Custom Actions", Notifications.NotificationPriority.High),
new NotificationChannel("reply", "Send Notification with Reply Action", Notifications.NotificationPriority.High)
{
Actions = new List<NativeNotificationAction>
{
new NativeNotificationAction()
{
Tag = "reply",
Caption = "Reply"
}
}
},
Actions =
[
new NativeNotificationAction("hello", "Hello"),
new NativeNotificationAction("world", "world")
]
},
new NotificationChannel("custom", "Send Notification with Custom Actions", NotificationPriority.High),
new NotificationChannel("reply", "Send Notification with Reply Action", NotificationPriority.High)
{
Actions = [new NativeNotificationAction("reply", "Reply")]
}
]
})
.AfterSetup(builder =>
{
CacheOptions.SetDefault(new CacheOptions()
CacheOptions.SetDefault(new CacheOptions
{
BaseCachePath = Path.Combine(Path.GetTempPath(), "Avalonia.Labs")
});
Expand Down
1 change: 0 additions & 1 deletion samples/Avalonia.Labs.Catalog/Avalonia.Labs.Catalog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
<ProjectReference Include="..\..\src\Avalonia.Labs.Controls\Avalonia.Labs.Controls.csproj" />
<ProjectReference Include="..\..\src\Avalonia.Labs.Lottie\Avalonia.Labs.Lottie.csproj" />
<ProjectReference Include="..\..\src\Avalonia.Labs.Notifications\Avalonia.Labs.Notifications.csproj" />
<ProjectReference Include="..\..\src\Avalonia.Labs.Notifications\Avalonia.Labs.Notifications\Avalonia.Labs.Notifications.csproj" />
<ProjectReference Include="..\..\src\Avalonia.Labs.Panels\Avalonia.Labs.Panels.csproj" />
<ProjectReference Include="..\..\src\Avalonia.Labs.Qr\Avalonia.Labs.Qr.csproj" />
<ProjectReference Include="..\..\src\Avalonia.Labs.CommandManager\Avalonia.Labs.CommandManager.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,7 @@ public void SendCustomActionsNotification()
{
notification.Title = "Hello Avalonia";
notification.Message = "Hello, this is a basic notification with custom actions. This is not supported on iOS";
notification.SetActions(new[]
{
new NativeNotificationAction()
{
Tag = "hey",
Caption = string.IsNullOrWhiteSpace(_customCaption) ? "Hey" : _customCaption,
}
});
notification.SetActions([new NativeNotificationAction("hey", string.IsNullOrWhiteSpace(_customCaption) ? "Hey" : _customCaption)]);

notification.Show();
}
Expand Down
25 changes: 12 additions & 13 deletions src/Avalonia.Labs.Notifications/Avalonia.Labs.Notifications.csproj
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net8.0-android</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<TargetFrameworks>net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Nullable>annotations</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPackable>true</IsPackable>
</PropertyGroup>

<ItemGroup>
<None Remove="Windows\WinRT\windows.ui.notifications.mcidl" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="Windows\WinRT\windows.ui.notifications.mcidl" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="$(AvaloniaVersion)" />
<PackageReference Include="Tmds.DBus.Protocol" Version="0.19.0" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those version of tmdbus are known to crash KDE and shouldn't be used with Avalonia codebase

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kekekeks @jmacato was it reported anywhere?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's not an actionable report.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<PackageReference Include="MicroCom.CodeGenerator.Roslyn" Version="0.11.0" PrivateAssets="all" />
<PackageReference Include="Tmds.DBus.SourceGenerator" Version="0.0.17" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MicroCom.CodeGenerator.Roslyn " Version="0.11.0" PrivateAssets="all" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0-android' ">
<PackageReference Include="Avalonia" Version="$(AvaloniaVersion)" />
<PackageReference Include="Avalonia.Android" Version="$(AvaloniaVersion)" />
<PackageReference Include="Xamarin.AndroidX.AppCompat" Version="1.6.1.5" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="Windows/WinRT/windows.ui.notifications.mcidl" />
<AdditionalFiles Include="Linux/DBusXml/org.freedesktop.portal.Notification.xml" DBusGeneratorMode="Proxy" />
</ItemGroup>

</Project>
18 changes: 14 additions & 4 deletions src/Avalonia.Labs.Notifications/INativeNotification.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Avalonia.Media.Imaging;
using System;
using System.Collections.Generic;

using Avalonia.Media.Imaging;

namespace Avalonia.Labs.Notifications
{
Expand Down Expand Up @@ -32,9 +35,16 @@ public interface INativeNotification

public class NativeNotificationAction
{
public string? Tag { get; set; }
public string? Caption { get; set; }
public Bitmap? Icon { get; set; }
public NativeNotificationAction(string tag, string caption, Bitmap? icon = null)
{
Tag = tag;
Caption = caption;
Icon = icon;
}

public string Tag { get; }
public string Caption { get; }
public Bitmap? Icon { get; }
}

public interface INativeNotificationManager
Expand Down
13 changes: 6 additions & 7 deletions src/Avalonia.Labs.Notifications/Linux/AppBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Labs.Notifications.Linux;

namespace Avalonia.Labs.Notifications.Linux
{
public static class AppBuilderExtensions
{
public static AppBuilder WithX11AppNotifications(this AppBuilder appBuilder, X11NotificationOptions options)
public static AppBuilder WithDBusAppNotifications(this AppBuilder appBuilder, DBusNotificationOptions options)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
return appBuilder;

var notificationManager = new LinuxNativeNotificationManager(options.AppName ?? "", options.AppIcon);
var notificationManager = new LinuxNativeNotificationManager();
NativeNotificationManager.RegisterNativeNotificationManager(notificationManager);

if (options.Channels != null)
if (options.Channels is not null)
{
foreach (var channel in options.Channels)
{
notificationManager.ChannelManager.AddChannel(channel);
}
}

var callback = appBuilder.AfterSetupCallback;
callback += (a) =>
Expand All @@ -42,10 +43,8 @@ public static AppBuilder WithX11AppNotifications(this AppBuilder appBuilder, X11
}
}

public class X11NotificationOptions
public class DBusNotificationOptions
{
public string? AppName { get; set; }
public IReadOnlyList<NotificationChannel>? Channels { get; set; }
public string? AppIcon { get; set; }
}
}
Loading
Loading