Browse Source

Fix WindowCloseReason of WindowClosingEventArgs Passed During System Shutdown (#19055)

Fix https://github.com/AvaloniaUI/Avalonia/issues/19027
pull/19083/head
lindexi 1 year ago
committed by GitHub
parent
commit
c0ba356a96
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 7
      src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs
  2. 5
      src/Avalonia.Controls/ApplicationLifetimes/ShutdownRequestedEventArgs.cs
  3. 7
      src/Avalonia.X11/X11PlatformLifetimeEvents.cs
  4. 17
      src/Windows/Avalonia.Win32/Win32Platform.cs

7
src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
@ -188,7 +188,10 @@ namespace Avalonia.Controls.ApplicationLifetimes
if (w.Owner is null)
{
var ignoreCancel = force || (ShutdownMode == ShutdownMode.OnMainWindowClose && w != MainWindow);
w.CloseCore(WindowCloseReason.ApplicationShutdown, isProgrammatic, ignoreCancel);
var reason = e.IsOSShutdown ?
WindowCloseReason.OSShutdown :
WindowCloseReason.ApplicationShutdown;
w.CloseCore(reason, isProgrammatic, ignoreCancel);
}
}

5
src/Avalonia.Controls/ApplicationLifetimes/ShutdownRequestedEventArgs.cs

@ -4,6 +4,9 @@ namespace Avalonia.Controls.ApplicationLifetimes
{
public class ShutdownRequestedEventArgs : CancelEventArgs
{
/// <summary>
/// Is the operating system shutting down
/// </summary>
internal bool IsOSShutdown { get; init; }
}
}

7
src/Avalonia.X11/X11PlatformLifetimeEvents.cs

@ -1,4 +1,4 @@
using System;
using System;
using System.Text;
using System.Collections.Concurrent;
using System.Runtime.InteropServices;
@ -229,7 +229,10 @@ namespace Avalonia.X11
{
if (state is IntPtr smcConn)
{
var e = new ShutdownRequestedEventArgs();
var e = new ShutdownRequestedEventArgs()
{
IsOSShutdown = true,
};
if (_platform.Options.EnableSessionManagement)
{

17
src/Windows/Avalonia.Win32/Win32Platform.cs

@ -1,4 +1,4 @@
using System;
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.IO;
@ -148,11 +148,22 @@ namespace Avalonia.Win32
{
if (ShutdownRequested != null)
{
var e = new ShutdownRequestedEventArgs();
// https://learn.microsoft.com/en-us/windows/win32/shutdown/wm-queryendsession
// > LPARAM lParam // logoff option
// >
// > This parameter can be one or more of the following values. If this parameter is 0, the system is shutting down or restarting (it is not possible to determine which event is occurring).
// >
// > - ENDSESSION_CLOSEAPP 0x00000001 The application is using a file that must be replaced, the system is being serviced, or system resources are exhausted. For more information, see Guidelines for Applications.
// > - ENDSESSION_CRITICAL 0x40000000 The application is forced to shut down.
// > - ENDSESSION_LOGOFF 0x80000000 The user is logging off.
var e = new ShutdownRequestedEventArgs()
{
IsOSShutdown = lParam == IntPtr.Zero,
};
ShutdownRequested(this, e);
if(e.Cancel)
if (e.Cancel)
{
return IntPtr.Zero;
}

Loading…
Cancel
Save