Repository: microsoft/terminal
File: src/cascadia/WindowsTerminal/IslandWindow.cpp (C++)

Code before:
    }

    case WM_QUERYENDSESSION:
    {
        // For WM_QUERYENDSESSION and WM_ENDSESSION, refer to:
        //
        // https://docs.microsoft.com/en-us/windows/win32/rstmgr/guidelines-for-applications
        //
        // The OS will send us a WM_QUERYENDSESSION when it's preparing an
        // update for our app. It will then send us a WM_ENDSESSION, which gives
        // us a small timeout (~30s) to actually shut down gracefully. After
        // that timeout, it will send us a WM_CLOSE. If we still don't close
        // after the WM_CLOSE, it'll force-kill us (causing a crash which will be
        // bucketed to MoAppHang).

        // If we need to do anything to prepare for being told to shutdown,
        // start it here.
        //
        // If (in the future) we need to prevent logoff, we can return false here.

        TraceLoggingWrite(
            g_hWindowsTerminalProvider,
            "QueryEndSession",
            TraceLoggingDescription("Emitted when the OS has begun the process of updating the Terminal"),
            TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE),
            TraceLoggingKeyword(TIL_KEYWORD_TRACE));

        return true;
    }
    case WM_ENDSESSION:
    {
        // The OS is going to shut us down here. We will manually start a quit,
        // so that we can persist the state. If we refuse to gracefully shut
        // down here, the OS will crash us to forcefully terminate us. We choose
        // to quit here, rather than just close, to skip over any warning
        // dialogs (e.g. "Are you sure you want to close all tabs?") which might
        // prevent a WM_CLOSE from cleanly closing the window.
        //
        // This will cause a appHost._RequestQuitAll, which will notify the
        // monarch to collect up all the window state and save it.

        TraceLoggingWrite(
            g_hWindowsTerminalProvider,
            "EndSession",
            TraceLoggingDescription("Emitted when the OS has sent a WM_ENDSESSION"),
            TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE),
            TraceLoggingKeyword(TIL_KEYWORD_TRACE));

        _AutomaticShutdownRequestedHandlers();
        return true;
    }

Review comment (refactor):
Isn't this event redundant with the one for `WM_ENDSESSION`? If we return `true` here, then almost always we should also receive a `WM_ENDSESSION` event anyways right?
In that case you could remove this entire `case` because the `DefWindowProc` already returns `true`.

Code after:
    }
    case WM_ENDSESSION:
    {
        // For WM_QUERYENDSESSION and WM_ENDSESSION, refer to:
        //
        // https://docs.microsoft.com/en-us/windows/win32/rstmgr/guidelines-for-applications
        //
        // The OS will send us a WM_QUERYENDSESSION when it's preparing an
        // update for our app. It will then send us a WM_ENDSESSION, which gives
        // us a small timeout (~30s) to actually shut down gracefully. After
        // that timeout, it will send us a WM_CLOSE. If we still don't close
        // after the WM_CLOSE, it'll force-kill us (causing a crash which will be
        // bucketed to MoAppHang).
        //
        // If we need to do anything to prepare for being told to shutdown,
        // start it in WM_QUERYENDSESSION. If (in the future) we need to prevent
        // logoff, we can return false there. (DefWindowProc returns true)
        //
        // The OS is going to shut us down here. We will manually start a quit,
        // so that we can persist the state. If we refuse to gracefully shut
        // down here, the OS will crash us to forcefully terminate us. We choose
        // to quit here, rather than just close, to skip over any warning
        // dialogs (e.g. "Are you sure you want to close all tabs?") which might
        // prevent a WM_CLOSE from cleanly closing the window.
        //
        // This will cause a appHost._RequestQuitAll, which will notify the
        // monarch to collect up all the window state and save it.

        TraceLoggingWrite(
            g_hWindowsTerminalProvider,
            "EndSession",
            TraceLoggingDescription("Emitted when the OS has sent a WM_ENDSESSION"),
            TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE),
            TraceLoggingKeyword(TIL_KEYWORD_TRACE));

        _AutomaticShutdownRequestedHandlers();
        return true;
    }
    default:
        // We'll want to receive this message when explorer.exe restarts
        // so that we can re-add our icon to the notification area.
        // This unfortunately isn't a switch case because we register the
        // message at runtime.
        if (message == WM_TASKBARCREATED)
        {
            _NotifyReAddNotificationIconHandlers();
            return 0;
        }
    }

    // TODO: handle messages here...