Skip to main content

blockworx_web/
frame_rate.rs

1//! The frame-rate readout, toggled from the ⌘K palette ("Diagnostics: frame
2//! rate"): frames in the last second, what the last frame spent in the kernel
3//! and on the canvas, and the slowest frame of the second.
4//!
5//! The editor draws only when something changes, so at rest the readout stands
6//! on the last frame it saw: it reads the rate of a drag or a pan, not of an
7//! idle page.
8
9use core::time::Duration;
10
11use blockworx_kernel::FrameRate;
12use dioxus::prelude::*;
13
14use crate::shell::Shell;
15
16fn millis(d: Duration) -> String {
17    format!("{:.1}", d.as_secs_f64() * 1000.0)
18}
19
20#[component]
21pub fn FrameRateBadge(shell: Shell) -> Element {
22    let chrome = shell.chrome();
23    if chrome.read().frame_rate != FrameRate::Shown {
24        return rsx! {};
25    }
26    let frames = shell.frames();
27    let Some(metered) = *frames.read() else {
28        return rsx! {};
29    };
30    rsx! {
31        div {
32            class: "pointer-events-none absolute left-4 top-4 z-30 rounded-lg bg-zinc-900/80 \
33                    px-2 py-1 font-mono text-xs tabular-nums text-zinc-100",
34            "{metered.frames} fps · kernel {millis(metered.kernel)} ms · canvas {millis(metered.paint)} ms · slowest {millis(metered.slowest)} ms"
35        }
36    }
37}