HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
gui_window.hpp
1// Copyright Take Vos 2019-2021.
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 "gui_window_delegate.hpp"
9#include "mouse_cursor.hpp"
10#include "hitbox.hpp"
11#include "gui_event.hpp"
12#include "keyboard_focus_direction.hpp"
13#include "keyboard_focus_group.hpp"
14#include "theme.hpp"
16#include "../geometry/axis_aligned_rectangle.hpp"
17#include "../widgets/window_widget.hpp"
18#include "../widgets/grid_widget.hpp"
19#include "../widgets/toolbar_widget.hpp"
20#include "../chrono.hpp"
21#include "../label.hpp"
22#include "../animator.hpp"
23#include <unordered_set>
24#include <memory>
25#include <mutex>
26
27namespace hi::inline v1 {
28class gfx_device;
29class gfx_system;
30class gfx_surface;
31class gui_system;
32class keyboard_bindings;
33
40public:
42
43 gui_system& gui;
44
46
57
65 mouse_cursor current_mouse_cursor = mouse_cursor::None;
66
71 bool resizing = false;
72
76 bool active = false;
77
78 label title;
79
84 float dpi = 72.0;
85
89 hi::theme theme = {};
90
94
98
103
104 gui_window(gui_system& gui, label const& title, std::weak_ptr<delegate_type> delegate = {}) noexcept;
105
106 virtual ~gui_window();
107
108 gui_window(gui_window const&) = delete;
109 gui_window& operator=(gui_window const&) = delete;
110 gui_window(gui_window&&) = delete;
111 gui_window& operator=(gui_window&&) = delete;
112
119 virtual void init();
120
123 [[nodiscard]] bool is_gui_thread() const noexcept;
124
125 void set_device(gfx_device *device) noexcept;
126
129 hi::keyboard_bindings const& keyboard_bindings() const noexcept;
130
133 void request_redraw(aarectangle redraw_rectangle) noexcept
134 {
135 _redraw_rectangle |= redraw_rectangle;
136 }
137
140 void request_redraw() noexcept
141 {
142 hi_axiom(is_gui_thread());
143 request_redraw(aarectangle{rectangle.size()});
144 }
145
146 void request_relayout(void const *w) noexcept
147 {
148 _relayout.store(w, std::memory_order::relaxed);
149 }
150
151 void request_reconstrain(void const *w) noexcept
152 {
153 _reconstrain.store(w, std::memory_order::relaxed);
154 }
155
156 void request_resize(void const *w) noexcept
157 {
158 _resize.store(w, std::memory_order::relaxed);
159 }
160
164 virtual void render(utc_nanoseconds displayTimePoint);
165
170 [[nodiscard]] grid_widget& content() noexcept
171 {
172 hi_axiom(is_gui_thread());
173 hi_axiom(widget);
174 return widget->content();
175 }
176
181 [[nodiscard]] toolbar_widget& toolbar() noexcept
182 {
183 hi_axiom(is_gui_thread());
184 hi_axiom(widget);
185 return widget->toolbar();
186 }
187
190 virtual void set_cursor(mouse_cursor cursor) = 0;
191
192 void set_resize_border_priority(bool left, bool right, bool bottom, bool top) noexcept;
193
196 virtual void close_window() = 0;
197
203 virtual void set_size_state(gui_window_size state) noexcept = 0;
204
207 virtual aarectangle workspace_rectangle() const noexcept = 0;
208
211 virtual aarectangle fullscreen_rectangle() const noexcept = 0;
212
213 virtual hi::subpixel_orientation subpixel_orientation() const noexcept = 0;
214
217 gui_window_size size_state() const noexcept
218 {
219 return _size_state;
220 }
221
226 virtual void open_system_menu() = 0;
227
230 virtual void set_window_size(extent2 extent) = 0;
231
234 [[nodiscard]] virtual std::string get_text_from_clipboard() const noexcept = 0;
235
238 virtual void set_text_on_clipboard(std::string str) noexcept = 0;
239
240 void update_mouse_target(hi::widget const *new_target_widget, point2 position = {}) noexcept;
241
248 void update_keyboard_target(hi::widget const *widget, keyboard_focus_group group = keyboard_focus_group::normal) noexcept;
249
258 void
259 update_keyboard_target(hi::widget const *widget, keyboard_focus_group group, keyboard_focus_direction direction) noexcept;
260
268 void update_keyboard_target(keyboard_focus_group group, keyboard_focus_direction direction) noexcept;
269
270 [[nodiscard]] translate2 window_to_screen() const noexcept
271 {
272 return translate2{rectangle.left(), rectangle.bottom()};
273 }
274
275 [[nodiscard]] translate2 screen_to_window() const noexcept
276 {
277 return ~window_to_screen();
278 }
279
288 bool process_event(gui_event const& event) noexcept;
289
290protected:
291 static constexpr std::chrono::nanoseconds _animation_duration = std::chrono::milliseconds(150);
292
294
295 std::atomic<aarectangle> _redraw_rectangle = aarectangle{};
296 std::atomic<void const *> _relayout = nullptr;
297 std::atomic<void const *> _reconstrain = nullptr;
298 std::atomic<void const *> _resize = nullptr;
299
302 gui_window_size _size_state = gui_window_size::normal;
303
306 aarectangle _restore_rectangle;
307
315 utc_nanoseconds last_forced_redraw = {};
316
319 animator<float> _animated_active = _animation_duration;
320
324 virtual void create_window(extent2 new_size) = 0;
325
326private:
327 notifier<>::token_type _setting_change_token;
328 observable<std::string>::token_type _selected_theme_token;
329
334 hi::widget const *_mouse_target_widget = nullptr;
335
339 hi::widget const *_keyboard_target_widget = nullptr;
340
345 void widget_is_destructing(hi::widget const *sender) noexcept
346 {
347 if (_mouse_target_widget == sender) {
348 _mouse_target_widget = nullptr;
349 }
350 if (_keyboard_target_widget == sender) {
351 _keyboard_target_widget = nullptr;
352 }
353 }
354
363 bool send_events_to_widget(hi::widget const *target_widget, std::vector<gui_event> const& events) noexcept;
364
365 friend class widget;
366};
367
368} // namespace hi::inline v1
subpixel_orientation
The orientation of the RGB sub-pixels of and LCD/LED panel.
Definition subpixel_orientation.hpp:18
STL namespace.
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
A user interface event.
Definition gui_event.hpp:58
Graphics system.
Definition gui_system.hpp:30
Definition gui_window.hpp:39
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:181
grid_widget & content() noexcept
Get a reference to the window's content widget.
Definition gui_window.hpp:170
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:56
extent2 widget_size
The size of the widget.
Definition gui_window.hpp:93
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:97
virtual void init()
2 phase constructor.
notifier< void()> closing
Notifier used when the window is closing.
Definition gui_window.hpp:102
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:140
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 gui_window_delegate.hpp:12
Definition keyboard_bindings.hpp:17
Definition theme.hpp:21
A label consisting of localizable text and an icon.
Definition label.hpp:27
A notifier which can be used to call a set of registered callbacks.
Definition notifier.hpp:24
A GUI widget that lays out child-widgets in a grid with variable sized cells.
Definition grid_widget.hpp:37
A toolbar widget is located at the top of a window and lays out its children horizontally.
Definition toolbar_widget.hpp:28
An interactive graphical object as part of the user-interface.
Definition widget.hpp:39