4#include <simplesquirrel/simplesquirrel.hpp>
9const char *kUIComponentScript = R
"SQ(
10if (!("_uiComponents" in eve)) eve._uiComponents <- []
11if (!("_flushingUIComponents" in eve)) eve._flushingUIComponents <- false
12if (!("_nextUIComponentOwner" in eve)) eve._nextUIComponentOwner <- 0
14eve._scheduleUIComponent <- function(component) {
15 if (component == null || component._scheduled) return
16 component._scheduled = true
17 eve._uiComponents.append(component)
20eve._flushUIComponents <- function() {
21 if (eve._flushingUIComponents) return
22 eve._flushingUIComponents = true
23 local pending = eve._uiComponents
24 eve._uiComponents = []
25 for (local i = 0; i < pending.len(); i += 1) {
26 local component = pending[i]
27 component._scheduled = false
29 if (component._mounted && component._parent == null && component.dirty)
30 component.updateIfDirty()
32 for (local j = i + 1; j < pending.len(); j += 1) eve._uiComponents.append(pending[j])
33 eve._flushingUIComponents = false
37 eve._flushingUIComponents = false
40::eve_ui_flush_components <- function() {
41 eve._flushUIComponents()
44eve.UIComponent <- class {
60 constructor(uiInstance = null, initialProps = null) {
65 props = initialProps != null ? initialProps : {}
74 eve._nextUIComponentOwner += 1
75 _eventOwner = eve._nextUIComponentOwner
78 function setUI(uiInstance) { _ui = uiInstance; return this }
80 function _applyProps(nextProps, replace, notify) {
81 if (nextProps == null) return this
82 if (replace) props = {}
83 foreach (key, value in nextProps) props.rawset(key, value)
84 if (notify) markDirty()
88 function setProps(nextProps, replace = false) {
89 return _applyProps(nextProps, replace, true)
92 function mountAs(name) {
100 function setState(patch = null) {
102 foreach (key, value in patch) state.rawset(key, value)
107 function markDirty() {
109 if (_parent != null && _parent != this) _parent.markDirty()
110 else if (_mounted) eve._scheduleUIComponent(this)
114 // Compose a persistent child instance inside this component's active build pass.
115 function renderChild(component, nextProps = null, replaceProps = false) {
116 if (component == null) return null
117 component._parent = this
118 component.setUI(this.ui())
119 component._applyProps(nextProps, replaceProps, false)
120 _nextChildren.append(component)
121 local firstMount = !component._mounted
122 component._nextChildren = []
124 component._finishChildPass()
125 component.dirty = false
126 component._mounted = true
127 if (firstMount) component.onMount()
128 else component.onUpdated()
132 // Reuse a child component by a stable key. factory is called only for a new key.
133 function renderKeyed(key, factory, nextProps = null, replaceProps = false) {
134 if (key == null) throw "UIComponent renderKeyed requires a key"
135 local component = null
136 if (key in _keyedChildren) component = _keyedChildren[key]
138 component = factory()
139 if (component == null) throw "UIComponent renderKeyed factory returned null"
140 _keyedChildren.rawset(key, component)
142 return renderChild(component, nextProps, replaceProps)
145 function _bindEvent(kind, id, fn) {
146 if (id == null || id == "" || fn == null) return this
147 foreach (binding in _eventBindings) {
148 if (binding.kind == kind && binding.id == id) {
150 if (_mounted) _syncEventHandlers()
154 _eventBindings.append({ kind = kind, id = id, fn = fn })
155 if (_mounted) _syncEventHandlers()
159 function bindClick(id, fn) { return _bindEvent("click", id, fn) }
160 function bindChange(id, fn) { return _bindEvent("change", id, fn) }
162 function clearEventBindings() {
164 this.ui().componentClearHandlers(_eventOwner)
168 function _rootComponent() {
170 while (root._parent != null && root._parent != root) root = root._parent
174 function _syncEventHandlers() {
176 u.componentClearHandlers(_eventOwner)
177 local root = _rootComponent()
178 if (root.hostName == "" || !u.select(root.hostName)) return
179 foreach (binding in _eventBindings) {
180 if (binding.kind == "click") u.componentOnClick(_eventOwner, binding.id, binding.fn)
181 else u.componentOnChange(_eventOwner, binding.id, binding.fn)
185 function _syncEventTree() {
187 foreach (child in _children) child._syncEventTree()
190 function _containsChild(items, child) {
191 foreach (candidate in items) {
192 if (candidate == child) return true
197 function _finishChildPass() {
198 foreach (child in _children) {
199 if (!_containsChild(_nextChildren, child)) child._unmountTree()
202 foreach (key, child in _keyedChildren) {
203 if (!_containsChild(_nextChildren, child)) staleKeys.append(key)
205 foreach (key in staleKeys) delete _keyedChildren[key]
206 _children = _nextChildren
210 function _unmountTree() {
211 if (!_mounted) return false
212 foreach (child in _children) child._unmountTree()
218 this.ui().componentClearHandlers(_eventOwner)
225 if (!_unmountTree()) return false
227 if (hostName != "" && u.select(hostName)) u.setHostVisible(false)
231 // Override in subclass: call this.ui().beginWindow / text / button / end ...
233 function onMount() {}
234 function onUpdated() {}
235 function onUnmount() {}
238 if (_ui != null) return _ui
240 if (::ui != null) return ::ui
246 function updateIfDirty() {
247 if (!dirty) return false
248 local firstMount = !_mounted
257 local name = hostName
258 if (name == null || name == "") name = "default"
261 if (!u.mountBuildAs(name)) throw "UIComponent mount failed: " + name
264 if (!u.remountBuildAs(name)) throw "UIComponent remount failed: " + name
267 u.setHostVisible(true)
268 if (firstMount) onMount()
271 } catch (error) { markDirty(); throw error }
275 function rebuild(force = false) {
278 return updateIfDirty()
281// Note: eve.Component is reserved for script ECS (see exposeECS). Use eve.UIComponent.
The single Squirrel projection for common Result, Status and Value.
SQRESULT compileAndRunChunk(HSQUIRRELVM vm, const char *name, const char *source)
Compile and run one embedded script chunk against the root table.
void injectUIComponentClass(ssq::Table &eveTable)