Blender V5.3
GHOST_WindowWaylandCSD.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
8
9/* Currently all of the logic is for CSD. */
10
11#include "GHOST_WindowWaylandCSD.hh" /* Own include. */
12#include "GHOST_Types.hh"
13#include "GHOST_utildefines.hh"
14
15#include <array> /* For `std::array`. */
16#include <string_view>
17
18/* -------------------------------------------------------------------- */
21
22static std::array<std::string_view, 2> string_partition(std::string_view s, const char delimiter)
23{
24 std::array<std::string_view, 2> result;
25 size_t pos = s.find(delimiter);
26 if (pos == std::string_view::npos) {
27 /* Follow Firefox in defaulting to the right when there is no delimiter. */
28 result[0] = {};
29 result[1] = s;
30 }
31 else {
32 result[0] = s.substr(0, pos);
33 result[1] = s.substr(pos + 1);
34 }
35 return result;
36}
37
38static int string_parse_buttons(std::string_view buttons,
39 uint32_t *button_mask_p,
41 const int output_capacity)
42{
43 const char delimiter = ',';
44 int i = 0;
45 while (!buttons.empty() && (i < output_capacity)) {
46 const size_t p = buttons.find(delimiter);
47 const std::string_view button_id = buttons.substr(0, p);
48
50 if (button_id == "close") {
52 }
53 else if (button_id == "maximize") {
55 }
56 else if (button_id == "minimize") {
58 }
59 else if (button_id == "icon") {
61 }
62
63 if (value != GHOST_kCSDTypeBody) {
64 /* Only allow each button once. */
65 const uint32_t value_mask = (1 << uint32_t(value));
66 if ((*button_mask_p & value_mask) == 0) {
67 *button_mask_p |= value_mask;
68 output[i++] = value;
69 }
70 }
71
72 if (p == std::string_view::npos) {
73 break;
74 }
75 buttons.remove_prefix(p + 1);
76 }
77 return i;
78}
79
81
82/* -------------------------------------------------------------------- */
85
86bool GHOST_WindowCSD_LayoutFromString(GHOST_CSD_Layout &layout, const std::string_view buttons)
87{
88 /* Access buttons from both sides of the `:` which represents the title bar. */
89 std::array<std::string_view, 2> buttons_pair = string_partition(buttons, ':');
90 int i = 0;
91 uint32_t button_mask = 0;
92 for (int side = 0; side < 2; side++) {
93 int buttons_capacity = ARRAY_SIZE(layout.buttons) - i;
94 if (side == 1) {
95 /* Add the title divider. */
96 if (buttons_capacity > 0) {
97 layout.buttons[i++] = GHOST_kCSDTypeTitlebar;
98 buttons_capacity--;
99 }
100 }
102 buttons_pair[side], &button_mask, &layout.buttons[i], buttons_capacity);
103 }
104 layout.buttons_num = i;
105 return button_mask != 0;
106}
107
109{
110 int i = 0;
111 layout.buttons[i++] = GHOST_kCSDTypeButtonMenu;
112 layout.buttons[i++] = GHOST_kCSDTypeTitlebar;
116 layout.buttons_num = i;
117}
118
120{
121 if (desktop_type == GWL_CurrentDesktopType::Gnome) {
122 return true;
123 }
124 return false;
125}
126
GWL_CurrentDesktopType
GHOST_TCSD_Type
@ GHOST_kCSDTypeBody
@ GHOST_kCSDTypeButtonClose
@ GHOST_kCSDTypeButtonMinimize
@ GHOST_kCSDTypeButtonMaximize
@ GHOST_kCSDTypeButtonMenu
@ GHOST_kCSDTypeTitlebar
bool GHOST_WindowCSD_LayoutFromString(GHOST_CSD_Layout &layout, const std::string_view buttons)
bool GHOST_WindowCSD_Check(GWL_CurrentDesktopType desktop_type)
static int string_parse_buttons(std::string_view buttons, uint32_t *button_mask_p, GHOST_TCSD_Type *output, const int output_capacity)
static std::array< std::string_view, 2 > string_partition(std::string_view s, const char delimiter)
void GHOST_WindowCSD_LayoutDefault(GHOST_CSD_Layout &layout)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
#define output
GHOST_TCSD_Type buttons[5]