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 { inline namespace v1 {
25
26template<typename Context>
28
35class icon_widget : public widget {
36public:
37 using super = widget;
38
41 observer<icon> icon = hi::icon{};
42
45 observer<color> color = color::foreground();
46
49 observer<alignment> alignment = hi::alignment::middle_center();
50
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 {
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 =
101 _glyph.get_metrics().bounding_rectangle.size() * theme().text_style(semantic_text_style::label)->size * theme().scale;
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 =
107 _glyph.get_metrics().bounding_rectangle.size() * theme().text_style(semantic_text_style::label)->size * theme().scale;
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 =
113 _glyph.get_metrics().bounding_rectangle.size() * theme().text_style(semantic_text_style::label)->size * theme().scale;
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 = scale2::uniform(_icon_size, extent2{narrow_cast<float>(width), narrow_cast<float>(height)});
136 auto const new_icon_size = narrow_cast<extent2>(icon_scale * _icon_size);
137 auto const resolved_alignment = resolve(*alignment, os_settings::left_to_right());
138 _icon_rectangle = align(context.rectangle(), new_icon_size, resolved_alignment);
139 }
140 }
141 }
142 void draw(draw_context const& context) noexcept override
143 {
144 if (mode() > widget_mode::invisible and overlaps(context, layout())) {
145 switch (_icon_type) {
146 case icon_type::no:
147 break;
148
149 case icon_type::pixmap:
150 if (not context.draw_image(layout(), _icon_rectangle, _pixmap_backing)) {
151 // Continue redrawing until the image is loaded.
153 }
154 break;
155
156 case icon_type::glyph:
157 {
158 context.draw_glyph(layout(), _icon_rectangle, _glyph, theme().color(*color));
159 }
160 break;
161
162 default:
163 hi_no_default();
164 }
165 }
166 }
168private:
169 enum class icon_type { no, glyph, pixmap };
170
171 icon_type _icon_type;
172 font_book::font_glyph_type _glyph;
173 gfx_pipeline_image::paged_image _pixmap_backing;
174 std::atomic<bool> _icon_has_modified = true;
175
176 extent2 _icon_size;
177 aarectangle _icon_rectangle;
178
179 callback<void(hi::icon)> _icon_cbt;
180
182 {
183 _icon_cbt = icon.subscribe([this](auto...) {
184 _icon_has_modified = true;
185 ++global_counter<"icon_widget:icon:constrain">;
187 });
188 }
189};
190
191}} // namespace hi::v1
Defines widget.
@ window_reconstrain
Request that widget get constraint on the next frame.
@ invisible
The widget is invisible.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
hi_export hi_inline auto find_glyph(font const &font, grapheme grapheme) noexcept
Find a glyph using the given code-point.
Definition font_book.hpp:440
The HikoGUI namespace.
Definition recursive_iterator.hpp:15
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition misc.hpp:53
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:378
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
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
Definition not_null.hpp:22
An simple GUI widget that displays an icon.
Definition icon_widget.hpp:35
observer< icon > icon
The icon to be displayed.
Definition icon_widget.hpp:41
observer< alignment > alignment
Alignment of the icon inside the widget.
Definition icon_widget.hpp:49
observer< color > color
The color a non-color icon will be displayed with.
Definition icon_widget.hpp:45
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:135
widget(widget_intf const *parent) noexcept
Definition widget.hpp:49
bool process_event(gui_event const &event) const noexcept override
Send a event to the window.
Definition widget.hpp:124
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:136
Definition icon_widget.hpp:27
T align(T... args)
T exchange(T... args)