Blender V5.3
wm_window_csd_draw.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <algorithm>
12
13#include "DNA_vec_types.h"
15
16#include "BLI_index_range.hh"
18#include "BLI_rect.hh"
19
20#include "GHOST_IWindow.hh"
21
22#include "GPU_batch.hh"
23#include "GPU_batch_presets.hh"
24#include "GPU_immediate.hh"
25#include "GPU_state.hh"
26#include "GPU_viewport.hh" /* #GLA_PIXEL_OFS */
27
28#include "WM_api.hh"
29#include "wm_window.hh"
30#include "wm_window_private.hh" /* Own include. */
31
32#include "UI_interface_c.hh"
33#include "UI_resources.hh"
34
35#include "BLF_api.hh"
36
37namespace blender {
38
39/* -------------------------------------------------------------------- */
44
45void WM_window_csd_draw_titlebar_ex(const int win_size[2],
46 const char win_state,
47 const GHOST_CSD_Layout *csd_layout,
48 const bool is_active,
49 const uint16_t dpi,
50 const char *title,
51 const int font_id,
52 const int font_size,
53 const uchar border_color[3],
54 const uchar text_color[3],
55 const float alpha)
56{
57 GHOST_CSD_Elem csd_elems_orig[GHOST_kCSDType_NUM];
58
59 const int fractional_scale[2] = {
61 dpi,
62 };
63 const int csd_elems_num = WM_window_csd_layout_callback(
64 win_size, fractional_scale, win_state, csd_layout, csd_elems_orig);
65
66 if (csd_elems_num <= 0) {
67 return;
68 }
69
70 const rcti window_rect = {
71 /*xmin*/ 0,
72 /*xmax*/ win_size[0],
73 /*ymin*/ 0,
74 /*ymax*/ win_size[1],
75 };
76
77 if (border_color) {
78 wmWindowViewportTitle_ex(window_rect, 0.0f);
80 border_color[0] / 255.0f, border_color[1] / 255.0f, border_color[2] / 255.0f, 1.0f);
81 }
82
83 /* Flip the Y axis. */
85 for (int i = 0; i < GHOST_kCSDType_NUM; i++) {
86 csd_elems[i].type = GHOST_kCSDTypeBody;
87 csd_elems[i].bounds[0][0] = 0;
88 csd_elems[i].bounds[0][1] = 0;
89 csd_elems[i].bounds[1][0] = 0;
90 csd_elems[i].bounds[1][1] = 0;
91 }
92 for (int i = 0; i < csd_elems_num; i++) {
93 GHOST_CSD_Elem *elem = &csd_elems[csd_elems_orig[i].type];
94 *elem = csd_elems_orig[i];
95 elem->bounds[1][0] = win_size[1] - elem->bounds[1][0];
96 elem->bounds[1][1] = win_size[1] - elem->bounds[1][1];
97 std::swap(elem->bounds[1][0], elem->bounds[1][1]);
98 }
99
101 const rcti title_rect = {
102 /*xmin*/ csd_elems[GHOST_kCSDTypeTitlebar].bounds[0][0],
103 /*xmax*/ csd_elems[GHOST_kCSDTypeTitlebar].bounds[0][1],
104 /*ymin*/ csd_elems[GHOST_kCSDTypeTitlebar].bounds[1][0],
105 /*ymax*/ csd_elems[GHOST_kCSDTypeTitlebar].bounds[1][1],
106 };
107 const int rect_size_y = BLI_rcti_size_y(&title_rect);
108
109 wmWindowViewportTitle_ex(title_rect, 0.0f);
110 if (title) {
111 const float px_offset = -GLA_PIXEL_OFS;
112 const size_t title_len = strlen(title);
113 uchar color[4];
114 if (!is_active) {
115 if (border_color) {
116 color[0] = uchar((int(border_color[0]) + int(text_color[0])) / 2);
117 color[1] = uchar((int(border_color[1]) + int(text_color[1])) / 2);
118 color[2] = uchar((int(border_color[2]) + int(text_color[2])) / 2);
119 }
120 else {
121 color[0] = text_color[0] / 2;
122 color[1] = text_color[1] / 2;
123 color[2] = text_color[2] / 2;
124 }
125 }
126 else {
127 ARRAY_SET_ITEMS(color, UNPACK3(text_color), uchar(255 * alpha));
128 }
129 BLF_color4ubv(font_id, color);
130 if (border_color == nullptr) {
131 const float shadow_color[4] = {0.0f, 0.0f, 0.0f, alpha};
132 BLF_enable(font_id, BLF_SHADOW);
133 BLF_shadow(font_id, FontShadowType::Outline, shadow_color);
134 BLF_shadow_offset(font_id, 0, 0);
135 }
136 BLF_enable(font_id, BLF_BOLD);
137 BLF_size(font_id, WM_window_csd_fractional_scale_apply(int(font_size), fractional_scale));
138
139 const int title_width = BLF_width(font_id, title, title_len);
140 const int title_decender = -BLF_descender(font_id);
141
142 const int title_height_max = BLF_height_max(font_id);
143 const int offset_y = rect_size_y > title_height_max ? (rect_size_y - title_height_max) / 2 : 0;
144
145 BLF_position(font_id,
146 float(title_rect.xmin + (BLI_rcti_cent_x(&title_rect) - (title_width / 2))) +
147 px_offset,
148 float(title_decender + offset_y) + px_offset,
149 0);
150
151 BLF_draw(font_id, title, title_len);
152 BLF_disable(font_id, BLF_BOLD);
153 if (border_color == nullptr) {
154 BLF_disable(font_id, BLF_SHADOW);
155 }
156 }
157
158 /* Draw buttons (starting at the title region offset). */
159 {
160 constexpr int circle_segments = 16;
161 GPUVertFormat *format = immVertexFormat();
162 const uint shdr_pos = GPU_vertformat_attr_add(format, "pos", gpu::VertAttrType::SFLOAT_32_32);
163
165
166 GPU_polygon_smooth(true);
167
168 const GHOST_TCSD_Type button_types[] = {
173 };
174
175 const int button_icons[] = {
176 ICON_X,
177 (win_state == GHOST_kWindowStateMaximized) ? ICON_AREA_DOCK : ICON_CHECKBOX_DEHLT,
178 ICON_DOT,
179 ICON_BLENDER,
180 };
181
182 {
183 const int button_margin = rect_size_y / 12;
185 if (border_color) {
186 immUniformColor4f(1.0f, 1.0f, 1.0f, 0.15f);
187 }
188 else {
189 immUniformColor4f(0.25f, 0.25f, 0.25f, 0.5f * alpha);
190 }
191
192 for (int i = 0; i < ARRAY_SIZE(button_types); i++) {
193 const GHOST_TCSD_Type ty = button_types[i];
194 if (csd_elems[ty].type == GHOST_kCSDTypeBody) [[unlikely]] {
195 continue;
196 }
197 const rcti butrect = {
198 /*xmin*/ csd_elems[ty].bounds[0][0] - title_rect.xmin,
199 /*xmax*/ csd_elems[ty].bounds[0][1] - title_rect.xmin,
200 /*ymin*/ csd_elems[ty].bounds[1][0] - title_rect.ymin,
201 /*ymax*/ csd_elems[ty].bounds[1][1] - title_rect.ymin,
202 };
203 const int but_radius = (BLI_rcti_size_x(&butrect) / 2) - button_margin;
204 const int center[2] = {
205 BLI_rcti_cent_x(&butrect),
206 BLI_rcti_cent_y(&butrect),
207 };
208 imm_draw_circle_fill_2d(shdr_pos, UNPACK2(center), but_radius, circle_segments);
209 }
211 }
212
213 const float button_color[4] = {1.0f, 1.0f, 1.0f, alpha};
215 fractional_scale);
216 for (int i = 0; i < ARRAY_SIZE(button_types); i++) {
217 const GHOST_TCSD_Type ty = button_types[i];
218 if (csd_elems[ty].type == GHOST_kCSDTypeBody) [[unlikely]] {
219 continue;
220 }
221 const rcti butrect = {
222 /*xmin*/ csd_elems[ty].bounds[0][0] - title_rect.xmin,
223 /*xmax*/ csd_elems[ty].bounds[0][1] - title_rect.xmin,
224 /*ymin*/ csd_elems[ty].bounds[1][0] - title_rect.ymin,
225 /*ymax*/ csd_elems[ty].bounds[1][1] - title_rect.ymin,
226 };
227
228 const int xy[2] = {
229 BLI_rcti_cent_x(&butrect) - (icon_size / 2),
230 BLI_rcti_cent_y(&butrect) - (icon_size / 2),
231 };
232 BLF_draw_svg_icon(button_icons[i], UNPACK2(xy), icon_size, button_color, 0, false, nullptr);
233 }
234
235 GPU_polygon_smooth(false);
236
238 }
239}
240
242{
244 const int2 win_size = WM_window_native_pixel_size(win);
245 const GHOST_CSD_Layout *csd_layout = WM_window_csd_layout_get();
246 GHOST_IWindow *ghost_window = static_cast<GHOST_IWindow *>(win->runtime->ghostwin);
247 const uint16_t dpi = ghost_window->getDPIHint();
248 const char win_state = GHOST_TWindowState(win->windowstate);
249 const std::string window_title = ghost_window->getTitle();
250 const char *title = window_title.c_str();
251 const bool is_active = (win->active != 0);
252
253 const bScreen *screen = WM_window_get_active_screen(win);
255
256 uchar border_color[3], text_color[3];
257 ui::theme::get_color_3ubv(TH_BACK, border_color);
259
260 const uiStyle *style = ui::style_get_dpi();
261 const uiFontStyle &fstyle = style->paneltitle;
262
263 const int font_id = fstyle.uifont_id;
264 const int font_size = fstyle.points;
265
266 const float alpha = 1.0f;
268 win_state,
269 csd_layout,
270 is_active,
271 dpi,
272 title,
273 font_id,
274 font_size,
275 border_color,
276 text_color,
277 alpha);
278}
279
281
282/* -------------------------------------------------------------------- */
286
288{
289 auto *ghost_window = static_cast<GHOST_IWindow *>(win->runtime->ghostwin);
290 if (!ghost_window->hasAlpha()) {
291 return;
292 }
293
294 const int2 win_size = WM_window_native_pixel_size(win);
295 const rcti window_rect = {
296 .xmin = 0,
297 .xmax = win_size[0],
298 .ymin = 0,
299 .ymax = win_size[1],
300 };
301 /* Clears are scissor tested and the window frame-buffer keeps its scissor between frames. */
302 wmWindowViewportTitle_ex(window_rect, 0.0f);
303 /* Only the alpha matters, every pixel of the color is drawn over by the screen below.
304 * Could be avoided if the blit shader for viewports wrote an alpha of 1 instead. */
305 GPU_clear_color(0.0f, 0.0f, 0.0f, 1.0f);
306}
307
309{
311
312 auto *ghost_window = static_cast<GHOST_IWindow *>(win->runtime->ghostwin);
313 if (!ghost_window->hasAlpha()) {
314 return;
315 }
316
317 const int fractional_scale[2] = {
319 ghost_window->getDPIHint(),
320 };
321 const int radius = (win->windowstate == GHOST_kWindowStateNormal) ?
323 fractional_scale) :
324 0;
325
326 if (radius <= 0) {
327 return;
328 }
329
330 const std::array<int, 4> corner_tiled_edges{
335 };
336 const GHOST_TWindowTiledFlag tiled_edges = ghost_window->getTiledEdges();
337 std::array<float, 4> radii;
338 for (const int corner : IndexRange(4)) {
339 radii[corner] = (tiled_edges & corner_tiled_edges[corner]) ? 0.0f : float(radius);
340 }
341 if (std::ranges::all_of(radii, [](const float value) { return value == 0.0f; })) {
342 return;
343 }
344
345 const int2 win_size = WM_window_native_pixel_size(win);
346 const rcti window_rect = {
347 .xmin = 0,
348 .xmax = win_size[0],
349 .ymin = 0,
350 .ymax = win_size[1],
351 };
352 wmWindowViewportTitle_ex(window_rect, 0.0f);
353
354 const rctf rect = {
355 .xmin = 0.0f,
356 .xmax = float(win_size[0]),
357 .ymin = 0.0f,
358 .ymax = float(win_size[1]),
359 };
360 /* Only the alpha is used, the blend mode scales the window down by one minus it. */
361 const float color[4] = {0.0f, 0.0f, 0.0f, 1.0f};
362
364
365 gpu::Batch *batch = GPU_batch_preset_quad();
367 GPU_batch_uniform_4fv(batch, "rect", (const float *)&rect);
368 GPU_batch_uniform_4fv(batch, "color", color);
369 GPU_batch_uniform_4fv(batch, "radii", radii.data());
371
373}
374
376
377} // namespace blender
#define GHOST_CSD_DPI_FRACTIONAL_BASE
GHOST_TWindowState
@ GHOST_kWindowStateMaximized
@ GHOST_kWindowStateNormal
GHOST_TCSD_Type
@ GHOST_kCSDTypeBody
@ GHOST_kCSDTypeButtonClose
@ GHOST_kCSDTypeButtonMinimize
@ GHOST_kCSDTypeButtonMaximize
@ GHOST_kCSDTypeButtonMenu
@ GHOST_kCSDTypeTitlebar
#define GHOST_kCSDType_NUM
GHOST_TWindowTiledFlag
@ GHOST_kWindowTiledRight
@ GHOST_kWindowTiledLeft
@ GHOST_kWindowTiledBottom
@ GHOST_kWindowTiledTop
#define GPU_batch_uniform_4fv(batch, name, val)
Definition GPU_batch.hh:275
#define GLA_PIXEL_OFS
#define ICON_DEFAULT_HEIGHT
#define UNPACK2(a)
#define ARRAY_SET_ITEMS(...)
#define UNPACK3(a)
virtual std::string getTitle() const =0
virtual uint16_t getDPIHint()=0
constexpr IndexRange()=default
void get_color_3ubv(int colorid, unsigned char col[3])
const uiStyle * style_get_dpi()
BMesh Iterators.
void WM_window_csd_draw_titlebar_ex(const int win_size[2], const char win_state, const GHOST_CSD_Layout *csd_layout, const bool is_active, const uint16_t dpi, const char *title, const int font_id, const int font_size, const uchar border_color[3], const uchar text_color[3], const float alpha)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BLF_shadow_offset(int fontid, int x, int y)
Definition blf.cc:979
void BLF_position(int fontid, float x, float y, float z)
Definition blf.cc:390
float BLF_width(int fontid, const char *str, size_t str_len, ResultBLF *r_info=nullptr) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2)
Definition blf.cc:835
unsigned int uint
Definition BKE_text.h:55
void GPU_polygon_smooth(bool enable)
Definition gpu_state.cc:85
void GPU_clear_color(float red, float green, float blue, float alpha)
int WM_window_csd_layout_callback(const int window_size[2], const int fractional_scale[2], const char window_state, const GHOST_CSD_Layout *csd_layout, GHOST_CSD_Elem *csd_elems)
void BLF_shadow(int fontid, FontShadowType type, const float rgba[4]=nullptr)
Definition blf.cc:967
void wmWindowViewportTitle_ex(const rcti &rect, float offset)
void BLF_draw_svg_icon(uint icon_id, float x, float y, float size, const float color[4]=nullptr, float outline_alpha=1.0f, bool multicolor=false, FunctionRef< void(std::string &)> edit_source_cb=nullptr)
Definition blf.cc:628
GPUVertFormat * immVertexFormat()
void BLF_disable(int fontid, FontFlags flag)
Definition blf.cc:331
int WM_window_csd_fractional_scale_apply(int value, const int fractional_scale[2])
void immUnbindProgram()
@ GPU_BLEND_OVERLAY_MASK_FROM_ALPHA
Definition GPU_state.hh:122
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
unsigned short uint16_t
Definition BKE_text.h:80
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
void BLF_size(int fontid, float size)
Definition blf.cc:445
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
VecBase< int32_t, 2 > int2
unsigned char uchar
Definition BKE_text.h:58
BLI_INLINE int BLI_rcti_cent_y(const struct rcti *rct)
Definition BLI_rect.hh:190
@ GPU_SHADER_2D_ROUNDED_CORNER_MASK
const GHOST_CSD_Layout * WM_window_csd_layout_get()
void GPU_batch_draw_instance_range(gpu::Batch *batch, int instance_first, int instance_count)
void immUniformColor4f(float r, float g, float b, float a)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
void WM_window_csd_draw_corner_mask(const wmWindow *win)
void wm_window_titlebar_theme_context_set(const wmWindow *win, const bScreen *screen)
Definition wm_window.cc:835
void BLF_draw(int fontid, const char *str, size_t str_len, ResultBLF *r_info=nullptr) ATTR_NONNULL(2)
Definition blf.cc:587
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
void WM_window_csd_draw_titlebar(const wmWindow *win)
void GPU_batch_program_set_builtin(gpu::Batch *batch, GPUBuiltinShader shader_id)
void BLF_color4ubv(int fontid, const unsigned char rgba[4])
Definition blf.cc:454
bool WM_window_is_csd(const wmWindow *win)
int BLF_height_max(int fontid) ATTR_WARN_UNUSED_RESULT
Definition blf.cc:883
gpu::Batch * GPU_batch_preset_quad()
int2 WM_window_native_pixel_size(const wmWindow *win)
int BLF_descender(int fontid) ATTR_WARN_UNUSED_RESULT
Definition blf.cc:905
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
BLI_INLINE int BLI_rcti_cent_x(const struct rcti *rct)
Definition BLI_rect.hh:186
void imm_draw_circle_fill_2d(uint shdr_pos, float x, float y, float radius, int nsegments)
bScreen * WM_window_get_active_screen(const wmWindow *win)
void WM_window_csd_clear_alpha(const wmWindow *win)
void BLF_enable(int fontid, FontFlags flag)
Definition blf.cc:322
@ BLF_SHADOW
Definition BLF_enums.hh:38
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
int32_t bounds[2][2]
GHOST_TCSD_Type type
bke::WindowRuntime * runtime
uiFontStyle paneltitle
#define WM_WINDOW_CSD_CORNER_RADIUS