docs
Loading...
Searching...
No Matches
Foreground.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
8
9#include <concepts>
10#include <utility>
11
12namespace ultralight {
13
14///
15/// Floating layer that displays panels above a window's layout.
16///
17/// The foreground layer hosts overlays like heads-up displays and dialogs without shifting the
18/// panels beneath them. You reach it through Window::foreground().
19///
20/// Call AddPanel() to float a panel with a new View:
21///
22/// ```
23/// RefPtr<Panel> hud = window->foreground()->AddPanel(
24/// { .width = "200px", .height = "120px", .placement = Anchor::At(24, 24) });
25/// hud->view()->LoadURL("file:///hud.html");
26/// ```
27///
28/// ## Adding Floating Panels
29///
30/// A floating panel is an ordinary Panel without a parent container (see Panel). Call AdoptPanel()
31/// to float a View you already have.
32///
33/// ## Size and Placement
34///
35/// Set dimensions and placement through ForegroundPanelOptions when adding a panel (see Anchor).
36/// The options also configure auto-dismissal and keyboard focus behavior.
37///
38/// A floating panel's size and placement are fixed at creation.
39///
40/// To move or resize a panel, re-adopt its View with updated options:
41///
42/// ```
43/// RefPtr<View> hud_view = hud->view(); // before Remove(), which drops it
44/// window->foreground()->Remove(hud);
45/// hud = window->foreground()->AdoptPanel(
46/// hud_view, { .width = "200px", .height = "120px",
47/// .placement = Anchor::WindowCenter() });
48/// ```
49///
50/// ## Stacking and Removal
51///
52/// Floating panels stack in the order you add them, with the newest panel on top. Call
53/// Panel::BringToFront() to move a panel to the top of the layer so it paints on top and receives
54/// mouse input first.
55///
56/// Dropping a panel handle doesn't remove it from the layer. Call Remove() to detach a floating
57/// panel.
58///
59/// Calls on a handle stay safe and do nothing after a panel is removed or after the window closes,
60/// and Panel::view() returns null (see LayoutNode).
61///
62/// @note Floating panels clip to the window (use Window::CreatePopup() for menus and dropdowns
63/// that extend past it).
64///
65/// @see Window::foreground(), ForegroundPanelOptions, Anchor, Panel::BringToFront(),
66/// Window::CreatePopup()
67///
69 public:
70 ///
71 /// Create a floating panel with a new View (window-default view configuration).
72 ///
73 /// @param options The panel's declared options.
74 ///
75 /// @return Returns the new panel (see LayoutNode if the window has closed).
76 ///
77 virtual RefPtr<Panel> AddPanel(const ForegroundPanelOptions& options = {}) = 0;
78
79 ///
80 /// Create a floating panel with a caller-supplied ViewConfig (the same rules as
81 /// Container::AddPanel()).
82 ///
83 /// @param options The panel's declared options.
84 ///
85 /// @param view_config The configuration for the new View. The fields describing the
86 /// hosting window are filled in from the window for you.
87 ///
88 /// @return Returns the new panel (see LayoutNode if the window has closed).
89 ///
91 const ViewConfig& view_config) = 0;
92
93 ///
94 /// Create a floating panel that adopts an existing View (including a View created through
95 /// a Session, or one that survived Remove()).
96 ///
97 /// The panel takes a reference to the View and resizes it to the panel's bounds.
98 ///
99 /// @param view The View to host.
100 ///
101 /// @param options The panel's declared options.
102 ///
103 /// @return Returns the new panel (see LayoutNode if the window has closed).
104 ///
105 /// @note The panel replaces the View's editor listener with its own (see Panel).
106 ///
108 const ForegroundPanelOptions& options = {}) = 0;
109
110 ///
111 /// Adopt an existing View (shorthand for AdoptPanel()).
112 ///
113 /// @param view The View to host.
114 ///
115 /// @param options The panel's declared options.
116 ///
117 /// @return Returns the new panel (see LayoutNode if the window has closed).
118 ///
119 template <typename ViewRef>
120 requires std::convertible_to<ViewRef&&, RefPtr<View>>
121 RefPtr<Panel> AddPanel(ViewRef&& view, const ForegroundPanelOptions& options = {}) {
122 return AdoptPanel(RefPtr<View>(std::forward<ViewRef>(view)), options);
123 }
124
125 ///
126 /// Remove a floating panel from the foreground layer.
127 ///
128 /// Removal drops the panel's reference to its View. A View the application holds a RefPtr
129 /// to survives and can be adopted elsewhere. For what removed handles do afterwards, see
130 /// LayoutNode.
131 ///
132 /// @param panel The floating panel to remove. A panel that is not one of this window's
133 /// floating panels is a no-op with a warning.
134 ///
135 /// @return Returns whether or not the panel was one of this window's floating panels
136 /// and was removed.
137 ///
138 virtual bool Remove(RefPtr<Panel> panel) = 0;
139
140 ///
141 /// Get the number of floating panels (hidden panels included).
142 ///
143 virtual int panel_count() const = 0;
144
145 ///
146 /// Get the floating panel at an index in current z-order, bottom-most first.
147 ///
148 /// @param index The zero-based index in z-order.
149 ///
150 /// @return Returns the panel, or null when the index is out of range.
151 ///
152 virtual RefPtr<Panel> panel_at(int index) const = 0;
153
154 ///
155 /// Find a floating panel by key.
156 ///
157 /// @param key The key to look for.
158 ///
159 /// @return Returns the matching panel, or null when nothing matches. With duplicate keys
160 /// the most recently created match wins.
161 ///
162 virtual RefPtr<Panel> FindPanel(const String& key) = 0;
163
164 protected:
165 virtual ~Foreground();
166};
167
168} // namespace ultralight
#define AExport
Definition Defines.h:41
Floating layer that displays panels above a window's layout.
Definition Foreground.h:68
virtual RefPtr< Panel > AddPanel(const ForegroundPanelOptions &options, const ViewConfig &view_config)=0
Create a floating panel with a caller-supplied ViewConfig (the same rules as Container::AddPanel()).
virtual bool Remove(RefPtr< Panel > panel)=0
Remove a floating panel from the foreground layer.
virtual RefPtr< Panel > FindPanel(const String &key)=0
Find a floating panel by key.
virtual RefPtr< Panel > AdoptPanel(RefPtr< View > view, const ForegroundPanelOptions &options={})=0
Create a floating panel that adopts an existing View (including a View created through a Session,...
RefPtr< Panel > AddPanel(ViewRef &&view, const ForegroundPanelOptions &options={})
Adopt an existing View (shorthand for AdoptPanel()).
Definition Foreground.h:121
virtual int panel_count() const =0
Get the number of floating panels (hidden panels included).
virtual RefPtr< Panel > panel_at(int index) const =0
Get the floating panel at an index in current z-order, bottom-most first.
virtual RefPtr< Panel > AddPanel(const ForegroundPanelOptions &options={})=0
Create a floating panel with a new View (window-default view configuration).
Interface for all ref-counted objects that will be managed using the RefPtr<> smart pointer.
Definition RefPtr.h:49
A nullable smart pointer.
Definition RefPtr.h:126
Unicode string container with conversions for UTF-8, UTF-16, and UTF-32.
Definition String.h:31
Root namespace for every public Ultralight type, function, and enumeration.
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
Configuration options for a floating panel.
Definition Options.h:95
View-specific configuration settings.
Definition View.h:100