Skip to main content

cranpose_app_shell/
dev_options.rs

1use cranpose_render_common::Renderer;
2use cranpose_ui::request_render_invalidation;
3
4use crate::AppShell;
5
6/// How the shell paces its frames against the display.
7#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
8pub enum FramePacingMode {
9    /// Pace frames to the display refresh interval. The production default:
10    /// animations advance once per vsync instead of re-rendering uncapped.
11    #[default]
12    Vsync,
13    /// Cap frames at 60 per second with a timer, whatever the display refresh rate is.
14    Hard60,
15    /// Cap frames at 120 per second with a timer, whatever the display refresh rate is.
16    Hard120,
17    /// Render as fast as possible. For perf harnesses and robot drivers that
18    /// measure work throughput; saturates the GPU if used in a real app.
19    NoVsync,
20}
21
22impl FramePacingMode {
23    /// Every pacing mode, in the order the dev overlay lists them.
24    pub const ALL: [Self; 4] = [Self::Vsync, Self::Hard60, Self::Hard120, Self::NoVsync];
25
26    /// Returns the short name the dev overlay shows for this mode.
27    pub fn label(self) -> &'static str {
28        match self {
29            Self::Vsync => "VSync",
30            Self::Hard60 => "60fps",
31            Self::Hard120 => "120fps",
32            Self::NoVsync => "NoVSync",
33        }
34    }
35
36    /// Returns the frames per second this mode caps at with a timer, or `None` when the mode sets
37    /// no cap.
38    pub fn target_fps(self) -> Option<u32> {
39        match self {
40            Self::Hard60 => Some(60),
41            Self::Hard120 => Some(120),
42            Self::Vsync | Self::NoVsync => None,
43        }
44    }
45}
46
47/// Development options for debugging and performance monitoring.
48///
49/// These are rendered directly by the renderer (not via composition)
50/// to avoid affecting performance measurements.
51#[derive(Clone, Debug, Default, PartialEq)]
52pub struct DevOptions {
53    /// Show FPS counter overlay
54    pub fps_counter: bool,
55    /// Show recomposition count
56    pub recomposition_counter: bool,
57    /// Show layout timing breakdown
58    pub layout_timing: bool,
59    /// Show buttons in the dev overlay that switch the frame pacing mode.
60    pub frame_pacing_controls: bool,
61    /// How the shell paces its frames.
62    pub frame_pacing_mode: FramePacingMode,
63}
64
65#[expect(clippy::multiple_inherent_impl, reason = "one file per shell concern")]
66impl<R: Renderer> AppShell<R>
67where
68    R::Error: std::fmt::Debug,
69{
70    /// Set development options for debugging and performance monitoring.
71    ///
72    /// The FPS counter and other overlays are rendered directly by the renderer
73    /// (not via composition) to avoid affecting performance measurements.
74    pub fn set_dev_options(&mut self, options: DevOptions) {
75        if self.app.dev_options == options {
76            return;
77        }
78        self.app.dev_options = options;
79        self.invalidate_dev_overlay_text();
80        self.app.app_context.enter(request_render_invalidation);
81        self.mark_all_dirty();
82    }
83
84    /// Get a reference to the current dev options.
85    pub fn dev_options(&self) -> &DevOptions {
86        &self.app.dev_options
87    }
88
89    /// Returns the frame pacing mode set in the dev options.
90    pub fn frame_pacing_mode(&self) -> FramePacingMode {
91        self.app.dev_options.frame_pacing_mode
92    }
93
94    /// Sets the frame pacing mode and redraws the dev overlay. Platforms read the mode back with
95    /// `frame_pacing_mode`.
96    pub fn set_frame_pacing_mode(&mut self, mode: FramePacingMode) {
97        if self.app.dev_options.frame_pacing_mode == mode {
98            return;
99        }
100        self.app.dev_options.frame_pacing_mode = mode;
101        self.invalidate_dev_overlay_text();
102        self.app.app_context.enter(request_render_invalidation);
103        self.mark_all_dirty();
104    }
105}