From 683cef96b9fdb89f019a313d77e788d85f8b5103 Mon Sep 17 00:00:00 2001 From: Il Harper Date: Thu, 25 May 2023 14:31:00 +0800 Subject: [PATCH] refactor(shellwin): refactor code --- .clang-tidy | 15 +- .../include/koishell/mode/webview.hpp | 1 + packages/shellwin/src/mode/dialog.cpp | 30 ++-- packages/shellwin/src/mode/webview.cpp | 152 ++++++++---------- packages/shellwin/src/util/strings.cpp | 6 +- 5 files changed, 103 insertions(+), 101 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 211f9fe7..8d5857b6 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,6 +1,19 @@ Checks: > *, - -llvmlibc-callee-namespace, + -llvmlibc-*, -google-runtime-int, + -hicpp-use-auto, + -modernize-use-trailing-return-type, + -modernize-use-auto, + -readability-implicit-bool-conversion, + -readability-braces-around-statements, + -hicpp-braces-around-statements, + -google-readability-braces-around-statements, + -cppcoreguidelines-init-variables, + -cppcoreguidelines-pro-type-reinterpret-cast, + -cppcoreguidelines-pro-type-member-init, + -cppcoreguidelines-pro-bounds-pointer-arithmetic, -cppcoreguidelines-avoid-magic-numbers, -readability-magic-numbers + +WarningsAsErrors: '*' diff --git a/packages/shellwin/include/koishell/mode/webview.hpp b/packages/shellwin/include/koishell/mode/webview.hpp index ed06ce73..08272a3b 100644 --- a/packages/shellwin/include/koishell/mode/webview.hpp +++ b/packages/shellwin/include/koishell/mode/webview.hpp @@ -32,6 +32,7 @@ class WebViewWindow { _In_ LPARAM lParam); void OnMessage(std::wstring *message); + void SyncTheme(std::wstring *message); public: WebViewWindow(_In_ HINSTANCE hInstance, _In_ int nCmdShow, _In_ njson arg); diff --git a/packages/shellwin/src/mode/dialog.cpp b/packages/shellwin/src/mode/dialog.cpp index 013bc7e0..c13f7883 100644 --- a/packages/shellwin/src/mode/dialog.cpp +++ b/packages/shellwin/src/mode/dialog.cpp @@ -3,47 +3,47 @@ namespace KoiShell { int RunDialog(_In_ HINSTANCE hInstance, _In_ njson arg) { - std::string titleS = arg["title"]; - wchar_t *title = KoiShell::UTF8ToWideChar(const_cast(titleS.c_str())); + const std::string titleS = arg["title"]; + wchar_t *title = KoiShell::UTF8ToWideChar(titleS.c_str()); if (!title) LogAndFailWithLastError(L"Failed to parse title."); - std::string text1S = arg["text1"]; - wchar_t *text1 = KoiShell::UTF8ToWideChar(const_cast(text1S.c_str())); + const std::string text1S = arg["text1"]; + wchar_t *text1 = KoiShell::UTF8ToWideChar(text1S.c_str()); if (!text1) LogAndFailWithLastError(L"Failed to parse text1."); - std::string text2S = arg["text2"]; - wchar_t *text2 = KoiShell::UTF8ToWideChar(const_cast(text2S.c_str())); + const std::string text2S = arg["text2"]; + wchar_t *text2 = KoiShell::UTF8ToWideChar(text2S.c_str()); if (!text2) LogAndFailWithLastError(L"Failed to parse text2."); - unsigned int buttonCount = arg["buttonCount"]; + const unsigned int buttonCount = arg["buttonCount"]; TASKDIALOG_BUTTON *buttons = new TASKDIALOG_BUTTON[buttonCount]; if (buttonCount >= 1) { - std::string textS = arg.value("button1Text", "OK"); - wchar_t *text = KoiShell::UTF8ToWideChar(const_cast(textS.c_str())); + const std::string textS = arg.value("button1Text", "OK"); + wchar_t *text = KoiShell::UTF8ToWideChar(textS.c_str()); if (!text) LogAndFailWithLastError(L"Failed to parse button1Text."); buttons[0].nButtonID = 10; buttons[0].pszButtonText = text; } if (buttonCount >= 2) { - std::string textS = arg.value("button2Text", "Cancel"); - wchar_t *text = KoiShell::UTF8ToWideChar(const_cast(textS.c_str())); + const std::string textS = arg.value("button2Text", "Cancel"); + wchar_t *text = KoiShell::UTF8ToWideChar(textS.c_str()); if (!text) LogAndFailWithLastError(L"Failed to parse button2Text."); buttons[0].nButtonID = 11; buttons[0].pszButtonText = text; } if (buttonCount >= 3) { - std::string textS = arg.value("button3Text", "Don't Save"); - wchar_t *text = KoiShell::UTF8ToWideChar(const_cast(textS.c_str())); + const std::string textS = arg.value("button3Text", "Don't Save"); + wchar_t *text = KoiShell::UTF8ToWideChar(textS.c_str()); if (!text) LogAndFailWithLastError(L"Failed to parse button3Text."); buttons[0].nButtonID = 12; buttons[0].pszButtonText = text; } wchar_t *icon; - std::string style = arg["style"]; + const std::string style = arg["style"]; if (style == "info") icon = TD_INFORMATION_ICON; else if (style == "warn") @@ -78,7 +78,7 @@ int RunDialog(_In_ HINSTANCE hInstance, _In_ njson arg) { dlg.cxWidth = 0; int result = 0; - long success = TaskDialogIndirect(&dlg, &result, nullptr, nullptr); + const long success = TaskDialogIndirect(&dlg, &result, nullptr, nullptr); result -= 9; if (success) { std::wstringstream s; diff --git a/packages/shellwin/src/mode/webview.cpp b/packages/shellwin/src/mode/webview.cpp index 9a3fdcb3..cf818936 100644 --- a/packages/shellwin/src/mode/webview.cpp +++ b/packages/shellwin/src/mode/webview.cpp @@ -29,7 +29,7 @@ int WebViewWindow::Run() { wchar_t udf[MAX_PATH]; if (!PathCombineW(udf, cwd, L"data\\home\\WebView2")) LogAndFailWithLastError(L"Failed to combine udf."); - int udfErr = SHCreateDirectoryExW(nullptr, udf, nullptr); + const int udfErr = SHCreateDirectoryExW(nullptr, udf, nullptr); if (udfErr != ERROR_SUCCESS && udfErr != ERROR_FILE_EXISTS && udfErr != ERROR_ALREADY_EXISTS) { std::wstringstream s; @@ -38,21 +38,21 @@ int WebViewWindow::Run() { LogAndFail(s.str()); } - std::string nameS = arg["name"]; + const std::string nameS = arg["name"]; wchar_t *nameC = KoiShell::UTF8ToWideChar(const_cast(nameS.c_str())); if (!nameC) LogAndFailWithLastError(L"Failed to parse nameC."); std::wostringstream nameStream; nameStream << nameC << KoiShellWebViewTitleSuffix; - std::wstring name = nameStream.str(); + const std::wstring name = nameStream.str(); - std::string urlS = arg["url"]; - wchar_t *url = KoiShell::UTF8ToWideChar(const_cast(urlS.c_str())); + const std::string urlS = arg["url"]; + wchar_t *url = KoiShell::UTF8ToWideChar(urlS.c_str()); if (!url) LogAndFailWithLastError(L"Failed to parse url."); HRSRC userscriptRc = FindResourceW(hInstance, MAKEINTRESOURCEW(102), MAKEINTRESOURCEW(256)); HGLOBAL userscriptRcData = LoadResource(hInstance, userscriptRc); - unsigned long userscriptSize = SizeofResource(hInstance, userscriptRc); + const unsigned long userscriptSize = SizeofResource(hInstance, userscriptRc); const char *userscriptData = static_cast(LockResource(userscriptRcData)); char *userscriptS = new char[userscriptSize + 1]; @@ -291,89 +291,77 @@ LRESULT CALLBACK WebViewWindow::WndProc( } void WebViewWindow::OnMessage(std::wstring *message) { - unsigned long long const len = message->length(); - - if ((*message)[0] == 'T') { - // Sync theme - int dwmUseDarkMode; - - switch ((*message)[1]) { - case 'D': - dwmUseDarkMode = 1; - DwmSetWindowAttribute( - hWnd, - supports >= 2 ? 20 - : // DWMWINDOWATTRIBUTE::DWMWA_USE_IMMERSIVE_DARK_MODE - // = 20 (starting from 18985) - 19, // DWMWINDOWATTRIBUTE::DWMWA_USE_IMMERSIVE_DARK_MODE - // = 19 (before 18985), - &dwmUseDarkMode, - sizeof(dwmUseDarkMode)); - break; - case 'L': - dwmUseDarkMode = 0; - DwmSetWindowAttribute( - hWnd, - supports >= 2 ? 20 - : // DWMWINDOWATTRIBUTE::DWMWA_USE_IMMERSIVE_DARK_MODE - // = 20 (starting from 18985) - 19, // DWMWINDOWATTRIBUTE::DWMWA_USE_IMMERSIVE_DARK_MODE - // = 19 (before 18985), - &dwmUseDarkMode, - sizeof(dwmUseDarkMode)); - break; - case 'R': - dwmUseDarkMode = 0; - DwmSetWindowAttribute( - hWnd, - supports >= 2 ? 20 - : // DWMWINDOWATTRIBUTE::DWMWA_USE_IMMERSIVE_DARK_MODE - // = 20 (starting from 18985) - 19, // DWMWINDOWATTRIBUTE::DWMWA_USE_IMMERSIVE_DARK_MODE - // = 19 (before 18985), - &dwmUseDarkMode, - sizeof(dwmUseDarkMode)); - break; - } + if ((*message)[0] == 'T') SyncTheme(message); +} - if (len < 3) return; - // Sync theme color - unsigned long color; +void WebViewWindow::SyncTheme(std::wstring *message) { + int dwmUseDarkMode; - // Border - color = std::stoul( - std::wstring() + (*message)[7] + (*message)[8] + (*message)[5] + - (*message)[6] + (*message)[3] + (*message)[4], - nullptr, - 16); - DwmSetWindowAttribute( - hWnd, - 34, // DWMWINDOWATTRIBUTE::DWMWA_BORDER_COLOR - &color, - sizeof(color)); - // Caption - color = std::stoul( - std::wstring() + (*message)[13] + (*message)[14] + (*message)[11] + - (*message)[12] + (*message)[9] + (*message)[10], - nullptr, - 16); + switch ((*message)[1]) { + case 'D': + dwmUseDarkMode = 1; DwmSetWindowAttribute( hWnd, - 35, // DWMWINDOWATTRIBUTE::DWMWA_CAPTION_COLOR - &color, - sizeof(color)); - // Caption text - color = std::stoul( - std::wstring() + (*message)[19] + (*message)[20] + (*message)[17] + - (*message)[18] + (*message)[15] + (*message)[16], - nullptr, - 16); + supports >= 2 ? 20 + : // DWMWINDOWATTRIBUTE::DWMWA_USE_IMMERSIVE_DARK_MODE + // = 20 (starting from 18985) + 19, // DWMWINDOWATTRIBUTE::DWMWA_USE_IMMERSIVE_DARK_MODE + // = 19 (before 18985), + &dwmUseDarkMode, + sizeof(dwmUseDarkMode)); + break; + case 'L': + case 'R': + dwmUseDarkMode = 0; DwmSetWindowAttribute( hWnd, - 36, // DWMWINDOWATTRIBUTE::DWMWA_TEXT_COLOR - &color, - sizeof(color)); + supports >= 2 ? 20 + : // DWMWINDOWATTRIBUTE::DWMWA_USE_IMMERSIVE_DARK_MODE + // = 20 (starting from 18985) + 19, // DWMWINDOWATTRIBUTE::DWMWA_USE_IMMERSIVE_DARK_MODE + // = 19 (before 18985), + &dwmUseDarkMode, + sizeof(dwmUseDarkMode)); + break; } + + if (message->length() < 3) return; + // Sync theme color + unsigned long color; + + // Border + color = std::stoul( + std::wstring() + (*message)[7] + (*message)[8] + (*message)[5] + + (*message)[6] + (*message)[3] + (*message)[4], + nullptr, + 16); + DwmSetWindowAttribute( + hWnd, + 34, // DWMWINDOWATTRIBUTE::DWMWA_BORDER_COLOR + &color, + sizeof(color)); + // Caption + color = std::stoul( + std::wstring() + (*message)[13] + (*message)[14] + (*message)[11] + + (*message)[12] + (*message)[9] + (*message)[10], + nullptr, + 16); + DwmSetWindowAttribute( + hWnd, + 35, // DWMWINDOWATTRIBUTE::DWMWA_CAPTION_COLOR + &color, + sizeof(color)); + // Caption text + color = std::stoul( + std::wstring() + (*message)[19] + (*message)[20] + (*message)[17] + + (*message)[18] + (*message)[15] + (*message)[16], + nullptr, + 16); + DwmSetWindowAttribute( + hWnd, + 36, // DWMWINDOWATTRIBUTE::DWMWA_TEXT_COLOR + &color, + sizeof(color)); } int RunWebView(_In_ HINSTANCE hInstance, _In_ int nCmdShow, _In_ njson arg) { diff --git a/packages/shellwin/src/util/strings.cpp b/packages/shellwin/src/util/strings.cpp index 245ff0fc..8b7c18d6 100644 --- a/packages/shellwin/src/util/strings.cpp +++ b/packages/shellwin/src/util/strings.cpp @@ -3,7 +3,7 @@ namespace KoiShell { char *WideCharToUTF8(_In_ wchar_t *w) { - int len = WideCharToMultiByte( + const int len = WideCharToMultiByte( CP_UTF8, WC_ERR_INVALID_CHARS, w, -1, nullptr, 0, nullptr, nullptr); if (!len) return nullptr; char *s = new char[len + 1]; @@ -14,7 +14,7 @@ char *WideCharToUTF8(_In_ wchar_t *w) { } wchar_t *UTF8ToWideChar(_In_ char *s) { - int len = + const int len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, s, -1, nullptr, 0); if (!len) return nullptr; wchar_t *w = new wchar_t[len + 1]; @@ -24,7 +24,7 @@ wchar_t *UTF8ToWideChar(_In_ char *s) { } wchar_t *UTF8ToWideChar(_In_ const char *s) { - int len = + const int len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, s, -1, nullptr, 0); if (!len) return nullptr; wchar_t *w = new wchar_t[len + 1];