HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
gui_window.hpp
1// Copyright Take Vos 2019-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
5#pragma once
6
7#include "gui_window_size.hpp"
8#include "mouse_cursor.hpp"
9#include "hitbox.hpp"
10#include "gui_event.hpp"
11#include "keyboard_focus_direction.hpp"
12#include "keyboard_focus_group.hpp"
13#include "theme.hpp"
15#include "../geometry/axis_aligned_rectangle.hpp"
19#include "../chrono.hpp"
20#include "../label.hpp"
21#include "../animator.hpp"
22#include <unordered_set>
23#include <memory>
24#include <mutex>
25
26namespace hi::inline v1 {
27class gfx_device;
28class gfx_system;
29class gfx_surface;
30class gui_system;
31class keyboard_bindings;
32
39public:
40 gui_system& gui;
41
43
54
62 mouse_cursor current_mouse_cursor = mouse_cursor::None;
63
68 bool resizing = false;
69
73 bool active = false;
74
75 label title;
76
81 float dpi = 72.0;
82
86 hi::theme theme = {};
87
91
95
100
101 gui_window(gui_system& gui, label const& title) noexcept;
102
103 virtual ~gui_window();
104
105 gui_window(gui_window const&) = delete;
106 gui_window& operator=(gui_window const&) = delete;
107 gui_window(gui_window&&) = delete;
108 gui_window& operator=(gui_window&&) = delete;
109
116 virtual void init();
117
120 [[nodiscard]] bool is_gui_thread() const noexcept;
121
122 void set_device(gfx_device *device) noexcept;
123
126 hi::keyboard_bindings const& keyboard_bindings() const noexcept;
127
130 void request_redraw(aarectangle redraw_rectangle) noexcept
131 {
132 _redraw_rectangle |= redraw_rectangle;
133 }
134
137 void request_redraw() noexcept
138 {
139 hi_axiom(is_gui_thread());
140 request_redraw(aarectangle{rectangle.size()});
141 }
142
143 void request_relayout(void const *w) noexcept
144 {
145 _relayout.store(w, std::memory_order::relaxed);
146 }
147
148 void request_reconstrain(void const *w) noexcept
149 {
150 _reconstrain.store(w, std::memory_order::relaxed);
151 }
152
153 void request_resize(void const *w) noexcept
154 {
155 _resize.store(w, std::memory_order::relaxed);
156 }
157
161 virtual void render(utc_nanoseconds displayTimePoint);
162
167 [[nodiscard]] grid_widget& content() noexcept
168 {
169 hi_axiom(is_gui_thread());
170 hi_axiom(widget);
171 return widget->content();
172 }
173
178 [[nodiscard]] toolbar_widget& toolbar() noexcept
179 {
180 hi_axiom(is_gui_thread());
181 hi_axiom(widget);
182 return widget->toolbar();
183 }
184
187 virtual void set_cursor(mouse_cursor cursor) = 0;
188
189 void set_resize_border_priority(bool left, bool right, bool bottom, bool top) noexcept;
190
193 virtual void close_window() = 0;
194
200 virtual void set_size_state(gui_window_size state) noexcept = 0;
201
204 virtual aarectangle workspace_rectangle() const noexcept = 0;
205
208 virtual aarectangle fullscreen_rectangle() const noexcept = 0;
209
210 virtual hi::subpixel_orientation subpixel_orientation() const noexcept = 0;
211
214 gui_window_size size_state() const noexcept
215 {
216 return _size_state;
217 }
218
223 virtual void open_system_menu() = 0;
224
227 virtual void set_window_size(extent2 extent) = 0;
228
231 [[nodiscard]] virtual std::string get_text_from_clipboard() const noexcept = 0;
232
235 virtual void set_text_on_clipboard(std::string str) noexcept = 0;
236
237 void update_mouse_target(hi::widget const *new_target_widget, point2 position = {}) noexcept;
238
245 void update_keyboard_target(hi::widget const *widget, keyboard_focus_group group = keyboard_focus_group::normal) noexcept;
246
255 void
257
266
267 [[nodiscard]] translate2 window_to_screen() const noexcept
268 {
269 return translate2{rectangle.left(), rectangle.bottom()};
270 }
271
272 [[nodiscard]] translate2 screen_to_window() const noexcept
273 {
274 return ~window_to_screen();
275 }
276
285 bool process_event(gui_event const& event) noexcept;
286
287protected:
288 static constexpr std::chrono::nanoseconds _animation_duration = std::chrono::milliseconds(150);
289
290 std::atomic<aarectangle> _redraw_rectangle = aarectangle{};
291 std::atomic<void const *> _relayout = nullptr;
292 std::atomic<void const *> _reconstrain = nullptr;
293 std::atomic<void const *> _resize = nullptr;
294
297 gui_window_size _size_state = gui_window_size::normal;
298
301 aarectangle _restore_rectangle;
302
310 utc_nanoseconds last_forced_redraw = {};
311
314 animator<float> _animated_active = _animation_duration;
315
319 virtual void create_window(extent2 new_size) = 0;
320
321private:
322 notifier<>::callback_token _setting_change_token;
323 observer<std::string>::callback_token _selected_theme_token;
324
329 hi::widget const *_mouse_target_widget = nullptr;
330
334 hi::widget const *_keyboard_target_widget = nullptr;
335
340 void widget_is_destructing(hi::widget const *sender) noexcept
341 {
342 if (_mouse_target_widget == sender) {
343 _mouse_target_widget = nullptr;
344 }
345 if (_keyboard_target_widget == sender) {
346 _keyboard_target_widget = nullptr;
347 }
348 }
349
358 bool send_events_to_widget(hi::widget const *target_widget, std::vector<gui_event> const& events) noexcept;
359
360 friend class widget;
361};
362
363} // namespace hi::inline v1
Functionality for labels, text and icons.
Defines toolbar_widget.
Defines window_widget.
Defines grid_widget.
Definition of GUI event types.
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:15
subpixel_orientation
The orientation of the RGB sub-pixels of and LCD/LED panel.
Definition subpixel_orientation.hpp:18
keyboard_focus_direction
The keyboard focus group used for finding a widget that will accept a particular focus.
Definition keyboard_focus_direction.hpp:11
keyboard_focus_group
The keyboard focus group used for finding a widget that will accept a particular focus.
Definition keyboard_focus_group.hpp:11
The HikoGUI namespace.
Definition ascii.hpp:19
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:20
A rectangle / parallelogram in 3D space.
Definition rectangle.hpp:20
constexpr extent2 size() const noexcept
The size, or length of the right and up vectors.
Definition rectangle.hpp:154
Definition translate.hpp:15
Definition gfx_device.hpp:22
Graphics system.
Definition gui_system.hpp:30
Definition gui_window.hpp:38
void update_keyboard_target(hi::widget const *widget, keyboard_focus_group group=keyboard_focus_group::normal) noexcept
Change the keyboard focus to the given widget.
virtual void set_cursor(mouse_cursor cursor)=0
Set the mouse cursor icon.
virtual void open_system_menu()=0
Open the system menu of the window.
void update_keyboard_target(keyboard_focus_group group, keyboard_focus_direction direction) noexcept
Change the keyboard focus to the given, previous or next widget.
void update_keyboard_target(hi::widget const *widget, keyboard_focus_group group, keyboard_focus_direction direction) noexcept
Change the keyboard focus to the previous or next widget from the given widget.
toolbar_widget & toolbar() noexcept
Get a reference to window's toolbar widget.
Definition gui_window.hpp:178
grid_widget & content() noexcept
Get a reference to the window's content widget.
Definition gui_window.hpp:167
virtual void set_size_state(gui_window_size state) noexcept=0
Set the size-state of the window.
aarectangle rectangle
The current rectangle of the window relative to the screen.
Definition gui_window.hpp:53
extent2 widget_size
The size of the widget.
Definition gui_window.hpp:90
bool is_gui_thread() const noexcept
Check if the current thread is the same as the gui_system loop.
virtual void render(utc_nanoseconds displayTimePoint)
Update window.
std::unique_ptr< window_widget > widget
The widget covering the complete window.
Definition gui_window.hpp:94
virtual void init()
2 phase constructor.
notifier< void()> closing
Notifier used when the window is closing.
Definition gui_window.hpp:99
virtual void close_window()=0
Ask the operating system to close this window.
void request_redraw() noexcept
Request a rectangle on the window to be redrawn.
Definition gui_window.hpp:137
virtual void set_window_size(extent2 extent)=0
Ask the operating system to set the size of this window.
virtual std::string get_text_from_clipboard() const noexcept=0
Retrieve a text string from the operating system's clip-board.
virtual aarectangle workspace_rectangle() const noexcept=0
The rectangle of the workspace of the screen where the window is currently located.
bool process_event(gui_event const &event) noexcept
Process the event.
Definition keyboard_bindings.hpp:17
Definition theme.hpp:23
A label consisting of localizable text and an icon.
Definition label.hpp:124
A notifier which can be used to call a set of registered callbacks.
Definition notifier.hpp:24
An interactive graphical object as part of the user-interface.
Definition widget.hpp:44