HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
abstract_toggle_button_widget.hpp
1// Copyright Take Vos 2020.
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 "abstract_button_widget.hpp"
8
9namespace tt {
10
14template<typename T>
16public:
18 using value_type = T;
19
20 value_type const false_value;
21
22 template<typename Value = observable<value_type>>
26 value_type true_value,
27 value_type false_value,
28 Value &&value = {}) noexcept :
29 super(window, parent, true_value, std::forward<Value>(value)),
30 false_value(std::move(false_value))
31 {
32 _value_callback = this->value.subscribe([this](auto...) {
33 ttlet lock = std::scoped_lock(gui_system_mutex);
34 this->window.request_redraw(this->window_clipping_rectangle());
35 });
36 _callback = this->subscribe([this]() {
37 this->toggle();
38 });
39 }
40
41 void toggle() noexcept
42 {
43 ttlet lock = std::scoped_lock(gui_system_mutex);
44
45 if (compare_then_assign(this->value, this->value == this->false_value ? this->true_value : this->false_value)) {
46 this->window.request_redraw(this->window_clipping_rectangle());
47 }
48 }
49
50private:
51 typename decltype(super::value)::callback_ptr_type _value_callback;
52 typename super::callback_ptr_type _callback;
53};
54
55} // namespace tt
Definition gui_window.hpp:39
void request_redraw(aarect rectangle=aarect::infinity()) noexcept
Request a rectangle on the window to be redrawn.
Definition gui_window.hpp:115
An abstract button widget.
Definition abstract_button_widget.hpp:16
callback_ptr_type subscribe(Callback &&callback) noexcept
Subscribe a callback to call when the button is activated.
Definition abstract_button_widget.hpp:109
An abstract toggle button widget.
Definition abstract_toggle_button_widget.hpp:15
gui_window & window
Convenient reference to the Window.
Definition widget.hpp:100
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
T move(T... args)