载入中...
搜索中...
未找到
Component.cpp
浏览该文件的文档.
1// eve-hot-path
2#include "ui/Component.h"
3#include <vector>
4
5#include "ui/UISystem.h"
6
7namespace eve::ui {
8namespace detail {
10 Component *component = nullptr; // Owner invalidates before destruction; never exposed as a handle.
11 bool queued = false;
12};
13namespace {
14std::vector<std::weak_ptr<ComponentSchedule>> pending;
15std::vector<std::weak_ptr<ComponentSchedule>> draining;
16bool flushing = false;
17} // namespace
19 if (flushing) return;
20 flushing = true;
21 struct FlushGuard {
22 ~FlushGuard() {
23 draining.clear();
24 flushing = false;
25 }
26 } guard;
27 draining.swap(pending);
28 for (size_t i = 0; i < draining.size(); ++i) {
29 auto &entry = draining[i];
30 auto registration = entry.lock();
31 if (!registration || !registration->component) continue;
32 registration->queued = false;
33 try {
34 registration->component->updateIfDirty();
35 } catch (...) {
36 pending.insert(pending.end(), draining.begin() + i + 1, draining.end());
37 throw;
38 }
39 }
40}
41} // namespace detail
42
43Component::Component() : schedule_(std::make_shared<detail::ComponentSchedule>()) { schedule_->component = this; }
44Component::~Component() { schedule_->component = nullptr; }
46 dirty_ = true;
47 if (schedule_->queued) return;
48 detail::pending.push_back(schedule_);
49 schedule_->queued = true;
50}
51
52
54 host_ = host;
55 markDirty();
56}
57
58void Component::mountAs(const std::string &hostName) {
60 if (!UIHost::resolve(h)) h = UIHost::createHost(hostName);
61 attach(h);
62 rebuild(true);
63}
64
65void Component::rebuild(bool forceFull) {
66 auto host = UIHost::resolve(host_);
67 if (!host || building_) return;
68 building_ = true;
69 dirty_ = false;
70 try {
71 auto tree = build();
72 host = UIHost::resolve(host_);
73 if (host) {
74 if (forceFull)
75 host->get().setTree(std::move(tree));
76 else
77 host->get().setTreeReconcile(std::move(tree));
78 }
79 } catch (...) {
80 building_ = false;
81 markDirty();
82 throw;
83 }
84 building_ = false;
85}
86
88 if (building_ || !dirty_ || !UIHost::resolve(host_)) return false;
89 rebuild(false);
90 return true;
91}
92
93} // namespace eve::ui
std::vector< QuestEvent > pending
bool draining
int h
int detail
UIHostHandle host
React-style UI component: implement build(); setState / markDirty schedules one update before UI rend...
Definition Component.h:25
virtual WidgetDesc build()=0
Builds .
void rebuild(bool forceFull=false)
Compatibility-only synchronous refresh; normal edits use markDirty / setState. @cost Builds the compl...
Definition Component.cpp:65
void markDirty()
Schedule one rebuild; repeated calls before the next preparation phase coalesce. @thread Owner thread...
Definition Component.cpp:45
virtual ~Component()
Component.
Definition Component.cpp:44
void mountAs(const std::string &hostName)
Mounts as.
Definition Component.cpp:58
bool updateIfDirty()
Compatibility-only synchronous update; normal edits are flushed automatically.
Definition Component.cpp:87
Component()
Create an unmounted component; attaching schedules its first update.
Definition Component.cpp:43
void attach(UIHostHandle host)
Attaches .
Definition Component.cpp:53
UIHostHandle host() const noexcept
Returns the attached host handle, or an empty handle before attach.
Definition Component.h:44
static UIHostHandle createHost(const std::string &name="")
Creates an ECS-owned UI host.
Definition UIHost.cpp:49
static eve::OptionalRef< UIHost > resolve(UIHostHandle handle) noexcept
Resolves a UI host handle with ECS generation checking.
Definition UIHost.cpp:62
static UIHostHandle findHost(const std::string &name)
Lookup by Meta.name across the UIHost View.
void flushPendingComponents()
Flush one batch of live dirty components before host traversal. @thread Owner thread; newly queued ed...
Definition Component.cpp:18
ecs::EntityHandle UIHostHandle
Definition UIHost.h:20