HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
icon_widget.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2021-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 "../GFX/GFX.hpp"
13#include "../geometry/geometry.hpp"
14#include "../l10n/l10n.hpp"
15#include "../macros.hpp"
16#include <memory>
17#include <string>
18#include <array>
19#include <optional>
20#include <future>
21
22hi_export_module(hikogui.widgets.icon_widget);
23
24hi_export namespace hi {
25inline namespace v1 {
26
27template<typename Context>
28concept icon_widget_attribute = forward_of<Context, observer<hi::icon>, observer<hi::alignment>, observer<hi::color>>;
29
36class icon_widget : public widget {
37public:
38 using super = widget;
39
42 observer<icon> icon = hi::icon{};
43
46 observer<color> color = color::foreground();
47
50 observer<alignment> alignment = hi::alignment::middle_center();
51
52 template<icon_widget_attribute... Attributes>
53 icon_widget(widget_intf const* parent, Attributes&&... attributes) noexcept : icon_widget(parent)
54 {
55 set_attributes(std::forward<Attributes>(attributes)...);
56 }
57
58 void set_attributes() noexcept {}
59
60 template<icon_widget_attribute First, icon_widget_attribute... Rest>
61 void set_attributes(First&& first, Rest&&... rest) noexcept
62 {
63 if constexpr (forward_of<First, observer<hi::icon>>) {
64 icon = std::forward<First>(first);
65 } else if constexpr (forward_of<First, observer<hi::alignment>>) {
66 alignment = std::forward<First>(first);
67 } else if constexpr (forward_of<First, observer<hi::color>>) {
68 color = std::forward<First>(first);
69 } else {
70 hi_static_no_default();
71 }
72 set_attributes(std::forward<Rest>(rest)...);
73 }
74
76 [[nodiscard]] box_constraints update_constraints() noexcept override
77 {
78 _layout = {};
79
80 if (_icon_has_modified.exchange(false)) {
81 _icon_type = icon_type::no;
82 _icon_size = {};
83 _glyph = {};
84 _pixmap_backing = {};
85
86 if (auto const pixmap = std::get_if<hi::pixmap<sfloat_rgba16>>(&icon)) {
87 _icon_type = icon_type::pixmap;
88 _icon_size = extent2{narrow_cast<float>(pixmap->width()), narrow_cast<float>(pixmap->height())};
89
90 if (not(_pixmap_backing = gfx_pipeline_image::paged_image{surface(), *pixmap})) {
91 // Could not get an image, retry.
92 _icon_has_modified = true;
93 ++global_counter<"icon_widget:no-backing-image:constrain">;
95 }
96
97 } else if (auto const g1 = std::get_if<font_book::font_glyph_type>(&icon)) {
98 _glyph = *g1;
99 _icon_type = icon_type::glyph;
100 _icon_size = _glyph.get_metrics().bounding_rectangle.size() *
101 (theme().text_style(semantic_text_style::label)->size * theme().pixel_density).in(pixels_per_em);
102
103 } else if (auto const g2 = std::get_if<elusive_icon>(&icon)) {
104 _glyph = find_glyph(*g2);
105 _icon_type = icon_type::glyph;
106 _icon_size = _glyph.get_metrics().bounding_rectangle.size() *
107 (theme().text_style(semantic_text_style::label)->size * theme().pixel_density).in(pixels_per_em);
108
109 } else if (auto const g3 = std::get_if<hikogui_icon>(&icon)) {
110 _glyph = find_glyph(*g3);
111 _icon_type = icon_type::glyph;
112 _icon_size = _glyph.get_metrics().bounding_rectangle.size() *
113 (theme().text_style(semantic_text_style::label)->size * theme().pixel_density).in(pixels_per_em);
114 }
115 }
116
117 auto const resolved_alignment = resolve(*alignment, os_settings::left_to_right());
118 auto const icon_constraints = box_constraints{
119 extent2{0, 0},
120 narrow_cast<extent2>(_icon_size),
121 narrow_cast<extent2>(_icon_size),
122 resolved_alignment,
123 theme().margin<float>()};
124 return icon_constraints.constrain(*minimum, *maximum);
125 }
126 void set_layout(widget_layout const& context) noexcept override
127 {
128 if (compare_store(_layout, context)) {
129 if (_icon_type == icon_type::no or not _icon_size) {
130 _icon_rectangle = {};
131 } else {
132 auto const width = std::clamp(context.shape.width(), minimum->width(), maximum->width());
133 auto const height = std::clamp(context.shape.height(), minimum->height(), maximum->height());
134
135 auto const icon_scale =
136 scale2::uniform(_icon_size, extent2{narrow_cast<float>(width), narrow_cast<float>(height)});
137 auto const new_icon_size = narrow_cast<extent2>(icon_scale * _icon_size);
138 auto const resolved_alignment = resolve(*alignment, os_settings::left_to_right());
139 _icon_rectangle = align(context.rectangle(), new_icon_size, resolved_alignment);
140 }
141 }
142 }
143 void draw(draw_context const& context) noexcept override
144 {
145 if (mode() > widget_mode::invisible and overlaps(context, layout())) {
146 switch (_icon_type) {
147 case icon_type::no:
148 break;
149
150 case icon_type::pixmap:
151 if (not context.draw_image(layout(), _icon_rectangle, _pixmap_backing)) {
152 // Continue redrawing until the image is loaded.
154 }
155 break;
156
157 case icon_type::glyph:
158 {
159 context.draw_glyph(layout(), _icon_rectangle, _glyph, theme().color(*color));
160 }
161 break;
162
163 default:
164 hi_no_default();
165 }
166 }
167 }
169private:
170 enum class icon_type { no, glyph, pixmap };
171
172 icon_type _icon_type;
173 font_book::font_glyph_type _glyph;
174 gfx_pipeline_image::paged_image _pixmap_backing;
175 std::atomic<bool> _icon_has_modified = true;
176
177 extent2 _icon_size;
178 aarectangle _icon_rectangle;
179
180 callback<void(hi::icon)> _icon_cbt;
181
182 icon_widget(widget_intf const* parent) noexcept : super(parent)
183 {
184 _icon_cbt = icon.subscribe([this](auto...) {
185 _icon_has_modified = true;
186 ++global_counter<"icon_widget:icon:constrain">;
188 });
189 }
190};
191
192} // namespace v1
193} // namespace hi::v1
Defines widget.
@ window_reconstrain
Request that widget get constraint on the next frame.
@ invisible
The widget is invisible.
The HikoGUI namespace.
Definition array_generic.hpp:20
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition misc.hpp:53
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
hi_export auto find_glyph(font const &font, grapheme grapheme) noexcept
Find a glyph using the given code-point.
Definition font_book.hpp:440
This is a RGBA floating point color.
Definition color_intf.hpp:49
Horizontal/Vertical alignment combination.
Definition alignment.hpp:244
static constexpr scale2 uniform(extent2 src_extent, extent2 dst_extent) noexcept
Get a uniform-scale-transform to scale an extent to another extent.
Definition scale2.hpp:51
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
A 2D pixel-based image.
Definition pixmap.hpp:38
A observer pointing to the whole or part of a observed_base.
Definition observer_intf.hpp:32
callback< void(value_type)> subscribe(Func &&func, callback_flags flags=callback_flags::synchronous) noexcept
Subscribe a callback to this observer.
Definition observer_intf.hpp:456
An simple GUI widget that displays an icon.
Definition icon_widget.hpp:36
observer< icon > icon
The icon to be displayed.
Definition icon_widget.hpp:42
observer< alignment > alignment
Alignment of the icon inside the widget.
Definition icon_widget.hpp:50
observer< color > color
The color a non-color icon will be displayed with.
Definition icon_widget.hpp:46
An interactive graphical object as part of the user-interface.
Definition widget.hpp:37
observer< extent2 > minimum
The minimum size this widget is allowed to be.
Definition widget.hpp:41
void request_redraw() const noexcept override
Request the widget to be redrawn on the next frame.
Definition widget.hpp:141
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
observer< extent2 > maximum
The maximum size this widget is allowed to be.
Definition widget.hpp:45
True if T is a forwarded type of Forward.
Definition concepts.hpp:137
Definition icon_widget.hpp:28
T align(T... args)
T exchange(T... args)