exit Settings on fatal error

fix error checking
This commit is contained in:
Enrico Giordani
2019-10-05 08:47:04 +02:00
committed by Enrico Giordani
parent 5f8c4ea143
commit 4b273a7e39

View File

@@ -205,16 +205,13 @@ void receive_message_from_webview(const std::wstring& msg) {
void initialize_webview() { void initialize_webview() {
try { try {
if (!g_webview_process) { g_webview_process = WebViewControlProcess();
g_webview_process = WebViewControlProcess();
WINRT_VERIFY(g_webview_process);
}
auto asyncwebview = g_webview_process.CreateWebViewControlAsync((int64_t)g_main_wnd, client_rect_to_bounds_rect(g_main_wnd)); auto asyncwebview = g_webview_process.CreateWebViewControlAsync((int64_t)g_main_wnd, client_rect_to_bounds_rect(g_main_wnd));
asyncwebview.Completed([=](IAsyncOperation<WebViewControl> const& sender, AsyncStatus status) { asyncwebview.Completed([=](IAsyncOperation<WebViewControl> const& sender, AsyncStatus status) {
if (status == AsyncStatus::Completed) { if (status == AsyncStatus::Completed) {
WINRT_VERIFY(sender); WINRT_VERIFY(sender != nullptr);
g_webview = sender.GetResults(); g_webview = sender.GetResults();
WINRT_VERIFY(g_webview); WINRT_VERIFY(g_webview != nullptr);
// In order to receive window.external.notify() calls in ScriptNotify // In order to receive window.external.notify() calls in ScriptNotify
g_webview.Settings().IsScriptNotifyAllowed(true); g_webview.Settings().IsScriptNotifyAllowed(true);
@@ -247,19 +244,18 @@ void initialize_webview() {
NavigateToLocalhostReactServer(); NavigateToLocalhostReactServer();
#else #else
// Navigates to settings-html/index.html. // Navigates to settings-html/index.html.
NavigateToUri(L"index.html"); NavigateToUri(L"index.html");
#endif #endif
} else if (status == AsyncStatus::Error) { } else if (status == AsyncStatus::Error) {
// TODO: report the error and quit, or try to use WebView2. MessageBox(NULL, L"Failed to create the WebView control.\nPlease report the bug to https://github.com/microsoft/PowerToys/issues", L"PowerToys Settings Error", MB_OK);
exit(1);
} else if (status == AsyncStatus::Started) { } else if (status == AsyncStatus::Started) {
// Ignore. // Ignore.
} else if (status == AsyncStatus::Canceled) { } else if (status == AsyncStatus::Canceled) {
// Ignore. // Ignore.
} }
}); });
} } catch (hresult_error const& e) {
catch (hresult_error const& e) {
WCHAR message[1024] = L""; WCHAR message[1024] = L"";
StringCchPrintf(message, ARRAYSIZE(message), L"failed: %ls", e.message().c_str()); StringCchPrintf(message, ARRAYSIZE(message), L"failed: %ls", e.message().c_str());
MessageBox(g_main_wnd, message, L"Error", MB_OK); MessageBox(g_main_wnd, message, L"Error", MB_OK);
@@ -269,7 +265,7 @@ void initialize_webview() {
LRESULT CALLBACK wnd_proc_static(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { LRESULT CALLBACK wnd_proc_static(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) { switch (message) {
case WM_CLOSE: case WM_CLOSE:
if(g_waiting_for_close_confirmation) { if (g_waiting_for_close_confirmation) {
// If another WM_CLOSE is received while waiting for webview confirmation, // If another WM_CLOSE is received while waiting for webview confirmation,
// allow DefWindowProc to be called and destroy the window. // allow DefWindowProc to be called and destroy the window.
break; break;
@@ -296,24 +292,24 @@ LRESULT CALLBACK wnd_proc_static(HWND hWnd, UINT message, WPARAM wParam, LPARAM
wm_destroy_window = RegisterWindowMessageW(L"PTSettingsParentTerminated"); wm_destroy_window = RegisterWindowMessageW(L"PTSettingsParentTerminated");
break; break;
case WM_DPICHANGED: case WM_DPICHANGED:
{ {
// Resize the window using the suggested rect // Resize the window using the suggested rect
RECT* const prcNewWindow = (RECT*)lParam; RECT* const prcNewWindow = (RECT*)lParam;
SetWindowPos(hWnd, SetWindowPos(hWnd,
nullptr, nullptr,
prcNewWindow->left, prcNewWindow->left,
prcNewWindow->top, prcNewWindow->top,
prcNewWindow->right - prcNewWindow->left, prcNewWindow->right - prcNewWindow->left,
prcNewWindow->bottom - prcNewWindow->top, prcNewWindow->bottom - prcNewWindow->top,
SWP_NOZORDER | SWP_NOACTIVATE); SWP_NOZORDER | SWP_NOACTIVATE);
} }
break; break;
case WM_NCCREATE: case WM_NCCREATE:
{ {
// Enable auto-resizing the title bar // Enable auto-resizing the title bar
EnableNonClientDpiScaling(hWnd); EnableNonClientDpiScaling(hWnd);
} }
break; break;
default: default:
if (message == wm_data_for_webview) { if (message == wm_data_for_webview) {
PCOPYDATASTRUCT msg = (PCOPYDATASTRUCT)lParam; PCOPYDATASTRUCT msg = (PCOPYDATASTRUCT)lParam;
@@ -358,7 +354,7 @@ void register_classes(HINSTANCE hInstance) {
HWND create_main_window(HINSTANCE hInstance) { HWND create_main_window(HINSTANCE hInstance) {
RECT desktopRect; RECT desktopRect;
const HWND hDesktop = GetDesktopWindow(); const HWND hDesktop = GetDesktopWindow();
WINRT_VERIFY(hDesktop); WINRT_VERIFY(hDesktop != nullptr);
WINRT_VERIFY(GetWindowRect(hDesktop, &desktopRect)); WINRT_VERIFY(GetWindowRect(hDesktop, &desktopRect));
int wind_width = 1024; int wind_width = 1024;
@@ -396,7 +392,7 @@ void wait_on_parent_process_thread(DWORD pid) {
void quit_when_parent_terminates(std::wstring parent_pid) { void quit_when_parent_terminates(std::wstring parent_pid) {
DWORD pid = std::stol(parent_pid); DWORD pid = std::stol(parent_pid);
std::thread(wait_on_parent_process_thread,pid).detach(); std::thread(wait_on_parent_process_thread, pid).detach();
} }
void initialize_message_pipe() { void initialize_message_pipe() {
@@ -427,7 +423,8 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
if (is_process_elevated()) { if (is_process_elevated()) {
if (!drop_elevated_privileges()) { if (!drop_elevated_privileges()) {
MessageBox(NULL, L"Failed to drop admin privileges.\nPlease report the bug to https://github.com/microsoft/PowerToys/issues.", L"PowerToys Settings Error", MB_OK); MessageBox(NULL, L"Failed to drop admin privileges.\nPlease report the bug to https://github.com/microsoft/PowerToys/issues", L"PowerToys Settings Error", MB_OK);
exit(1);
} }
} }
@@ -436,7 +433,8 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
register_classes(hInstance); register_classes(hInstance);
g_main_wnd = create_main_window(hInstance); g_main_wnd = create_main_window(hInstance);
if (g_main_wnd == nullptr) { if (g_main_wnd == nullptr) {
MessageBox(NULL, L"Failed to create main window.\nPlease report the bug to https://github.com/microsoft/PowerToys/issues.", L"PowerToys Settings Error", MB_OK); MessageBox(NULL, L"Failed to create main window.\nPlease report the bug to https://github.com/microsoft/PowerToys/issues", L"PowerToys Settings Error", MB_OK);
exit(1);
} }
initialize_webview(); initialize_webview();
ShowWindow(g_main_wnd, nShowCmd); ShowWindow(g_main_wnd, nShowCmd);