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
8
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
23class toolbar_button_widget : public abstract_button_widget {
24public:
25 using super = abstract_button_widget;
26 using delegate_type = typename super::delegate_type;
27
28 template<button_widget_attribute... Attributes>
31 Attributes&&...attributes) noexcept :
32 super(std::move(delegate))
33 {
34 alignment = alignment::middle_left();
35 set_attributes<0>(std::forward<Attributes>(attributes)...);
36 }
37
38 template<button_widget_attribute... Attributes>
39 toolbar_button_widget(Attributes&&...attributes) noexcept :
41 {
42 }
43
44 [[nodiscard]] std::stop_token get_stop_token() const noexcept
45 {
46 return _stop_source.get_token();
47 }
48
49 template<typename Awaiter>
50 task<> wait_for(Awaiter &&awaiter) noexcept
51 {
52 co_await std::forward<Awaiter>(awaiter);
53 set_value(widget_value::off);
54 }
55
57 [[nodiscard]] box_constraints update_constraints() noexcept override
58 {
59 _label_constraints = super::update_constraints();
60
61 // On left side a check mark, on right side short-cut. Around the label extra margin.
62 auto const extra_size = extent2{theme().margin<float>() * 2.0f, theme().margin<float>() * 2.0f};
63
64 auto constraints = _label_constraints + extra_size;
65 constraints.margins = 0;
66 return constraints;
67 }
68 void set_layout(widget_layout const& context) noexcept override
69 {
70 if (compare_store(_layout, context)) {
71 auto const label_rectangle =
72 aarectangle{theme().margin<float>(), 0.0f, context.width() - theme().margin<float>() * 2.0f, context.height()};
73 _on_label_shape = _off_label_shape = _other_label_shape =
74 box_shape{_label_constraints, label_rectangle, theme().baseline_adjustment()};
75 }
76 super::set_layout(context);
77 }
78 void draw(draw_context const& context) noexcept override
79 {
80 if (mode() > widget_mode::invisible and overlaps(context, layout())) {
81 draw_toolbar_button(context);
82 draw_button(context);
83 }
84 }
85 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override
86 {
87 return mode() >= widget_mode::partial and to_bool(group & hi::keyboard_focus_group::toolbar);
88 }
89 // @endprivatesection
90private:
91 box_constraints _label_constraints;
92 std::stop_source _stop_source;
93
94 void draw_toolbar_button(draw_context const& context) noexcept
95 {
96 auto const border_color = focus() ? focus_color() : color::transparent();
97 context.draw_box(
98 layout(), layout().rectangle(), background_color(), border_color, theme().border_width(), border_side::inside);
99 }
100};
101
102}} // namespace hi::v1
Defines abstract_button_widget.
@ rectangle
The gui_event has rectangle data.
Definition gui_event_variant.hpp:44
@ 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
@ focus
The widget has keyboard focus.
Definition style_pseudo_class.hpp:45
@ inside
The border is drawn inside the edge of a quad.
Definition draw_context_intf.hpp:35
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_layout const & layout() const noexcept
Get the current layout for this widget.
Definition widget_intf.hpp:241
virtual box_constraints update_constraints() noexcept=0
Update the constraints of the widget.
The layout of a widget.
Definition widget_layout.hpp:56
2D constraints.
Definition box_constraints.hpp:25
Definition box_shape.hpp:18
std::shared_ptr< delegate_type > delegate
The delegate that controls the button widget.
Definition abstract_button_widget.hpp:43
observer< alignment > alignment
The alignment of the button and on/off/other label.
Definition abstract_button_widget.hpp:59
A momentary button used as a child in the toolbar.
Definition toolbar_button_widget.hpp:23
void set_layout(widget_layout const &context) noexcept override
Update the internal layout of the widget.
Definition widget.hpp:116
void draw(draw_context const &context) noexcept override
Draw the widget.
Definition widget.hpp:121
box_constraints update_constraints() noexcept override
Update the constraints of the widget.
Definition widget.hpp:110
T forward(T... args)
T make_shared(T... args)
T move(T... args)