1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! Frame rate statistics hook
//!
//! This module provides the `use_frame_rate` hook for accessing
//! frame rate statistics from within components.
use crate::renderer::FrameRateStats;
/// Hook to access frame rate statistics.
///
/// Returns `None` if frame rate statistics collection is not enabled.
/// Enable it with `.collect_frame_stats()` on the `AppBuilder`.
///
/// # Example
///
/// ```ignore
/// use rnk::prelude::*;
///
/// fn my_component() -> Element {
/// if let Some(stats) = use_frame_rate() {
/// Text::new(format!("FPS: {:.1}", stats.current_fps)).into_element()
/// } else {
/// Text::new("Stats not enabled").into_element()
/// }
/// }
///
/// // Enable stats collection
/// render(my_component)
/// .collect_frame_stats()
/// .run()?;
/// ```
pub fn use_frame_rate() -> Option<FrameRateStats> {
// Get from RuntimeContext
if let Some(ctx) = crate::runtime::current_runtime() {
let borrowed = ctx.borrow();
if let Some(stats) = borrowed.frame_rate_stats() {
return Some(stats.snapshot());
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_use_frame_rate_returns_none_when_not_set() {
// Clear any existing runtime
crate::runtime::set_current_runtime(None);
assert!(use_frame_rate().is_none());
}
#[test]
fn test_use_frame_rate_with_runtime() {
use crate::renderer::SharedFrameRateStats;
use crate::runtime::{RuntimeContext, with_runtime};
use std::cell::RefCell;
use std::rc::Rc;
let stats = SharedFrameRateStats::new();
let ctx = Rc::new(RefCell::new(RuntimeContext::new()));
ctx.borrow_mut().set_frame_rate_stats(Some(stats));
with_runtime(ctx.clone(), || {
let result = use_frame_rate();
assert!(result.is_some());
});
}
}