Skip to content

Commit

Permalink
refactor(shellwin): refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
ilharp committed May 25, 2023
1 parent ebd24ae commit 683cef9
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 101 deletions.
15 changes: 14 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
@@ -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: '*'
1 change: 1 addition & 0 deletions packages/shellwin/include/koishell/mode/webview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 15 additions & 15 deletions packages/shellwin/src/mode/dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char *>(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<char *>(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<char *>(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<char *>(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<char *>(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<char *>(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")
Expand Down Expand Up @@ -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;
Expand Down
152 changes: 70 additions & 82 deletions packages/shellwin/src/mode/webview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<char *>(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<char *>(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<const char *>(LockResource(userscriptRcData));
char *userscriptS = new char[userscriptSize + 1];
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions packages/shellwin/src/util/strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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];
Expand All @@ -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];
Expand Down

0 comments on commit 683cef9

Please sign in to comment.