From a6aa9fc6ac345eceea3a305584c7ba09c044d527 Mon Sep 17 00:00:00 2001 From: weisj Date: Tue, 12 May 2020 13:50:37 +0200 Subject: [PATCH] Updated formatting for c++. --- darklaf_cpp.eclipseformat.xml | 28 +- windows/src/main/cpp/Decorations.cpp | 401 ++++++++++++--------------- windows/src/main/cpp/Decorations.h | 9 +- windows/src/main/cpp/ThemeInfo.cpp | 116 +++----- 4 files changed, 238 insertions(+), 316 deletions(-) diff --git a/darklaf_cpp.eclipseformat.xml b/darklaf_cpp.eclipseformat.xml index 09e4c03c..757623af 100644 --- a/darklaf_cpp.eclipseformat.xml +++ b/darklaf_cpp.eclipseformat.xml @@ -45,7 +45,7 @@ - + @@ -67,7 +67,7 @@ - + - + - + - + @@ -97,9 +97,9 @@ - + - + - + - + @@ -167,7 +167,7 @@ - + @@ -205,10 +205,10 @@ - + - + @@ -216,7 +216,7 @@ value="do not insert"/> - + @@ -259,7 +259,7 @@ - + wrapper_map = std::map(); -bool Maximized(HWND hwnd) -{ +bool Maximized(HWND hwnd) { WINDOWPLACEMENT placement; - if (!GetWindowPlacement(hwnd, &placement)) - return false; + if (!GetWindowPlacement(hwnd, &placement)) return false; return placement.showCmd == SW_MAXIMIZE; } -LRESULT HandleHitTest(WindowWrapper *wrapper, int x, int y) -{ - if (wrapper->popup_menu) - return HTCLIENT; +LRESULT HandleHitTest(WindowWrapper *wrapper, int x, int y) { + if (wrapper->popup_menu) return HTCLIENT; - POINT ptMouse = { x, - y }; + POINT ptMouse = { x, y }; // Get the window rectangle. RECT rcWindow; @@ -58,12 +53,13 @@ LRESULT HandleHitTest(WindowWrapper *wrapper, int x, int y) USHORT uRow = 1; USHORT uCol = 1; - if (!Maximized(wrapper->window)) - { - /* The horizontal frame should be the same size as the vertical frame, - since the NONCLIENTMETRICS structure does not distinguish between them */ + if (!Maximized(wrapper->window)) { + /* + * The horizontal frame should be the same size as the vertical frame, + * since the NONCLIENTMETRICS structure does not distinguish between them + */ int frame_size = GetSystemMetrics(SM_CXFRAME) + GetSystemMetrics(SM_CXPADDEDBORDER); - /* The diagonal size handles are wider than the frame */ + // The diagonal size handles are wider than the frame int diagonal_width = frame_size * 2 + GetSystemMetrics(SM_CXBORDER); bool top = ptMouse.y >= rcWindow.top && ptMouse.y < rcWindow.top + frame_size; @@ -78,105 +74,82 @@ LRESULT HandleHitTest(WindowWrapper *wrapper, int x, int y) bool diag_left = ptMouse.x >= rcWindow.left && ptMouse.x < rcWindow.left + frame_size; bool diag_right = !diag_left && (ptMouse.x < rcWindow.right && ptMouse.x >= rcWindow.right - diagonal_width); - if (top) - uRow = 0; - if (bottom) - uRow = 2; - - if (top || bottom) - { - if (diag_left) - uCol = 0; - else if (diag_right) - uCol = 2; - } - else - { - if (left) - uCol = 0; - if (right) - uCol = 2; - - if (left || right) - { - if (diag_top) - uRow = 0; - else if (diag_bottom) - uRow = 2; + if (top) uRow = 0; + if (bottom) uRow = 2; + + if (top || bottom) { + if (diag_left) uCol = 0; + else if (diag_right) uCol = 2; + } else { + if (left) uCol = 0; + if (right) uCol = 2; + + if (left || right) { + if (diag_top) uRow = 0; + else if (diag_bottom) uRow = 2; } } } // Hit test (HTTOPLEFT, ... HTBOTTOMRIGHT) - LRESULT hitTests[3][3] = { { HTTOPLEFT, - HTTOP, - HTTOPRIGHT }, - { HTLEFT, - HTNOWHERE, - HTRIGHT }, - { HTBOTTOMLEFT, - HTBOTTOM, - HTBOTTOMRIGHT } }; +// @formatter:off + LRESULT hitTests[3][3] = { { HTTOPLEFT, HTTOP, HTTOPRIGHT }, + { HTLEFT, HTNOWHERE, HTRIGHT }, + { HTBOTTOMLEFT, HTBOTTOM, HTBOTTOMRIGHT } }; + LRESULT hit = hitTests[uRow][uCol]; - if (hit == HTNOWHERE || !wrapper->resizable) - { + if (hit == HTNOWHERE || !wrapper->resizable) { //Handle window drag. - if (ptMouse.y < rcWindow.top + wrapper->title_height && ptMouse.x >= rcWindow.left + wrapper->left - && ptMouse.x <= rcWindow.right - wrapper->right) - { + if (ptMouse.y < rcWindow.top + wrapper->title_height + && ptMouse.x >= rcWindow.left + wrapper->left + && ptMouse.x <= rcWindow.right - wrapper->right) { return HTCAPTION; } return HTCLIENT; - } - else - { + } else { return hit; } +// @formatter:on } -void UpdateRegion(WindowWrapper *wrapper) -{ - if (wrapper->popup_menu) - return; +void UpdateRegion(WindowWrapper *wrapper) { + if (wrapper->popup_menu) return; RECT old_rgn = wrapper->rgn; - if (Maximized(wrapper->window)) - { + if (Maximized(wrapper->window)) { WINDOWINFO wi; wi.cbSize = sizeof(WINDOWINFO); GetWindowInfo(wrapper->window, &wi); - /* For maximized windows, a region is needed to cut off the non-client - borders that hang over the edge of the screen */ + /* + * For maximized windows, a region is needed to cut off the non-client + * borders that hang over the edge of the screen + */ wrapper->rgn.left = wi.rcClient.left - wi.rcWindow.left; wrapper->rgn.top = wi.rcClient.top - wi.rcWindow.top; wrapper->rgn.right = wi.rcClient.right - wi.rcWindow.left; wrapper->rgn.bottom = wi.rcClient.bottom - wi.rcWindow.top; - } - else - { - /* Don't mess with the region when composition is enabled and the - window is not maximized, otherwise it will lose its shadow */ + } else { + /* + * Don't mess with the region when composition is enabled and the + * window is not maximized, otherwise it will lose its shadow + */ wrapper->rgn.left = 0; wrapper->rgn.top = 0; wrapper->rgn.right = 0; wrapper->rgn.bottom = 0; } - /* Avoid unnecessarily updating the region to avoid unnecessary redraws */ - if (EqualRect(&wrapper->rgn, &old_rgn)) - return; - /* Treat empty regions as NULL regions */ + // Avoid unnecessarily updating the region to avoid unnecessary redraws + if (EqualRect(&wrapper->rgn, &old_rgn)) return; + // Treat empty regions as NULL regions RECT empty = { 0 }; - if (EqualRect(&wrapper->rgn, &empty)) - SetWindowRgn(wrapper->window, NULL, TRUE); - else - SetWindowRgn(wrapper->window, CreateRectRgnIndirect(&wrapper->rgn), TRUE); + if (EqualRect(&wrapper->rgn, &empty)) SetWindowRgn(wrapper->window, NULL, TRUE); + else SetWindowRgn(wrapper->window, CreateRectRgnIndirect(&wrapper->rgn), TRUE); } -bool AutoHideTaskbar(UINT edge, RECT mon) -{ +bool AutoHideTaskbar(UINT edge, RECT mon) { APPBARDATA data; data.cbSize = sizeof(APPBARDATA); data.uEdge = edge; @@ -184,47 +157,47 @@ bool AutoHideTaskbar(UINT edge, RECT mon) return SHAppBarMessage(ABM_GETAUTOHIDEBAREX, &data); } -void AdjustMaximizedClientArea(HWND window, RECT &rect) -{ - if (!Maximized(window)) - return; +void AdjustMaximizedClientArea(HWND window, RECT &rect) { + if (!Maximized(window)) return; HMONITOR monitor = MonitorFromWindow(window, MONITOR_DEFAULTTOPRIMARY); - if (!monitor) - return; + if (!monitor) return; MONITORINFO monitor_info {}; monitor_info.cbSize = sizeof(MONITORINFO); - if (!GetMonitorInfo(monitor, &monitor_info)) - return; + if (!GetMonitorInfo(monitor, &monitor_info)) return; rect = monitor_info.rcWork; } -void HandleNCCalcSize(WindowWrapper *wrapper, WPARAM wparam, LPARAM lparam) -{ - union - { +/** + * Adjust the maximized frame size to respect auto hiding taskbars. + */ +void HandleNCCalcSize(WindowWrapper *wrapper, WPARAM wparam, LPARAM lparam) { + union { LPARAM lparam; RECT *rect; } params; params.lparam = lparam; - /* DefWindowProc must be called in both the maximized and non-maximized - cases, otherwise tile/cascade windows won't work */ + /* + * DefWindowProc must be called in both the maximized and non-maximized + * cases, otherwise tile/cascade windows won't work + */ RECT nonclient = *params.rect; DefWindowProc(wrapper->window, WM_NCCALCSIZE, wparam, lparam); RECT client = *params.rect; - if (Maximized(wrapper->window)) - { + if (Maximized(wrapper->window)) { WINDOWINFO wi; wi.cbSize = sizeof(wi); GetWindowInfo(wrapper->window, &wi); - /* Maximized windows always have a non-client border that hangs over - the edge of the screen, so the size proposed by WM_NCCALCSIZE is - fine. Just adjust the top border to remove the window title. */ + /* + * Maximized windows always have a non-client border that hangs over + * the edge of the screen, so the size proposed by WM_NCCALCSIZE is + * fine. Just adjust the top border to remove the window title. + */ (*params.rect).left = client.left; (*params.rect).top = nonclient.top + wi.cyWindowBorders; (*params.rect).right = client.right; @@ -244,29 +217,23 @@ void HandleNCCalcSize(WindowWrapper *wrapper, WPARAM wparam, LPARAM lparam) * of the monitor is likely to contain an auto-hide appbar, so the * missing client area is covered by it. */ - if (EqualRect(params.rect, &mi.rcMonitor)) - { - if (AutoHideTaskbar(ABE_BOTTOM, mi.rcMonitor)) - params.rect->bottom--; - else if (AutoHideTaskbar(ABE_LEFT, mi.rcMonitor)) - params.rect->left++; - else if (AutoHideTaskbar(ABE_TOP, mi.rcMonitor)) - params.rect->top++; - else if (AutoHideTaskbar(ABE_RIGHT, mi.rcMonitor)) - params.rect->right--; + if (EqualRect(params.rect, &mi.rcMonitor)) { + if (AutoHideTaskbar(ABE_BOTTOM, mi.rcMonitor)) params.rect->bottom--; + else if (AutoHideTaskbar(ABE_LEFT, mi.rcMonitor)) params.rect->left++; + else if (AutoHideTaskbar(ABE_TOP, mi.rcMonitor)) params.rect->top++; + else if (AutoHideTaskbar(ABE_RIGHT, mi.rcMonitor)) params.rect->right--; } - } - else - { - /* For the non-maximized case, set the output RECT to what it was - before WM_NCCALCSIZE modified it. This will make the client size the - same as the non-client size. */ + } else { + /* + * For the non-maximized case, set the output RECT to what it was + * before WM_NCCALCSIZE modified it. This will make the client size the + * same as the non-client size. + */ *params.rect = nonclient; } } -void HandleWindowPosChanged(WindowWrapper *wrapper, const WINDOWPOS *pos) -{ +void HandleWindowPosChanged(WindowWrapper *wrapper, const WINDOWPOS *pos) { RECT client; GetClientRect(wrapper->window, &client); LONG old_width = wrapper->width; @@ -275,90 +242,128 @@ void HandleWindowPosChanged(WindowWrapper *wrapper, const WINDOWPOS *pos) wrapper->height = client.bottom; bool client_changed = wrapper->width != old_width || wrapper->height != old_height; - if (client_changed || (pos->flags & SWP_FRAMECHANGED)) - UpdateRegion(wrapper); + if (client_changed || (pos->flags & SWP_FRAMECHANGED)) UpdateRegion(wrapper); } -void PaintBackground(HWND hwnd, WPARAM wParam, WindowWrapper *wrapper) -{ +void PaintBackground(HWND hwnd, WPARAM wParam, WindowWrapper *wrapper) { HDC hdc = reinterpret_cast(wParam); RECT clientRect; GetClientRect(hwnd, &clientRect); FillRect(hdc, &clientRect, wrapper->bgBrush); } -LRESULT CALLBACK WindowWrapper::WindowProc(_In_ HWND hwnd, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam) -{ +/** + * Extend the client area into the frame. Leaves a the margin at the top to make sure windows still recognizes the + * window to have a size frame. Otherwise no shadow is drawn. + */ +void ExtendClientFrame(HWND handle) { + MARGINS margins = { 0, 0, 1, 0 }; + DwmExtendFrameIntoClientArea(handle, &margins); +} + +/** + * Make sure windows recognizes the window to be resizable. Necessary for shadows and aero-snap. + */ +void SetupWindowStyle(HWND handle, bool is_popup) { + auto style = GetWindowLongPtr(handle, GWL_STYLE); + style |= WS_THICKFRAME; + SetWindowLongPtr(handle, GWL_STYLE, style); +} + +bool InstallDecorations(HWND handle, bool is_popup) { + // Prevent multiple installations overriding the real window procedure. + auto it = wrapper_map.find(handle); + if (it != wrapper_map.end()) return false; + + SetupWindowStyle(handle, is_popup); + ExtendClientFrame(handle); + + WNDPROC proc = reinterpret_cast(GetWindowLongPtr(handle, GWLP_WNDPROC)); + + WindowWrapper *wrapper = new WindowWrapper(); + wrapper->window = handle; + wrapper->prev_proc = proc; + wrapper->popup_menu = is_popup; + wrapper_map[handle] = wrapper; + + // Update the window procedure with our custom procedure. + SetWindowLongPtr(handle, GWLP_WNDPROC, (LONG_PTR) WindowWrapper::WindowProc); + UINT flags = SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED; + SetWindowPos(handle, NULL, 0, 0, 0, 0, flags); + return true; +} + +// @formatter:off +LRESULT CALLBACK WindowWrapper::WindowProc(_In_ HWND hwnd, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam) { HWND handle = reinterpret_cast(hwnd); auto wrapper = wrapper_map[handle]; - switch(uMsg) - { + switch (uMsg) { case WM_NCACTIVATE: - return TRUE; + // Prevents window flickering when being blocked by dialogs or when activating. + return TRUE; case WM_NCCALCSIZE: - if (wParam == TRUE) - { - HandleNCCalcSize(wrapper, wParam, lParam); - UpdateRegion(wrapper); - return 0; - } - break; + // If wParam is TRUE return 0 to signify the whole frame is the client area. + if (wParam == TRUE) { + // Adjust the maximized frame size to respect auto hiding taskbars. + HandleNCCalcSize(wrapper, wParam, lParam); + // Cut off unnecessary part of the window region (e.g. in maximized state) + UpdateRegion wrapper); + return 0; + } + break; case WM_NCHITTEST: - return HandleHitTest(wrapper, GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam)); + return HandleHitTest(wrapper, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)); case WM_MOVE: - wrapper->moving = wrapper->move_mode; - break; + wrapper->moving = wrapper->move_mode; + break; case WM_ENTERSIZEMOVE: - wrapper->move_mode = TRUE; - break; + wrapper->move_mode = TRUE; + break; case WM_EXITSIZEMOVE: - wrapper->moving = FALSE; - wrapper->move_mode = FALSE; - break; + wrapper->moving = FALSE; + wrapper->move_mode = FALSE; + break; case WM_ERASEBKGND: case WM_PAINT: - if (!wrapper->bgBrush) - break; - if (!wrapper->moving || wrapper->popup_menu) - PaintBackground(hwnd, wParam, wrapper); - if (uMsg == WM_ERASEBKGND) - return TRUE; - break; + if (!wrapper->bgBrush) break; + if (!wrapper->moving || wrapper->popup_menu) { + // Don't paint the background if the window is moving to avoid teared graphics on the window edge. + PaintBackground(hwnd, wParam, wrapper); + } + if (uMsg == WM_ERASEBKGND) return TRUE; + break; case WM_NCUAHDRAWCAPTION: case WM_NCUAHDRAWFRAME: - /* These undocumented messages are sent to draw themed window borders. - Block them to prevent drawing borders over the client area. */ - return 0; + /* + * These undocumented messages are sent to draw themed window borders. + * Block them to prevent drawing borders over the client area. + */ + return 0; case WM_WINDOWPOSCHANGED: - HandleWindowPosChanged(wrapper, (WINDOWPOS*)lParam); - break; + // Update window region if necessary. + HandleWindowPosChanged(wrapper, (WINDOWPOS*) lParam); + break; default: - break; + break; } - return CallWindowProc(wrapper->prev_proc, hwnd, uMsg, wParam, lParam); } - +// @formatter:on JNIEXPORT void JNICALL -Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_setResizable(JNIEnv *env, jclass obj, jlong hwnd, jboolean res) -{ +Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_setResizable(JNIEnv *env, jclass obj, jlong hwnd, jboolean res) { HWND handle = reinterpret_cast(hwnd); auto wrap = wrapper_map[handle]; - if (wrap) - { + if (wrap) { wrap->resizable = res; } } JNIEXPORT void JNICALL -Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_updateValues(JNIEnv *env, jclass obj, jlong hwnd, - jint l, jint r, jint h) -{ +Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_updateValues(JNIEnv *env, jclass obj, jlong hwnd, jint l, jint r, jint h) { HWND handle = reinterpret_cast(hwnd); auto wrap = wrapper_map[handle]; - if (wrap) - { + if (wrap) { wrap->left = l; wrap->right = r; wrap->title_height = h; @@ -366,69 +371,25 @@ Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_updateValue } JNIEXPORT void JNICALL -Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_setBackground(JNIEnv *env, jclass obj, jlong hwnd, jint r, jint g, jint b) -{ +Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_setBackground(JNIEnv *env, jclass obj, jlong hwnd, jint r, jint g, jint b) { HWND handle = reinterpret_cast(hwnd); auto wrap = wrapper_map[handle]; - if (wrap) - { + if (wrap) { wrap->bgBrush = CreateSolidBrush(RGB(r, g, b)); } } -void ExtendClientFrame(HWND handle) -{ - MARGINS margins = { 0, - 0, - 1, - 0 }; - DwmExtendFrameIntoClientArea(handle, &margins); -} - -void SetupWindowStyle(HWND handle, bool is_popup) -{ - auto style = GetWindowLongPtr(handle, GWL_STYLE); - style |= WS_THICKFRAME; - SetWindowLongPtr(handle, GWL_STYLE, style); -} - -bool InstallDecorations(HWND handle, bool is_popup) -{ - //Prevent multiple installations overriding the real window procedure. - auto it = wrapper_map.find(handle); - if (it != wrapper_map.end()) - return false; - - SetupWindowStyle(handle, is_popup); - ExtendClientFrame(handle); - - WNDPROC proc = reinterpret_cast(GetWindowLongPtr(handle, GWLP_WNDPROC)); - - WindowWrapper *wrapper = new WindowWrapper(); - wrapper->window = handle; - wrapper->prev_proc = proc; - wrapper->popup_menu = is_popup; - wrapper_map[handle] = wrapper; - SetWindowLongPtr(handle, GWLP_WNDPROC, (LONG_PTR) WindowWrapper::WindowProc); - UINT flags = SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED; - SetWindowPos(handle, NULL, 0, 0, 0, 0, flags); - return true; -} - JNIEXPORT jboolean JNICALL -Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_installDecorations(JNIEnv *env, jclass obj, jlong hwnd) -{ +Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_installDecorations(JNIEnv *env, jclass obj, jlong hwnd) { HWND handle = reinterpret_cast(hwnd); return (jboolean) InstallDecorations(handle, false); } JNIEXPORT void JNICALL -Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_uninstallDecorations(JNIEnv *env, jclass obj, jlong hwnd) -{ +Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_uninstallDecorations(JNIEnv *env, jclass obj, jlong hwnd) { HWND handle = reinterpret_cast(hwnd); auto wrap = wrapper_map[handle]; - if (wrap) - { + if (wrap) { SetWindowLongPtr(handle, GWLP_WNDPROC, reinterpret_cast(wrap->prev_proc)); wrapper_map.erase(handle); delete (wrap); @@ -436,8 +397,7 @@ Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_uninstallDe } JNIEXPORT jboolean JNICALL -Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_installPopupMenuDecorations(JNIEnv *env, jclass obj, jlong hwnd) -{ +Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_installPopupMenuDecorations(JNIEnv *env, jclass obj, jlong hwnd) { HWND handle = reinterpret_cast(hwnd); return (jboolean) InstallDecorations(handle, true); } @@ -445,22 +405,19 @@ Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_installPopu //Window functions. JNIEXPORT void JNICALL -Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_minimize(JNIEnv *env, jclass obj, jlong hwnd) -{ +Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_minimize(JNIEnv *env, jclass obj, jlong hwnd) { HWND handle = reinterpret_cast(hwnd); ShowWindow(handle, SW_MINIMIZE); } JNIEXPORT void JNICALL -Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_maximize(JNIEnv *env, jclass obj, jlong hwnd) -{ +Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_maximize(JNIEnv *env, jclass obj, jlong hwnd) { HWND handle = reinterpret_cast(hwnd); ShowWindow(handle, SW_MAXIMIZE); } JNIEXPORT void JNICALL -Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_restore(JNIEnv *env, jclass obj, jlong hwnd) -{ +Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_restore(JNIEnv *env, jclass obj, jlong hwnd) { HWND handle = reinterpret_cast(hwnd); ShowWindow(handle, SW_RESTORE); } diff --git a/windows/src/main/cpp/Decorations.h b/windows/src/main/cpp/Decorations.h index f19774cf..dad1d88a 100644 --- a/windows/src/main/cpp/Decorations.h +++ b/windows/src/main/cpp/Decorations.h @@ -31,23 +31,28 @@ #include #include -class WindowWrapper -{ +class WindowWrapper { public: bool resizable = true; bool popup_menu = false; bool moving = false; bool move_mode = false; bool maximized = false; + + // The original window procedure. WNDPROC prev_proc; + + // The background brush. HBRUSH bgBrush; HWND window; int width; int height; + // The window region. RECT rgn; + // The insets for the title bar area that is draggable. int left = 0; int right = 0; int title_height = 0; diff --git a/windows/src/main/cpp/ThemeInfo.cpp b/windows/src/main/cpp/ThemeInfo.cpp index d30ee431..0d33bb81 100644 --- a/windows/src/main/cpp/ThemeInfo.cpp +++ b/windows/src/main/cpp/ThemeInfo.cpp @@ -50,8 +50,7 @@ #define FONT_SCALE_DEFAULT_VALUE 100 #define ACCENT_COLOR_DEFAULT_VALUE 0 -void ModifyFlags(DWORD &flags) -{ +void ModifyFlags(DWORD &flags) { #ifdef _WIN64 flags |= RRF_SUBKEY_WOW6464KEY; #else @@ -59,105 +58,84 @@ void ModifyFlags(DWORD &flags) #endif } -DWORD RegGetDword(HKEY hKey, const LPCSTR subKey, const LPCSTR value) -{ +DWORD RegGetDword(HKEY hKey, const LPCSTR subKey, const LPCSTR value) { DWORD data {}; DWORD dataSize = sizeof(data); DWORD flags = RRF_RT_REG_DWORD; ModifyFlags(flags); LONG retCode = ::RegGetValue(hKey, subKey, value, flags, nullptr, &data, &dataSize); - if (retCode != ERROR_SUCCESS) - throw retCode; + if (retCode != ERROR_SUCCESS) throw retCode; return data; } -std::string RegGetString(HKEY hKey, const LPCSTR subKey, const LPCSTR value) -{ +std::string RegGetString(HKEY hKey, const LPCSTR subKey, const LPCSTR value) { DWORD dataSize {}; DWORD flags = RRF_RT_REG_SZ; ModifyFlags(flags); LONG retCode = ::RegGetValue(hKey, subKey, value, flags, nullptr, nullptr, &dataSize); - if (retCode != ERROR_SUCCESS) - throw retCode; + if (retCode != ERROR_SUCCESS) throw retCode; std::string data; DWORD stringLengthInChars = dataSize / sizeof(char); data.resize(stringLengthInChars); retCode = ::RegGetValue(hKey, subKey, value, flags, nullptr, &data[0], &dataSize); - if (retCode != ERROR_SUCCESS) - throw retCode; + if (retCode != ERROR_SUCCESS) throw retCode; return data; } -bool IsHighContrastMode() -{ +bool IsHighContrastMode() { HIGHCONTRAST info = { 0 }; info.cbSize = sizeof(HIGHCONTRAST); BOOL ok = SystemParametersInfo(SPI_GETHIGHCONTRAST, 0, &info, 0); - if (ok) - { + if (ok) { return info.dwFlags & HCF_HIGHCONTRASTON; } return HIGH_CONTRAST_DEFAULT_VALUE; } -bool IsDarkMode() -{ - try - { +bool IsDarkMode() { + try { bool appsUseDark = (0 == RegGetDword(HKEY_CURRENT_USER, DARK_MODE_PATH, DARK_MODE_KEY)); bool isHighContrast = IsHighContrastMode(); - if (!isHighContrast) - return appsUseDark; + if (!isHighContrast) return appsUseDark; std::string themeValue = RegGetString(HKEY_CURRENT_USER, HIGH_CONTRAST_PATH, HIGH_CONTRAST_THEME_KEY); return (strcmp(themeValue.c_str(), HIGH_CONTRAST_LIGHT_THEME) != 0); - } catch (LONG e) - { + } catch (LONG e) { return DARK_MODE_DEFAULT_VALUE; } } -unsigned int GetTextScaleFactor() -{ - try - { +unsigned int GetTextScaleFactor() { + try { return RegGetDword(HKEY_CURRENT_USER, FONT_SCALE_PATH, FONT_SCALE_KEY); - } catch (LONG e) - { + } catch (LONG e) { return FONT_SCALE_DEFAULT_VALUE; } } -bool RegisterRegistryEvent(const LPCSTR subKey, HANDLE event) -{ +bool RegisterRegistryEvent(const LPCSTR subKey, HANDLE event) { HKEY hKey; REGSAM flags = KEY_NOTIFY; ModifyFlags(flags); DWORD res = RegOpenKeyEx(HKEY_CURRENT_USER, subKey, 0, flags, &hKey); - if (res == ERROR_SUCCESS) - { + if (res == ERROR_SUCCESS) { LSTATUS status = RegNotifyChangeKeyValue(hKey, FALSE, REG_NOTIFY_CHANGE_LAST_SET, event, TRUE); return status == ERROR_SUCCESS; - } - else - { + } else { return FALSE; } } -int GetAccentColor() -{ - try - { +int GetAccentColor() { + try { return RegGetDword(HKEY_CURRENT_USER, ACCENT_COLOR_PATH, ACCENT_COLOR_KEY); - } catch (LONG e) - { + } catch (LONG e) { return ACCENT_COLOR_DEFAULT_VALUE; } } @@ -186,8 +164,7 @@ Java_com_github_weisj_darklaf_platform_windows_JNIThemeInfoWindows_getAccentColo return (jint) GetAccentColor(); } -struct EventHandler -{ +struct EventHandler { JavaVM *jvm; JNIEnv *env; jobject callback; @@ -195,48 +172,33 @@ struct EventHandler std::thread notificationLoop; std::atomic running = FALSE; - void runCallBack() - { + void runCallBack() { jclass runnableClass = env->GetObjectClass(callback); jmethodID runMethodId = env->GetMethodID(runnableClass, "run", "()V"); - if (runMethodId) - { + if (runMethodId) { env->CallVoidMethod(callback, runMethodId); } } - bool awaitPreferenceChange() - { - if (!RegisterRegistryEvent(FONT_SCALE_PATH, eventHandle)) - return FALSE; - if (!RegisterRegistryEvent(DARK_MODE_PATH, eventHandle)) - return FALSE; - if (!RegisterRegistryEvent(HIGH_CONTRAST_PATH, eventHandle)) - return FALSE; - if (!RegisterRegistryEvent(ACCENT_COLOR_PATH, eventHandle)) - return FALSE; + bool awaitPreferenceChange() { + if (!RegisterRegistryEvent(FONT_SCALE_PATH, eventHandle)) return FALSE; + if (!RegisterRegistryEvent(DARK_MODE_PATH, eventHandle)) return FALSE; + if (!RegisterRegistryEvent(HIGH_CONTRAST_PATH, eventHandle)) return FALSE; + if (!RegisterRegistryEvent(ACCENT_COLOR_PATH, eventHandle)) return FALSE; return WaitForSingleObject(eventHandle, INFINITE) != WAIT_FAILED; } - void run() - { + void run() { int getEnvStat = jvm->GetEnv((void**) &env, JNI_VERSION_1_6); - if (getEnvStat == JNI_EDETACHED) - { - if (jvm->AttachCurrentThread((void**) &env, NULL) != 0) - return; - } - else if (getEnvStat == JNI_EVERSION) - { + if (getEnvStat == JNI_EDETACHED) { + if (jvm->AttachCurrentThread((void**) &env, NULL) != 0) return; + } else if (getEnvStat == JNI_EVERSION) { return; } - while (running && awaitPreferenceChange()) - { - if (running) - { + while (running && awaitPreferenceChange()) { + if (running) { runCallBack(); - if (env->ExceptionCheck()) - { + if (env->ExceptionCheck()) { env->ExceptionDescribe(); break; } @@ -245,15 +207,13 @@ struct EventHandler jvm->DetachCurrentThread(); } - void stop() - { + void stop() { running = FALSE; SetEvent(eventHandle); notificationLoop.join(); } - EventHandler(JavaVM *jvm_, jobject callback_, HANDLE eventHandle_) - { + EventHandler(JavaVM *jvm_, jobject callback_, HANDLE eventHandle_) { jvm = jvm_; callback = callback_; eventHandle = eventHandle_;