Skip to content

Commit f7ba52a

Browse files
committed
review
1 parent e62f27e commit f7ba52a

File tree

4 files changed

+33
-39
lines changed

4 files changed

+33
-39
lines changed

App/App.xaml.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.Extensions.DependencyInjection;
1717
using Microsoft.Extensions.Hosting;
1818
using Microsoft.Extensions.Logging;
19+
using Microsoft.UI.Dispatching;
1920
using Microsoft.UI.Xaml;
2021
using Microsoft.Win32;
2122
using Microsoft.Windows.AppLifecycle;
@@ -26,7 +27,7 @@
2627

2728
namespace Coder.Desktop.App;
2829

29-
public partial class App : Application
30+
public partial class App : Application, IDispatcherQueueManager
3031
{
3132
private const string MutagenControllerConfigSection = "MutagenController";
3233
private const string UpdaterConfigSection = "Updater";
@@ -50,13 +51,11 @@ public partial class App : Application
5051
private readonly ILogger<App> _logger;
5152
private readonly IUriHandler _uriHandler;
5253
private readonly IUserNotifier _userNotifier;
53-
54-
private bool _handleWindowClosed = true;
55-
5654
private readonly ISettingsManager<CoderConnectSettings> _settingsManager;
57-
5855
private readonly IHostApplicationLifetime _appLifetime;
5956

57+
private bool _handleWindowClosed = true;
58+
6059
public App()
6160
{
6261
var builder = Host.CreateApplicationBuilder();
@@ -91,7 +90,7 @@ public App()
9190
services.AddSingleton<ICoderApiClientFactory, CoderApiClientFactory>();
9291
services.AddSingleton<IAgentApiClientFactory, AgentApiClientFactory>();
9392

94-
services.AddSingleton<IDispatcherQueueManager, AppDispatcherQueueManager>();
93+
services.AddSingleton<IDispatcherQueueManager>(_ => this);
9594
services.AddSingleton<ICredentialBackend>(_ =>
9695
new WindowsCredentialBackend(WindowsCredentialBackend.CoderCredentialsTargetName));
9796
services.AddSingleton<ICredentialManager, CredentialManager>();
@@ -323,4 +322,17 @@ private static void AddDefaultConfig(IConfigurationBuilder builder)
323322
#endif
324323
});
325324
}
325+
326+
public void RunInUiThread(DispatcherQueueHandler action)
327+
{
328+
var dispatcherQueue = TrayWindow?.DispatcherQueue;
329+
if (dispatcherQueue is null)
330+
throw new InvalidOperationException("DispatcherQueue is not available");
331+
if (dispatcherQueue.HasThreadAccess)
332+
{
333+
action();
334+
return;
335+
}
336+
dispatcherQueue.TryEnqueue(action);
337+
}
326338
}
Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,8 @@
1-
using System;
21
using Microsoft.UI.Dispatching;
3-
using Microsoft.UI.Xaml;
42

53
namespace Coder.Desktop.App.Services;
64

75
public interface IDispatcherQueueManager
86
{
97
public void RunInUiThread(DispatcherQueueHandler action);
108
}
11-
12-
public class AppDispatcherQueueManager : IDispatcherQueueManager
13-
{
14-
private static DispatcherQueue? DispatcherQueue =>
15-
((App)Application.Current).TrayWindow?.DispatcherQueue;
16-
17-
public void RunInUiThread(DispatcherQueueHandler action)
18-
{
19-
var dispatcherQueue = DispatcherQueue;
20-
if (dispatcherQueue is null)
21-
throw new InvalidOperationException("DispatcherQueue is not available");
22-
if (dispatcherQueue.HasThreadAccess)
23-
{
24-
action();
25-
return;
26-
}
27-
dispatcherQueue.TryEnqueue(action);
28-
}
29-
}

App/Services/UpdateController.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ public async Task CheckForUpdatesNow()
163163

