Skip to content

Commit e8fb6ce

Browse files
feat: support light color
1 parent 61538b2 commit e8fb6ce

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

src/shell/contextmenu/menu_render.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include "../utils.h"
23
#include "shell.h"
34
#include "ui.h"
45
#include <optional>
@@ -7,6 +8,7 @@ namespace mb_shell {
78
struct menu_render {
89
std::unique_ptr<ui::render_target> rt;
910
std::optional<int> selected_menu;
11+
bool light_color = is_light_mode();
1012
enum class menu_style {
1113
fluentui,
1214
materialyou

src/shell/contextmenu/menu_widget.cc

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111

1212
void mb_shell::menu_item_widget::render(ui::nanovg_context ctx) {
1313
super::render(ctx);
14+
15+
auto c = menu_render::current.value()->light_color ? 0 : 1;
1416
if (item.type == menu_item::type::spacer) {
15-
ctx.fillColor(nvgRGBAf(1, 1, 1, 0.1));
17+
ctx.fillColor(nvgRGBAf(c, c, c, 0.1));
1618
ctx.fillRect(x->dest(), *y, *width, *height);
1719
return;
1820
}
1921

20-
ctx.fillColor(nvgRGBAf(1, 1, 1, *bg_opacity / 255.f));
22+
ctx.fillColor(nvgRGBAf(c, c, c, *bg_opacity / 255.f));
2123

2224
float roundcorner = 4;
2325

@@ -29,10 +31,6 @@ void mb_shell::menu_item_widget::render(ui::nanovg_context ctx) {
2931
ctx.fillRoundedRect(*x + margin, *y, *width - margin * 2, *height,
3032
roundcorner);
3133

32-
ctx.fillColor(nvgRGBAf(1, 1, 1, *opacity / 255.f));
33-
ctx.fontFace("Yahei");
34-
ctx.fontSize(14);
35-
3634
if (item.icon_bitmap.has_value()) {
3735
auto paintY = floor(*y + (*height - icon_width) / 2);
3836
if (!icon_img_bmp) {
@@ -50,6 +48,10 @@ void mb_shell::menu_item_widget::render(ui::nanovg_context ctx) {
5048
ctx.fill();
5149
}
5250

51+
ctx.fillColor(nvgRGBAf(c, c, c, *opacity / 255.f));
52+
ctx.fontFace("Yahei");
53+
ctx.fontSize(14);
54+
5355
ctx.text(floor(*x + text_padding + icon_width + icon_padding * 2), *y + 16,
5456
item.name->c_str(), nullptr);
5557

@@ -59,7 +61,7 @@ void mb_shell::menu_item_widget::render(ui::nanovg_context ctx) {
5961
ctx.moveTo(*x + *width - 20, *y + 8);
6062
ctx.lineTo(*x + *width - 15, *y + 12);
6163
ctx.lineTo(*x + *width - 20, *y + 16);
62-
ctx.strokeColor(nvgRGBAf(1, 1, 1, *opacity * 0.6 / 255.f));
64+
ctx.strokeColor(nvgRGBAf(c, c, c, *opacity * 0.6 / 255.f));
6365
ctx.strokeWidth(1);
6466
ctx.stroke();
6567
}
@@ -70,7 +72,7 @@ void mb_shell::menu_item_widget::render(ui::nanovg_context ctx) {
7072
ctx.moveTo(*x + icon_padding + margin + 2, *y + 4 + 8);
7173
ctx.lineTo(*x + icon_padding + margin + 6, *y + 8 + 8);
7274
ctx.lineTo(*x + icon_padding + margin + 14, *y + 0 + 8);
73-
ctx.strokeColor(nvgRGBAf(1, 1, 1, *opacity * 0.6 / 255.f));
75+
ctx.strokeColor(nvgRGBAf(c, c, c, *opacity * 0.6 / 255.f));
7476
ctx.strokeWidth(2);
7577
ctx.stroke();
7678
}
@@ -126,12 +128,11 @@ void mb_shell::menu_item_widget::update(ui::update_context &ctx) {
126128
submenu_wid.get(), ctx, anchor_x, anchor_y, direction);
127129

128130
submenu_wid->direction = direction;
129-
submenu_wid->have_overlap = ctx.rt.root->check_hit(ui::update_context{
130-
.mouse_x = x / ctx.rt.dpi_scale,
131-
.mouse_y = y / ctx.rt.dpi_scale,
132-
.screen = ctx.screen,
133-
.rt = ctx.rt
134-
});
131+
submenu_wid->have_overlap = ctx.rt.root->check_hit(
132+
ui::update_context{.mouse_x = x / ctx.rt.dpi_scale,
133+
.mouse_y = y / ctx.rt.dpi_scale,
134+
.screen = ctx.screen,
135+
.rt = ctx.rt});
135136

136137
submenu_wid->x->reset_to(x / ctx.rt.dpi_scale - ctx.offset_x);
137138
submenu_wid->y->reset_to(y / ctx.rt.dpi_scale - ctx.offset_y);
@@ -213,6 +214,11 @@ void mb_shell::menu_widget::update(ui::update_context &ctx) {
213214
ctx.hovered_hit(bg.get());
214215
}
215216
void mb_shell::menu_widget::render(ui::nanovg_context ctx) {
217+
auto c = menu_render::current.value()->light_color ? 1 : 0;
218+
ctx.fillColor(nvgRGBAf(c, c, c, 0.6));
219+
ctx.fillRoundedRect(*x, *y - bg_padding_vertical, *width,
220+
*height + 2 * bg_padding_vertical, 6);
221+
216222
std::lock_guard lock(data_lock);
217223
bg->render(ctx);
218224
super::render(ctx);

src/shell/utils.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,13 @@ bool mb_shell::is_win11_or_later() {
3939
RtlGetNtVersionNumbers(&major, &minor, &build);
4040
build &= 0xFFFF;
4141
return (major >= 10 && build >= 22000);
42-
}
42+
}
43+
bool mb_shell::is_light_mode() {
44+
auto key = HKEY_CURRENT_USER;
45+
auto subkey = L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";
46+
auto value = L"AppsUseLightTheme";
47+
DWORD data = 0;
48+
DWORD size = sizeof(data);
49+
auto res = RegGetValueW(key, subkey, value, RRF_RT_REG_DWORD, nullptr, &data, &size);
50+
return res == ERROR_SUCCESS && data == 1;
51+
}

src/shell/utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ namespace mb_shell {
55
std::string wstring_to_utf8(std::wstring const &str);
66
std::wstring utf8_to_wstring(std::string const &str);
77
bool is_win11_or_later();
8+
bool is_light_mode();
89
} // namespace mb_shell

0 commit comments

Comments
 (0)