Class Idle
Track user activity/inactivity changes. There are two ways to react to user inactivity: Change the CSS styles or receive notification when user becomes inactive. Both can be used at the same time.
When user becomes inactive a style userinactive is application
to BODY element. When user comes back active this style is
replaced with classname useractive.
Furthermore, if listeners have been attached using
addUserActiveListener(Idle.UserActiveListener) and addUserInactiveListener(Idle.UserInactiveListener), they
will be invoked.
User activity is tracked by following the mouse and keyboard events on the browser window.
The inactivity timeout period can be specified using the
setTimeout(long) method.
- Author:
- Sami Ekblad
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBasic Idle add-on eventstatic classUser active event.static interfaceListener interface for user activity status changes to active.static classUser inactive event.static interfaceListener interface for user activity status changes to inactive. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncom.vaadin.flow.shared.RegistrationaddUserActiveListener(Idle.UserActiveListener listener) Adds the user active listener.com.vaadin.flow.shared.RegistrationAdds the user inactive listener.protected voidcheckNotTracked(com.vaadin.flow.component.UI ui) Checks if the given UI is already monitored by Idle.protected voidFires anIdle.UserActiveEvent.protected voidFires anIdle.UserInactiveEvent.static Idleget(com.vaadin.flow.component.UI ui) Gets the Idle instance a monitored UI.longUser inactivity timeout in milliseconds.booleanDo we apply css class when user becomes active/inactive.protected voidloadResources(com.vaadin.flow.component.UI ui) Load the necessary JS and CSS files.voidsetTimeout(long timeout) User inactivity timeout in milliseconds.voidsetUpdateCss(boolean updateCssClass) Apply css class to body when user becomes active/inactive.static Idletrack(com.vaadin.flow.component.UI ui) Create new user activity tracker for UI with default timeout and no server-side listener.static Idletrack(com.vaadin.flow.component.UI ui, long timeoutMs) Create new user activity tracker for UI with timeout.static Idletrack(com.vaadin.flow.component.UI ui, long timeoutMs, boolean updateCssClass) Create new user activity tracker for UI with timeout.protected voidtrackUI(com.vaadin.flow.component.UI ui, long timeout) This is an internal method for tracking the UI.voidStop tracking the UI.
-
Field Details
-
DEFAULT_TIMEOUT
public static final long DEFAULT_TIMEOUT- See Also:
-
-
Constructor Details
-
Idle
Creates a new Idle instance.- Parameters:
ui- UI instance to monitor- Throws:
IllegalArgumentException- If the UI is already monitored by Idle
-
Idle
Creates a new Idle instance.- Parameters:
ui- UI instance to monitortimeoutMs- Inactivity timeout in milliseconds- Throws:
IllegalArgumentException- If the UI is already monitored by Idle
-
Idle
public Idle(com.vaadin.flow.component.UI ui, long timeoutMs, boolean updateCssClass) throws IllegalArgumentException Creates a new Idle instance.- Parameters:
ui- UI instance to monitortimeoutMs- Inactivity timeout in millisecondsupdateCssClass- CSS styles are updated when user becomes active/inactive.- Throws:
IllegalArgumentException- If the UI is already monitored by Idle
-
-
Method Details
-
loadResources
protected void loadResources(com.vaadin.flow.component.UI ui) Load the necessary JS and CSS files. If you need to customize the resources, you can override this method. -
get
Gets the Idle instance a monitored UI.- Parameters:
ui- A monitored UI instance- Returns:
- Idle instance of the UI or
null, if the UI is not monitored.
-
track
Create new user activity tracker for UI with default timeout and no server-side listener.- Parameters:
ui- UI instance to monitor- Returns:
- Idle Created instance
- Throws:
IllegalArgumentException- If the UI is already monitored by Idle
-
track
public static Idle track(com.vaadin.flow.component.UI ui, long timeoutMs) throws IllegalArgumentException Create new user activity tracker for UI with timeout.- Parameters:
ui- UI instance to monitortimeoutMs- Inactivity timeout in milliseconds- Returns:
- Idle Created instance
- Throws:
IllegalArgumentException- If the UI is already monitored by Idle
-
track
public static Idle track(com.vaadin.flow.component.UI ui, long timeoutMs, boolean updateCssClass) throws IllegalArgumentException Create new user activity tracker for UI with timeout.- Parameters:
ui- UI instance to monitortimeoutMs- Inactivity timeout in millisecondsupdateCssClass- CSS styles are updated when user becomes active/inactive.- Returns:
- Idle Created instance
- Throws:
IllegalArgumentException- If the UI is already monitored by Idle
-
trackUI
protected void trackUI(com.vaadin.flow.component.UI ui, long timeout) This is an internal method for tracking the UI.- Parameters:
ui- UI instance to tracktimeout- Inactivity timeout in milliseconds
-
checkNotTracked
Checks if the given UI is already monitored by Idle.- Parameters:
ui- UI instance to check- Throws:
IllegalArgumentException- If the UI is already monitored by Idle
-
getTimeout
public long getTimeout()User inactivity timeout in milliseconds.- Returns:
- Current timeout in milliseconds.
-
setTimeout
public void setTimeout(long timeout) User inactivity timeout in milliseconds.This is the time that needs to pass before user is considered inactive. Default timeout is 5000ms (5 seconds).
- Parameters:
timeout- New timeout.
-
isUpdateCss
public boolean isUpdateCss()Do we apply css class when user becomes active/inactive.- Returns:
- If true, the CSS styles are updated when user becomes active/inactive.
-
setUpdateCss
public void setUpdateCss(boolean updateCssClass) Apply css class to body when user becomes active/inactive.- Parameters:
updateCssClass- If true, the CSS styles are updated when user becomes
-
fireUserActive
protected void fireUserActive()Fires anIdle.UserActiveEvent. -
addUserActiveListener
Adds the user active listener.- Parameters:
listener- the Listener to be added.- Returns:
- A registration object for removing the listener.
- See Also:
-
fireUserInactive
protected void fireUserInactive()Fires anIdle.UserInactiveEvent. -
addUserInactiveListener
public com.vaadin.flow.shared.Registration addUserInactiveListener(Idle.UserInactiveListener listener) Adds the user inactive listener.- Parameters:
listener- The Listener to be added.- Returns:
- A registration object for removing the listener.
- See Also:
-
untrackUI
public void untrackUI()Stop tracking the UI.
-