Skip to content

Commit 8c3e05d

Browse files
authored
fixed daemon parameter not working (#4591)
1 parent c1fbd88 commit 8c3e05d

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/UniGetUI.Avalonia/App.axaml.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using UniGetUI.Avalonia.Infrastructure;
99
using UniGetUI.Avalonia.Views;
1010
using UniGetUI.Avalonia.Views.DialogPages;
11+
using UniGetUI.Core.Data;
1112
using UniGetUI.PackageEngine;
1213
using CoreSettings = global::UniGetUI.Core.SettingsEngine.Settings;
1314

@@ -48,6 +49,13 @@ public override void OnFrameworkInitializationCompleted()
4849
ApplyTheme(CoreSettings.GetValue(CoreSettings.K.PreferredTheme));
4950
var mainWindow = new MainWindow();
5051
desktop.MainWindow = mainWindow;
52+
53+
if (CoreData.WasDaemon)
54+
{
55+
// Start silently: hide the window as soon as Avalonia opens it.
56+
mainWindow.Opened += (_, _) => mainWindow.Hide();
57+
}
58+
5159
_ = StartupAsync(mainWindow);
5260
}
5361

@@ -94,7 +102,13 @@ private static async Task StartupAsync(MainWindow mainWindow)
94102
// Yield once so the main window has time to open before
95103
// ShowDialog tries to attach to it as owner.
96104
await Task.Yield();
105+
106+
// ShowDialog requires a visible owner. In daemon mode the main window
107+
// is hidden, so temporarily show it and re-hide after the dialog closes.
108+
bool reshide = CoreData.WasDaemon;
109+
if (reshide) mainWindow.Show();
97110
await new CrashReportWindow(report).ShowDialog(mainWindow);
111+
if (reshide) mainWindow.Hide();
98112
}
99113
catch { /* must not prevent normal startup */ }
100114
}

src/UniGetUI.Avalonia/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using Avalonia;
3+
using UniGetUI.Core.Data;
34

45
namespace UniGetUI.Avalonia;
56

@@ -14,6 +15,8 @@ public static void Main(string[] args)
1415
AppDomain.CurrentDomain.UnhandledException += (_, e) =>
1516
CrashHandler.ReportFatalException((Exception)e.ExceptionObject);
1617

18+
CoreData.WasDaemon = CoreData.IsDaemon = args.Contains("--daemon");
19+
1720
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
1821
}
1922

src/UniGetUI/App.xaml.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,21 +332,37 @@ private async Task LoadComponentsAsync()
332332

333333
// Create MainWindow
334334
InitializeMainWindow();
335-
MainWindow.Activate();
335+
if (!CoreData.WasDaemon)
336+
MainWindow.Activate();
336337

337338
// Show crash report from the previous session on top of the loading
338339
// screen and wait for the user to dismiss it before continuing.
339340
if (File.Exists(CrashHandler.PendingCrashFile))
340341
{
341342
try
342343
{
344+
// In daemon mode the DWM/XAML threads are suspended; resume them
345+
// temporarily so the crash window can render, then re-suspend.
346+
bool resumedForCrash = CoreData.WasDaemon;
347+
if (resumedForCrash)
348+
{
349+
DWMThreadHelper.ChangeState_DWM(false);
350+
DWMThreadHelper.ChangeState_XAML(false);
351+
}
352+
343353
string report = File.ReadAllText(CrashHandler.PendingCrashFile);
344354
File.Delete(CrashHandler.PendingCrashFile);
345355
var tcs = new TaskCompletionSource();
346356
var crashWindow = new CrashReportWindow(report);
347357
crashWindow.Closed += (_, _) => tcs.TrySetResult();
348358
crashWindow.Activate();
349359
await tcs.Task;
360+
361+
if (resumedForCrash)
362+
{
363+
DWMThreadHelper.ChangeState_DWM(true);
364+
DWMThreadHelper.ChangeState_XAML(true);
365+
}
350366
}
351367
catch { /* must not prevent normal startup */ }
352368
}
@@ -505,7 +521,8 @@ private async Task CheckForMissingDependencies()
505521

506522
protected override void OnLaunched(LaunchActivatedEventArgs args)
507523
{
508-
MainWindow?.Activate();
524+
if (!CoreData.WasDaemon)
525+
MainWindow?.Activate();
509526
}
510527

511528
public async Task ShowMainWindowFromRedirectAsync(AppActivationArguments rawArgs)

0 commit comments

Comments
 (0)