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"
20hi_export_module(hikogui.widgets.async_delegate);
22hi_export
namespace hi {
32 virtual void init(
widget_intf const& sender)
noexcept {}
34 virtual void deinit(
widget_intf const& sender)
noexcept {}
51 return widget_value::off;
56 template<forward_of<
void()> Func>
57 [[nodiscard]]
callback<void()>
subscribe(Func&& func, callback_flags flags = callback_flags::synchronous)
noexcept
63 notifier<void()> _notifier;
74template<
typename ResultType =
void>
77 using result_type = ResultType;
88 template<
typename Func,
typename... Args>
92 _task_controller_cbt = _task_controller.subscribe([
this] {
98 [[nodiscard]] widget_value state(
widget_intf const& sender)
const noexcept override
100 if (not _task_controller.
runnable()) {
101 return widget_value::other;
103 }
else if (_task_controller.
running()) {
104 return widget_value::on;
107 return widget_value::off;
113 return _task_controller.features();
116 void activate(widget_intf
const& sender)
noexcept override
118 if (not _task_controller.runnable()) {
121 }
else if (not _task_controller.running()) {
122 _task_controller.run();
127 _task_controller.request_stop();
132 task_controller<result_type> _task_controller;
133 task_controller<result_type>::callback_type _task_controller_cbt;
136template<
typename FuncType,
typename... ArgTypes>
137using default_async_delegate_result_type =
140template<
typename F,
typename... Args>
The HikoGUI namespace.
Definition array_generic.hpp:21
The HikoGUI API version 1.
Definition array_generic.hpp:22
cancel_features_type
Features of an invocable.
Definition async_task.hpp:71
@ none
This invocable does not have extra arguments.
Definition async_task.hpp:74
@ stop_and_progress
The extra arguments are a std::stop_token, followed by a hi::progress_token.
Definition async_task.hpp:86
@ stop
The extra argument is a std::stop_token.
Definition async_task.hpp:78
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
Definition callback.hpp:77
Token to pass to a function to report its progress.
Definition progress.hpp:23
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
Definition widget_intf.hpp:25
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