Skip to content

Commit

Permalink
Merge pull request #58 from Zorbn/dark-mode
Browse files Browse the repository at this point in the history
Support dark mode (for dark titlebar)
  • Loading branch information
RMichelsen committed Sep 2, 2022
2 parents 652dac7 + 24f8fba commit dda84bd
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,20 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
return DefWindowProc(hwnd, msg, wparam, lparam);
}

BOOL ShouldUseDarkMode()
{
constexpr const LPCWSTR key = L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";
constexpr const LPCWSTR value = L"AppsUseLightTheme";

DWORD type;
DWORD data;
DWORD size = sizeof(DWORD);
LSTATUS st = RegGetValue(HKEY_CURRENT_USER, key, value, RRF_RT_REG_DWORD, &type, &data, &size);

if (st == ERROR_SUCCESS && type == REG_DWORD) return data == 0;
return false;
}

int WINAPI wWinMain(HINSTANCE instance, HINSTANCE prev_instance, PWSTR p_cmd_line, int n_cmd_show) {
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE);

Expand Down Expand Up @@ -371,14 +385,16 @@ int WINAPI wWinMain(HINSTANCE instance, HINSTANCE prev_instance, PWSTR p_cmd_lin

const wchar_t *window_class_name = L"Nvy_Class";
const wchar_t *window_title = L"Nvy";
BOOL should_use_dark_mode = ShouldUseDarkMode();
HBRUSH bg_brush = CreateSolidBrush(should_use_dark_mode ? 0x00202020 : 0x00FFFFFF);
WNDCLASSEX window_class {
.cbSize = sizeof(WNDCLASSEX),
.style = CS_HREDRAW | CS_VREDRAW,
.lpfnWndProc = WndProc,
.hInstance = instance,
.hIcon = static_cast<HICON>(LoadImage(GetModuleHandle(NULL), L"NVIM_ICON", IMAGE_ICON, LR_DEFAULTSIZE, LR_DEFAULTSIZE, 0)),
.hCursor = LoadCursor(NULL, IDC_ARROW),
.hbrBackground = nullptr,
.hbrBackground = bg_brush,
.lpszClassName = window_class_name,
.hIconSm = static_cast<HICON>(LoadImage(GetModuleHandle(NULL), L"NVIM_ICON", IMAGE_ICON, LR_DEFAULTSIZE, LR_DEFAULTSIZE, 0))
};
Expand Down Expand Up @@ -421,6 +437,8 @@ int WINAPI wWinMain(HINSTANCE instance, HINSTANCE prev_instance, PWSTR p_cmd_lin
DwmGetWindowAttribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &window_rect, sizeof(RECT));
HMONITOR monitor = MonitorFromPoint({window_rect.left, window_rect.top}, MONITOR_DEFAULTTONEAREST);
GetDpiForMonitor(monitor, MDT_EFFECTIVE_DPI, &(context.saved_dpi_scaling), &(context.saved_dpi_scaling));
constexpr int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &should_use_dark_mode, sizeof(BOOL));
RendererInitialize(&renderer, hwnd, disable_ligatures, linespace_factor, context.saved_dpi_scaling);
NvimInitialize(&nvim, nvim_command_line, hwnd);

Expand All @@ -441,6 +459,7 @@ int WINAPI wWinMain(HINSTANCE instance, HINSTANCE prev_instance, PWSTR p_cmd_lin
RendererShutdown(&renderer);
NvimShutdown(&nvim);
UnregisterClass(window_class_name, instance);
DeleteObject(bg_brush);
DestroyWindow(hwnd);

return nvim.exit_code;
Expand Down

0 comments on commit dda84bd

Please sign in to comment.