CYD-UI
A C++ library for building native graphic user interfaces
Loading...
Searching...
No Matches
component_state.cppm
Go to the documentation of this file.
1// Copyright (c) 2024, Víctor Castillo Agüero.
2// SPDX-License-Identifier: GPL-3.0-or-later
3
5
6import std;
7
8import fabric.logging;
9import fabric.async;
10
11export import cydui.events;
12
13export import :holder;
14// export import :event_handler;
15
16export class with_context;
17
18namespace cydui {
19 export template <typename ContextType>
20 struct provide_context;
21}
22
23export using children_list = std::vector<cydui::components::component_holder_t>;
24
25namespace cydui::components {
26 export class component_state_t {
27 public:
28 virtual ~component_state_t() = default;
29
30 component_state_t() = default;
31 explicit component_state_t(void* props) { }
32
33
46 void mark_dirty() {
47 _dirty = true;
48 }
49
50 template<fabric::async::EventType EV>
51 void emit(EV ev) {
52 if (nullptr == this->window) return;
53 this->window->emit<EV>(ev);
54 }
55
64 void force_redraw() {
65 mark_dirty();
66 emit<RedrawEvent>({.component = this});
67 }
68
69 std::string component_name() const {
70 return component_name_;
71 }
72
74 return parent_;
75 }
76
77 void add_children_state(const std::string& id, const component_state_ref &child) {
78 children_states[id] = child;
79 child->parent_ = this;
80 child->window = window;
81 }
82
83 bool is_animated() const {
84 return animated;
85 }
86
87 bool is_text_input() const {
88 return is_text_input_;
89 }
90
91 private:
92 friend class component_base_t;
94
95
96 protected:
97 void set_component_name(const std::string& name) {
98 component_name_ = name;
99 }
100
101 public:
102 std::string component_name_;
103
105 std::shared_ptr<fabric::async::async_bus_t> window = nullptr;
106
107 std::optional<std::shared_ptr<component_base_t>> component_instance = std::nullopt;
108 std::unordered_map<std::string, component_state_ref> children_states { };
109
110 bool _dirty = false;
111 bool focused = false;
112 bool hovering = false;
113
114 private:
115 bool animated = false;
116 bool is_text_input_ = false;
117 };
118
120 public:
121 static void set_animated(component_state_t* it, bool animated) {
122 it->animated = animated;
123 }
124
125 static void set_is_text_input(component_state_t* it, bool is_text_input) {
126 it->is_text_input_ = is_text_input;
127 }
128 };
129}
static void set_is_text_input(component_state_t *it, bool is_text_input)
static void set_animated(component_state_t *it, bool animated)
void set_component_name(const std::string &name)
void force_redraw()
Emits a RedrawEvent for this component.
std::optional< std::shared_ptr< component_base_t > > component_instance
void add_children_state(const std::string &id, const component_state_ref &child)
std::shared_ptr< fabric::async::async_bus_t > window
void mark_dirty()
Marks this component state as needing to be redrawn.
std::unordered_map< std::string, component_state_ref > children_states
std::vector< cydui::components::component_holder_t > children_list
std::shared_ptr< component_state_t > component_state_ref