-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solid_fill.h
172 lines (148 loc) · 5.86 KB
/
solid_fill.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#pragma once
namespace uih {
class FillWindow {
public:
enum class Mode {
SolidFill,
ThemeBackgroundFill,
ThemeTextColourFill,
};
void set_fill_colour(COLORREF value)
{
m_mode = Mode::SolidFill;
m_fill_colour = value;
if (m_container_window->get_wnd())
redraw();
}
void set_fill_themed(Mode mode, const WCHAR* pclass, int part, int state, bool is_dark, COLORREF fallback)
{
m_mode = mode;
m_theme_state = state;
m_theme_part = part;
m_fill_colour = fallback;
m_theme_class = pclass;
m_theme.reset();
m_is_dark = is_dark;
if (m_container_window->get_wnd()) {
if (IsThemeActive() && IsAppThemed())
m_theme.reset(OpenThemeData(m_container_window->get_wnd(), m_theme_class.c_str()));
redraw();
}
}
FillWindow()
{
auto window_config = uih::ContainerWindowConfig{L"columns_ui_fill"};
window_config.window_styles = WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE;
window_config.window_ex_styles = WS_EX_CONTROLPARENT | WS_EX_STATICEDGE;
m_container_window = std::make_unique<uih::ContainerWindow>(
window_config, [this](auto&&... args) { return on_message(std::forward<decltype(args)>(args)...); });
}
template <typename... Ts>
HWND create(Ts&&... vals)
{
return m_container_window->create(std::forward<Ts>(vals)...);
}
void destroy() const { m_container_window->destroy(); }
HWND get_wnd() const { return m_container_window->get_wnd(); }
private:
void redraw() const
{
RedrawWindow(m_container_window->get_wnd(), nullptr, nullptr, RDW_INVALIDATE | RDW_UPDATENOW);
}
LRESULT on_message(HWND wnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch (msg) {
case WM_CREATE:
if (m_mode == Mode::ThemeBackgroundFill || m_mode == Mode::ThemeTextColourFill)
m_theme.reset(IsThemeActive() && IsAppThemed() ? OpenThemeData(wnd, m_theme_class.c_str()) : nullptr);
break;
case WM_DESTROY:
m_theme.reset();
break;
case WM_THEMECHANGED:
m_theme.reset();
if (m_mode == Mode::ThemeBackgroundFill || m_mode == Mode::ThemeTextColourFill)
m_theme.reset(IsThemeActive() && IsAppThemed() ? OpenThemeData(wnd, m_theme_class.c_str()) : nullptr);
redraw();
break;
case WM_ENABLE:
redraw();
break;
case WM_ERASEBKGND:
return FALSE;
case WM_PAINT: {
PAINTSTRUCT ps{};
const auto dc = wil::BeginPaint(wnd, &ps);
RECT client_rect{};
GetClientRect(wnd, &client_rect);
if (!IsWindowEnabled(wnd)) {
FillRect(dc.get(), &ps.rcPaint, GetSysColorBrush(COLOR_BTNFACE));
break;
}
if (m_mode == Mode::SolidFill || !m_theme
|| !IsThemePartDefined(m_theme.get(), m_theme_part, m_theme_state)) {
const wil::unique_hbrush brush(CreateSolidBrush(m_fill_colour));
FillRect(dc.get(), &ps.rcPaint, brush.get());
} else if (m_mode == Mode::ThemeBackgroundFill) {
if (IsThemeBackgroundPartiallyTransparent(m_theme.get(), m_theme_part, m_theme_state))
DrawThemeParentBackground(wnd, dc.get(), &client_rect);
RECT background_rect = client_rect;
if (m_is_dark) {
InflateRect(&background_rect, 1, 1);
}
if (FAILED(DrawThemeBackground(
m_theme.get(), dc.get(), m_theme_part, m_theme_state, &background_rect, &client_rect))) {
const wil::unique_hbrush brush(CreateSolidBrush(m_fill_colour));
FillRect(dc.get(), &ps.rcPaint, brush.get());
}
} else if (m_mode == Mode::ThemeTextColourFill) {
COLORREF fill_colour = m_fill_colour;
GetThemeColor(m_theme.get(), m_theme_part, m_theme_state, TMT_TEXTCOLOR, &fill_colour);
const wil::unique_hbrush brush(CreateSolidBrush(fill_colour));
FillRect(dc.get(), &ps.rcPaint, brush.get());
}
return 0;
}
}
return DefWindowProc(wnd, msg, wp, lp);
}
Mode m_mode{Mode::SolidFill};
std::wstring m_theme_class;
COLORREF m_fill_colour = NULL;
wil::unique_htheme m_theme;
int m_theme_part = NULL;
int m_theme_state = NULL;
int m_theme_colour_index = NULL;
std::unique_ptr<uih::ContainerWindow> m_container_window;
bool m_is_dark{};
};
class TranslucentFillWindow {
public:
void set_fill_colour(COLORREF value)
{
m_fill_colour = value;
if (m_container_window->get_wnd())
redraw();
}
TranslucentFillWindow()
{
auto window_config = uih::ContainerWindowConfig{L"columns_ui_transparent_fill"};
window_config.window_styles = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
window_config.window_ex_styles = WS_EX_LAYERED;
m_container_window = std::make_unique<uih::ContainerWindow>(
window_config, [this](auto&&... args) { return on_message(std::forward<decltype(args)>(args)...); });
}
template <typename... Ts>
HWND create(Ts&&... vals)
{
return m_container_window->create(std::forward<Ts>(vals)...);
}
void destroy() { m_container_window->destroy(); }
HWND get_wnd() const { return m_container_window->get_wnd(); }
private:
void redraw() { RedrawWindow(m_container_window->get_wnd(), nullptr, nullptr, RDW_INVALIDATE | RDW_UPDATENOW); }
LRESULT on_message(HWND wnd, UINT msg, WPARAM wp, LPARAM lp);
COLORREF m_fill_colour{NULL};
std::unique_ptr<uih::ContainerWindow> m_container_window;
};
} // namespace uih