Class Page
- All Implemented Interfaces:
Serializable
- Since:
- 1.0
- Author:
- Vaadin Ltd
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceCallback for receiving extended client-side details. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddBrowserWindowResizeListener(BrowserWindowResizeListener resizeListener) Adds a newBrowserWindowResizeListenerto this UI.voidaddDynamicImport(String expression) Adds a dynamic import using a JavaScriptexpressionwhich is supposed to return a JavaScriptPromise.Adds a listener that is notified when the fullscreen state changes.voidaddJavaScript(String url) Adds the given JavaScript to the page and ensures that it is loaded successfully.voidaddJavaScript(String url, LoadMode loadMode) Adds the given JavaScript to the page and ensures that it is loaded successfully.voidaddJsModule(String url) Adds the given external JavaScript module to the page and ensures that it is loaded successfully.addStyleSheet(String url) Adds the given style sheet to the page and ensures that it is loaded successfully.addStyleSheet(String url, LoadMode loadMode) Adds the given style sheet to the page and ensures that it is loaded successfully.Adds a listener that is notified when the screen wake lock is released, either by the browser or by a call toreleaseWakeLock().executeJs(String expression, Serializable[] parameters) Deprecated.Asynchronously runs the given JavaScript expression in the browser.voidExits fullscreen mode if the page is currently in fullscreen.voidfetchCurrentURL(SerializableConsumer<URL> callback) Retrieves the current url from the browser.voidfetchPageDirection(SerializableConsumer<Direction> callback) Retrievesdocument.dirof the current UI from the browser and passes it to thecallbackparameter.Gets the color scheme for the page.Gets the extended client details, such as screen resolution and time zone information.Gets a representation ofwindow.historyfor this page.Checks if the browser supports fullscreen mode.booleanReturns whether the browser supports the Web Share API (navigator.share).Checks whether a screen wake lock is currently active.lockOrientation(ScreenOrientation orientation) Locks the screen orientation to the specified type.voidOpens the given url in a new tab.voidOpens the given URL in a window with the given name.Returns a signal that tracks the current page visibility state.voidReleases a previously acquired screen wake lock.voidreload()Reloads the page in the browser.voidRequests that the browser display the entire page in fullscreen mode.voidRequests the browser to keep the screen awake using the Screen Wake Lock API.voidDeprecated.UsegetExtendedClientDetails()to get the cached details, orExtendedClientDetails.refresh(SerializableConsumer)to refresh the cached values.Returns a signal that tracks the current screen orientation.voidsetColorScheme(ColorScheme.Value colorScheme) Sets the color scheme for the page.voidsetLocation(String uri) Navigates this page to the given URI.voidsetLocation(URI uri) Navigates this page to the given URI.voidSets the page title.Invokes the browser's native share dialog using the Web Share API.voidUnlocks the screen orientation, allowing it to rotate freely again.Returns a signal that tracks the current browser window size.
-
Constructor Details
-
Page
Creates a page instance for the given UI.- Parameters:
ui- the UI that this page instance is connected to
-
-
Method Details
-
setTitle
Sets the page title. The title is displayed by the browser e.g. as the title of the browser window or tab.To clear the page title, use an empty string.
- Parameters:
title- the page title to set, notnull
-
setColorScheme
Sets the color scheme for the page.The color scheme is applied via both a theme attribute and the color-scheme CSS property on the html element. The theme attribute allows CSS to target different color schemes (e.g.,
html[theme~="dark"]), while the color-scheme property ensures browser UI adaptation works even for custom themes that don't define their own color-scheme CSS rules.- Parameters:
colorScheme- the color scheme to set (e.g., ColorScheme.Value.DARK, ColorScheme.Value.LIGHT), ornullto reset to NORMAL
-
getColorScheme
Gets the color scheme for the page.Note that this method returns the server-side cached value and will not detect color scheme changes made directly via JavaScript or browser developer tools.
- Returns:
- the color scheme value, never
null
-
addStyleSheet
Adds the given style sheet to the page and ensures that it is loaded successfully.Relative URLs are interpreted as relative to the static web resources directory. So if the
urlvalue is"some.js"andsrc/main/webappis used as a location for static web resources (which is the default location) then the file system path for the resource should besrc/main/webapp/some.js.You can prefix the URL with
context://to make it relative to the context path or use an absolute URL to refer to files outside the frontend directory.For component related style sheet dependencies, you should use the
@StyleSheetannotation.Is is guaranteed that style sheet will be loaded before the first page load. For more options, refer to
addStyleSheet(String, LoadMode)- Parameters:
url- the URL to load the style sheet from, notnull- Returns:
- a registration object that can be used to remove the style sheet
-
addJavaScript
Adds the given JavaScript to the page and ensures that it is loaded successfully.Relative URLs are interpreted as relative to the static web resources directory. So if the
urlvalue is"some.js"andsrc/main/webappis used as a location for static web resources (which is the default location) then the file system path for the resource should besrc/main/webapp/some.js.You can prefix the URL with
context://to make it relative to the context path or use an absolute URL to refer to files outside the frontend directory.For component related JavaScript dependencies, you should use the
@JavaScriptannotation.Is is guaranteed that script will be loaded before the first page load. For more options, refer to
addJavaScript(String, LoadMode)- Parameters:
url- the URL to load the JavaScript from, notnull
-
addJsModule
Adds the given external JavaScript module to the page and ensures that it is loaded successfully.If the JavaScript modules do not need to be added dynamically, you should use the
@JsModuleannotation instead.- Parameters:
url- the URL to load the JavaScript module from, notnull
-
addDynamicImport
Adds a dynamic import using a JavaScriptexpressionwhich is supposed to return a JavaScriptPromise.No change will be applied on the client side until resulting
Promiseof theexpressionis completed. It behaves like other dependencies (addJavaScript(String),addJsModule(String), etc.)- Parameters:
expression- the JavaScript expression which return a Promise
-
executeJs
Asynchronously runs the given JavaScript expression in the browser.The expression is executed in an
asyncJavaScript method, so you can utilizeawaitsyntax when consuming JavaScript API returning aPromise. The returnedPendingJavaScriptResultcan be used to retrieve thereturnvalue from the JavaScript expression. If aPromiseis returned in the JavaScript expression,PendingJavaScriptResultwill report the resolved value once it becomes available. If no return value handler is registered, the return value will be ignored.Return values from JavaScript can be automatically deserialized into Java objects. All types supported by Jackson for JSON deserialization are supported as return values, including custom bean classes.
The given parameters will be available to the expression as variables named
$0,$1, and so on. All types supported by Jackson for JSON serialization are supported as parameters. Special cases:Element(will be sent as a DOM element reference to the browser if the server-side element instance is attached when the invocation is sent to the client, or asnullif not attached)BaseJsonNode(sent as-is without additional wrapping)
'prefix' + $0instead of'prefix$0'andvalue[$0]instead ofvalue.$0since JavaScript variables aren't evaluated inside strings or property names.- Parameters:
expression- the JavaScript expression to invokeparameters- parameters to pass to the expression- Returns:
- a pending result that can be used to get a value returned from the expression
-
executeJs
Deprecated.UseexecuteJs(String, Object...)instead. This method exists only for binary compatibility.Executes the given JavaScript expression in the browser.- Parameters:
expression- the JavaScript expression to executeparameters- parameters to pass to the expression- Returns:
- a pending result that can be used to get a value returned from the expression
-
getHistory
Gets a representation ofwindow.historyfor this page.- Returns:
- the history representation
-
reload
public void reload()Reloads the page in the browser. -
windowSizeSignal
Returns a signal that tracks the current browser window size.The signal is lazily initialized on first access and automatically updates when the browser window is resized. The initial value uses dimensions from
ExtendedClientDetailsif available, otherwise defaults to 0x0.The returned signal is read-only.
- Returns:
- a read-only signal with the current window size
-
addBrowserWindowResizeListener
Adds a newBrowserWindowResizeListenerto this UI. The listener will be notified whenever the browser window within which this UI resides is resized.- Parameters:
resizeListener- the listener to add, notnull- Returns:
- a registration object for removing the listener
- See Also:
-
requestWakeLock
public void requestWakeLock()Requests the browser to keep the screen awake using the Screen Wake Lock API. This prevents the screen from dimming or locking, which is useful for dashboards, kiosk applications, and presentations.The wake lock is automatically re-acquired when the page becomes visible again after being hidden (e.g., switching tabs and back).
If the wake lock is released by the browser (e.g., due to low battery), listeners registered with
addWakeLockReleaseListener(com.vaadin.flow.function.SerializableRunnable)are notified. -
releaseWakeLock
public void releaseWakeLock()Releases a previously acquired screen wake lock. -
isWakeLockActive
Checks whether a screen wake lock is currently active.- Returns:
- a pending result that resolves to
trueif a wake lock is active
-
addWakeLockReleaseListener
Adds a listener that is notified when the screen wake lock is released, either by the browser or by a call toreleaseWakeLock().- Parameters:
listener- the listener to add, notnull- Returns:
- a registration object for removing the listener
-
pageVisibilitySignal
Returns a signal that tracks the current page visibility state.The signal is lazily initialized on first access and automatically updates when the browser tab visibility or focus state changes. It distinguishes between three states: fully visible and focused, visible but not focused (e.g. behind another window), and hidden (e.g. background tab or minimized window).
The returned signal is read-only.
- Returns:
- a read-only signal with the current page visibility
-
screenOrientationSignal
Returns a signal that tracks the current screen orientation.The signal is lazily initialized on first access and automatically updates when the screen orientation changes. The initial value defaults to
ScreenOrientation.PORTRAIT_PRIMARYwith angle 0.The returned signal is read-only.
- Returns:
- a read-only signal with the current screen orientation data
-
lockOrientation
Locks the screen orientation to the specified type. This requires fullscreen mode in most browsers.The returned result resolves when the lock succeeds, or rejects if the browser denies the request (e.g., not in fullscreen mode).
- Parameters:
orientation- the orientation to lock to, notnull- Returns:
- a pending result that resolves when the orientation is locked
-
unlockOrientation
public void unlockOrientation()Unlocks the screen orientation, allowing it to rotate freely again. -
open
Opens the given url in a new tab.- Parameters:
url- the URL to open.
-
open
Opens the given URL in a window with the given name.The supplied
windowNameis used as the target name in a window.open call in the client. This means that special values such as "_blank", "_self", "_top", "_parent" have special meaning. An empty ornullwindow name is also a special case."", null and "_self" as
windowNameall causes the URL to be opened in the current window, replacing any old contents. For downloadable content you should avoid "_self" as "_self" causes the client to skip rendering of any other changes as it considers them irrelevant (the page will be replaced by the response from the URL). This can speed up the opening of a URL, but it might also put the client side into an inconsistent state if the window content is not completely replaced e.g., if the URL is downloaded instead of displayed in the browser."_blank" as
windowNamecauses the URL to always be opened in a new window or tab (depends on the browser and browser settings)."_top" and "_parent" as
windowNameworks as specified by the HTML standard.Any other
windowNamewill open the URL in a window with that name, either by opening a new window/tab in the browser or by replacing the contents of an existing window with that name.- Parameters:
url- the URL to open.windowName- the name of the window.
-
setLocation
Navigates this page to the given URI. The contents of this page in the browser is replaced with whatever is returned for the given URI.- Parameters:
uri- the URI to show
-
setLocation
Navigates this page to the given URI. The contents of this page in the browser is replaced with whatever is returned for the given URI.- Parameters:
uri- the URI to show
-
getExtendedClientDetails
Gets the extended client details, such as screen resolution and time zone information.Browser details are automatically collected and sent during UI initialization, making them immediately available. In normal operation, this method returns complete details right after the UI is created.
If details are not yet available, this method returns a placeholder instance with default values (dimensions set to -1). If you need to fetch the actual values in such cases, use
ExtendedClientDetails.refresh(SerializableConsumer)to explicitly retrieve updated values from the browser.To refresh the cached values with updated data from the browser at any time, use
ExtendedClientDetails.refresh(SerializableConsumer).- Returns:
- the extended client details (never
null)
-
retrieveExtendedClientDetails
Deprecated.UsegetExtendedClientDetails()to get the cached details, orExtendedClientDetails.refresh(SerializableConsumer)to refresh the cached values.Obtain extended client side details, such as time screen and time zone information, via callback. If already obtained, the callback is called directly. Otherwise, a client-side roundtrip will be carried out.- Parameters:
receiver- the callback to which the details are provided
-
fetchCurrentURL
Retrieves the current url from the browser. The URL is fetched from the browser in another request asynchronously and passed to the callback. The URL is the full URL that the user sees in the browser (including hash #) and works even when client side routing is used or there is a reverse proxy between the client and the server.In case you need more control over the execution you can use
executeJs(String, Object...)by passingreturn window.location.href.NOTE: the URL is not escaped, use
URL.toURI()to escape it.- Parameters:
callback- to be notified when the url is resolved.
-
fetchPageDirection
Retrievesdocument.dirof the current UI from the browser and passes it to thecallbackparameter. If thedocument.dirhas not been set explicitly, thenDirection.LEFT_TO_RIGHTwill be the fallback value.Note that direction is fetched from the browser in an asynchronous request and passed to the callback.
In case you need more control over the execution you can use
executeJs(String, Object...)by passingreturn document.dir.- Parameters:
callback- to be notified when the direction is resolved.
-
requestFullscreen
public void requestFullscreen()Requests that the browser display the entire page in fullscreen mode.This calls
document.documentElement.requestFullscreen()which fullscreens the entire page. Themes and overlay components (such as Notification and ComboBox popups) work correctly in this mode.Note that browsers require transient user activation (e.g., a button click) to enter fullscreen mode. Calling this method from a server push or view constructor will likely not work.
- See Also:
-
exitFullscreen
public void exitFullscreen()Exits fullscreen mode if the page is currently in fullscreen.This calls
document.exitFullscreen()on the browser. If a component was previously fullscreened viaComponent.requestFullscreen(), it will be automatically restored to its original position.- See Also:
-
isFullscreenEnabled
Checks if the browser supports fullscreen mode.Returns a
PendingJavaScriptResultthat resolves to a boolean. Use it like:page.isFullscreenEnabled().then(Boolean.class, enabled -> { if (enabled) { page.requestFullscreen(); } });- Returns:
- a pending result that resolves to
trueif fullscreen is supported
-
addFullscreenChangeListener
Adds a listener that is notified when the fullscreen state changes.The listener is called when the page enters or exits fullscreen mode, whether triggered programmatically or by the user (e.g., pressing Escape).
- Parameters:
listener- the listener to add, notnull- Returns:
- a registration object for removing the listener
- See Also:
-
executeJs(String, Object...)instead.