setting_re1

This commit is contained in:
lincube
2026-03-12 21:01:23 +08:00
parent 4679ee006f
commit 40a3a00cfe
42 changed files with 2367 additions and 1017 deletions

View File

@@ -11,18 +11,21 @@ public sealed class HostApplicationLifecycleService : IHostApplicationLifecycle
{
public bool TryExit(HostApplicationLifecycleRequest? request = null)
{
App? app = null;
try
{
AppLogger.Info(
"HostLifecycle",
$"Exit requested. Source='{request?.Source ?? "Unknown"}'; Reason='{request?.Reason ?? string.Empty}'.");
if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
app = Application.Current as App;
if (app?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
{
AppLogger.Warn("HostLifecycle", "Exit request ignored because desktop lifetime is unavailable.");
return false;
}
app.PrepareForShutdown(isRestart: false, request?.Source ?? "Unknown");
if (Dispatcher.UIThread.CheckAccess())
{
desktop.Shutdown();
@@ -36,6 +39,7 @@ public sealed class HostApplicationLifecycleService : IHostApplicationLifecycle
}
catch (Exception ex)
{
app?.ResetShutdownIntent(request?.Source ?? "Unknown");
AppLogger.Warn("HostLifecycle", "Failed to exit the application.", ex);
return false;
}
@@ -43,6 +47,7 @@ public sealed class HostApplicationLifecycleService : IHostApplicationLifecycle
public bool TryRestart(HostApplicationLifecycleRequest? request = null)
{
App? app = null;
try
{
var startInfo = AppRestartService.CreateRestartStartInfo();
@@ -55,6 +60,8 @@ public sealed class HostApplicationLifecycleService : IHostApplicationLifecycle
}
Process.Start(startInfo);
app = Application.Current as App;
app?.PrepareForShutdown(isRestart: true, request?.Source ?? "Unknown");
var exitRequest = request is null
? new HostApplicationLifecycleRequest(Reason: "Restart accepted.")
: request with
@@ -68,6 +75,7 @@ public sealed class HostApplicationLifecycleService : IHostApplicationLifecycle
}
catch (Exception ex)
{
app?.ResetShutdownIntent(request?.Source ?? "Unknown");
AppLogger.Warn("HostLifecycle", "Failed to restart the application.", ex);
return false;
}

View File

@@ -0,0 +1,88 @@
using System;
using Avalonia.Threading;
using LanMountainDesktop.Views;
namespace LanMountainDesktop.Services;
internal sealed class IndependentSettingsModuleService
{
private SettingsWindow? _window;
public void ShowOrActivate(string source, string? pageTag = null)
{
AppLogger.Info("IndependentSettingsModule", $"OpenRequested; Source='{source}'; PageTag='{pageTag ?? "<default>"}'.");
void ShowCore()
{
try
{
if (_window is not { } window)
{
AppLogger.Info("IndependentSettingsModule", $"WindowConstructionStarted; Source='{source}'.");
window = new SettingsWindow();
AppLogger.Info("IndependentSettingsModule", $"WindowConstructionCompleted; Source='{source}'.");
window.Closed += (_, _) =>
{
if (ReferenceEquals(_window, window))
{
_window = null;
}
AppLogger.Info("IndependentSettingsModule", "WindowClosed.");
};
_window = window;
}
window.Open(pageTag);
AppLogger.Info(
"IndependentSettingsModule",
$"WindowActivated; Source='{source}'; ReusedExisting={ReferenceEquals(_window, window)}; WasVisible={window.IsVisible}; PageTag='{pageTag ?? "<default>"}'.");
}
catch (Exception ex)
{
AppLogger.Warn("IndependentSettingsModule", $"Failed to open independent settings module window. Source='{source}'.", ex);
}
}
if (Dispatcher.UIThread.CheckAccess())
{
ShowCore();
return;
}
Dispatcher.UIThread.Post(ShowCore, DispatcherPriority.Normal);
}
public void CloseIfOpen()
{
void CloseCore()
{
if (_window is null)
{
return;
}
try
{
_window.PrepareForForceClose();
_window.Close();
}
catch (Exception ex)
{
AppLogger.Warn("IndependentSettingsModule", "Failed to close independent settings module window during shutdown.", ex);
}
finally
{
_window = null;
}
}
if (Dispatcher.UIThread.CheckAccess())
{
CloseCore();
return;
}
Dispatcher.UIThread.Post(CloseCore, DispatcherPriority.Send);
}
}