Skip to content

Commit

Permalink
add dark title bar support on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
chirag-droid committed Feb 14, 2024
1 parent 4a525fd commit 3cbc53f
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
4 changes: 4 additions & 0 deletions library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ else ()
$<BUILD_INTERFACE:${F3D_SOURCE_DIR}/external/nlohmann_json>)
endif ()

if (WIN32)
target_link_libraries(libf3d PRIVATE Dwmapi)
endif()

set_target_properties(libf3d PROPERTIES
CXX_STANDARD 17
CXX_VISIBILITY_PRESET hidden
Expand Down
113 changes: 113 additions & 0 deletions library/src/window_impl.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,21 @@
#include <vtkWindowToImageFilter.h>
#include <vtksys/SystemTools.hxx>

#include <charconv>

#if F3D_MODULE_EXTERNAL_RENDERING
#include <vtkExternalOpenGLRenderWindow.h>
#endif

#ifdef _WIN32
#include <Windows.h>
#include <dwmapi.h>

#define DWMWA_USE_IMMERSIVE_DARK_MODE 20

constexpr auto MAX_VALUE_NAME = 16383;
#endif

namespace f3d::detail
{
class window_impl::internals
Expand All @@ -44,6 +55,97 @@ class window_impl::internals
return this->CachePath;
}

#if _WIN32
/**
* Helper function to detect if the
* Windows Build Number is equal or greater to a number
*/
BOOL IsWindowsBuildNumberOrGreater(int buildNumber)
{
int windowsBuildNumber = 0;

// Receives the handle to Reg key
HKEY versionHKey;
LONG result = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_READ, &versionHKey);

// Query value from the reg key
if (result == ERROR_SUCCESS)
{
// Receives code indicating the type of data stored in the reg value.
// CurrentBuildNumber is of type REG_SZ
DWORD type;
DWORD dataSize = MAX_VALUE_NAME;
CHAR data[MAX_VALUE_NAME]{};

RegGetValueA(versionHKey, "", "CurrentBuildNumber", RRF_RT_REG_SZ, &type, data, &dataSize);

if (result == ERROR_SUCCESS && type == REG_SZ)
{
std::from_chars(data, data + dataSize, windowsBuildNumber);
}
else
{
f3d::log::debug("Error reading registry value.");
}
}
else
{
f3d::log::debug("Error opening registry key.");
}

RegCloseKey(versionHKey);
return windowsBuildNumber >= buildNumber;
}

/**
* Helper function to detect user theme
*/
BOOL IsWindowsInDarkMode()
{
HKEY hKey;
LONG result = RegOpenKeyExA(HKEY_CURRENT_USER,
"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", 0, KEY_READ, &hKey);

if (result != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return false;
}

{
DWORD type;
DWORD value = 0;
DWORD dataSize = sizeof(DWORD);

RegQueryValueExA(hKey, "AppsUseLightTheme", NULL, &type, (LPBYTE)&value, &dataSize);

if (result == ERROR_SUCCESS)
{
RegCloseKey(hKey);
return value == 0;
}
}

{
DWORD type;
DWORD value = 0;
DWORD dataSize = sizeof(DWORD);

RegQueryValueExA(hKey, "SystemUsesLightTheme", NULL, &type, (LPBYTE)&value, &dataSize);

if (result == ERROR_SUCCESS)
{
RegCloseKey(hKey);
return value == 0;
}
}

RegCloseKey(hKey);
return false;
}
#endif

std::unique_ptr<camera_impl> Camera;
vtkSmartPointer<vtkRenderWindow> RenWin;
vtkNew<vtkF3DRendererWithColoring> Renderer;
Expand Down Expand Up @@ -215,6 +317,17 @@ void window_impl::Initialize(bool withColoring)
this->Internals->WithColoring = withColoring;
this->Internals->Renderer->Initialize(this->Internals->Options.getAsString("scene.up-direction"));
this->Internals->Initialized = true;

#ifdef _WIN32
HWND hwnd = static_cast<HWND>(this->Internals->RenWin->GetGenericWindowId());

BOOL useDarkMode = this->Internals->IsWindowsInDarkMode();

if (this->Internals->IsWindowsBuildNumberOrGreater(22000))
{
DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &useDarkMode, sizeof(useDarkMode));
}
#endif
}

//----------------------------------------------------------------------------
Expand Down

0 comments on commit 3cbc53f

Please sign in to comment.