aylis_core/
memory_panel.rs1use std::sync::OnceLock;
10
11use crate::{Brush, Canvas, Color, FontSpec, RectF, Stroke, TextAlign, TextStyle, Ui};
12
13#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
15pub struct ProcessMemory {
16 pub private: usize,
18 pub working_set: usize,
20}
21
22static HEAP: OnceLock<fn() -> usize> = OnceLock::new();
23
24pub fn set_heap_counter(counter: fn() -> usize) {
26 let _ = HEAP.set(counter);
27}
28
29pub fn heap() -> Option<usize> {
31 HEAP.get().map(|f| f())
32}
33
34pub fn format_bytes(bytes: usize) -> String {
36 let mb = bytes as f64 / 1_048_576.0;
37 if mb >= 1.0 { format!("{mb:.1} MB").replace('.', ",") } else { format!("{} KB", bytes / 1024) }
38}
39
40#[derive(Clone, Copy, Debug, Default, PartialEq)]
43pub struct Frames {
44 pub fps: f32,
45 pub max_fps: f32,
46}
47
48pub fn lines(frames: Option<Frames>, process: Option<ProcessMemory>) -> Vec<String> {
50 let mut out = Vec::new();
51 if let Some(f) = frames {
52 let limit = if f.max_fps > 0.0 { format!(" (limite {:.0})", f.max_fps) } else { String::new() };
54 out.push(format!("Quadros: {:.0} por segundo{limit}", f.fps));
55 }
56 match process {
57 Some(p) => out.push(format!("Processo: {} comprometida · {} working set", format_bytes(p.private), format_bytes(p.working_set))),
58 None => out.push("Processo: — (esta plataforma não informa)".into()),
59 }
60 if let Some(h) = heap() {
61 out.push(format!("Heap do Rust: {}", format_bytes(h)));
62 }
63 let stats = crate::image_cache::stats();
64 let (images, bytes) = stats.iter().fold((0, 0), |(i, b), s| (i + s.images, b + s.bytes));
65 out.push(format!("Imagens no cache: {images} · {}", format_bytes(bytes)));
66 for s in &stats {
67 out.push(format!(" {}: {} · {}", s.name, s.images, format_bytes(s.bytes)));
68 }
69 out
70}
71
72pub fn paint(canvas: &mut dyn Canvas, form: RectF, frames: Option<Frames>, process: Option<ProcessMemory>) {
74 let lines = lines(frames, process);
75 let style = TextStyle { font: FontSpec { size: 11.0, ..FontSpec::default() }, color: Color::WHITE, v_align: TextAlign::Center, ..TextStyle::default() };
76 let line_h = 16.0;
77 let width = lines.iter().map(|l| canvas.measure_text(l, &style).width).fold(0.0f32, f32::max) + 20.0;
78 let height = line_h * lines.len() as f32 + 12.0;
79 let r = RectF::new(form.right() - width - 8.0, form.y + 8.0, width, height);
80 canvas.fill_rect(r, 6.0, &Brush::Solid(Color::rgba(0x10, 0x12, 0x16, 0xE0)));
81 canvas.stroke_rect(r, 6.0, &Stroke::new(Color::WHITE.with_opacity(0.15), 1.0));
82 for (i, line) in lines.iter().enumerate() {
83 let muted = line.starts_with(" ");
84 let s = TextStyle { color: if muted { Color::WHITE.with_opacity(0.65) } else { Color::WHITE }, ..style.clone() };
85 canvas.draw_text(line, RectF::new(r.x + 10.0, r.y + 6.0 + line_h * i as f32, width - 20.0, line_h), &s);
86 }
87}
88
89impl Ui {
90 pub fn set_memory_panel(&mut self, panel: Option<bool>) {
93 self.memory_panel = panel;
94 self.request_repaint();
95 }
96
97 pub fn memory_panel_visible(&self) -> bool {
99 self.memory_panel == Some(true)
100 }
101
102 pub fn toggle_memory_panel(&mut self) -> bool {
105 let Some(visible) = self.memory_panel else { return false };
106 self.memory_panel = Some(!visible);
107 self.invalidate();
108 true
109 }
110}
111
112#[cfg(test)]
113mod tests {
114 use super::*;
115
116 #[test]
117 fn bytes_read_in_portuguese() {
118 assert_eq!(format_bytes(52 * 1_048_576 + 300 * 1024), "52,3 MB");
119 assert_eq!(format_bytes(812 * 1024), "812 KB");
120 }
121}