HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
Window_base.hpp
1// Copyright 2019 Pokitec
2// All rights reserved.
3
4#pragma once
5
6#include "TTauri/GUI/globals.hpp"
7#include "TTauri/GUI/WindowDelegate.hpp"
8#include "TTauri/GUI/GUIDevice_forward.hpp"
9#include "TTauri/GUI/Cursor.hpp"
10#include "TTauri/GUI/HitBox.hpp"
11#include "TTauri/GUI/MouseEvent.hpp"
12#include "TTauri/GUI/KeyboardEvent.hpp"
13#include "TTauri/GUI/SubpixelOrientation.hpp"
14#include "TTauri/Cells/Label.hpp"
15#include "TTauri/Text/gstring.hpp"
16#include "TTauri/Foundation/logger.hpp"
17#include "TTauri/Foundation/vec.hpp"
18#include "TTauri/Foundation/aarect.hpp"
19#include "TTauri/Foundation/ivec.hpp"
20#include "TTauri/Foundation/iaarect.hpp"
21#include "TTauri/Foundation/Trigger.hpp"
22#include "TTauri/Foundation/cpu_utc_clock.hpp"
23#include "TTauri/Foundation/fast_mutex.hpp"
24#include <rhea/simplex_solver.hpp>
25#include <unordered_set>
26#include <memory>
27#include <mutex>
28
29namespace tt {
30class Widget;
31
38public:
39 enum class State {
41 NoWindow,
42 NoDevice,
43 NoSurface,
44 NoSwapchain,
50 };
51
52 enum class Size {
53 Normal,
54 Minimized,
55 Maximized
56 };
57
58 State state = State::NoDevice;
59
67 Cursor currentCursor = Cursor::None;
68
72
76
82
87
90 Size size = Size::Normal;
91
96
99
102
105
107
108 Label title;
109
110 GUIDevice *device = nullptr;
111
114 SubpixelOrientation subpixelOrientation = SubpixelOrientation::BlueRight;
115
120 float dpi = 72.0;
121
125 [[nodiscard]] float fontScale() const noexcept {
126 return dpi / (windowScale() * 72.0f);
127 }
128
131
137
142
148
153
154 Window_base(std::shared_ptr<WindowDelegate> const &delegate, Label &&title);
155 virtual ~Window_base();
156
157 Window_base(Window_base const &) = delete;
158 Window_base &operator=(Window_base const &) = delete;
159 Window_base(Window_base &&) = delete;
160 Window_base &operator=(Window_base &&) = delete;
161
162 virtual void initialize();
163
167 void setDevice(GUIDevice *device);
168
171 void unsetDevice() { setDevice({}); }
172
173 void layout(hires_utc_clock::time_point displayTimePoint);
174
178 int layoutChildren(hires_utc_clock::time_point displayTimePoint, bool force);
179
183 virtual void render(hires_utc_clock::time_point displayTimePoint) = 0;
184
185 bool isClosed();
186
190 template<typename T, typename... Args>
191 T &makeWidget(Args &&... args);
192
193 rhea::constraint addConstraint(rhea::constraint const& constraint) noexcept;
194
195 rhea::constraint addConstraint(
196 rhea::linear_equation const& equation,
197 rhea::strength const &strength = rhea::strength::required(),
198 double weight = 1.0
199 ) noexcept;
200
201 rhea::constraint addConstraint(
202 rhea::linear_inequality const& equation,
203 rhea::strength const &strength = rhea::strength::required(),
204 double weight = 1.0
205 ) noexcept;
206
207 void removeConstraint(rhea::constraint const& constraint) noexcept;
208
209 rhea::constraint replaceConstraint(
210 rhea::constraint const &oldConstraint,
211 rhea::constraint const &newConstraint
212 ) noexcept;
213
214 rhea::constraint replaceConstraint(
215 rhea::constraint const &oldConstraint,
216 rhea::linear_equation const &newEquation,
217 rhea::strength const &strength = rhea::strength::required(),
218 double weight = 1.0
219 ) noexcept;
220
221 rhea::constraint replaceConstraint(
222 rhea::constraint const &oldConstraint,
223 rhea::linear_inequality const &newEquation,
224 rhea::strength const &strength = rhea::strength::required(),
225 double weight = 1.0
226 ) noexcept;
227
228 virtual void setCursor(Cursor cursor) = 0;
229
230 virtual void closeWindow() = 0;
231
232 virtual void minimizeWindow() = 0;
233 virtual void maximizeWindow() = 0;
234 virtual void normalizeWindow() = 0;
235 virtual void setWindowSize(ivec extent) = 0;
236
237 [[nodiscard]] virtual std::string getTextFromClipboard() const noexcept = 0;
238 virtual void setTextOnClipboard(std::string str) noexcept = 0;
239
240 void updateToNextKeyboardTarget(Widget *currentTargetWidget) noexcept;
241
242 void updateToPrevKeyboardTarget(Widget *currentTargetWidget) noexcept;
243
244protected:
245
252 iaarect OSWindowRectangle;
253
258 [[nodiscard]] float windowScale() const noexcept;
259
262 virtual void windowChangedSize(ivec extent);
263
266 virtual void openingWindow();
267
270 virtual void closingWindow();
271
274 virtual void teardown() = 0;
275
278 virtual void build() = 0;
279
280 void updateMouseTarget(Widget const *newTargetWidget, vec position=vec{0.0f, 0.0f}) noexcept;
281
282 void updateKeyboardTarget(Widget const *newTargetWidget) noexcept;
283
289 void handleMouseEvent(MouseEvent event) noexcept;
290
295 void handleKeyboardEvent(KeyboardEvent const &event) noexcept;
296
297 void handleKeyboardEvent(KeyboardState _state, KeyboardModifiers modifiers, KeyboardVirtualKey key) noexcept;
298
299 void handleKeyboardEvent(Grapheme grapheme, bool full=true) noexcept;
300
301 void handleKeyboardEvent(char32_t c, bool full=true) noexcept;
302
305 HitBox hitBoxTest(vec position) const noexcept;
306
307private:
309 rhea::simplex_solver widgetSolver;
310
311
314 bool constraintsUpdated = false;
315
317 rhea::constraint currentWindowExtentWidthConstraint;
318
320 rhea::constraint currentWindowExtentHeightConstraint;
321
329 vec suggestWidgetExtent(vec extent) noexcept;
330
334 [[nodiscard]] std::pair<vec,vec> getMinimumAndMaximumWidgetExtent() noexcept;
335
338 void layoutWindow() noexcept;
339
340
341};
342
343}
STL namespace.
Definition Label.hpp:12
Definition fast_mutex.hpp:14
Class which represents an axis-aligned rectangle.
Definition iaarect.hpp:12
A 4D vector.
Definition ivec.hpp:37
A 4D vector.
Definition vec.hpp:37
Definition GUIDevice_vulkan.hpp:22
Definition HitBox.hpp:12
Definition KeyboardEvent.hpp:37
Definition MouseEvent.hpp:12
Definition Window_base.hpp:37
void setDevice(GUIDevice *device)
std::atomic< bool > forceLayout
When set to true the widgets will be layed out.
Definition Window_base.hpp:71
std::atomic< bool > resizing
Definition Window_base.hpp:81
Size size
Definition Window_base.hpp:90
State
Definition Window_base.hpp:39
@ NoDevice
No device is associated with the Window and can therefor not be rendered on.
@ Initializing
The window has not been initialized yet.
@ SwapchainLost
The window was resized, the swapchain needs to be rebuild and can not be rendered on.
@ ReadyToRender
Need to request a swapchain before rendering.
@ WindowLost
The window was destroyed, need to cleanup.
@ NoWindow
The window was destroyed, the device will drop the window on the next render cycle.
@ NoSurface
Need to request a new surface before building a swapchain.
@ SurfaceLost
The Vulkan surface on the window was destroyed.
@ DeviceLost
The device was lost, but the window could move to a new device, or the device can be recreated.
std::unique_ptr< Widget > widget
The widget covering the complete window.
Definition Window_base.hpp:130
virtual void render(hires_utc_clock::time_point displayTimePoint)=0
int layoutChildren(hires_utc_clock::time_point displayTimePoint, bool force)
Layout the widgets in the window.
void unsetDevice()
Definition Window_base.hpp:171
Cursor currentCursor
The current cursor.
Definition Window_base.hpp:67
SubpixelOrientation subpixelOrientation
Definition Window_base.hpp:114
ivec currentWindowExtent
The current window extent as set by the GPU library.
Definition Window_base.hpp:104
ivec maximumWindowExtent
The maximum window extent as calculated by laying out all the widgets.
Definition Window_base.hpp:101
Widget * firstKeyboardWidget
The first widget in the window that needs to be selected.
Definition Window_base.hpp:147
std::atomic< bool > active
Definition Window_base.hpp:86
ivec minimumWindowExtent
The minimum window extent as calculated by laying out all the widgets.
Definition Window_base.hpp:98
Widget * keyboardTargetWidget
Target of the keyboard widget where keyboard events are sent to.
Definition Window_base.hpp:141
Widget * lastKeyboardWidget
The first widget in the window that needs to be selected.
Definition Window_base.hpp:152
float dpi
Definition Window_base.hpp:120
float fontScale() const noexcept
By how much the font needs to be scaled compared to current windowScale.
Definition Window_base.hpp:125
Widget * mouseTargetWidget
Target of the mouse Since any mouse event will change the target this is used to check if the target ...
Definition Window_base.hpp:136
fast_mutex widgetSolverMutex
Mutex for access to rhea objects registered with the widgetSolver.
Definition Window_base.hpp:95
std::atomic< bool > forceRedraw
When set to true the widgets will be redrawn.
Definition Window_base.hpp:75
T & makeWidget(Args &&... args)
Add a widget to main widget of the window.
Definition widgets.hpp:23
Definition Grapheme.hpp:20
Definition Widget.hpp:64