Skip to main content

CLOCK_CARD

Constant CLOCK_CARD 

Source
pub const CLOCK_CARD: &str = "//! A sidebar card: the time since the viewer started and a bar of the\n//! seconds. It repaints once a second and only while the card is visible.\n//!\n//! Manifest (`bundle/standard-plugin.json`):\n//!\n//! ```json\n//! {\n//!   \"apiVersion\": 2, \"kind\": \"ui\", \"id\": \"clock-card\", \"version\": \"0.1.0\",\n//!   \"module\": \"clock_card.wasm\",\n//!   \"surfaces\": [{ \"id\": \"card\", \"anchor\": \"sidebar.card\", \"model\": \"cells\", \"height\": 2 }],\n//!   \"grants\": {}\n//! }\n//! ```\n#![no_std]\n\nuse standard_plugin::prelude::*;\n\n#[standard_plugin::ui]\nstruct ClockCard {\n    card: Surface<Cells>,\n    shown: Option<u64>,\n}\n\nimpl UiPlugin for ClockCard {\n    fn activate(_cx: &mut Context) -> Self {\n        Self {\n            card: Surface::new(\"card\"),\n            shown: None,\n        }\n    }\n\n    fn frame(&mut self, frame: &Frame) {\n        let second = frame.now_ms() / 1000;\n        if self.shown != Some(second) {\n            self.shown = Some(second);\n            let (cols, _) = self.card.size();\n            let time = format!(\n                \"{:02}:{:02}:{:02}\",\n                second / 3600 % 24,\n                second / 60 % 60,\n                second % 60\n            );\n            self.card.text(0, 0, &time, Style::fg(Colour::FG).bold());\n            let fraction = (second % 60) as f32 / 60.0;\n            let track = Style::fg(Colour::RECEDE_FG);\n            self.card.bar(\n                Area::new(0, 1, cols, 1),\n                fraction,\n                Style::fg(Colour::ACCENT),\n                track,\n            );\n            // Publishes only the cells that changed.\n            let _ = self.card.commit();\n        }\n        // The next second boundary: an idle viewer wakes once a second.\n        frame.wake_at((second + 1) * 1000);\n    }\n\n    fn event(&mut self, event: Event, _cx: &mut Context) {\n        if let Event::Resize { .. } = event {\n            // The card has a new, empty buffer: repaint on the next frame.\n            self.shown = None;\n        }\n    }\n}\n";
Expand description

A cell-model sidebar card (examples/clock_card.rs).