code refactoring

fix casting warning
disable automatic window content refresh on resize, this is required by the upcoming support for WebView2
This commit is contained in:
Enrico Giordani
2019-10-03 18:26:09 +02:00
committed by Enrico Giordani
parent 7db627df1b
commit 1fc351b088

View File

@@ -205,7 +205,7 @@ void receive_message_from_webview(const std::wstring& msg) {
} }
} }
void initialize_webview(HWND hwnd, int nCmdShow) { void initialize_webview() {
try { try {
if (!g_webview_process) { if (!g_webview_process) {
g_webview_process = WebViewControlProcess(); g_webview_process = WebViewControlProcess();
@@ -225,7 +225,7 @@ void initialize_webview(HWND hwnd, int nCmdShow) {
g_webview.NewWindowRequested([=](IWebViewControl sender_requester, WebViewControlNewWindowRequestedEventArgs args) { g_webview.NewWindowRequested([=](IWebViewControl sender_requester, WebViewControlNewWindowRequestedEventArgs args) {
// Open the requested link in the default browser registered in the Shell // Open the requested link in the default browser registered in the Shell
int res = (int)ShellExecute(nullptr, L"open", args.Uri().AbsoluteUri().c_str(), nullptr, nullptr, SW_SHOWNORMAL); int res = static_cast<int>(reinterpret_cast<uintptr_t>(ShellExecute(nullptr, L"open", args.Uri().AbsoluteUri().c_str(), nullptr, nullptr, SW_SHOWNORMAL)));
WINRT_VERIFY(res > 32); WINRT_VERIFY(res > 32);
}); });
@@ -249,7 +249,7 @@ void initialize_webview(HWND hwnd, int nCmdShow) {
NavigateToLocalhostReactServer(); NavigateToLocalhostReactServer();
#else #else
// Navigates to settings-html/index.html. // Navigates to settings-html/index.html.
ShowWindow(g_main_wnd, nCmdShow);
NavigateToUri(L"index.html"); NavigateToUri(L"index.html");
#endif #endif
} else if (status == AsyncStatus::Error) { } else if (status == AsyncStatus::Error) {
@@ -343,7 +343,7 @@ void register_classes(HINSTANCE hInstance) {
WNDCLASSEXW wcex; WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX); wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.style = 0;
wcex.lpfnWndProc = wnd_proc_static; wcex.lpfnWndProc = wnd_proc_static;
wcex.cbClsExtra = 0; wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0; wcex.cbWndExtra = 0;
@@ -358,9 +358,7 @@ void register_classes(HINSTANCE hInstance) {
WINRT_VERIFY(RegisterClassExW(&wcex)); WINRT_VERIFY(RegisterClassExW(&wcex));
} }
int init_instance(HINSTANCE hInstance, int nShowCmd) { HWND create_main_window(HINSTANCE hInstance) {
g_hinst = hInstance;
RECT desktopRect; RECT desktopRect;
const HWND hDesktop = GetDesktopWindow(); const HWND hDesktop = GetDesktopWindow();
WINRT_VERIFY(hDesktop); WINRT_VERIFY(hDesktop);
@@ -370,7 +368,7 @@ int init_instance(HINSTANCE hInstance, int nShowCmd) {
int wind_height = 700; int wind_height = 700;
DPIAware::Convert(nullptr, wind_width, wind_height); DPIAware::Convert(nullptr, wind_width, wind_height);
g_main_wnd = CreateWindowW( return CreateWindowW(
L"PTSettingsClass", L"PTSettingsClass",
L"PowerToys Settings", L"PowerToys Settings",
WS_OVERLAPPEDWINDOW, WS_OVERLAPPEDWINDOW,
@@ -382,12 +380,6 @@ int init_instance(HINSTANCE hInstance, int nShowCmd) {
nullptr, nullptr,
hInstance, hInstance,
nullptr); nullptr);
WINRT_VERIFY(g_main_wnd);
initialize_webview(g_main_wnd, nShowCmd);
WINRT_VERIFY(UpdateWindow(g_main_wnd));
return TRUE;
} }
void wait_on_parent_process_thread(DWORD pid) { void wait_on_parent_process_thread(DWORD pid) {
@@ -412,7 +404,7 @@ void quit_when_parent_terminates(std::wstring parent_pid) {
std::thread(wait_on_parent_process_thread,pid).detach(); std::thread(wait_on_parent_process_thread,pid).detach();
} }
void read_arguments() { void initialize_message_pipe() {
// Expected calling arguments: // Expected calling arguments:
// [0] - This executable's path. // [0] - This executable's path.
// [1] - PowerToys pipe server. // [1] - PowerToys pipe server.
@@ -435,14 +427,19 @@ void read_arguments() {
LocalFree(argument_list); LocalFree(argument_list);
} }
int start_webview_window(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd) {
// To be unlocked after the Window has finished being created. CoInitialize(nullptr);
g_window_created.lock();
read_arguments(); g_hinst = hInstance;
g_window_created.lock(); // To be unlocked after the main window has finished being created.
initialize_message_pipe();
register_classes(hInstance); register_classes(hInstance);
init_instance(hInstance, nShowCmd); g_main_wnd = create_main_window(hInstance);
initialize_webview();
WINRT_VERIFY(ShowWindow(g_main_wnd, nShowCmd));
// Main message loop.
MSG msg; MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0)) { while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg); TranslateMessage(&msg);
DispatchMessage(&msg); DispatchMessage(&msg);
@@ -450,8 +447,3 @@ int start_webview_window(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpC
return (int)msg.wParam; return (int)msg.wParam;
} }
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd) {
CoInitialize(nullptr);
return start_webview_window(hInstance, hPrevInstance, lpCmdLine, nShowCmd);
}