The viewport of a DOM document.
A Window is a handle to the visual window that displays a document. You use it to inspect viewport dimensions and to listen for events that fire only on the window, such as load and resize.
This example updates a native HUD when the viewport resizes:
});
Window defaultView() const
Get the document's window (defaultView).
Definition Window.h:480
The viewport of a DOM document.
Definition Window.h:73
int innerHeight() const
Get the viewport height (innerHeight).
Definition Window.h:157
EventListener addEventListener(std::string_view type, F &&callback, const AddEventListenerOptions &options={}) const
Listen for an event on the window (addEventListener).
Definition Window.h:302
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
int innerWidth() const
Get the viewport width (innerWidth).
Definition Window.h:149
Getting a Window
You can access a window through the View or a specific document:
- Pass a View to Window(view) for the main frame.
- Call Document::defaultView() on any frame's document. This returns the window of that specific frame inside the page.
You should obtain a Window in LoadListener::OnDOMReady() or later. Before your first page loads, a View holds only the frame's initial blank page, and its window goes away when the new page loads.
Viewport Coordinates
Viewport sizes and scroll positions use CSS pixels. To convert CSS pixels to View pixels, multiply by View::device_scale() (or devicePixelRatio()), or divide View pixels by the scale factor to convert back.
Window Events
Events such as load and resize fire on the window only, so document listeners never receive them. Viewport scroll events fire on the document first and bubble up to the window.
Listeners receive resize and scroll events when the View next paints, rather than during the call that caused the change.
A load listener added in LoadListener::OnDOMReady() still receives the page's load event, which fires once all external resources finish loading.
- Note
- Like other DOM handles, a Window is Valid, Empty, or Gone, and never keeps its page alive (see dom::Element).
- See also
- dom::Document::defaultView(), LoadListener::OnDOMReady(), dom::Element::addEventListener(), View::device_scale()
|
| | Window ()=default |
| | Create an empty Window.
|
| | Window (View *view) |
| | Get the window of a View's main frame.
|
| | Window (const Window &other) |
| | Copy constructor (both handles refer to the same window).
|
| | Window (Window &&other) noexcept |
| | Move constructor (other becomes empty).
|
| Window & | operator= (Window other) noexcept |
| | Assignment (copies or moves).
|
| | ~Window () |
| | Destroy this handle (the window itself isn't affected).
|
| | operator bool () const |
| | Whether or not this Window is valid (see IsAlive()).
|
| bool | IsEmpty () const |
| | Whether or not this Window is empty (it holds no handle).
|
| bool | IsAlive () const |
| | Whether or not this Window is valid (it isn't empty and its page is still alive).
|
| int | innerWidth () const |
| | Get the viewport width (innerWidth).
|
| int | innerHeight () const |
| | Get the viewport height (innerHeight).
|
| double | devicePixelRatio () const |
| | Get the ratio of device pixels to CSS pixels (devicePixelRatio).
|
| double | scrollX () const |
| | Get the viewport's horizontal scroll position (scrollX).
|
| double | scrollY () const |
| | Get the viewport's vertical scroll position (scrollY).
|
| void | scrollTo (double x, double y) const |
| | Scroll the viewport to a position (scrollTo).
|
| void | scrollBy (double x, double y) const |
| | Scroll the viewport by an offset (scrollBy).
|
| ComputedStyle | getComputedStyle (const Element &element) const |
| | Get an element's computed style (getComputedStyle).
|
| Document | document () const |
| | Get the window's document (document).
|
| Selection | getSelection () const |
| | Get the page's selection (getSelection), the caret or the highlighted text.
|
| bool | dispatchEvent (std::string_view type, const EventInit &init={}) const |
| | Dispatch a synthetic event to the window (dispatchEvent).
|
| 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.
|
| template<typename F> |
| EventListener | addEventListener (std::string_view type, F &&callback, const AddEventListenerOptions &options={}) const |
| | Listen for an event on the window (addEventListener).
|
| template<typename F> |
| 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).
|
template<typename C, typename M>
requires (std::is_class_v<C> && std::is_member_function_pointer_v<M> && !LockableHolder<C>) |
| 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.
|
template<typename C, typename M>
requires (std::is_class_v<C> && std::is_member_function_pointer_v<M> && !LockableHolder<C>) |
| 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.
|
template<typename H, typename M>
requires (LockableHolder<H> && std::is_member_function_pointer_v<M>) |
| 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.
|
template<typename H, typename M>
requires (LockableHolder<H> && std::is_member_function_pointer_v<M>) |
| 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.
|
| ULDOMWindow | raw () const |
| | Get the C handle, for passing to the <Ultralight/CAPI/CAPI_DOMWindow.h> functions.
|
| ULDOMWindow | LeakRef () |
| | Give up ownership of the C handle and return it.
|