Skip to main content

leptos_use_idle/
options.rs

1use crate::{DebounceOptions, FilterOptions, ThrottleOptions, filter_builder_methods};
2use default_struct_builder::DefaultBuilder;
3use leptos::reactive::wrappers::read::Signal;
4
5/// Options for [`use_idle_with_options`](crate::use_idle_with_options).
6#[derive(DefaultBuilder)]
7pub struct UseIdleOptions {
8    /// Event names to listen to on `window` for detected user activity.
9    /// Default: `vec!["mousemove", "mousedown", "resize", "keydown", "touchstart", "wheel"]`.
10    pub(crate) events: Vec<String>,
11
12    /// Whether to listen for document visibility change.
13    /// Defaults to `true`.
14    pub(crate) listen_for_visibility_change: bool,
15
16    /// Initial state of the returned `idle`.
17    /// Defaults to `false`.
18    pub(crate) initial_state: bool,
19
20    /// Allows to debounce or throttle the event listener that is called for
21    /// every event (from `events`). Defaults to a throttle by 50ms.
22    pub(crate) filter: FilterOptions,
23}
24
25impl Default for UseIdleOptions {
26    fn default() -> Self {
27        Self {
28            events: vec![
29                "mousemove".to_string(),
30                "mousedown".to_string(),
31                "resize".to_string(),
32                "keydown".to_string(),
33                "touchstart".to_string(),
34                "wheel".to_string(),
35            ],
36            listen_for_visibility_change: true,
37            initial_state: false,
38            filter: FilterOptions::throttle(50.0),
39        }
40    }
41}
42
43impl UseIdleOptions {
44    filter_builder_methods!(
45        /// the event listener
46        filter
47    );
48}