feat: add query-session-end and improve session-end events on Windows (#44598)

* feat: add query-session-end event for Windows

* fix: remove debug line

* feat: notify with reason on session-end

* docs: add comments and return params

* docs: add same docs to the BrowserWindow

* fix: add shutdown reason if lParam == 0

* docs: remove 'force' word

* docs: revert multithreading.md change

* docs: add reasons documentation, reason variable renamed to reasons

* docs: improve 'shutdown' reason wording

* docs: reword with 'can be'

* fix: pass reasons by reference

* fix: use newer approach which expose reasons value directly on Event object

* docs: add escaping

* style: linter fixes

* fix: project now should compile

* fix: EmitWithoutEvent method added, EmitWithEvent moved to private again

* docs: typo fix

Co-authored-by: Sam Maddock <samuel.maddock@gmail.com>

* docs: dedicated WindowSessionEndEvent type created

* docs: better wording for session-end event description

Co-authored-by: Will Anderson <will@itsananderson.com>

---------

Co-authored-by: Sam Maddock <samuel.maddock@gmail.com>
Co-authored-by: Will Anderson <will@itsananderson.com>
This commit is contained in:
Savely Krasovsky 2024-11-22 20:47:36 +01:00 committed by GitHub
parent 0285592d61
commit c5ea177b3d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 135 additions and 12 deletions

View file

@ -27,6 +27,24 @@ namespace electron {
namespace {
// Convert Win32 WM_QUERYENDSESSIONS to strings.
const std::vector<std::string> EndSessionToStringVec(LPARAM end_session_id) {
std::vector<std::string> params;
if (end_session_id == 0) {
params.push_back("shutdown");
return params;
}
if (end_session_id & ENDSESSION_CLOSEAPP)
params.push_back("close-app");
if (end_session_id & ENDSESSION_CRITICAL)
params.push_back("critical");
if (end_session_id & ENDSESSION_LOGOFF)
params.push_back("logoff");
return params;
}
// Convert Win32 WM_APPCOMMANDS to strings.
constexpr std::string_view AppCommandToString(int command_id) {
switch (command_id) {
@ -389,9 +407,20 @@ bool NativeWindowViews::PreHandleMSG(UINT message,
}
return false;
}
case WM_QUERYENDSESSION: {
bool prevent_default = false;
std::vector<std::string> reasons = EndSessionToStringVec(l_param);
NotifyWindowQueryEndSession(reasons, &prevent_default);
// Result should be TRUE by default, otherwise WM_ENDSESSION will not be
// fired in some cases: More:
// https://learn.microsoft.com/en-us/windows/win32/rstmgr/guidelines-for-applications
*result = !prevent_default;
return prevent_default;
}
case WM_ENDSESSION: {
std::vector<std::string> reasons = EndSessionToStringVec(l_param);
if (w_param) {
NotifyWindowEndSession();
NotifyWindowEndSession(reasons);
}
return false;
}