#timer #dioxus #duration #timer-state

dioxus-timer

Simple timer that works with dioxus

5 releases (3 breaking)

new 0.4.1 Dec 18, 2024
0.4.0 Dec 18, 2024
0.3.0 Jun 14, 2024
0.2.0 Feb 7, 2024
0.1.0 Feb 6, 2024

#284 in GUI

Download history 9/week @ 2024-09-23 1/week @ 2024-10-07 27/week @ 2024-10-28 97/week @ 2024-11-04 14/week @ 2024-11-11 43/week @ 2024-11-18 13/week @ 2024-12-09

56 downloads per month

MIT license

14KB
234 lines

dioxus_timer

Simple timer that works with dioxus.
Provide use_timer.

Setup

Add the following crate

cargo add dioxus-timer

Dioxus support table

dioxus dioxus-timer
^0.6 0.4
^0.5 0.3
^0.4 0.2

Example

use dioxus::prelude::*;
use dioxus_timer::{use_timer, DioxusTimer, TimerState};
use std::time::Duration;

fn main() {
    dioxus::launch(App);
}

#[component]
fn App() -> Element {
    let timer = use_timer(Duration::from_millis(16));

    let state = timer.read().state();
    use_effect(move || {
        if state == TimerState::Finished {
            println!("finished!");
        }
    });

    let time = timer.read().to_string();
    rsx! {
        div {
            h1 {"Timer"}
            p {"{time}"}
            p {"{state}"}
            TimerControll {timer}
            TimerSet {timer}
        }
    }
}

#[component]
fn TimerControll(timer: Signal<DioxusTimer>) -> Element {
    let start_handle = move |_| {
        timer.write().start();
    };
    let pause_handle = move |_| {
        timer.write().pause();
    };
    let reset_handle = move |_| {
        timer.write().reset();
    };

    let controller = match timer.read().state() {
        TimerState::Inactive => rsx! {
            button { onclick: start_handle, "Start" }
            button { onclick: reset_handle, "Reset" }
        },
        TimerState::Working => rsx! {
            button { onclick: pause_handle, "Pause" }
            button { onclick: reset_handle, "Reset" }
        },
        TimerState::Paused => rsx! {
            button { onclick: start_handle, "Resume" }
            button { onclick: reset_handle, "Reset" }
        },
        TimerState::Finished => rsx! {
            button { onclick: reset_handle, "Reset" }
        },
    };
    controller
}

#[component]
fn TimerSet(timer: Signal<DioxusTimer>) -> Element {
    let submit_handle = move |ev: Event<FormData>| {
        let values = ev.values();
        let hours = values["hours"].first().unwrap().parse::<u64>().unwrap();
        let minutes = values["minutes"].first().unwrap().parse::<u64>().unwrap();
        let seconds = values["seconds"].first().unwrap().parse::<u64>().unwrap();
        let preset_dur = Duration::from_secs(hours * 3600 + minutes * 60 + seconds);
        timer.write().set_preset_time(preset_dur);
    };

    rsx! {
        div {
            form {
                onsubmit: submit_handle,
                label { "hour:" }
                input { r#type: "number", name: "hours", value: "0", min: "0", max: "23" }
                label { "minutes:" }
                input { r#type: "number", name: "minutes", value: "0", min: "0", max: "59" }
                label { "seconds:" }
                input { r#type: "number", name: "seconds", value: "0", min: "0", max: "59" }
                button { r#type: "submit", "set" }
            }
        }
    }
}

Dependencies

~3–14MB
~198K SLoC