HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
async_delegate.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2023.
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
11#include "../observer/observer.hpp"
12#include "../utility/utility.hpp"
13#include "../concurrency/concurrency.hpp"
14#include "../dispatch/dispatch.hpp"
15#include "../GUI/GUI.hpp"
16#include "../macros.hpp"
17#include <type_traits>
18#include <memory>
19
20hi_export_module(hikogui.widgets.async_delegate);
21
22hi_export namespace hi {
23inline namespace v1 {
24
29public:
30 virtual ~async_delegate() = default;
31
32 virtual void init(widget_intf const& sender) noexcept {}
33
34 virtual void deinit(widget_intf const& sender) noexcept {}
35
38 virtual void activate(widget_intf const& sender) noexcept {}
39
42 [[nodiscard]] virtual cancel_features_type features() const noexcept
43 {
45 }
46
49 [[nodiscard]] virtual widget_value state(widget_intf const& sender) const noexcept
50 {
51 return widget_value::off;
52 }
53
56 template<forward_of<void()> Func>
57 [[nodiscard]] callback<void()> subscribe(Func&& func, callback_flags flags = callback_flags::synchronous) noexcept
58 {
59 return _notifier.subscribe(std::forward<Func>(func), flags);
60 }
61
62protected:
63 notifier<void()> _notifier;
64};
65
74template<typename ResultType = void>
76public:
77 using result_type = ResultType;
78
88 template<typename Func, typename... Args>
89 default_async_delegate(Func&& func, Args&&... args) noexcept :
90 _task_controller(std::forward<Func>(func), std::forward<Args>(args)...)
91 {
92 _task_controller_cbt = _task_controller.subscribe([this] {
93 this->_notifier();
94 });
95 }
96
98 [[nodiscard]] widget_value state(widget_intf const& sender) const noexcept override
99 {
100 if (not _task_controller.runnable()) {
101 return widget_value::other;
102
103 } else if (_task_controller.running()) {
104 return widget_value::on;
105
106 } else {
107 return widget_value::off;
108 }
109 }
110
111 [[nodiscard]] cancel_features_type features() const noexcept override
112 {
113 return _task_controller.features();
114 }
115
116 void activate(widget_intf const& sender) noexcept override
117 {
118 if (not _task_controller.runnable()) {
119 return;
120
121 } else if (not _task_controller.running()) {
122 _task_controller.run();
123
124 } else if (
125 _task_controller.features() == cancel_features_type::stop or
127 _task_controller.request_stop();
128 }
129 }
131private:
132 task_controller<result_type> _task_controller;
133 task_controller<result_type>::callback_type _task_controller_cbt;
134};
135
136template<typename FuncType, typename... ArgTypes>
137using default_async_delegate_result_type =
138 invoke_task_result_t<decltype(cancelable_async_task<FuncType, ArgTypes...>), FuncType, std::stop_token, progress_token, ArgTypes...>;
139
140template<typename F, typename... Args>
141default_async_delegate(F&& func, Args&&... args) -> default_async_delegate<default_async_delegate_result_type<F, Args...>>;
142
143} // namespace v1
144}
The HikoGUI namespace.
Definition array_generic.hpp:20
cancel_features_type
Features of an invocable.
Definition async_task.hpp:71
@ none
This invocable does not have extra arguments.
@ stop_and_progress
The extra arguments are a std::stop_token, followed by a hi::progress_token.
@ stop
The extra argument is a std::stop_token.
auto cancelable_async_task(Func func, std::stop_token stop_token, ::hi::progress_token progress_token, Args... args)
Run a function asynchronously as a co-routine task.
Definition async_task.hpp:197
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition callback.hpp:77
void run()
Run the assigned coroutine or function with the previous given arguments.
Definition task_controller.hpp:200
hi::notifier ::callback_type subscribe(Callback &&callback, callback_flags flags=callback_flags::synchronous)
Register a callback to be called when a coroutine or function reports progress.
Definition task_controller.hpp:239
bool request_stop() noexcept
Request stop.
Definition task_controller.hpp:211
bool runnable() const noexcept
Check if a function is assigned.
Definition task_controller.hpp:148
bool running() const noexcept
Check if the function is currently running.
Definition task_controller.hpp:165
cancel_features_type features() const noexcept
The features of the coroutine or function that was assigned.
Definition task_controller.hpp:137
Definition widget_intf.hpp:24
A button delegate controls the state of a button widget.
Definition async_delegate.hpp:28
virtual cancel_features_type features() const noexcept
Used by the widget to determine if it can stop.
Definition async_delegate.hpp:42
virtual widget_value state(widget_intf const &sender) const noexcept
Used by the widget to check the state of the button.
Definition async_delegate.hpp:49
callback< void()> subscribe(Func &&func, callback_flags flags=callback_flags::synchronous) noexcept
Subscribe a callback for notifying the widget of a data change.
Definition async_delegate.hpp:57
virtual void activate(widget_intf const &sender) noexcept
Called when the button is pressed by the user.
Definition async_delegate.hpp:38
A default async button delegate.
Definition async_delegate.hpp:75
default_async_delegate(Func &&func, Args &&... args) noexcept
Construct a delegate.
Definition async_delegate.hpp:89