diff --git a/Windows/MainWindow.cpp b/Windows/MainWindow.cpp index 4a39af7694..afef226700 100644 --- a/Windows/MainWindow.cpp +++ b/Windows/MainWindow.cpp @@ -816,6 +816,29 @@ namespace MainWindow }; } + bool ConfirmExit(HWND hWnd) { + GlobalUIState state = GetUIState(); + if (state != UISTATE_MENU && state != UISTATE_EXIT && g_Config.iAskForExitConfirmationAfterSeconds > 0) { + const double timeSinceSaveState = SaveState::SecondsSinceLastSavestate(); + const double timeSinceGameSave = SecondsSinceLastGameSave(); + + const double minTime = std::min(timeSinceSaveState, timeSinceGameSave); + if (minTime > g_Config.iAskForExitConfirmationAfterSeconds) { + auto di = GetI18NCategory(I18NCat::DIALOG); + auto mm = GetI18NCategory(I18NCat::MAINMENU); + std::string dlgMsg = ApplySafeSubstitutions(di->T("You haven't saved your progress for %1."), NiceTimeFormat((int)minTime)); + dlgMsg += '\n'; + dlgMsg += '\n'; + dlgMsg += di->T("Are you sure you want to exit? All unsaved progress will be lost."); + + if (IDNO == MessageBox(hWnd, ConvertUTF8ToWString(dlgMsg).c_str(), ConvertUTF8ToWString(mm->T("Exit")).c_str(), MB_YESNO | MB_ICONQUESTION)) { + return false; + } + } + } + return true; + } + LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { LRESULT darkResult = 0; if (UAHDarkModeWndProc(hWnd, message, wParam, lParam, &darkResult)) { @@ -1093,27 +1116,12 @@ namespace MainWindow break; case WM_CLOSE: - if (GetUIState() != UISTATE_MENU && g_Config.iAskForExitConfirmationAfterSeconds > 0) { - const double timeSinceSaveState = SaveState::SecondsSinceLastSavestate(); - const double timeSinceGameSave = SecondsSinceLastGameSave(); - - const double minTime = std::min(timeSinceSaveState, timeSinceGameSave); - if (minTime > g_Config.iAskForExitConfirmationAfterSeconds) { - auto di = GetI18NCategory(I18NCat::DIALOG); - auto mm = GetI18NCategory(I18NCat::MAINMENU); - std::string dlgMsg = ApplySafeSubstitutions(di->T("You haven't saved your progress for %1."), NiceTimeFormat((int)minTime)); - dlgMsg += '\n'; - dlgMsg += '\n'; - dlgMsg += di->T("Are you sure you want to exit? All unsaved progress will be lost."); - - if (IDNO == MessageBox(hWnd, ConvertUTF8ToWString(dlgMsg).c_str(), ConvertUTF8ToWString(mm->T("Exit")).c_str(), MB_YESNO | MB_ICONQUESTION)) { - return 0; - } - } + { + if (ConfirmExit(hWnd)) { + DestroyWindow(hWnd); } - - DestroyWindow(hWnd); return 0; + } case WM_DESTROY: InputDevice::StopPolling(); diff --git a/Windows/MainWindow.h b/Windows/MainWindow.h index 5bed554c71..2d3b395e3f 100644 --- a/Windows/MainWindow.h +++ b/Windows/MainWindow.h @@ -82,6 +82,7 @@ namespace MainWindow void SetWindowSize(int zoom); void RunCallbackInWndProc(void (*callback)(void *window, void *userdata), void *userdata); void SetKeepScreenBright(bool keepBright); + bool ConfirmExit(HWND hWnd); } #endif diff --git a/Windows/MainWindowMenu.cpp b/Windows/MainWindowMenu.cpp index 7b2443a3b9..b567ebd5e6 100644 --- a/Windows/MainWindowMenu.cpp +++ b/Windows/MainWindowMenu.cpp @@ -443,7 +443,7 @@ namespace MainWindow { PostMessage(MainWindow::GetHWND(), WM_USER_RESTART_EMUTHREAD, 0, 0); } else { g_Config.bRestartRequired = true; - PostMessage(MainWindow::GetHWND(), WM_CLOSE, 0, 0); + DestroyWindow(MainWindow::GetHWND()); } } @@ -755,7 +755,9 @@ namespace MainWindow { case ID_OPTIONS_FRAMESKIPTYPE_PRCNT: setFrameSkippingType(FRAMESKIPTYPE_PRCNT); break; case ID_FILE_EXIT: - PostMessage(hWnd, WM_CLOSE, 0, 0); + if (MainWindow::ConfirmExit(hWnd)) { + DestroyWindow(hWnd); + } break; case ID_DEBUG_BREAKONLOAD: diff --git a/Windows/main.cpp b/Windows/main.cpp index 1cd48783ec..f95e043512 100644 --- a/Windows/main.cpp +++ b/Windows/main.cpp @@ -537,7 +537,8 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string switch (type) { case SystemRequestType::EXIT_APP: if (!NativeIsRestarting()) { - PostMessage(MainWindow::GetHWND(), WM_CLOSE, 0, 0); + // Clean exit is handled in WM_DESTROY. + DestroyWindow(MainWindow::GetHWND()); } return true; case SystemRequestType::RESTART_APP: @@ -549,7 +550,7 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string PostMessage(MainWindow::GetHWND(), MainWindow::WM_USER_RESTART_EMUTHREAD, 0, 0); } else { g_Config.bRestartRequired = true; - PostMessage(MainWindow::GetHWND(), WM_CLOSE, 0, 0); + DestroyWindow(MainWindow::GetHWND()); } return true; }