HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
window_widget.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2020-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
9#pragma once
10
11#include "toolbar_widget.hpp"
13#include "grid_widget.hpp"
15#include "../GUI/module.hpp"
16#include "../label.hpp"
17#include <memory>
18
19namespace hi { inline namespace v1 {
20
27template<fixed_string Name = "">
28class window_widget final : public widget {
29public:
30 using super = widget;
31 constexpr static auto prefix = Name / "window";
32
33 observer<label> title;
34
35 window_widget(forward_of<observer<label>> auto&& title) noexcept :
36 super(nullptr), title(hi_forward(title))
37 {
38 _toolbar = std::make_unique<toolbar_widget<prefix>>(this);
39
40 if (operating_system::current == operating_system::windows) {
41#if HI_OPERATING_SYSTEM == HI_OS_WINDOWS
42 _system_menu = &_toolbar->make_widget<system_menu_widget<prefix>>();
43 this->_system_menu->icon = this->title.get<"icon">();
44#endif
45 _toolbar->make_widget<window_traffic_lights_widget<prefix>, horizontal_alignment::right>();
46 } else if (operating_system::current == operating_system::macos) {
47 _toolbar->make_widget<window_traffic_lights_widget<prefix>>();
48 } else {
50 }
51
52 _content = std::make_unique<grid_widget<prefix>>(this);
53 }
54
59 [[nodiscard]] grid_widget<prefix>& content() noexcept
60 {
61 hi_axiom(loop::main().on_thread());
62 hi_assert_not_null(_content);
63 return *_content;
64 }
65
70 [[nodiscard]] toolbar_widget<prefix>& toolbar() noexcept
71 {
72 hi_axiom(loop::main().on_thread());
73 hi_assert_not_null(_toolbar);
74 return *_toolbar;
75 }
76
78 [[nodiscard]] generator<widget const&> children(bool include_invisible) const noexcept override
79 {
80 co_yield *_toolbar;
81 co_yield *_content;
82 }
83
84 [[nodiscard]] box_constraints update_constraints() noexcept override
85 {
86 hi_assert_not_null(_content);
87 hi_assert_not_null(_toolbar);
88
89 _content_constraints = _content->update_constraints();
90 _toolbar_constraints = _toolbar->update_constraints();
91
92 auto r = box_constraints{};
93 r.minimum.width() = std::max(
94 _toolbar_constraints.margins.left() + _toolbar_constraints.minimum.width() + _toolbar_constraints.margins.right(),
95 _content_constraints.margins.left() + _content_constraints.minimum.width() + _content_constraints.margins.right());
96 r.preferred.width() = std::max(
97 _toolbar_constraints.margins.left() + _toolbar_constraints.preferred.width() + _toolbar_constraints.margins.right(),
98 _content_constraints.margins.left() + _content_constraints.preferred.width() + _content_constraints.margins.right());
99 r.maximum.width() = std::min(
100 _toolbar_constraints.margins.left() + _toolbar_constraints.maximum.width() + _toolbar_constraints.margins.right(),
101 _content_constraints.margins.left() + _content_constraints.maximum.width() + _content_constraints.margins.right());
102
103 // clang-format off
104 r.minimum.height() =
105 _toolbar_constraints.margins.top() +
106 _toolbar_constraints.preferred.height() +
107 std::max(_toolbar_constraints.margins.bottom(), _content_constraints.margins.top()) +
108 _content_constraints.minimum.height() +
109 _content_constraints.margins.bottom();
110 r.preferred.height() =
111 _toolbar_constraints.margins.top() +
112 _toolbar_constraints.preferred.height() +
113 std::max(_toolbar_constraints.margins.bottom(), _content_constraints.margins.top()) +
114 _content_constraints.preferred.height() +
115 _content_constraints.margins.bottom();
116 r.maximum.height() =
117 _toolbar_constraints.margins.top() +
118 _toolbar_constraints.preferred.height() +
119 std::max(_toolbar_constraints.margins.bottom(), _content_constraints.margins.top()) +
120 _content_constraints.maximum.height() +
121 _content_constraints.margins.bottom();
122 // clang-format on
123
124 // The operating system also has a minimum and maximum size, these sizes
125 // are more important than the calculated sizes.
126 inplace_max(r.minimum.width(), os_settings::minimum_window_width());
127 inplace_max(r.minimum.height(), os_settings::minimum_window_height());
128
129 inplace_clamp(r.maximum.width(), r.minimum.width(), os_settings::maximum_window_width());
130 inplace_clamp(r.maximum.height(), r.minimum.height(), os_settings::maximum_window_height());
131
132 inplace_clamp(r.preferred.width(), r.minimum.width(), r.maximum.width());
133 inplace_clamp(r.preferred.height(), r.minimum.height(), r.maximum.height());
134
135 _can_resize_width = r.minimum.width() != r.maximum.width();
136 _can_resize_height = r.minimum.height() != r.maximum.height();
137
138 return r;
139 }
140
141 void set_layout(widget_layout const& context) noexcept override
142 {
143 if (compare_store(layout, context)) {
144 hilet toolbar_height = _toolbar_constraints.preferred.height();
145 hilet between_margin = std::max(_toolbar_constraints.margins.bottom(), _content_constraints.margins.top());
146
147 hilet toolbar_rectangle = aarectanglei{
148 point2i{
149 _toolbar_constraints.margins.left(), context.height() - toolbar_height - _toolbar_constraints.margins.top()},
150 point2i{
151 context.width() - _toolbar_constraints.margins.right(),
152 context.height() - _toolbar_constraints.margins.top()}};
153 _toolbar_shape = box_shape{_toolbar_constraints, toolbar_rectangle, theme<prefix>.cap_height(this)};
154
155 hilet content_rectangle = aarectanglei{
156 point2i{_content_constraints.margins.left(), _content_constraints.margins.bottom()},
157 point2i{context.width() - _content_constraints.margins.right(), toolbar_rectangle.bottom() - between_margin}};
158 _content_shape = box_shape{_content_constraints, content_rectangle, theme<prefix>.cap_height(this)};
159 }
160 _toolbar->set_layout(context.transform(_toolbar_shape));
161 _content->set_layout(context.transform(_content_shape));
162 }
163
164 void draw(widget_draw_context const& context) noexcept override
165 {
167 _toolbar->draw(context);
168 _content->draw(context);
169 }
170 }
171
172 [[nodiscard]] hitbox hitbox_test(point2i position) const noexcept override
173 {
174 constexpr float BORDER_WIDTH = 10.0f;
175
176 hi_axiom(loop::main().on_thread());
177
178 auto r = _toolbar->hitbox_test_from_parent(position);
179 r = _content->hitbox_test_from_parent(position, r);
180
181 hilet is_on_l_edge = position.x() <= BORDER_WIDTH;
182 hilet is_on_r_edge = position.x() >= (layout.width() - BORDER_WIDTH);
183 hilet is_on_b_edge = position.y() <= BORDER_WIDTH;
184 hilet is_on_t_edge = position.y() >= (layout.height() - BORDER_WIDTH);
185
186 // Corner resize has always priority.
187 if (is_on_l_edge and is_on_b_edge) {
188 if (_can_resize_width and _can_resize_height) {
189 return {id, layout.elevation, hitbox_type::bottom_left_resize_corner};
190 } else if (_can_resize_width) {
191 return {id, layout.elevation, hitbox_type::left_resize_border};
192 } else if (_can_resize_height) {
193 return {id, layout.elevation, hitbox_type::bottom_resize_border};
194 }
195 } else if (is_on_r_edge and is_on_b_edge) {
196 if (_can_resize_width and _can_resize_height) {
197 return {id, layout.elevation, hitbox_type::bottom_right_resize_corner};
198 } else if (_can_resize_width) {
199 return {id, layout.elevation, hitbox_type::right_resize_border};
200 } else if (_can_resize_height) {
201 return {id, layout.elevation, hitbox_type::bottom_resize_border};
202 }
203 } else if (is_on_l_edge and is_on_t_edge) {
204 if (_can_resize_width and _can_resize_height) {
205 return {id, layout.elevation, hitbox_type::top_left_resize_corner};
206 } else if (_can_resize_width) {
207 return {id, layout.elevation, hitbox_type::left_resize_border};
208 } else if (_can_resize_height) {
209 return {id, layout.elevation, hitbox_type::top_resize_border};
210 }
211 } else if (is_on_r_edge and is_on_t_edge) {
212 if (_can_resize_width and _can_resize_height) {
213 return {id, layout.elevation, hitbox_type::top_right_resize_corner};
214 } else if (_can_resize_width) {
215 return {id, layout.elevation, hitbox_type::right_resize_border};
216 } else if (_can_resize_height) {
217 return {id, layout.elevation, hitbox_type::top_resize_border};
218 }
219 }
220
221 // Border resize only has priority if there is no scroll-bar in the way.
222 if (r.type != hitbox_type::scroll_bar) {
223 if (is_on_l_edge and _can_resize_width) {
224 return {id, layout.elevation, hitbox_type::left_resize_border};
225 } else if (is_on_r_edge and _can_resize_width) {
226 return {id, layout.elevation, hitbox_type::right_resize_border};
227 } else if (is_on_b_edge and _can_resize_height) {
228 return {id, layout.elevation, hitbox_type::bottom_resize_border};
229 } else if (is_on_t_edge and _can_resize_height) {
230 return {id, layout.elevation, hitbox_type::top_resize_border};
231 }
232 }
233
234 return r;
235 }
236
237 bool handle_event(gui_event const& event) noexcept override
238 {
239 using enum gui_event_type;
240
241 switch (event.type()) {
242 case gui_toolbar_open:
243 process_event(
244 gui_event::window_set_keyboard_target(id, keyboard_focus_group::toolbar, keyboard_focus_direction::forward));
245 return true;
246 }
247 return super::handle_event(event);
248 }
249
250 bool process_event(gui_event const& event) const noexcept override
251 {
253 return window->process_event(event);
254 }
256private:
258 box_constraints _content_constraints;
259 box_shape _content_shape;
260
262 box_constraints _toolbar_constraints;
263 box_shape _toolbar_shape;
264
265 mutable bool _can_resize_width;
266 mutable bool _can_resize_height;
267
268#if HI_OPERATING_SYSTEM == HI_OS_WINDOWS
269 system_menu_widget<prefix> *_system_menu = nullptr;
270#endif
271};
272
273}} // namespace hi::v1
Functionality for labels, text and icons.
Defines window_traffic_lights_widget.
Defines system_menu_widget.
Defines toolbar_widget.
Defines grid_widget.
#define hi_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:279
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:238
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
gui_event_type
GUI event type.
Definition gui_event_type.hpp:21
@ invisible
The widget is invisible.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition utility.hpp:212
constexpr value_type & width() noexcept
Access the x-as-width element from the extent.
Definition extent.hpp:166
constexpr value_type & height() noexcept
Access the y-as-height element from the extent.
Definition extent.hpp:177
Definition widget.hpp:26
widget_id id
The numeric identifier of a widget.
Definition widget.hpp:35
virtual bool handle_event(gui_event const &event) noexcept
Handle command.
Definition widget.hpp:279
gui_window * window
The window this widget is on.
Definition widget.hpp:44
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:53
float elevation
The elevation of the widget above the window.
Definition widget_layout.hpp:72
2D constraints.
Definition box_constraints.hpp:22
A GUI widget that lays out child-widgets in a grid with variable sized cells.
Definition grid_widget.hpp:41
The system menu widget.
Definition system_menu_widget.hpp:27
A toolbar widget is located at the top of a window and lays out its children horizontally.
Definition toolbar_widget.hpp:37
Window control button widget.
Definition window_traffic_lights_widget.hpp:27
The top-level window widget.
Definition window_widget.hpp:28
toolbar_widget< prefix > & toolbar() noexcept
Get a reference to window's toolbar widget.
Definition window_widget.hpp:70
grid_widget< prefix > & content() noexcept
Get a reference to the window's content widget.
Definition window_widget.hpp:59
T max(T... args)
T min(T... args)