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 "../numeric_array.hpp"
21#include "../aarect.hpp"
22#include "../cpu_utc_clock.hpp"
23#include "../cell_address.hpp"
24#include "../label.hpp"
25#include <unordered_set>
26#include <memory>
27#include <mutex>
28
29namespace tt {
30class gui_device;
31class gui_system;
32class window_widget;
33
40public:
41 gui_system &system;
42
43 gui_window_state state = gui_window_state::no_device;
44
52 mouse_cursor currentmouse_cursor = mouse_cursor::None;
53
57
61
67
72
75 gui_window_size size_state = gui_window_size::normal;
76
79
81
82 label title;
83
86 subpixel_orientation subpixel_orientation = subpixel_orientation::BlueRight;
87
92 float dpi = 72.0;
93
96
97 gui_window(gui_system &system, std::weak_ptr<gui_window_delegate> const &delegate, label const &title);
98 virtual ~gui_window();
99
100 gui_window(gui_window const &) = delete;
101 gui_window &operator=(gui_window const &) = delete;
102 gui_window(gui_window &&) = delete;
103 gui_window &operator=(gui_window &&) = delete;
104
111 virtual void init();
112
115 void request_redraw(aarect rectangle = aarect::infinity()) noexcept
116 {
117 tt_axiom(gui_system_mutex.recurse_lock_count());
118 _request_redraw_rectangle |= rectangle;
119 }
120
124 [[nodiscard]] float fontScale() const noexcept
125 {
126 return dpi / (window_scale() * 72.0f);
127 }
128
132 void set_device(gui_device *device);
133
137 {
138 set_device({});
139 }
140
141 gui_device *device() const noexcept
142 {
143 tt_axiom(gui_system_mutex.recurse_lock_count());
144 return _device;
145 }
146
150 virtual void render(hires_utc_clock::time_point displayTimePoint) = 0;
151
154 bool is_closed();
155
159 template<typename T, cell_address CellAddress, typename... Args>
160 std::shared_ptr<T> make_widget(Args &&...args);
161
165 template<typename T, horizontal_alignment Alignment = horizontal_alignment::left, typename... Args>
167
170 virtual void set_cursor(mouse_cursor cursor) = 0;
171
172 void set_resize_border_priority(bool left, bool right, bool bottom, bool top) noexcept;
173
176 virtual void close_window() = 0;
177
180 virtual void minimize_window() = 0;
181
184 virtual void maximize_window() = 0;
185
188 virtual void normalize_window() = 0;
189
192 virtual void set_window_size(f32x4 extent) = 0;
193
196 [[nodiscard]] virtual std::string get_text_from_clipboard() const noexcept = 0;
197
200 virtual void set_text_on_clipboard(std::string str) noexcept = 0;
201
202 void update_mouse_target(std::shared_ptr<tt::widget> new_target_widget, f32x4 position = f32x4{0.0f, 0.0f}) noexcept;
203
212 keyboard_focus_group group = keyboard_focus_group::normal) noexcept;
213
224 keyboard_focus_group group,
225 keyboard_focus_direction direction) noexcept;
226
231 [[nodiscard]] virtual f32x4 virtual_screen_size() const noexcept = 0;
232
233protected:
238 gui_device *_device = nullptr;
239
251 iaarect _screen_rectangle;
252
253 bool _request_setting_change = true;
254
255 aarect _request_redraw_rectangle = aarect{};
256
261 virtual void create_window(const std::u8string &title, f32x4 extent) = 0;
262
267 [[nodiscard]] float window_scale() const noexcept;
268
271 virtual void window_changed_size(f32x4 new_extent);
272
275 virtual void teardown() = 0;
276
279 virtual void build() = 0;
280
284 [[nodiscard]] virtual bool handle_event(tt::command command) noexcept;
285
286 [[nodiscard]] virtual bool handle_event(std::vector<tt::command> const &commands) noexcept
287 {
288 for (ttlet command : commands) {
289 if (handle_event(command)) {
290 return true;
291 }
292 }
293 return false;
294 }
295
299 [[nodiscard]] virtual bool handle_event(mouse_event const &event) noexcept
300 {
301 return false;
302 }
303
307 [[nodiscard]] virtual bool handle_event(keyboard_event const &event) noexcept
308 {
309 return false;
310 }
311
317 bool send_event(mouse_event const &event) noexcept;
318
323 bool send_event(keyboard_event const &event) noexcept;
324
325 bool send_event(KeyboardState _state, keyboard_modifiers modifiers, keyboard_virtual_key key) noexcept;
326
327 bool send_event(grapheme grapheme, bool full = true) noexcept;
328
329 bool send_event(char32_t c, bool full = true) noexcept;
330
331private:
336 std::weak_ptr<tt::widget> _mouse_target_widget = {};
337
341 std::weak_ptr<tt::widget> _keyboard_target_widget = {};
342
351 template<typename Event>
352 bool send_event_to_widget(std::shared_ptr<tt::widget> target_widget, Event const &event) noexcept;
353};
354
355} // namespace tt
STL namespace.
Definition cell_address.hpp:149
Definition gui_device.hpp:22
Vulkan gui_device controller.
Definition gui_system.hpp:24
Definition gui_window.hpp:39
std::atomic< bool > active
Definition gui_window.hpp:71
mouse_cursor currentmouse_cursor
The current cursor.
Definition gui_window.hpp:52
gui_window_size size_state
Definition gui_window.hpp:75
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(f32x4 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:47
void set_device(gui_device *device)
f32x4 extent
The current window extent as set by the GPU library.
Definition gui_window.hpp:78
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:60
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:56
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:66
void unset_device()
Definition gui_window.hpp:136
std::shared_ptr< window_widget > widget
The widget covering the complete window.
Definition gui_window.hpp:95
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.
virtual f32x4 virtual_screen_size() const noexcept=0
Get the size of the virtual-screen.
virtual void render(hires_utc_clock::time_point displayTimePoint)=0
Update window.
void request_redraw(aarect rectangle=aarect::infinity()) noexcept
Request a rectangle on the window to be redrawn.
Definition gui_window.hpp:115
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:124
float dpi
Definition gui_window.hpp:92
std::shared_ptr< T > make_widget(Args &&...args)
Add a widget to main widget of the window.
Definition widgets.hpp:36
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:96