Nui
text.hpp
Go to the documentation of this file.
1 #pragma once
2 
6 
7 #include <string_view>
8 
9 namespace Nui::Elements
10 {
11  struct text : HtmlElement
12  {
13  text(text const&) = default;
14  text(text&&) = default;
15  text(std::string_view content)
16  : HtmlElement{"", &TextElementBridge, Attribute{content, {}, {}}}
17  {}
19  : HtmlElement{
20  "",
22  Attribute{
23  obs.value(),
24  [&obs](std::weak_ptr<Dom::ChildlessElement>&& element) {
25  const auto eventId = globalEventContext.registerEvent(Event{
26  [element, &obs](auto eventId) {
27  if (auto shared = element.lock(); shared)
28  {
29  shared->setNodeValue(obs.value());
30  return true;
31  }
32  obs.detachEvent(eventId);
33  return false;
34  },
35  [element]() {
36  return !element.expired();
37  }});
38  obs.attachEvent(eventId);
39  return eventId;
40  },
41  [&obs](EventContext::EventIdType const& id) {
42  obs.detachEvent(id);
43  },
44  },
45  }
46  {}
47  };
48 }
Definition: attribute.hpp:15
EventRegistry::EventIdType EventIdType
Definition: event_context.hpp:40
EventIdType registerEvent(Event event)
Definition: event_context.hpp:52
Definition: event.hpp:40
Definition: html_element.hpp:60
ContainedT const & value() const
Definition: observed_value.hpp:313
void attachEvent(EventContext::EventIdType eventId) const
Definition: observed_value.hpp:62
void detachEvent(EventContext::EventIdType eventId) const
Definition: observed_value.hpp:70
Definition: comment.hpp:10
thread_local EventContext globalEventContext
Definition: event_context.cpp:5
constexpr auto TextElementBridge
Definition: html_element_bridges.hpp:26
Definition: text.hpp:12
text(text &&)=default
text(Nui::Observed< std::string > &obs)
Definition: text.hpp:18
text(text const &)=default
text(std::string_view content)
Definition: text.hpp:15