6#include <Ultralight/CAPI/CAPI_DOMWindow.h>
10#include <Ultralight/dom/detail/Receivers.h>
99 : handle_(other.handle_ ? ulCreateDOMWindowRef(other.handle_) : nullptr) {}
106 Window(
Window&& other) noexcept : handle_(other.handle_) { other.handle_ =
nullptr; }
116 std::swap(handle_, other.handle_);
128 explicit operator bool()
const {
return IsAlive(); }
133 bool IsEmpty()
const {
return handle_ ==
nullptr; }
140 bool IsAlive()
const {
return handle_ && ulDOMWindowIsAlive(handle_); }
149 int innerWidth()
const {
return ulDOMWindowGetInnerWidth(handle_); }
157 int innerHeight()
const {
return ulDOMWindowGetInnerHeight(handle_); }
171 double scrollX()
const {
return ulDOMWindowGetScrollX(handle_); }
178 double scrollY()
const {
return ulDOMWindowGetScrollY(handle_); }
192 void scrollTo(
double x,
double y)
const { ulDOMWindowScrollTo(handle_, x, y); }
203 void scrollBy(
double x,
double y)
const { ulDOMWindowScrollBy(handle_, x, y); }
254 detail::CString t(type);
255 return ulDOMWindowDispatchEvent(handle_, t.c_str(), detail::EventInitFlags(init),
276 detail::CString t(type);
277 detail::CString d(event_detail);
278 return ulDOMWindowDispatchCustomEvent(handle_, t.c_str(),
279 detail::EventInitFlags(init), d.c_str(),
301 template <
typename F>
304 using Fn = std::decay_t<F>;
305 static_assert(detail::InvocableWithEvent<Fn> || std::is_invocable_v<Fn&>,
306 "addEventListener takes a callable invocable with (dom::Event) or ()");
307 detail::CString t(type);
308 ULDOMEventCallback thunk = &detail::EventThunk<Fn>;
309 ULUserDataDestroyCallback destroy = &detail::DeleteCallable<Fn>;
310 return detail::AddListener<Fn>(std::forward<F>(callback), options,
311 [&](
unsigned flags,
void* fn) {
312 return ulDOMWindowAddEventListener(handle_, t.c_str(), flags,
329 template <
typename F>
331 return addEventListener(type, std::forward<F>(callback), detail::ToOptions(flags));
353 template <
typename C,
typename M>
354 requires(std::is_class_v<C> && std::is_member_function_pointer_v<M>
358 return addEventListener(type, detail::WrapBorrowedMember(receiver, method), options);
379 template <
typename C,
typename M>
380 requires(std::is_class_v<C> && std::is_member_function_pointer_v<M>
381 && !LockableHolder<C>)
384 return addEventListener(type, detail::WrapBorrowedMember(receiver, method), flags);
402 template <
typename H,
typename M>
406 return addEventListener(type, detail::WrapHolderMember(std::move(holder), method), options);
424 template <
typename H,
typename M>
425 requires(LockableHolder<H> && std::is_member_function_pointer_v<M>)
428 return addEventListener(type, detail::WrapHolderMember(std::move(holder), method), flags);
451 return Window(handle ? ulCreateDOMWindowRef(handle) :
nullptr);
460 ULDOMWindow
raw()
const {
return handle_; }
468 ULDOMWindow handle = handle_;
474 explicit Window(ULDOMWindow handle) : handle_(handle) {}
477 ULDOMWindow handle_ =
nullptr;
Web-page container rendered to an offscreen surface.
Definition View.h:483
A read-only view of an element's computed style (getComputedStyle).
Definition ComputedStyle.h:122
The root of a page's DOM tree in a View or frame.
Definition Document.h:106
static Document Adopt(ULDOMDocument handle)
Wrap a C handle you own, taking ownership of it.
Definition Document.h:940
Window defaultView() const
Get the document's window (defaultView).
Definition Window.h:480
A handle to an element on a page.
Definition Element.h:142
A handle to a registered DOM event listener.
Definition EventListener.h:151
A handle to the active user selection or caret in a document.
Definition Selection.h:101
The viewport of a DOM document.
Definition Window.h:73
EventListener addEventListener(std::string_view type, F &&callback, EventListenerFlags flags) const
Listen for an event on the window, with options as flags (eg, dom::Once | dom::Capture).
Definition Window.h:330
static Window Adopt(ULDOMWindow handle)
Wrap a C handle you own, taking ownership of it.
Definition Window.h:441
Window(View *view)
Get the window of a View's main frame.
Definition Window.h:91
~Window()
Destroy this handle (the window itself isn't affected).
Definition Window.h:123
EventListener addEventListener(std::string_view type, C &receiver, M method, EventListenerFlags flags) const
Listen by calling a member function on an object you keep alive, with options as flags.
Definition Window.h:382
EventListener addEventListener(std::string_view type, H holder, M method, const AddEventListenerOptions &options={}) const
Listen by calling a member function through a smart pointer that's checked before each call.
Definition Window.h:404
double scrollX() const
Get the viewport's horizontal scroll position (scrollX).
Definition Window.h:171
void scrollBy(double x, double y) const
Scroll the viewport by an offset (scrollBy).
Definition Window.h:203
int innerHeight() const
Get the viewport height (innerHeight).
Definition Window.h:157
Window(const Window &other)
Copy constructor (both handles refer to the same window).
Definition Window.h:98
EventListener addEventListener(std::string_view type, F &&callback, const AddEventListenerOptions &options={}) const
Listen for an event on the window (addEventListener).
Definition Window.h:302
Selection getSelection() const
Get the page's selection (getSelection), the caret or the highlighted text.
Definition Selection.h:555
Window()=default
Create an empty Window.
double devicePixelRatio() const
Get the ratio of device pixels to CSS pixels (devicePixelRatio).
Definition Window.h:164
Document document() const
Get the window's document (document).
Definition Window.h:227
static Window FromBorrowed(ULDOMWindow handle)
Wrap a C handle the library owns, adding a reference.
Definition Window.h:450
Window(Window &&other) noexcept
Move constructor (other becomes empty).
Definition Window.h:106
ComputedStyle getComputedStyle(const Element &element) const
Get an element's computed style (getComputedStyle).
Definition Window.h:216
bool IsEmpty() const
Whether or not this Window is empty (it holds no handle).
Definition Window.h:133
bool IsAlive() const
Whether or not this Window is valid (it isn't empty and its page is still alive).
Definition Window.h:140
bool dispatchEvent(std::string_view type, const EventInit &init={}) const
Dispatch a synthetic event to the window (dispatchEvent).
Definition Window.h:253
EventListener addEventListener(std::string_view type, H holder, M method, EventListenerFlags flags) const
Listen by calling a member function through a smart pointer, with options as flags.
Definition Window.h:426
ULDOMWindow raw() const
Get the C handle, for passing to the <Ultralight/CAPI/CAPI_DOMWindow.h> functions.
Definition Window.h:460
int innerWidth() const
Get the viewport width (innerWidth).
Definition Window.h:149
bool dispatchCustomEvent(std::string_view type, std::string_view event_detail, const EventInit &init={}) const
Dispatch a synthetic CustomEvent with a string payload to the window.
Definition Window.h:274
void scrollTo(double x, double y) const
Scroll the viewport to a position (scrollTo).
Definition Window.h:192
EventListener addEventListener(std::string_view type, C &receiver, M method, const AddEventListenerOptions &options={}) const
Listen by calling a member function on an object you keep alive.
Definition Window.h:356
Window(ULDOMWindow handle)
Definition Window.h:474
ULDOMWindow LeakRef()
Give up ownership of the C handle and return it.
Definition Window.h:467
Window & operator=(Window other) noexcept
Assignment (copies or moves).
Definition Window.h:115
double scrollY() const
Get the viewport's vertical scroll position (scrollY).
Definition Window.h:178
Whether or not the DOM API can hold an object through H (ignoring const and references),...
Definition Holders.h:41
Direct C++ access to modify page elements and handle events.
EventListenerFlags
Options for adding an event listener, as flags (addEventListener() and On()).
Definition EventListener.h:41
ComputedStyle getComputedStyle(const Element &element)
Get an element's computed style (getComputedStyle).
Definition ComputedStyle.h:232
Root namespace for every public Ultralight type, function, and enumeration.
Options for adding an event listener, like the web's options object for addEventListener().
Definition EventListener.h:526
Options for an event you dispatch yourself (the web's EventInit).
Definition Event.h:30