WebCFace 2.9.0
Web-based Communication Framework & Dashboard-like UI
Loading...
Searching...
No Matches
component_view.h
Go to the documentation of this file.
1#pragma once
3#include <functional>
4#include <optional>
5#include <unordered_map>
6#include <vector>
7#ifdef WEBCFACE_MESON
8#include "webcface-config.h"
9#else
10#include "webcface/common/webcface-config.h"
11#endif
13#include "webcface/text.h"
14
15#ifdef min
16// clang-format off
17#pragma message("warning: Disabling macro definition of 'min' and 'max', since they conflicts in webcface/component_view.h.")
18// clang-format on
19#undef min
20#undef max
21#endif
22
23extern "C" {
26typedef struct wcfMultiVal wcfMultiVal;
28}
30namespace message {
31struct ViewComponentData;
32}
33namespace internal {
34struct TemporalViewComponentData;
35struct ClientData;
36class ViewBuf;
37} // namespace internal
38class Func;
39class FuncListener;
40
41enum class ViewColor {
42 inherit = 0,
43 black = 1,
44 white = 2,
45 // slate = 3,
46 gray = 4,
47 // zinc = 5,
48 // neutral = 6,
49 // stone = 7,
50 red = 8,
51 orange = 9,
52 // amber = 10,
53 yellow = 11,
54 // lime = 12,
55 green = 13,
56 // emerald = 14,
57 teal = 15,
58 cyan = 16,
59 // sky = 17,
60 blue = 18,
61 indigo = 19,
62 // violet = 20,
63 purple = 21,
64 // fuchsia = 22,
65 pink = 23,
66 // rose = 24,
67};
76WEBCFACE_DLL ViewColor colorFromRGB(double r, double g, double b);
77
79 text = 0,
80 new_line = 1,
81 button = 2,
82 text_input = 3,
83 decimal_input = 4,
84 number_input = 5,
85 toggle_input = 6,
86 select_input = 7,
87 slider_input = 8,
88 check_input = 9,
89};
90
112 std::shared_ptr<message::ViewComponentData> msg_data;
113 std::weak_ptr<internal::ClientData> data_w;
114 SharedString id_;
115
116 // for cData()
117 mutable std::unique_ptr<wcfMultiVal[]> options_s;
118 mutable std::unique_ptr<wcfMultiValW[]> options_sw;
119
120 void checkData() const;
121
122 template <typename CComponent, typename CVal, std::size_t v_index>
123 CComponent cDataT() const;
124
125 public:
131
132 ViewComponent(const std::shared_ptr<message::ViewComponentData> &msg_data,
133 const std::weak_ptr<internal::ClientData> &data_w,
134 const SharedString &id);
135
137 ViewComponent &operator=(const ViewComponent &);
139 ViewComponent &operator=(ViewComponent &&) noexcept;
140 ~ViewComponent() noexcept;
141
142 wcfViewComponent cData() const;
143 wcfViewComponentW cDataW() const;
144
155 StringView id() const;
166 WStringView idW() const;
167
178 bool operator==(const ViewComponent &other) const;
182 bool operator!=(const ViewComponent &other) const {
183 return !(*this == other);
184 }
185
190 ViewComponentType type() const;
197 StringView text() const;
205 WStringView textW() const;
210 template <WEBCFACE_COMPLETE(Func)>
211 std::optional<Func_> onClick() const;
222 template <WEBCFACE_COMPLETE(Func)>
223 std::optional<Func_> onChange() const;
233 template <WEBCFACE_COMPLETE(Variant)>
234 std::optional<Variant_> bind() const;
235
240 ViewColor textColor() const;
245 ViewColor bgColor() const;
251 std::optional<double> min() const;
252
258 std::optional<double> max() const;
264 std::optional<double> step() const;
270 std::vector<ValAdaptor> option() const;
275 int width() const;
280 int height() const;
281};
282extern template std::optional<Func> ViewComponent::onClick<Func, true>() const;
283extern template std::optional<Func> ViewComponent::onChange<Func, true>() const;
284extern template std::optional<Variant>
285ViewComponent::bind<Variant, true>() const;
286
293 std::unique_ptr<internal::TemporalViewComponentData> msg_data;
294
295 public:
300 explicit TemporalViewComponent(std::nullptr_t = nullptr);
307 TemporalViewComponent &operator=(const TemporalViewComponent &other);
317
318 friend class View;
319 friend class internal::ViewBuf;
320
329 std::unique_ptr<internal::TemporalViewComponentData>
330 lockTmp(const std::shared_ptr<internal::ClientData> &data,
331 const SharedString &view_name,
332 std::unordered_map<ViewComponentType, int> *idx_next = nullptr);
333
357 this->text(std::move(text));
358 return std::move(*this);
359 }
365 TemporalViewComponent &onClick(const Func &func) &;
371 this->onClick(func);
372 return std::move(*this);
373 }
379 TemporalViewComponent &onClick(const FuncListener &func) &;
385 this->onClick(func);
386 return std::move(*this);
387 }
397 template <typename T, decltype(std::declval<T>()(), nullptr) = nullptr>
399 return onClick(std::make_shared<std::function<void WEBCFACE_CALL_FP()>>(
400 std::move(func)));
401 }
406 template <typename T, decltype(std::declval<T>()(), nullptr) = nullptr>
408 this->onClick(std::move(func));
409 return std::move(*this);
410 }
414 TemporalViewComponent &onClick(
415 const std::shared_ptr<std::function<void WEBCFACE_CALL_FP()>> &func);
416
424 TemporalViewComponent &bind(const InputRef &ref) &;
430 this->bind(ref);
431 return std::move(*this);
432 }
442 template <typename T>
444 static_assert(traits::InvokeObjTrait<T>::ArgsSize == 1);
445 using FirstArgType =
446 typename traits::InvokeObjTrait<T>::template ArgsAt<0>;
447 InputRef ref;
448 return onChange(
449 std::make_shared<std::function<void WEBCFACE_CALL_FP(ValAdaptor)>>(
450 [ref, func = std::move(func)](const ValAdaptor &val) {
451 ref.lockedField().set(val);
452 return func(val.as<FirstArgType>());
453 }),
454 ref);
455 }
460 template <typename T>
462 this->onChange(std::move(func));
463 return std::move(*this);
464 }
468 TemporalViewComponent &onChange(
469 const std::shared_ptr<std::function<void WEBCFACE_CALL_FP(ValAdaptor)>>
470 &func,
471 const InputRef &ref);
472
477 TemporalViewComponent &textColor(ViewColor c) &;
482 this->textColor(c);
483 return std::move(*this);
484 }
489 TemporalViewComponent &bgColor(ViewColor c) &;
494 this->bgColor(c);
495 return std::move(*this);
496 }
506 template <typename T>
507 TemporalViewComponent &init(const T &init) & {
508 return this->init(ValAdaptor{init});
509 }
513 template <std::size_t N>
514 TemporalViewComponent &init(const char (&init)[N]) & {
515 return this->init(ValAdaptor(init));
516 }
520 template <std::size_t N>
521 TemporalViewComponent &init(const wchar_t (&init)[N]) & {
522 return this->init(ValAdaptor(init));
523 }
528 template <typename T>
529 TemporalViewComponent &&init(const T &init) && {
530 this->init(init);
531 return std::move(*this);
532 }
536 template <std::size_t N>
537 TemporalViewComponent &&init(const char (&init)[N]) && {
538 this->init(init);
539 return std::move(*this);
540 }
544 template <std::size_t N>
545 TemporalViewComponent &&init(const wchar_t (&init)[N]) && {
546 this->init(init);
547 return std::move(*this);
548 }
549
550 protected:
551 TemporalViewComponent &init(const ValAdaptor &init);
552
553 public:
563 TemporalViewComponent &min(double min) &;
568 TemporalViewComponent &&min(double min) && {
569 this->min(min);
570 return std::move(*this);
571 }
581 TemporalViewComponent &max(double max) &;
586 TemporalViewComponent &&max(double max) && {
587 this->max(max);
588 return std::move(*this);
589 }
597 TemporalViewComponent &step(double step) &;
602 TemporalViewComponent &&step(double step) && {
603 this->step(step);
604 return std::move(*this);
605 }
606
614 template <typename T>
615 TemporalViewComponent &option(std::initializer_list<T> option) & {
616 std::vector<ValAdaptor> option_v;
617 for (const auto &v : option) {
618 option_v.emplace_back(ValAdaptor(v));
619 }
620 return this->option(std::move(option_v));
621 }
626 template <typename T>
627 TemporalViewComponent &&option(std::initializer_list<T> option) && {
628 this->option(option);
629 return std::move(*this);
630 }
631
632 TemporalViewComponent &option(std::vector<ValAdaptor> option) &;
636 TemporalViewComponent &&option(std::vector<ValAdaptor> option) && {
637 this->option(std::move(option));
638 return std::move(*this);
639 }
640
647 TemporalViewComponent &width(int width) &;
652 TemporalViewComponent &&width(int width) && {
653 this->width(width);
654 return std::move(*this);
655 }
662 TemporalViewComponent &height(int height) &;
667 TemporalViewComponent &&height(int height) && {
668 this->height(height);
669 return std::move(*this);
670 }
671};
672
Definition func.h:616
関数1つを表すクラス
Definition func.h:106
名前を指定しないText
Definition text.h:489
u8stringとstringとwstringをshared_ptrで持ち共有する
Definition encoding.h:159
SharedString のpublicなコンストラクタインタフェース (入力専用)
Definition encoding.h:215
webcfaceで管理されている文字列を参照するstring_view
Definition encoding.h:60
Viewを構築するときに使う一時的なViewComponent.
Definition component_view.h:292
TemporalViewComponent && onChange(T func) &&
値が変化した時に実行される関数を設定
Definition component_view.h:461
TemporalViewComponent && textColor(ViewColor c) &&
Definition component_view.h:481
TemporalViewComponent && init(const T &init) &&
デフォルト値を設定する。
Definition component_view.h:529
TemporalViewComponent && step(double step) &&
数値の刻み幅を設定する。
Definition component_view.h:602
TemporalViewComponent & init(const T &init) &
デフォルト値を設定する。
Definition component_view.h:507
TemporalViewComponent && onClick(T func) &&
クリック時に実行される関数を設定
Definition component_view.h:407
TemporalViewComponent & onChange(T func) &
値が変化した時に実行される関数を設定
Definition component_view.h:443
TemporalViewComponent & init(const char(&init)[N]) &
Definition component_view.h:514
TemporalViewComponent && onClick(const Func &func) &&
クリック時に実行される関数を設定 (登録済みFunc)
Definition component_view.h:370
TemporalViewComponent & option(std::initializer_list< T > option) &
引数の選択肢を設定する。
Definition component_view.h:615
TemporalViewComponent & onClick(T func) &
クリック時に実行される関数を設定
Definition component_view.h:398
TemporalViewComponent && init(const char(&init)[N]) &&
Definition component_view.h:537
TemporalViewComponent && width(int width) &&
要素の幅を設定する。
Definition component_view.h:652
TemporalViewComponent && bind(const InputRef &ref) &&
変更した値を格納するInputRefを設定
Definition component_view.h:429
TemporalViewComponent & operator=(TemporalViewComponent &&other) noexcept
TemporalViewComponent && option(std::vector< ValAdaptor > option) &&
Definition component_view.h:636
TemporalViewComponent && max(double max) &&
最大値を設定する。
Definition component_view.h:586
TemporalViewComponent && min(double min) &&
最小値を設定する。
Definition component_view.h:568
TemporalViewComponent && option(std::initializer_list< T > option) &&
引数の選択肢を設定する。
Definition component_view.h:627
TemporalViewComponent(TemporalViewComponent &&other) noexcept
TemporalViewComponent && onClick(const FuncListener &func) &&
クリック時に実行される関数を設定 (FuncListener)
Definition component_view.h:384
TemporalViewComponent & init(const wchar_t(&init)[N]) &
Definition component_view.h:521
TemporalViewComponent && height(int height) &&
要素の高さを設定する。
Definition component_view.h:667
TemporalViewComponent && bgColor(ViewColor c) &&
Definition component_view.h:493
TemporalViewComponent && init(const wchar_t(&init)[N]) &&
Definition component_view.h:545
数値、文字列などの値を相互変換するクラス
Definition val_adaptor.h:87
T as() const
数値として返す 明示的なキャストをする
Definition val_adaptor.h:335
Viewに表示する要素です
Definition component_view.h:111
std::optional< Func_ > onClick() const
クリック時に実行される関数を取得
std::optional< Variant_ > bind() const
inputの現在の値を取得
std::optional< Func_ > onChange() const
inputの値の変化時に実行される関数を取得
ViewComponent(ViewComponent &&) noexcept
Viewの送受信データを表すクラス
Definition view.h:36
TemporalViewComponent button(const StringInitializer &text, T &&func)
buttonコンポーネント
Definition components.h:627
ViewComponentType
Definition component_view.h:78
ViewColor
Definition component_view.h:41
数値と文字列をまとめて扱うためのstruct (wstring)
Definition def_types.h:84
数値と文字列をまとめて扱うためのstruct
Definition def_types.h:50
Viewの要素を表すstruct (wstring)
Definition def_types.h:289
Viewの要素を表すstruct.
Definition def_types.h:218
Definition func_trait.h:21
#define WEBCFACE_DLL
Definition webcface-config.h:69
#define WEBCFACE_NS_END
Definition webcface-config.h:118
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:117
#define WEBCFACE_CALL_FP
Definition webcface-config.h:107