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
8
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#include <coroutine>
19
20hi_export_module(hikogui.widgets.system_menu_widget);
21
22hi_export namespace hi { inline namespace v1 {
23
30class system_menu_widget : public widget {
31public:
32 using super = widget;
33
34 observer<icon> icon;
35
37
38 system_menu_widget() noexcept : super()
39 {
40 _icon_widget = std::make_unique<icon_widget>(icon);
41 _icon_widget->set_parent(this);
42 }
43
44 template<forward_of<observer<hi::icon>> Icon>
45 system_menu_widget(Icon&& icon) noexcept :
47 {
48 this->icon = std::forward<Icon>(icon);
49 }
50
52 [[nodiscard]] generator<widget_intf &> children(bool include_invisible) noexcept override
53 {
54 co_yield *_icon_widget;
55 }
56
57 [[nodiscard]] box_constraints update_constraints() noexcept override
58 {
59 hi_assert_not_null(_icon_widget);
60
61 _layout = {};
62 _icon_constraints = _icon_widget->update_constraints();
63
64 auto const size = extent2{theme().large_size(), theme().large_size()};
65 return {size, size, size};
66 }
67
68 void set_layout(widget_layout const& context) noexcept override
69 {
70 if (compare_store(_layout, context)) {
71 auto const icon_height =
72 context.height() < round_cast<int>(theme().large_size() * 1.2f) ? context.height() : theme().large_size();
73 auto const icon_rectangle = aarectangle{0, context.height() - icon_height, context.width(), icon_height};
74 _icon_shape = box_shape{_icon_constraints, icon_rectangle, theme().baseline_adjustment()};
75 // Leave space for window resize handles on the left and top.
76 _system_menu_rectangle = aarectangle{
77 theme().margin<float>(),
78 0.0f,
79 context.width() - theme().margin<float>(),
80 context.height() - theme().margin<float>()};
81 }
82
83 _icon_widget->set_layout(context.transform(_icon_shape));
84 }
85
86 void draw(draw_context const& context) noexcept override
87 {
88 if (mode() > widget_mode::invisible and overlaps(context, layout())) {
89 _icon_widget->draw(context);
90 }
91 }
92
93 [[nodiscard]] hitbox hitbox_test(point2 position) const noexcept override
94 {
95 hi_axiom(loop::main().on_thread());
96
97 if (mode() >= widget_mode::partial and layout().contains(position)) {
98 // Only the top-left square should return ApplicationIcon, leave
99 // the reset to the toolbar implementation.
100 return {id, _layout.elevation, hitbox_type::application_icon};
101 } else {
102 return {};
103 }
104 }
105
107private:
108 std::unique_ptr<icon_widget> _icon_widget;
109 box_constraints _icon_constraints;
110 box_shape _icon_shape;
111
112 aarectangle _system_menu_rectangle;
113};
114
115}} // namespace hi::v1
Defines icon_widget.
Defines widget.
@ partial
A widget is partially enabled.
Definition widget_state.hpp:73
@ invisible
The widget is invisible.
Definition widget_state.hpp:41
The HikoGUI namespace.
Definition array_generic.hpp:21
The HikoGUI API version 1.
Definition array_generic.hpp:22
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition misc.hpp:53
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:33
A high-level geometric extent.
Definition extent2.hpp:32
Draw context for drawing using the HikoGUI shaders.
Definition draw_context_intf.hpp:209
widget_id id
The numeric identifier of a widget.
Definition widget_intf.hpp:31
widget_layout const & layout() const noexcept
Get the current layout for this widget.
Definition widget_intf.hpp:241
The layout of a widget.
Definition widget_layout.hpp:56
constexpr widget_layout transform(box_shape const &child_shape, transform_command command, aarectangle new_clipping_rectangle) const noexcept
Create a new widget_layout for the child widget.
Definition widget_layout.hpp:236
2D constraints.
Definition box_constraints.hpp:25
Definition box_shape.hpp:18
A observer pointing to the whole or part of a observed_base.
Definition observer_intf.hpp:32
The system menu widget.
Definition system_menu_widget.hpp:30
void set_layout(widget_layout const &context) noexcept override
Update the internal layout of the widget.
Definition widget.hpp:116
generator< widget_intf & > children(bool include_invisible) noexcept override
Get a list of child widgets.
Definition widget.hpp:61
void draw(draw_context const &context) noexcept override
Draw the widget.
Definition widget.hpp:121
widget() noexcept
Constructor for creating sub views.
Definition widget.hpp:50
hitbox hitbox_test(point2 position) const noexcept override
Find the widget that is under the mouse cursor.
Definition widget.hpp:73
box_constraints update_constraints() noexcept override
Update the constraints of the widget.
Definition widget.hpp:110
T forward(T... args)