HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
overlay_widget.hpp
Go to the documentation of this file.
1// Copyright Take Vos 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
9#pragma once
10
11#include "widget.hpp"
12#include "../macros.hpp"
13#include <coroutine>
14
15hi_export_module(hikogui.widgets.overlay_widget);
16
17hi_export namespace hi { inline namespace v1 {
18
42class overlay_widget : public widget {
43public:
44 using super = widget;
45
47
53 {
54 }
55
56 void set_widget(std::unique_ptr<widget> new_widget) noexcept
57 {
58 _content = std::move(new_widget);
59 ++global_counter<"overlay_widget:set_widget:constrain">;
61 }
62
72 template<typename Widget, typename... Args>
73 Widget& emplace(Args&&...args) noexcept
74 {
75 hi_axiom(loop::main().on_thread());
76 hi_assert(_content == nullptr);
77
78 auto tmp = std::make_unique<Widget>(this, std::forward<Args>(args)...);
79 auto& ref = *tmp;
80 set_widget(std::move(tmp));
81 return ref;
82 }
83
85 [[nodiscard]] generator<widget_intf &> children(bool include_invisible) noexcept override
86 {
87 co_yield *_content;
88 }
89
90 [[nodiscard]] box_constraints update_constraints() noexcept override
91 {
92 _layout = {};
93 _content_constraints = _content->update_constraints();
94 return _content_constraints;
95 }
96
97 void set_layout(widget_layout const& context) noexcept override
98 {
99 _layout = context;
100
101 // The clipping rectangle of the overlay matches the rectangle exactly, with a border around it.
102 _layout.clipping_rectangle = context.rectangle() + theme().border_width();
103
104 auto const content_rectangle = context.rectangle();
105 _content_shape = box_shape{_content_constraints, content_rectangle, theme().baseline_adjustment()};
106
107 // The content should not draw in the border of the overlay, so give a tight clipping rectangle.
108 _content->set_layout(_layout.transform(_content_shape, context.rectangle()));
109 }
110
111 void draw(draw_context const& context) noexcept override
112 {
113 if (mode() > widget_mode::invisible) {
114 if (overlaps(context, layout())) {
115 draw_background(context);
116 }
117 _content->draw(context);
118 }
119 }
120
121 [[nodiscard]] color background_color() const noexcept override
122 {
123 return theme().color(semantic_color::fill, _layout.layer + 1);
124 }
125
126 [[nodiscard]] color foreground_color() const noexcept override
127 {
128 return theme().color(semantic_color::border, _layout.layer + 1);
129 }
130
131 void scroll_to_show(hi::aarectangle rectangle) noexcept override
132 {
133 // An overlay is in an absolute position on the window,
134 // so do not forward the scroll_to_show message to its parent.
135 }
136
137 [[nodiscard]] hitbox hitbox_test(point2 position) const noexcept override
138 {
139 hi_axiom(loop::main().on_thread());
140
141 if (mode() >= widget_mode::partial) {
142 return _content->hitbox_test_from_parent(position);
143 } else {
144 return {};
145 }
146 }
147
148 bool handle_event(gui_event const& event) noexcept override
149 {
150 // Short-cut event handling, no-events should be passed below the overlay.
151 return true;
152 }
154private:
156 box_constraints _content_constraints;
157 box_shape _content_shape;
158
159 void draw_background(draw_context const& context) noexcept
160 {
161 context.draw_box(
162 layout(), layout().rectangle(), background_color(), foreground_color(), theme().border_width(), border_side::outside);
163 }
164};
165
166}} // namespace hi::v1
Defines widget.
@ window_reconstrain
Request that widget get constraint on the next frame.
@ rectangle
The gui_event has rectangle data.
@ partial
A widget is partially enabled.
@ invisible
The widget is invisible.
The HikoGUI namespace.
Definition array_generic.hpp:20
@ outside
The border is drawn outside the edge of a quad.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:33
Definition widget_intf.hpp:24
widget_layout const & layout() const noexcept
Get the current layout for this widget.
Definition widget_intf.hpp:206
widget_intf * parent
Pointer to the parent widget.
Definition widget_intf.hpp:35
2D constraints.
Definition box_constraints.hpp:25
A GUI widget which may exist anywhere on a window overlaid above any other widget.
Definition overlay_widget.hpp:42
overlay_widget(widget_intf const *parent) noexcept
Constructs an empty overlay widget.
Definition overlay_widget.hpp:52
Widget & emplace(Args &&...args) noexcept
Add a content widget directly to this overlay widget.
Definition overlay_widget.hpp:73
An interactive graphical object as part of the user-interface.
Definition widget.hpp:37
void scroll_to_show() noexcept
Scroll to show the important part of the widget.
Definition widget_intf.hpp:312
widget() noexcept
Constructor for creating sub views.
Definition widget.hpp:55
bool process_event(gui_event const &event) const noexcept override
Send a event to the window.
Definition widget.hpp:130
T move(T... args)