Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
spikecurtis merged 1 commit intomainfromspike/windows-notifications
May 2, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionApp/App.xaml.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 deletionApp/Program.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionsApp/Services/UserNotifier.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 beShowNotification, 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
CollaboratorAuthor

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

[8]ページ先頭

©2009-2025 Movatter.jp