164164
public ValueTask DisposeAsync()
165165
{
166+
_userNotifier.UnregisterHandler(NotificationHandlerName);
166167
_sparkle?.Dispose();
167168
return ValueTask.CompletedTask;
168169
}
@@ -203,9 +204,9 @@ public class CoderSparkleUIFactory(IUserNotifier userNotifier, IUpdaterUpdateAva
203204
{
204205
public bool ForceDisableToastMessages;
205206

206-
bool IUIFactory.HideReleaseNotes { get; set; }
207-
bool IUIFactory.HideSkipButton { get; set; }
208-
bool IUIFactory.HideRemindMeLaterButton { get; set; }
207+
public bool HideReleaseNotes { get; set; }
208+
public bool HideSkipButton { get; set; }
209+
public bool HideRemindMeLaterButton { get; set; }
209210

210211
// This stuff is ignored as we use our own template in the ViewModel
211212
// directly:
@@ -215,8 +216,6 @@ public class CoderSparkleUIFactory(IUserNotifier userNotifier, IUpdaterUpdateAva
215216
public IUpdateAvailable CreateUpdateAvailableWindow(List<AppCastItem> updates, ISignatureVerifier? signatureVerifier,
216217
string currentVersion = "", string appName = "Coder Desktop", bool isUpdateAlreadyDownloaded = false)
217218
{
218-
IUIFactory factory = this;
219-
220219
var viewModel = updateAvailableViewModelFactory.Create(
221220
updates,
222221
signatureVerifier,
@@ -225,11 +224,11 @@ public IUpdateAvailable CreateUpdateAvailableWindow(List<AppCastItem> updates, I
225224
isUpdateAlreadyDownloaded);
226225

227226
var window = new UpdaterUpdateAvailableWindow(viewModel);
228-
if (factory.HideReleaseNotes)
227+
if (HideReleaseNotes)
229228
(window as IUpdateAvailable).HideReleaseNotes();
230-
if (factory.HideSkipButton)
229+
if (HideSkipButton)
231230
(window as IUpdateAvailable).HideSkipButton();
232-
if (factory.HideRemindMeLaterButton)
231+
if (HideRemindMeLaterButton)
233232
(window as IUpdateAvailable).HideRemindMeLaterButton();
234233

235234
return window;

App/Services/UserNotifier.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public interface INotificationHandler
1717
public interface IUserNotifier : INotificationHandler, IAsyncDisposable
1818
{
1919
public void RegisterHandler(string name, INotificationHandler handler);
20+
public void UnregisterHandler(string name);
2021

2122
public Task ShowErrorNotification(string title, string message, CancellationToken ct = default);
2223
public Task ShowActionNotification(string title, string message, string handlerName, IDictionary<string, string>? args = null, CancellationToken ct = default);
@@ -47,6 +48,12 @@ public void RegisterHandler(string name, INotificationHandler handler)
4748
throw new InvalidOperationException($"A handler with the name '{name}' is already registered.");
4849
}
4950

51+
public void UnregisterHandler(string name)
52+
{
53+
if (!Handlers.TryRemove(name, out _))
54+
throw new InvalidOperationException($"No handler with the name '{name}' is registered.");
55+
}
56+
5057
public Task ShowErrorNotification(string title, string message, CancellationToken ct = default)
5158
{
5259
var builder = new AppNotificationBuilder().AddText(title).AddText(message);
@@ -57,10 +64,7 @@ public Task ShowErrorNotification(string title, string message, CancellationToke
5764
public Task ShowActionNotification(string title, string message, string handlerName, IDictionary<string, string>? args = null, CancellationToken ct = default)
5865
{
5966
if (!Handlers.TryGetValue(handlerName, out _))
60-
{
61-
logger.LogWarning("no action handler found for notification with name {HandlerName}, ignoring", handlerName);
62-
return Task.CompletedTask;
63-
}
67+
throw new InvalidOperationException($"No action handler with the name '{handlerName}' is registered.");
6468

6569
var builder = new AppNotificationBuilder()
6670
.AddText(title)

0 commit comments

Comments
 (0)