HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
system_menu_widget.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2020-2022.
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 "icon_widget.hpp"
13#include "../l10n/l10n.hpp"
14#include "../macros.hpp"
15#include <memory>
16#include <string>
17#include <array>
18
19namespace hi { inline namespace v1 {
20
28public:
29 using super = widget;
30
31 observer<icon> icon;
32
34
36 {
37 _icon_widget = std::make_unique<icon_widget>(this, icon);
38 }
39
42 {
43 this->icon = hi_forward(icon);
44 }
45
47 [[nodiscard]] generator<widget_intf &> children(bool include_invisible) noexcept override
48 {
49 co_yield *_icon_widget;
50 }
51
52 [[nodiscard]] box_constraints update_constraints() noexcept override
53 {
54 hi_assert_not_null(_icon_widget);
55
56 _layout = {};
57 _icon_constraints = _icon_widget->update_constraints();
58
59 hilet size = extent2{theme().large_size(), theme().large_size()};
60 return {size, size, size};
61 }
62
63 void set_layout(widget_layout const& context) noexcept override
64 {
65 if (compare_store(_layout, context)) {
66 hilet icon_height =
67 context.height() < round_cast<int>(theme().large_size() * 1.2f) ? context.height() : theme().large_size();
68 hilet icon_rectangle = aarectangle{0, context.height() - icon_height, context.width(), icon_height};
69 _icon_shape = box_shape{_icon_constraints, icon_rectangle, theme().baseline_adjustment()};
70 // Leave space for window resize handles on the left and top.
71 _system_menu_rectangle = aarectangle{
72 theme().margin<float>(),
73 0.0f,
74 context.width() - theme().margin<float>(),
75 context.height() - theme().margin<float>()};
76 }
77
78 _icon_widget->set_layout(context.transform(_icon_shape));
79 }
80
81 void draw(draw_context const& context) noexcept override
82 {
83 if (*mode > widget_mode::invisible and overlaps(context, layout())) {
84 _icon_widget->draw(context);
85 }
86 }
87
88 [[nodiscard]] hitbox hitbox_test(point2 position) const noexcept override
89 {
90 hi_axiom(loop::main().on_thread());
91
92 if (*mode >= widget_mode::partial and layout().contains(position)) {
93 // Only the top-left square should return ApplicationIcon, leave
94 // the reset to the toolbar implementation.
95 return {id, _layout.elevation, hitbox_type::application_icon};
96 } else {
97 return {};
98 }
99 }
100
102private:
103 std::unique_ptr<icon_widget> _icon_widget;
104 box_constraints _icon_constraints;
105 box_shape _icon_shape;
106
107 aarectangle _system_menu_rectangle;
108};
109
110}} // namespace hi::v1
Defines widget.
Defines icon_widget.
@ partial
A widget is partially enabled.
@ invisible
The widget is invisible.
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition misc.hpp:56
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:29
A high-level geometric extent.
Definition extent2.hpp:29
Draw context for drawing using the HikoGUI shaders.
Definition draw_context.hpp:208
widget_id id
The numeric identifier of a widget.
Definition widget_intf.hpp:23
widget_intf * parent
Pointer to the parent widget.
Definition widget_intf.hpp:28
The layout of a widget.
Definition widget_layout.hpp:38
2D constraints.
Definition box_constraints.hpp:25
Definition box_shape.hpp:18
The system menu widget.
Definition system_menu_widget.hpp:27
An interactive graphical object as part of the user-interface.
Definition widget.hpp:37
widget_layout const & layout() const noexcept override
Get the current layout for this widget.
Definition widget.hpp:169
widget(widget *parent) noexcept
Definition widget.hpp:87
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:42