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 "../GUI/module.hpp"
12#include "icon_widget.hpp"
13#include "../label.hpp"
14#include <memory>
15#include <string>
16#include <array>
17
18namespace hi { inline namespace v1 {
19
26template<fixed_string Name = "">
27class system_menu_widget final : public widget {
28public:
29 using super = widget;
30 constexpr static auto prefix = Name / "system-menu";
31
32 observer<icon> icon;
33
35
36 system_menu_widget(widget *parent) noexcept : super(parent)
37 {
38 _icon_widget = std::make_unique<icon_widget<prefix>>(this, icon);
39 }
40
41 system_menu_widget(widget *parent, forward_of<observer<hi::icon>> auto&& icon) noexcept : system_menu_widget(parent)
42 {
43 this->icon = hi_forward(icon);
44 }
45
47 [[nodiscard]] generator<widget const&> children(bool include_invisible) const 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 _icon_constraints = _icon_widget->update_constraints();
57
58 hilet size = theme<prefix>.size(this);
59 return {size, size, size};
60 }
61
62 void set_layout(widget_layout const& context) noexcept override
63 {
64 if (compare_store(layout, context)) {
65 hilet size = theme<prefix>.size(this);
66 hilet margin = theme<prefix>.margin(this);
67
68 hilet icon_height = context.height() < round_cast<int>(size.height() * 1.2f) ? context.height() : size.height();
69 hilet icon_rectangle = aarectanglei{0, context.height() - icon_height, context.width(), icon_height};
70 _icon_shape = box_shape{_icon_constraints, icon_rectangle, theme<prefix>.cap_height(this)};
71 // Leave space for window resize handles on the left and top.
72 _system_menu_rectangle =
73 aarectanglei{margin.left(), 0, context.width() - margin.right(), context.height() - margin.top()};
74 }
75
76 _icon_widget->set_layout(context.transform(_icon_shape));
77 }
78
79 void draw(widget_draw_context& context) noexcept override
80 {
81 if (*mode > widget_mode::invisible and overlaps(context, layout)) {
82 _icon_widget->draw(context);
83 }
84 }
85
86 [[nodiscard]] hitbox hitbox_test(point2i position) const noexcept override
87 {
88 hi_axiom(loop::main().on_thread());
89
90 if (*mode >= widget_mode::partial and layout.contains(position)) {
91 // Only the top-left square should return ApplicationIcon, leave
92 // the reset to the toolbar implementation.
93 return {id, layout.elevation, hitbox_type::application_icon};
94 } else {
95 return {};
96 }
97 }
99private:
101 box_constraints _icon_constraints;
102 box_shape _icon_shape;
103
104 aarectanglei _system_menu_rectangle;
105};
106
107}} // namespace hi::v1
Functionality for labels, text and icons.
Defines icon_widget.
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:238
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
@ partial
A widget is partially enabled.
@ invisible
The widget is invisible.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition utility.hpp:212
Definition widget.hpp:26
widget_id id
The numeric identifier of a widget.
Definition widget.hpp:35
widget * parent
Pointer to the parent widget.
Definition widget.hpp:40
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:49
Draw context for drawing using the HikoGUI shaders.
Definition widget_draw_context.hpp:205
The layout of a widget.
Definition widget_layout.hpp:37
constexpr bool contains(point3i mouse_position) const noexcept
Check if the mouse position is inside the widget.
Definition widget_layout.hpp:126
float elevation
The elevation of the widget above the window.
Definition widget_layout.hpp:72
constexpr widget_layout transform(box_shape const &child_shape, float child_elevation, aarectanglei new_clipping_rectangle) const noexcept
Create a new widget_layout for the child widget.
Definition widget_layout.hpp:203
2D constraints.
Definition box_constraints.hpp:22
Definition box_shape.hpp:15
The system menu widget.
Definition system_menu_widget.hpp:27