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
2#include <functional>
3#include <optional>
4#include <unordered_map>
5#include <vector>
6#ifdef WEBCFACE_MESON
7#include "webcface-config.h"
8#else
9#include "webcface/common/webcface-config.h"
10#endif
12#include "webcface/text.h"
13
14#ifdef min
15// clang-format off
16#pragma message("warning: Disabling macro definition of 'min' and 'max', since they conflicts in webcface/component_view.h.")
17// clang-format on
18#undef min
19#undef max
20#endif
21
22extern "C" {
25typedef struct wcfMultiVal wcfMultiVal;
27}
29namespace message {
30struct ViewComponentData;
31}
32namespace internal {
33struct TemporalViewComponentData;
34struct ClientData;
35class ViewBuf;
36} // namespace internal
37class Func;
38class FuncListener;
39
40enum class ViewColor {
41 inherit = 0,
42 black = 1,
43 white = 2,
44 // slate = 3,
45 gray = 4,
46 // zinc = 5,
47 // neutral = 6,
48 // stone = 7,
49 red = 8,
50 orange = 9,
51 // amber = 10,
52 yellow = 11,
53 // lime = 12,
54 green = 13,
55 // emerald = 14,
56 teal = 15,
57 cyan = 16,
58 // sky = 17,
59 blue = 18,
60 indigo = 19,
61 // violet = 20,
62 purple = 21,
63 // fuchsia = 22,
64 pink = 23,
65 // rose = 24,
66};
75WEBCFACE_DLL ViewColor colorFromRGB(double r, double g, double b);
76
78 text = 0,
79 new_line = 1,
80 button = 2,
81 text_input = 3,
82 decimal_input = 4,
83 number_input = 5,
84 toggle_input = 6,
85 select_input = 7,
86 slider_input = 8,
87 check_input = 9,
88};
89
111 std::shared_ptr<message::ViewComponentData> msg_data;
112 std::weak_ptr<internal::ClientData> data_w;
113 SharedString id_;
114
115 // for cData()
116 mutable std::unique_ptr<wcfMultiVal[]> options_s;
117 mutable std::unique_ptr<wcfMultiValW[]> options_sw;
118
119 void checkData() const;
120
121 template <typename CComponent, typename CVal, std::size_t v_index>
122 CComponent cDataT() const;
123
124 public:
130
131 ViewComponent(const std::shared_ptr<message::ViewComponentData> &msg_data,
132 const std::weak_ptr<internal::ClientData> &data_w,
133 const SharedString &id);
134
136 ViewComponent &operator=(const ViewComponent &);
138 ViewComponent &operator=(ViewComponent &&) noexcept;
139 ~ViewComponent() noexcept;
140
141 wcfViewComponent cData() const;
142 wcfViewComponentW cDataW() const;
143
153 std::string id() const;
163 std::wstring idW() const;
164
175 bool operator==(const ViewComponent &other) const;
179 bool operator!=(const ViewComponent &other) const {
180 return !(*this == other);
181 }
182
187 ViewComponentType type() const;
192 std::string text() const;
197 std::wstring textW() const;
202 template <WEBCFACE_COMPLETE(Func)>
203 std::optional<Func_> onClick() const;
214 template <WEBCFACE_COMPLETE(Func)>
215 std::optional<Func_> onChange() const;
225 template <WEBCFACE_COMPLETE(Variant)>
226 std::optional<Variant_> bind() const;
227
232 ViewColor textColor() const;
237 ViewColor bgColor() const;
243 std::optional<double> min() const;
244
250 std::optional<double> max() const;
256 std::optional<double> step() const;
262 std::vector<ValAdaptor> option() const;
267 int width() const;
272 int height() const;
273};
274extern template std::optional<Func>
275ViewComponent::onClick<Func, true>() const;
276extern template std::optional<Func>
277ViewComponent::onChange<Func, true>() const;
278extern template std::optional<Variant>
279ViewComponent::bind<Variant, true>() const;
280
287 std::unique_ptr<internal::TemporalViewComponentData> msg_data;
288
289 public:
294 explicit TemporalViewComponent(std::nullptr_t = nullptr);
301 TemporalViewComponent &operator=(const TemporalViewComponent &other);
311
312 friend class View;
313 friend class internal::ViewBuf;
314
323 std::unique_ptr<internal::TemporalViewComponentData>
324 lockTmp(const std::shared_ptr<internal::ClientData> &data,
325 const SharedString &view_name,
326 std::unordered_map<ViewComponentType, int> *idx_next = nullptr);
327
332 TemporalViewComponent &id(std::string_view id);
337 TemporalViewComponent &id(std::wstring_view id);
344 TemporalViewComponent &text(std::string_view text) &;
348 TemporalViewComponent &&text(std::string_view text) && {
349 this->text(text);
350 return std::move(*this);
351 }
356 TemporalViewComponent &text(std::wstring_view text) &;
361 TemporalViewComponent &&text(std::wstring_view text) && {
362 this->text(text);
363 return std::move(*this);
364 }
370 TemporalViewComponent &onClick(const Func &func) &;
376 this->onClick(func);
377 return std::move(*this);
378 }
384 TemporalViewComponent &onClick(const FuncListener &func) &;
390 this->onClick(func);
391 return std::move(*this);
392 }
402 template <typename T, decltype(std::declval<T>()(), nullptr) = nullptr>
404 return onClick(std::make_shared<std::function<void WEBCFACE_CALL_FP()>>(
405 std::move(func)));
406 }
411 template <typename T, decltype(std::declval<T>()(), nullptr) = nullptr>
413 this->onClick(std::move(func));
414 return std::move(*this);
415 }
419 TemporalViewComponent &onClick(
420 const std::shared_ptr<std::function<void WEBCFACE_CALL_FP()>> &func);
421
429 TemporalViewComponent &bind(const InputRef &ref) &;
435 this->bind(ref);
436 return std::move(*this);
437 }
447 template <typename T>
449 InputRef ref;
450 return onChange(
451 std::make_shared<std::function<void WEBCFACE_CALL_FP(ValAdaptor)>>(
452 [ref, func = std::move(func)](ValAdaptor val) {
453 ref.lockedField().set(val);
454 return func(val);
455 }),
456 ref);
457 }
462 template <typename T>
464 this->onChange(std::move(func));
465 return std::move(*this);
466 }
470 TemporalViewComponent &onChange(
471 const std::shared_ptr<std::function<void WEBCFACE_CALL_FP(ValAdaptor)>>
472 &func,
473 const InputRef &ref);
474
479 TemporalViewComponent &textColor(ViewColor c) &;
484 this->textColor(c);
485 return std::move(*this);
486 }
491 TemporalViewComponent &bgColor(ViewColor c) &;
496 this->bgColor(c);
497 return std::move(*this);
498 }
508 template <typename T>
509 TemporalViewComponent &init(const T &init) & {
510 return this->init(ValAdaptor{init});
511 }
516 template <typename T>
517 TemporalViewComponent &&init(const T &init) && {
518 this->init(init);
519 return std::move(*this);
520 }
521
522 protected:
523 TemporalViewComponent &init(const ValAdaptor &init);
524
525 public:
535 TemporalViewComponent &min(double min) &;
540 TemporalViewComponent &&min(double min) && {
541 this->min(min);
542 return std::move(*this);
543 }
553 TemporalViewComponent &max(double max) &;
558 TemporalViewComponent &&max(double max) && {
559 this->max(max);
560 return std::move(*this);
561 }
569 TemporalViewComponent &step(double step) &;
574 TemporalViewComponent &&step(double step) && {
575 this->step(step);
576 return std::move(*this);
577 }
578
586 template <typename T>
587 TemporalViewComponent &option(std::initializer_list<T> option) & {
588 std::vector<ValAdaptor> option_v;
589 for (const auto &v : option) {
590 option_v.emplace_back(ValAdaptor(v));
591 }
592 return this->option(std::move(option_v));
593 }
598 template <typename T>
599 TemporalViewComponent &&option(std::initializer_list<T> option) && {
600 this->option(option);
601 return std::move(*this);
602 }
603
604 TemporalViewComponent &option(std::vector<ValAdaptor> option) &;
608 TemporalViewComponent &&option(std::vector<ValAdaptor> option) && {
609 this->option(std::move(option));
610 return std::move(*this);
611 }
612
619 TemporalViewComponent &width(int width) &;
624 TemporalViewComponent &&width(int width) && {
625 this->width(width);
626 return std::move(*this);
627 }
634 TemporalViewComponent &height(int height) &;
639 TemporalViewComponent &&height(int height) && {
640 this->height(height);
641 return std::move(*this);
642 }
643};
644
Definition func.h:619
関数1つを表すクラス
Definition func.h:95
名前を指定しないText
Definition text.h:447
u8stringとstringとwstringをshared_ptrで持ち共有する
Definition encoding.h:67
Viewを構築するときに使う一時的なViewComponent.
Definition component_view.h:286
TemporalViewComponent && onChange(T func) &&
値が変化した時に実行される関数を設定
Definition component_view.h:463
TemporalViewComponent && textColor(ViewColor c) &&
Definition component_view.h:483
TemporalViewComponent && init(const T &init) &&
デフォルト値を設定する。
Definition component_view.h:517
TemporalViewComponent && step(double step) &&
数値の刻み幅を設定する。
Definition component_view.h:574
TemporalViewComponent & init(const T &init) &
デフォルト値を設定する。
Definition component_view.h:509
TemporalViewComponent && onClick(T func) &&
クリック時に実行される関数を設定
Definition component_view.h:412
TemporalViewComponent & onChange(T func) &
値が変化した時に実行される関数を設定
Definition component_view.h:448
TemporalViewComponent && onClick(const Func &func) &&
クリック時に実行される関数を設定 (登録済みFunc)
Definition component_view.h:375
TemporalViewComponent & option(std::initializer_list< T > option) &
引数の選択肢を設定する。
Definition component_view.h:587
TemporalViewComponent & onClick(T func) &
クリック時に実行される関数を設定
Definition component_view.h:403
TemporalViewComponent && width(int width) &&
要素の幅を設定する。
Definition component_view.h:624
TemporalViewComponent && bind(const InputRef &ref) &&
変更した値を格納するInputRefを設定
Definition component_view.h:434
TemporalViewComponent & operator=(TemporalViewComponent &&other) noexcept
TemporalViewComponent && option(std::vector< ValAdaptor > option) &&
Definition component_view.h:608
TemporalViewComponent && max(double max) &&
最大値を設定する。
Definition component_view.h:558
TemporalViewComponent && min(double min) &&
最小値を設定する。
Definition component_view.h:540
TemporalViewComponent && option(std::initializer_list< T > option) &&
引数の選択肢を設定する。
Definition component_view.h:599
TemporalViewComponent(TemporalViewComponent &&other) noexcept
TemporalViewComponent && onClick(const FuncListener &func) &&
クリック時に実行される関数を設定 (FuncListener)
Definition component_view.h:389
TemporalViewComponent && height(int height) &&
要素の高さを設定する。
Definition component_view.h:639
TemporalViewComponent && bgColor(ViewColor c) &&
Definition component_view.h:495
TemporalViewComponent && text(std::wstring_view text) &&
表示する文字列を設定 (wstring)
Definition component_view.h:361
数値、文字列などの値を相互変換するクラス
Definition val_adaptor.h:87
Viewに表示する要素です
Definition component_view.h:110
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(std::string_view text, T &&func)
buttonコンポーネント
Definition components.h:654
ViewComponentType
Definition component_view.h:77
ViewColor
Definition component_view.h:40
数値と文字列をまとめて扱うための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
#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