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_state.hpp"
8#include "gui_window_size.hpp"
9#include "gui_window_delegate.hpp"
10#include "gui_system_globals.hpp"
11#include "mouse_cursor.hpp"
12#include "hit_box.hpp"
13#include "mouse_event.hpp"
14#include "keyboard_event.hpp"
15#include "subpixel_orientation.hpp"
16#include "keyboard_focus_direction.hpp"
17#include "keyboard_focus_group.hpp"
18#include "../text/gstring.hpp"
19#include "../logger.hpp"
20#include "../geometry/axis_aligned_rectangle.hpp"
21#include "../cpu_utc_clock.hpp"
22#include "../label.hpp"
23#include <unordered_set>
24#include <memory>
25#include <mutex>
26
27namespace tt {
28class gui_device;
29class gui_system;
30class window_widget;
31
38public:
39 gui_system &system;
40
41 gui_window_state state = gui_window_state::no_device;
42
50 mouse_cursor currentmouse_cursor = mouse_cursor::None;
51
55
59
65
70
73 gui_window_size size_state = gui_window_size::normal;
74
77
79
80 label title;
81
84 subpixel_orientation subpixel_orientation = subpixel_orientation::BlueRight;
85
90 float dpi = 72.0;
91
94
95 gui_window(gui_system &system, std::weak_ptr<gui_window_delegate> const &delegate, label const &title);
96 virtual ~gui_window();
97
98 gui_window(gui_window const &) = delete;
99 gui_window &operator=(gui_window const &) = delete;
100 gui_window(gui_window &&) = delete;
101 gui_window &operator=(gui_window &&) = delete;
102
109 virtual void init();
110
114 {
115 tt_axiom(gui_system_mutex.recurse_lock_count());
116 _request_redraw_rectangle |= rectangle;
117 }
118
121 void request_redraw() noexcept
122 {
123 tt_axiom(gui_system_mutex.recurse_lock_count());
125 }
126
130 [[nodiscard]] float fontScale() const noexcept
131 {
132 return dpi / (window_scale() * 72.0f);
133 }
134
138 void set_device(gui_device *device);
139
143 {
144 set_device({});
145 }
146
147 gui_device *device() const noexcept
148 {
149 tt_axiom(gui_system_mutex.recurse_lock_count());
150 return _device;
151 }
152
156 virtual void render(hires_utc_clock::time_point displayTimePoint) = 0;
157
160 bool is_closed();
161
165 template<typename T, typename... Args>
166 std::shared_ptr<T> make_widget(size_t column_nr, size_t row_nr, Args &&...args);
167
171 template<typename T, typename... Args>
172 std::shared_ptr<T> make_widget(std::string_view address, Args &&...args);
173
177 template<typename T, horizontal_alignment Alignment = horizontal_alignment::left, typename... Args>
179
182 virtual void set_cursor(mouse_cursor cursor) = 0;
183
184 void set_resize_border_priority(bool left, bool right, bool bottom, bool top) noexcept;
185
188 virtual void close_window() = 0;
189
192 virtual void minimize_window() = 0;
193
196 virtual void maximize_window() = 0;
197
200 virtual void normalize_window() = 0;
201
204 virtual void set_window_size(extent2 extent) = 0;
205
208 [[nodiscard]] virtual std::string get_text_from_clipboard() const noexcept = 0;
209
212 virtual void set_text_on_clipboard(std::string str) noexcept = 0;
213
214 void update_mouse_target(std::shared_ptr<tt::widget> new_target_widget, point2 position = {}) noexcept;
215
224 keyboard_focus_group group = keyboard_focus_group::normal) noexcept;
225
236 keyboard_focus_group group,
237 keyboard_focus_direction direction) noexcept;
238
243 [[nodiscard]] virtual extent2 virtual_screen_size() const noexcept = 0;
244
245 [[nodiscard]] translate2 window_to_screen() const noexcept
246 {
247 return translate2{_screen_rectangle.left(), _screen_rectangle.bottom()};
248 }
249
250 [[nodiscard]] translate2 screen_to_window() const noexcept
251 {
252 return ~window_to_screen();
253 }
254
255protected:
260 gui_device *_device = nullptr;
261
273 aarectangle _screen_rectangle;
274
275 bool _request_setting_change = true;
276
277 aarectangle _request_redraw_rectangle = aarectangle{};
278
283 virtual void create_window(const std::u8string &title, extent2 extent) = 0;
284
289 [[nodiscard]] float window_scale() const noexcept;
290
293 virtual void window_changed_size(extent2 new_extent);
294
297 virtual void teardown() = 0;
298
301 virtual void build() = 0;
302
306 [[nodiscard]] virtual bool handle_event(tt::command command) noexcept;
307
308 [[nodiscard]] virtual bool handle_event(std::vector<tt::command> const &commands) noexcept
309 {
310 for (ttlet command : commands) {
311 if (handle_event(command)) {
312 return true;
313 }
314 }
315 return false;
316 }
317
321 [[nodiscard]] virtual bool handle_event(mouse_event const &event) noexcept
322 {
323 return false;
324 }
325
329 [[nodiscard]] virtual bool handle_event(keyboard_event const &event) noexcept
330 {
331 return false;
332 }
333
339 bool send_event(mouse_event const &event) noexcept;
340
345 bool send_event(keyboard_event const &event) noexcept;
346
347 bool send_event(KeyboardState _state, keyboard_modifiers modifiers, keyboard_virtual_key key) noexcept;
348
349 bool send_event(grapheme grapheme, bool full = true) noexcept;
350
351 bool send_event(char32_t c, bool full = true) noexcept;
352
353private:
358 std::weak_ptr<tt::widget> _mouse_target_widget = {};
359
363 std::weak_ptr<tt::widget> _keyboard_target_widget = {};
364
373 template<typename Event>
374 bool send_event_to_widget(std::shared_ptr<tt::widget> target_widget, Event const &event) noexcept;
375};
376
377} // namespace tt
STL namespace.
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:18
Class which represents an rectangle.
Definition rectangle.hpp:16
Definition gui_device.hpp:22
Vulkan gui_device controller.
Definition gui_system.hpp:24
Definition gui_window.hpp:37
std::atomic< bool > active
Definition gui_window.hpp:69
mouse_cursor currentmouse_cursor
The current cursor.
Definition gui_window.hpp:50
gui_window_size size_state
Definition gui_window.hpp:73
virtual void set_text_on_clipboard(std::string str) noexcept=0
Place a text string on the operating system's clip-board.
virtual void set_window_size(extent2 extent)=0
Ask the operating system to set the size of this window.
virtual void close_window()=0
Ask the operating system to close this window.
std::shared_ptr< T > make_toolbar_widget(Args &&...args)
Add a widget to main widget of the window.
Definition widgets.hpp:56
void set_device(gui_device *device)
virtual void init()
2 phase constructor.
std::atomic< bool > requestResize
When set to true the window will resize to the size of the contained widget.
Definition gui_window.hpp:58
virtual void maximize_window()=0
Ask the operating system to maximize this window.
std::atomic< bool > requestLayout
When set to true the widgets will be layed out.
Definition gui_window.hpp:54
extent2 extent
The current window extent as set by the GPU library.
Definition gui_window.hpp:76
bool is_closed()
Check if the window was closed by the operating system.
void update_keyboard_target(std::shared_ptr< tt::widget > const &widget, keyboard_focus_group group, keyboard_focus_direction direction) noexcept
Change the keyboard focus to the given, previous or next widget.
std::atomic< bool > resizing
Definition gui_window.hpp:64
void unset_device()
Definition gui_window.hpp:142
std::shared_ptr< window_widget > widget
The widget covering the complete window.
Definition gui_window.hpp:93
virtual void set_cursor(mouse_cursor cursor)=0
Set the mouse cursor icon.
virtual void minimize_window()=0
Ask the operating system to minimize this window.
void update_keyboard_target(std::shared_ptr< tt::widget > widget, keyboard_focus_group group=keyboard_focus_group::normal) noexcept
Change the keyboard focus to the given widget.
virtual std::string get_text_from_clipboard() const noexcept=0
Retrieve a text string from the operating system's clip-board.
std::shared_ptr< T > make_widget(size_t column_nr, size_t row_nr, Args &&...args)
Add a widget to main widget of the window.
Definition widgets.hpp:35
virtual void render(hires_utc_clock::time_point displayTimePoint)=0
Update window.
virtual extent2 virtual_screen_size() const noexcept=0
Get the size of the virtual-screen.
void request_redraw() noexcept
Request a rectangle on the window to be redrawn.
Definition gui_window.hpp:121
virtual void normalize_window()=0
Ask the operating system to normalize this window.
float fontScale() const noexcept
By how much the font needs to be scaled compared to current windowScale.
Definition gui_window.hpp:130
void request_redraw(aarectangle rectangle) noexcept
Request a rectangle on the window to be redrawn.
Definition gui_window.hpp:113
float dpi
Definition gui_window.hpp:90
A localized text + icon label.
Definition label.hpp:76
int recurse_lock_count() const noexcept
This function should be used in tt_axiom() to check if the lock is held by current thread.
Definition unfair_recursive_mutex.hpp:60
Definition widget.hpp:97