docs
Loading...
Searching...
No Matches
Callback.h
Go to the documentation of this file.
1///
2/// Copyright (C) 2026 Ultralight, Inc. All rights reserved.
3/// A license is required for commercial use. https://ultralig.ht
4///
5#pragma once
6#include <AppCore/Defines.h>
8#include <Ultralight/RefPtr.h>
9
10namespace ultralight {
11
12class Container;
13class LayoutNode;
14class LayoutRegistration;
15class Panel;
16class Window;
17struct EditableState;
18
19///
20/// Destroy hook for a callback registration's user data.
21///
22/// @param user_data The user data you passed to the registration method.
23///
24/// @note This is invoked exactly once, when the registration finally drops the user data. It is
25/// never invoked while the callback itself is executing.
26///
27typedef void (*LayoutDestroyUserDataCallback)(void* user_data);
28
29///
30/// Callback fired for a layout node event.
31///
32/// @param user_data The user data you passed to the registration method.
33///
34/// @param node The node the event is for.
35///
36/// @see LayoutNode::OnLayoutChange()
37///
38typedef void (*LayoutNodeCallback)(void* user_data, LayoutNode* node);
39
40///
41/// Callback fired for a panel event.
42///
43/// @param user_data The user data you passed to the registration method.
44///
45/// @param panel The panel the event is for.
46///
47/// @see Panel::OnDismiss()
48///
49typedef void (*LayoutPanelCallback)(void* user_data, Panel* panel);
50
51///
52/// Callback fired when a window's focused panel changes.
53///
54/// @param user_data The user data you passed to the registration method.
55///
56/// @param window The window whose focus changed.
57///
58/// @param focused The newly focused panel (can be null when nothing holds panel focus).
59///
60/// @see Window::OnFocusChange()
61///
62typedef void (*LayoutFocusCallback)(void* user_data, Window* window, Panel* focused);
63
64///
65/// Callback fired when the focused panel's editable state changes.
66///
67/// @param user_data The user data you passed to the registration method.
68///
69/// @param window The window the state belongs to.
70///
71/// @param panel The panel the state describes (can be null when nothing holds panel
72/// focus).
73///
74/// @param state The new editable state.
75///
76/// @see Window::OnEditableStateChange()
77///
78typedef void (*LayoutEditableStateCallback)(void* user_data, Window* window, Panel* panel,
79 const EditableState& state);
80
81///
82/// Layout delegate, called to place a container's children yourself.
83///
84/// @param user_data The user data you passed to the registration method.
85///
86/// @param container The container being laid out.
87///
88/// @param content_box The container's content box, in container-local logical pixels.
89///
90/// @see Container::SetLayoutOverride()
91///
92typedef void (*LayoutOverrideCallback)(void* user_data, Container* container, Rect content_box);
93
94///
95/// Scoped handle that controls how long a layout callback stays registered.
96///
97/// Methods that register layout and panel callbacks return a LayoutCallback. The callback stays
98/// active for as long as this handle exists.
99///
100/// To keep receiving events, store the handle in an object whose lifetime matches the listener.
101///
102/// This status bar keeps a focus-change callback as a member variable:
103///
104/// ```
105/// class StatusBar {
106/// public:
107/// explicit StatusBar(Window* window)
108/// : on_focus_(window->OnFocusChange([](Panel* focused) {
109/// ShowFocusedKey(focused ? focused->key() : String());
110/// })) {}
111///
112/// private:
113/// LayoutCallback on_focus_; // unregisters when the StatusBar goes away
114/// };
115/// ```
116///
117/// ## Callback Lifetime
118///
119/// A registered callback stops firing when:
120///
121/// - **The handle is destroyed.** Discarding the return value of a registration call unregisters
122/// the callback immediately.
123/// - **You call Remove().** The callback unregisters early, even from inside the callback itself.
124/// - **The target node is removed or its window closes.**
125///
126/// Call IsActive() to check whether the callback can still fire.
127///
128/// ## Detaching Callbacks
129///
130/// To keep a callback running without storing its handle, call Detach(). The callback remains
131/// registered for the rest of the node's lifetime. Once detached, the callback can't be removed.
132///
133/// Chain Detach() onto the registration call:
134///
135/// ```
136/// sidebar->OnLayoutChange([] { Relayout(); }).Detach();
137/// ```
138///
139/// @note Callbacks must not let exceptions propagate.
140///
141/// @see LayoutNode::OnLayoutChange(), LayoutNode::OnUserResize(), Panel::OnDismiss(),
142/// Window::OnFocusChange(), Window::OnEditableStateChange()
143///
145 public:
146 ///
147 /// Create an empty LayoutCallback (it owns no registration and IsActive() is false).
148 ///
150
153
154 ///
155 /// Move constructor (transfers the registration, `other` becomes empty).
156 ///
158
159 ///
160 /// Move assignment. Removes any registration this object held, then takes over `other`'s.
161 ///
163
164 ///
165 /// Unregister the callback (if it is still registered) and release the registration.
166 ///
168
169 ///
170 /// Whether or not the callback can still fire. This is false for an empty object, after
171 /// Remove(), and once the node is removed or its window closes.
172 ///
173 explicit operator bool() const { return IsActive(); }
174
175 ///
176 /// The named form of operator bool.
177 ///
178 bool IsActive() const;
179
180 ///
181 /// Stop receiving callbacks now.
182 ///
183 /// You can call this more than once, after the node is removed or its window closes, and
184 /// from inside the callback itself.
185 ///
186 void Remove();
187
188 ///
189 /// Leave the callback registered for the rest of its node's lifetime and give up this object.
190 ///
191 /// The callback keeps firing until the node is removed or its window closes. There is no way
192 /// to remove it afterwards.
193 ///
194 void Detach();
195
196 /// \cond ignore
198
199 LayoutRegistration* registration() const;
200 /// \endcond
201
202 private:
203 explicit LayoutCallback(RefPtr<LayoutRegistration> registration);
204 void Reset();
205
206 RefPtr<LayoutRegistration> registration_;
207};
208
209} // namespace ultralight
#define AExport
Definition Defines.h:41
@ Adopt
Definition JSRetainPtr.h:48
A layout node that organizes child nodes into a row or column.
Definition Container.h:101
void Remove()
Stop receiving callbacks now.
LayoutCallback()
Create an empty LayoutCallback (it owns no registration and IsActive() is false).
~LayoutCallback()
Unregister the callback (if it is still registered) and release the registration.
LayoutCallback & operator=(LayoutCallback &&other) noexcept
Move assignment.
LayoutCallback(const LayoutCallback &)=delete
bool IsActive() const
The named form of operator bool.
void Detach()
Leave the callback registered for the rest of its node's lifetime and give up this object.
LayoutCallback(LayoutCallback &&other) noexcept
Move constructor (transfers the registration, other becomes empty).
LayoutCallback & operator=(const LayoutCallback &)=delete
Base class for Panels and Containers in a window layout tree.
Definition LayoutNode.h:108
A layout node that hosts a single web view.
Definition Panel.h:59
A nullable smart pointer.
Definition RefPtr.h:126
A native OS window that displays web content.
Definition Window.h:636
Root namespace for every public Ultralight type, function, and enumeration.
void(*) LayoutPanelCallback(void *user_data, Panel *panel)
Callback fired for a panel event.
Definition Callback.h:49
void(*) LayoutFocusCallback(void *user_data, Window *window, Panel *focused)
Callback fired when a window's focused panel changes.
Definition Callback.h:62
void(*) LayoutNodeCallback(void *user_data, LayoutNode *node)
Callback fired for a layout node event.
Definition Callback.h:38
void(*) LayoutOverrideCallback(void *user_data, Container *container, Rect content_box)
Layout delegate, called to place a container's children yourself.
Definition Callback.h:92
PanelSpec panel(PanelOptions options={}, RefPtr< Panel > *out=nullptr)
Describe a panel in a builder expression (see Builder.h for an example).
Definition Builder.h:90
void(*) LayoutEditableStateCallback(void *user_data, Window *window, Panel *panel, const EditableState &state)
Callback fired when the focused panel's editable state changes.
Definition Callback.h:78
void(*) LayoutDestroyUserDataCallback(void *user_data)
Destroy hook for a callback registration's user data.
Definition Callback.h:27
The conditions that decide whether an input method should be active.
Definition Editor.h:145
Float Rectangle Helper.
Definition Geometry.h:416