Skip to content

feat: add support for notifications #85

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

Merged
merged 1 commit into from
May 2, 2025
Merged
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
14 changes: 13 additions & 1 deletion App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Microsoft.Windows.AppLifecycle;
using Serilog;
using LaunchActivatedEventArgs = Microsoft.UI.Xaml.LaunchActivatedEventArgs;
using Microsoft.Windows.AppNotifications;

namespace Coder.Desktop.App;

Expand Down Expand Up @@ -70,6 +71,7 @@ public App()
services.AddOptions<MutagenControllerConfig>()
.Bind(builder.Configuration.GetSection(MutagenControllerConfigSection));
services.AddSingleton<ISyncSessionController, MutagenController>();
services.AddSingleton<IUserNotifier, UserNotifier>();

// SignInWindow views and view models
services.AddTransient<SignInViewModel>();
Expand Down Expand Up @@ -188,10 +190,14 @@ public void OnActivated(object? sender, AppActivationArguments args)
_logger.LogWarning("URI activation with null data");
return;
}

HandleURIActivation(protoArgs.Uri);
break;

case ExtendedActivationKind.AppNotification:
var notificationArgs = (args.Data as AppNotificationActivatedEventArgs)!;
HandleNotification(null, notificationArgs);
break;

default:
_logger.LogWarning("activation for {kind}, which is unhandled", args.Kind);
break;
Expand All @@ -204,6 +210,12 @@ public void HandleURIActivation(Uri uri)
_logger.LogInformation("handling URI activation for {path}", uri.AbsolutePath);
}

public void HandleNotification(AppNotificationManager? sender, AppNotificationActivatedEventArgs args)
{
// right now, we don't do anything other than log
_logger.LogInformation("handled notification activation");
}

private static void AddDefaultConfig(IConfigurationBuilder builder)
{
var logPath = Path.Combine(
Expand Down
12 changes: 11 additions & 1 deletion App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using Microsoft.Windows.AppLifecycle;
using Microsoft.Windows.AppNotifications;
using WinRT;

namespace Coder.Desktop.App;
Expand All @@ -28,9 +29,9 @@ private static void Main(string[] args)
{
ComWrappersSupport.InitializeComWrappers();
var mainInstance = GetMainInstance();
var activationArgs = AppInstance.GetCurrent().GetActivatedEventArgs();
if (!mainInstance.IsCurrent)
{
var activationArgs = AppInstance.GetCurrent().GetActivatedEventArgs();
mainInstance.RedirectActivationToAsync(activationArgs).AsTask().Wait();
return;
}
Expand Down Expand Up @@ -58,6 +59,15 @@ private static void Main(string[] args)

// redirections via RedirectActivationToAsync above get routed to the App
mainInstance.Activated += app.OnActivated;
var notificationManager = AppNotificationManager.Default;
notificationManager.NotificationInvoked += app.HandleNotification;
notificationManager.Register();
if (activationArgs.Kind != ExtendedActivationKind.Launch)
{
// this means we were activated without having already launched, so handle
// the activation as well.
app.OnActivated(null, activationArgs);
}
});
}
catch (Exception e)
Expand Down
29 changes: 29 additions & 0 deletions App/Services/UserNotifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Threading.Tasks;
using Microsoft.Windows.AppNotifications;
using Microsoft.Windows.AppNotifications.Builder;

namespace Coder.Desktop.App.Services;

public interface IUserNotifier : IAsyncDisposable
{
public Task ShowErrorNotification(string title, string message);
}

public class UserNotifier : IUserNotifier
{
private readonly AppNotificationManager _notificationManager = AppNotificationManager.Default;

public ValueTask DisposeAsync()
{
return ValueTask.CompletedTask;
}

public Task ShowErrorNotification(string title, string message)
Copy link
Member

Choose a reason for hiding this comment

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

This could just be ShowNotification, the function itself doesn't seem to be only relevant for errors. I imagine we could use it for other notifications like workspace lifetime notifications in the future

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I kinda want to keep the information that this is an "error" in case we want to refactor this class to use, say, a dialog pop-up instead, or decide to style the notifications further to make errors stand out from other notifications.

{
var builder = new AppNotificationBuilder().AddText(title).AddText(message);
_notificationManager.Show(builder.BuildNotification());
return Task.CompletedTask;
}
}

Loading