HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
toolbar_button_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
12#include "../macros.hpp"
13#include <stop_token>
14
15hi_export_module(hikogui.widgets.toolbar_button_widget);
16
17hi_export namespace hi { inline namespace v1 {
18
24public:
26 using delegate_type = typename super::delegate_type;
27
28 template<button_widget_attribute... Attributes>
30 widget_intf const* parent,
32 Attributes&&...attributes) noexcept :
34 {
35 alignment = alignment::middle_left();
36 set_attributes<0>(std::forward<Attributes>(attributes)...);
37 }
38
39 template<button_widget_attribute... Attributes>
40 toolbar_button_widget(widget_intf const* parent, Attributes&&...attributes) noexcept :
41 toolbar_button_widget(parent, std::make_shared<button_delegate>(), std::forward<Attributes>(attributes)...)
42 {
43 }
44
45 [[nodiscard]] std::stop_token get_stop_token() const noexcept
46 {
47 return _stop_source.get_token();
48 }
49
50 template<typename Awaiter>
51 task<> wait_for(Awaiter &&awaiter) noexcept
52 {
53 co_await std::forward<Awaiter>(awaiter);
54 set_value(widget_value::off);
55 }
56
58 [[nodiscard]] box_constraints update_constraints() noexcept override
59 {
60 _label_constraints = super::update_constraints();
61
62 // On left side a check mark, on right side short-cut. Around the label extra margin.
63 auto const extra_size = extent2{theme().margin<float>() * 2.0f, theme().margin<float>() * 2.0f};
64
65 auto constraints = _label_constraints + extra_size;
66 constraints.margins = 0;
67 return constraints;
68 }
69 void set_layout(widget_layout const& context) noexcept override
70 {
71 if (compare_store(_layout, context)) {
72 auto const label_rectangle =
73 aarectangle{theme().margin<float>(), 0.0f, context.width() - theme().margin<float>() * 2.0f, context.height()};
74 _on_label_shape = _off_label_shape = _other_label_shape =
75 box_shape{_label_constraints, label_rectangle, theme().baseline_adjustment()};
76 }
77 super::set_layout(context);
78 }
79 void draw(draw_context const& context) noexcept override
80 {
81 if (mode() > widget_mode::invisible and overlaps(context, layout())) {
82 draw_toolbar_button(context);
83 draw_button(context);
84 }
85 }
86 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override
87 {
88 return mode() >= widget_mode::partial and to_bool(group & hi::keyboard_focus_group::toolbar);
89 }
90 // @endprivatesection
91private:
92 box_constraints _label_constraints;
93 std::stop_source _stop_source;
94
95 void draw_toolbar_button(draw_context const& context) noexcept
96 {
97 auto const border_color = focus() ? focus_color() : color::transparent();
98 context.draw_box(
99 layout(), layout().rectangle(), background_color(), border_color, theme().border_width(), border_side::inside);
100 }
101};
102
103}} // namespace hi::v1
Defines abstract_button_widget.
@ rectangle
The gui_event has rectangle data.
@ partial
A widget is partially enabled.
@ invisible
The widget is invisible.
The HikoGUI namespace.
Definition array_generic.hpp:20
@ inside
The border is drawn inside the edge of a quad.
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
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:33
Horizontal/Vertical alignment combination.
Definition alignment.hpp:244
A high-level geometric extent.
Definition extent2.hpp:32
Draw context for drawing using the HikoGUI shaders.
Definition draw_context_intf.hpp:209
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
The layout of a widget.
Definition widget_layout.hpp:56
2D constraints.
Definition box_constraints.hpp:25
Definition box_shape.hpp:18
Base class for implementing button widgets.
Definition abstract_button_widget.hpp:36
std::shared_ptr< delegate_type > delegate
The delegate that controls the button widget.
Definition abstract_button_widget.hpp:43
A button delegate controls the state of a button widget.
Definition button_delegate.hpp:28
A momentary button used as a child in the toolbar.
Definition toolbar_button_widget.hpp:23
An interactive graphical object as part of the user-interface.
Definition widget.hpp:37
T move(T... args)