HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
label_widget.hpp
1// Copyright Take Vos 2020-2021.
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
5#pragma once
6
7#include "widget.hpp"
8#include "../GUI/draw_context.hpp"
9#include "../stencils/label_stencil.hpp"
10#include "../observable.hpp"
11#include "../alignment.hpp"
12#include <memory>
13#include <string>
14#include <array>
15#include <optional>
16#include <future>
17
18namespace tt {
19
20class label_widget final : public widget {
21public:
22 using super = widget;
23
25
26 template<typename Label>
30 alignment alignment,
31 Label &&label) noexcept
32 :
34 _alignment(alignment),
35 label(std::forward<Label>(label))
36 {
37 }
38
39 template<typename Label>
41 super(window, parent), _alignment(alignment::top_right), label(std::forward<Label>(label))
42 {
43 }
44
46 }
47
48 void init() noexcept override {
49 _label_callback = label.subscribe([this](auto...) {
50 _request_reconstrain = true;
51 });
52 }
53
54 [[nodiscard]] bool update_constraints(hires_utc_clock::time_point display_time_point, bool need_reconstrain) noexcept override
55 {
56 tt_axiom(gui_system_mutex.recurse_lock_count());
57
58 if (super::update_constraints(display_time_point, need_reconstrain)) {
59 _label_cell = stencil::make_unique(_alignment, *label, theme::global->labelStyle);
60 _preferred_size = interval_vec2::make_minimum(_label_cell->preferred_extent());
61 return true;
62 } else {
63 return false;
64 }
65 }
66
67 [[nodiscard]] void update_layout(hires_utc_clock::time_point displayTimePoint, bool need_layout) noexcept override
68 {
69 tt_axiom(gui_system_mutex.recurse_lock_count());
70
71 need_layout |= std::exchange(this->_request_relayout, false);
72 if (need_layout) {
73 _label_cell->set_layout_parameters(rectangle(), base_line());
74 }
75 super::update_layout(displayTimePoint, need_layout);
76 }
77
78 void draw(draw_context context, hires_utc_clock::time_point display_time_point) noexcept override {
79 tt_axiom(gui_system_mutex.recurse_lock_count());
80
81 if (overlaps(context, this->window_clipping_rectangle())) {
82 if (*enabled) {
83 context.line_color = theme::global->labelStyle.color;
84 }
85
86 _label_cell->draw(context, true);
87 }
88
89 super::draw(std::move(context), display_time_point);
90 }
91
92private:
93 typename decltype(label)::callback_ptr_type _label_callback;
94
96 alignment _alignment;
97};
98
99}
Draw context for drawing using the TTauri shaders.
Definition draw_context.hpp:33
Definition gui_window.hpp:39
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
Definition label_widget.hpp:20
void init() noexcept override
Should be called right after allocating and constructing a widget.
Definition label_widget.hpp:48
bool update_constraints(hires_utc_clock::time_point display_time_point, bool need_reconstrain) noexcept override
Update the constraints of the widget.
Definition label_widget.hpp:54
void draw(draw_context context, hires_utc_clock::time_point display_time_point) noexcept override
Draw the widget.
Definition label_widget.hpp:78
void update_layout(hires_utc_clock::time_point displayTimePoint, bool need_layout) noexcept override
Update the internal layout of the widget.
Definition label_widget.hpp:67
Definition widget.hpp:96
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
widget(gui_window &window, std::shared_ptr< abstract_container_widget > parent) noexcept
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
float base_line() const noexcept
Get the base-line in local coordinates.
Definition widget.hpp:350
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 move(T... args)