Blender V5.3
wm_window_csd_layout.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 "GHOST_IWindow.hh"
12
13#include "BLI_rect.hh"
14
15#include "WM_api.hh"
16#include "wm_window.hh"
17#include "wm_window_private.hh"
18
19namespace blender {
20
21/* -------------------------------------------------------------------- */
26
27int WM_window_csd_fractional_scale_apply(int value, const int fractional_scale[2])
28{
29 return (value * fractional_scale[1]) / fractional_scale[0];
30}
31
32int WM_window_csd_layout_callback(const int window_size[2],
33 const int fractional_scale[2],
34 const char window_state,
35 const GHOST_CSD_Layout *csd_layout,
36 GHOST_CSD_Elem *csd_elems)
37{
38 constexpr int csd_title_height = 25;
39
40 const int title = WM_window_csd_fractional_scale_apply(csd_title_height, fractional_scale);
41
42 /* The caller is expected not to run the callback for full screen windows. */
44 UNUSED_VARS_NDEBUG(window_state);
45 int decor_num = 0;
46
47 GHOST_CSD_Elem *elem;
48
49 /* Window contents. */
50 elem = &csd_elems[decor_num++];
52 elem->bounds[0][0] = 0;
53 elem->bounds[0][1] = window_size[0];
54 elem->bounds[1][0] = title;
55 elem->bounds[1][1] = window_size[1];
56
57 /* Allow this to be null for callers that only need to know about
58 * the "title" & "body" regions. */
59 if (csd_layout != nullptr) {
60 int button_layout_title_index = 0;
61
62 /* Buttons on the left. */
63 {
64 int button_index = 0;
65 for (int i = 0; i < csd_layout->buttons_num; i++) {
66 if (csd_layout->buttons[i] == GHOST_kCSDTypeTitlebar) {
67 button_layout_title_index = i;
68 break;
69 }
70
71 GHOST_TCSD_Type type = GHOST_TCSD_Type(csd_layout->buttons[i]);
72 elem = &csd_elems[decor_num++];
73 elem->type = type;
74 elem->bounds[0][0] = title * button_index;
75 elem->bounds[0][1] = title + (title * button_index);
76 elem->bounds[1][0] = 0;
77 elem->bounds[1][1] = title;
78
79 button_index++;
80 }
81 }
82
83 /* Buttons on the right. */
84 {
85 int button_index = 0;
86 for (int i = csd_layout->buttons_num - 1; i > button_layout_title_index; i--) {
87 GHOST_TCSD_Type type = csd_layout->buttons[i];
88 elem = &csd_elems[decor_num++];
89 elem->type = type;
90 elem->bounds[0][0] = (window_size[0] - title) - (title * button_index);
91 elem->bounds[0][1] = window_size[0] - (title * button_index);
92 elem->bounds[1][0] = 0;
93 elem->bounds[1][1] = title;
94 button_index++;
95 }
96 }
97 }
98
99 /* Title bar. */
100 elem = &csd_elems[decor_num++];
102 elem->bounds[0][0] = 0;
103 elem->bounds[0][1] = window_size[0];
104 elem->bounds[1][0] = 0;
105 elem->bounds[1][1] = title;
106
107 /* Border elements are intentionally not included, resizing runs via the invisible
108 * margin outside the window which resolves borders from its own geometry,
109 * see #GHOST_CSD_Params::resize_margin_size. */
110
111 return decor_num;
112}
113
114void WM_window_csd_rect_calc(const wmWindow *win, rcti *r_rect)
115{
116 const GHOST_CSD_Layout *csd_layout = WM_window_csd_layout_get();
117 GHOST_IWindow *ghost_window = static_cast<GHOST_IWindow *>(win->runtime->ghostwin);
118 const int fractional_scale[2] = {GHOST_CSD_DPI_FRACTIONAL_BASE, ghost_window->getDPIHint()};
119
121
122 const int2 win_size = WM_window_native_pixel_size(win);
123 const int decor_num = WM_window_csd_layout_callback(
124 win_size, fractional_scale, GHOST_TWindowState(win->windowstate), csd_layout, csd_elems);
125
126 const GHOST_CSD_Elem *elem = nullptr;
127 for (int i = 0; i < decor_num; i++) {
128 /* Typically the first. */
129 if (csd_elems[i].type == GHOST_kCSDTypeBody) {
130 elem = &csd_elems[i];
131 break;
132 }
133 }
134 if (elem == nullptr) {
135 BLI_assert_msg(0, "unexpected, no window contents");
136 BLI_rcti_init(r_rect, 0, win_size[0], 0, win_size[1]);
137 }
138
139 /* Flip the Y. */
140 r_rect->xmin = elem->bounds[0][0];
141 r_rect->xmax = elem->bounds[0][1];
142 r_rect->ymin = win_size.y - elem->bounds[1][1];
143 r_rect->ymax = win_size.y - elem->bounds[1][0];
144}
145
147
148} // namespace blender
#define GHOST_CSD_DPI_FRACTIONAL_BASE
GHOST_TWindowState
@ GHOST_kWindowStateFullScreen
GHOST_TCSD_Type
@ GHOST_kCSDTypeBody
@ GHOST_kCSDTypeTitlebar
#define GHOST_kCSDType_NUM
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert_msg(a, msg)
virtual uint16_t getDPIHint()=0
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
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)
int WM_window_csd_fractional_scale_apply(int value, const int fractional_scale[2])
VecBase< int32_t, 2 > int2
const GHOST_CSD_Layout * WM_window_csd_layout_get()
void BLI_rcti_init(struct rcti *rect, int xmin, int xmax, int ymin, int ymax)
Definition rct.cc:428
int2 WM_window_native_pixel_size(const wmWindow *win)
void WM_window_csd_rect_calc(const wmWindow *win, rcti *r_rect)
int32_t bounds[2][2]
GHOST_TCSD_Type type
GHOST_TCSD_Type buttons[5]
bke::WindowRuntime * runtime