7#include "abstract_button_widget.hpp"
8#include "toolbar_widget.hpp"
9#include "../stencils/label_stencil.hpp"
10#include "../graphic_path.hpp"
11#include "../GUI/draw_context.hpp"
51 using value_type =
typename super::value_type;
55 template<
typename Value = observable<value_type>>
59 value_type true_value,
60 Value &&value = {})
noexcept :
62 _parent_is_toolbar(
parent->is_toolbar())
68 void init() noexcept
override
71 _label_callback = this->label.subscribe([
this](
auto...) {
72 this->_request_reconstrain =
true;
81 this->_show_check_mark = flag;
82 this->_request_reconstrain =
true;
97 return _show_check_mark;
105 this->_show_icon = flag;
106 this->_request_reconstrain =
true;
132 this->_show_short_cut = flag;
133 this->_request_reconstrain =
true;
150 return _show_short_cut;
156 if (_parent_is_toolbar) {
157 return is_toolbar(group) && *this->
enabled;
159 return is_menu(group) && *this->
enabled;
166 case command::gui_menu_next:
167 if (!_parent_is_toolbar && !this->
is_last(keyboard_focus_group::menu)) {
169 this->
shared_from_this(), keyboard_focus_group::menu, keyboard_focus_direction::forward);
174 case command::gui_menu_prev:
175 if (!_parent_is_toolbar && !this->
is_first(keyboard_focus_group::menu)) {
177 this->
shared_from_this(), keyboard_focus_group::menu, keyboard_focus_direction::backward);
182 case command::gui_toolbar_next:
183 if (_parent_is_toolbar && !this->
is_last(keyboard_focus_group::toolbar)) {
185 this->
shared_from_this(), keyboard_focus_group::toolbar, keyboard_focus_direction::forward);
190 case command::gui_toolbar_prev:
191 if (_parent_is_toolbar && !this->
is_first(keyboard_focus_group::toolbar)) {
193 this->
shared_from_this(), keyboard_focus_group::toolbar, keyboard_focus_direction::backward);
198 case command::gui_activate:
199 case command::gui_enter:
200 if (!_parent_is_toolbar) {
202 command == command::gui_enter ? keyboard_focus_direction::forward : keyboard_focus_direction::backward;
205 ttlet focus_widget_after_commit = this->window.
widget->find_next_widget(
227 _label_stencil = stencil::make_unique(alignment::middle_left, *
label, theme::global->labelStyle);
228 _label_stencil->set_show_icon(_show_icon);
230 _check_mark_stencil = stencil::make_unique(alignment::middle_center, elusive_icon::Ok);
232 auto width = _label_stencil->preferred_extent().width() + theme::global->margin * 2.0f;
233 if (_show_check_mark) {
234 width += theme::global->small_icon_size + theme::global->margin;
236 if (_show_short_cut) {
237 width += theme::global->margin + theme::global->small_icon_size * 3.0f;
240 ttlet height = _label_stencil->preferred_extent().height() + theme::global->margin * 2.0f;
241 this->_preferred_size = {
253 need_layout |= std::exchange(this->_request_relayout,
false);
255 ttlet check_mark_x = this->
rectangle().left() + theme::global->margin;
256 ttlet check_mark_width = theme::global->small_icon_size;
257 ttlet check_mark_height = theme::global->small_icon_size;
258 ttlet check_mark_y = this->
rectangle().
middle() - check_mark_height * 0.5f;
259 ttlet check_mark_rectangle =
aarect{check_mark_x, check_mark_y, check_mark_width, check_mark_height};
260 _check_mark_stencil->set_layout_parameters(check_mark_rectangle);
262 ttlet short_cut_width = theme::global->small_icon_size * 3.0f;
263 ttlet short_cut_height = this->
rectangle().height();
264 ttlet short_cut_x = this->
rectangle().right() - theme::global->margin - short_cut_width;
265 ttlet short_cut_y = this->
rectangle().bottom();
266 ttlet short_cut_rectangle =
aarect{short_cut_x, short_cut_y, short_cut_width, short_cut_height};
268 ttlet label_height = this->
rectangle().height();
269 ttlet label_y = this->
rectangle().bottom();
270 auto label_width = this->
rectangle().width() - theme::global->margin * 2.0f;
271 auto label_x = this->
rectangle().left() + theme::global->margin;
272 if (_show_check_mark) {
273 label_width -= (check_mark_width + theme::global->margin);
274 label_x += (check_mark_width + theme::global->margin);
276 if (_show_short_cut) {
277 label_width -= (theme::global->margin + short_cut_width);
279 ttlet label_rectangle =
aarect{label_x, label_y, label_width, label_height};
281 _label_stencil->set_layout_parameters(label_rectangle);
291 draw_background(context);
292 draw_check_mark(context);
300 typename decltype(
label)::callback_ptr_type _label_callback;
304 bool _parent_is_toolbar;
305 bool _show_check_mark =
false;
306 bool _show_icon =
false;
307 bool _show_short_cut =
false;
313 context.line_color = context.fill_color;
314 if (this->_focus && this->window.
active) {
315 context.line_color = theme::global->accentColor;
318 context.draw_box_with_border_inside(this->
rectangle());
321 void draw_label(draw_context context)
noexcept
323 context.transform = translate3{0.0f, 0.0f, 0.1f} * context.transform;
325 context.line_color = theme::global->foregroundColor;
327 _label_stencil->draw(context);
330 void draw_check_mark(draw_context context)
noexcept
332 if (this->value == this->true_value) {
333 context.transform = translate3{0.0f, 0.0f, 0.1f} * context.transform;
335 context.line_color = theme::global->foregroundColor;
337 _check_mark_stencil->draw(context);
T middle() const noexcept
The middle on the y-axis between bottom and top.
Definition aarect.hpp:272
Draw context for drawing using the TTauri shaders.
Definition draw_context.hpp:33
Definition gui_window.hpp:39
std::atomic< bool > active
Definition gui_window.hpp:71
std::shared_ptr< window_widget > widget
The widget covering the complete window.
Definition gui_window.hpp:95
void update_keyboard_target(std::shared_ptr< tt::widget > widget, keyboard_focus_group group=keyboard_focus_group::normal) noexcept
Change the keyboard focus to the given widget.
A localized text + icon label.
Definition label.hpp:76
Definition observable.hpp:20
int recurse_lock_count() const noexcept
This function should be used in tt_axiom() to check if the lock is held by current thread.
Definition unfair_recursive_mutex.hpp:60
An abstract button widget.
Definition abstract_button_widget.hpp:16
bool handle_event(command command) noexcept
Handle command.
Definition abstract_button_widget.hpp:55
Menu item widget.
Definition menu_item_widget.hpp:48
bool show_icon() const noexcept
Whether the text in the label will align to an optional icon in the label.
Definition menu_item_widget.hpp:121
void set_show_check_mark(bool flag) noexcept
Set the show_check_mark() flag.
Definition menu_item_widget.hpp:78
bool handle_event(tt::command command) noexcept override
Handle command.
Definition menu_item_widget.hpp:163
void update_layout(hires_utc_clock::time_point display_time_point, bool need_layout) noexcept
Update the internal layout of the widget.
Definition menu_item_widget.hpp:249
bool show_check_mark() const noexcept
Whether the label aligns to an optional check-mark.
Definition menu_item_widget.hpp:94
void draw(draw_context context, hires_utc_clock::time_point display_time_point) noexcept override
Draw the widget.
Definition menu_item_widget.hpp:286
bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override
Check if the widget will accept keyboard focus.
Definition menu_item_widget.hpp:153
bool update_constraints(hires_utc_clock::time_point display_time_point, bool need_reconstrain) noexcept override
Update the constraints of the widget.
Definition menu_item_widget.hpp:222
void set_show_short_cut(bool flag) noexcept
Set the show_short_cut() flag.
Definition menu_item_widget.hpp:129
void set_show_icon(bool flag) noexcept
Set the show_icon() flag.
Definition menu_item_widget.hpp:102
void init() noexcept override
Should be called right after allocating and constructing a widget.
Definition menu_item_widget.hpp:68
bool show_short_cut() const noexcept
Whether the menu item should make space for an optional short-cut.
Definition menu_item_widget.hpp:147
bool is_last(keyboard_focus_group group) const noexcept
Is this widget the last widget in the parent container.
virtual void init() noexcept
Should be called right after allocating and constructing a widget.
Definition widget.hpp:119
virtual void update_layout(hires_utc_clock::time_point display_time_point, bool need_layout) noexcept
Update the internal layout of the widget.
observable< bool > enabled
The widget is enabled.
Definition widget.hpp:105
gui_window & window
Convenient reference to the Window.
Definition widget.hpp:100
virtual void draw(draw_context context, hires_utc_clock::time_point display_time_point) noexcept
Draw the widget.
Definition widget.hpp:460
bool is_first(keyboard_focus_group group) const noexcept
Is this widget the first widget in the parent container.
virtual bool update_constraints(hires_utc_clock::time_point display_time_point, bool need_reconstrain) noexcept
Update the constraints of the widget.
abstract_container_widget const & parent() const noexcept
Get a reference to the parent.
virtual aarect window_clipping_rectangle() const noexcept
Get the clipping-rectangle in window coordinates.
Definition widget.hpp:320
aarect rectangle() const noexcept
Get the rectangle in local coordinates.
Definition widget.hpp:340
T shared_from_this(T... args)