//! Apps built from views. An app describes its interface with Mithril — //! elements, the component library's classes, signals for what changes — //! and a [`Screen`] styles it with the library's stylesheet, lays it out for //! the window's size, draws it into the window's pixels and turns the //! window's input into the view's events. Only what changed is drawn again, //! and only that area is sent to the display (`present`). //! //! It replaces, app by app, the hand-placed rectangles of `pinnacle_layout` //! and the immediate drawing of `pinnacle_ui::theme`: an interface that is //! responsive because the stylesheet is (flex, wrapping, the shell's //! container widths), and the same library the web and Cartopolis use. #![no_std] extern crate alloc; use alloc::string::String; use alloc::vec::Vec; use mithril_native::{Font as Spec, Fonts, Item, Modifiers, Press, Ui}; use mithril_style::cascade::Rgba; use pinnacle_gfx::font::Face; use pinnacle_gfx::{Canvas, Color, Font, Rect}; use pinnacle_rt::abi::InputEvent; use pinnacle_ui::access; use pinnacle_ui::keyboard::{Key, Keyboard}; pub use mithril_native as native; pub use mithril_signals::{Computed, Effect, Signal, batch, computed, effect, signal}; pub use mithril_style::{Tokens, css}; /// An app's own rules, written with [`css!`], for [`Screen::with_rules`]. pub type Rules = Vec; pub use mithril_view::{Event, IntoView, View, el, fragment}; /// Named controls and their centres, in window pixels. type Places = Vec<(String, i32, i32)>; /// Logical pixels to the window's: the library's sheet is drawn for a /// 15 px body, the system's text is 20 px. pub const SCALE: f32 = 4.0 / 3.0; /// The library's tokens in the system's colours (`pinnacle_ui::theme`): /// one dark look, the same in both of the sheet's themes. The ground is /// the app icon's (`docs/brand/`), the accent the mark's orange, focus its /// gold. pub fn tokens() -> Tokens { let mut t = normal_tokens(); if pinnacle_ui::access::current().high_contrast { // High contrast: a black ground, white text, dim text that is // still bright, borders that show, the accent lighter. let same = mithril_style::Pair::same; t.bg = same("#000000"); t.surface = same("#000000"); t.surface_raised = same("#0d0d0d"); t.surface_sunken = same("#000000"); t.control_bg = same("#000000"); t.hover = same("#1f1f1f"); t.pressed = same("#333333"); t.text = same("#ffffff"); t.text_strong = same("#ffffff"); t.text_dim = same("#e0e0e0"); t.text_faint = same("#c8c8c8"); t.border = same("#ffffff"); t.border_strong = same("#ffffff"); t.accent = same("#ff7a33"); t.accent_hover = same("#ff9a5c"); t.accent_text = same("#ffb27f"); t.accent_soft = same("rgb(255 122 51 / 28%)"); t.on_accent = same("#000000"); t.focus = same("#ffd75e"); } t } /// The tokens in the system's ordinary colours. fn normal_tokens() -> Tokens { use mithril_style::Pair; let mut t = Tokens::garage44(); let same = Pair::same; t.bg = same("#0f1416"); t.surface = same("#192023"); t.surface_raised = same("#21292d"); t.surface_sunken = same("#0a0e10"); t.control_bg = same("#141a1d"); t.hover = same("#222b2f"); t.pressed = same("#2a3439"); t.text = same("#eceeec"); t.text_strong = same("#ffffff"); t.text_dim = same("#8f9a9b"); t.text_faint = same("#5f6a6c"); t.border = same("#2a3439"); t.border_strong = same("#3a464b"); t.accent = same("#e85a12"); t.accent_hover = same("#f56f24"); t.accent_text = same("#ff9a5c"); t.accent_soft = same("rgb(232 90 18 / 16%)"); t.on_accent = same("#ffffff"); t.focus = same("#f0c46a"); t.success = same("#3fb97a"); t.danger = same("#e56b6b"); t } /// The system face at the sizes asked for so far. struct Faces { face: &'static Face<'static>, /// The fixed-width face, for `font-family: monospace`. mono: &'static Face<'static>, /// By size (quarter pixels) and whether fixed-width. sizes: Vec<((i32, bool), Font)>, } impl Faces { fn new() -> Faces { Faces { face: pinnacle_ui::theme::body().face(), mono: pinnacle_ui::theme::mono().face(), sizes: Vec::new(), } } /// The face for `family` at `px` window pixels, to the quarter pixel. fn at(&mut self, px: f32, family: &str) -> &Font { let mono = family.contains("mono"); let key = ((px * 4.0 + 0.5) as i32, mono); let i = match self.sizes.binary_search_by_key(&key, |(k, _)| *k) { Ok(i) => i, Err(i) => { let face = if mono { self.mono } else { self.face }; self.sizes.insert(i, (key, Font::new(face, key.0 as f32 / 4.0))); i } }; &self.sizes[i].1 } } /// Text widths for the layout, in logical pixels. struct Measure<'a> { faces: &'a mut Faces, scale: f32, } impl Fonts for Measure<'_> { fn width(&mut self, font: &Spec<'_>, text: &str) -> f32 { let f = self.faces.at(font.size * self.scale, font.family); let bold = embolden(font.weight, f.size); (f.measure(text) + bold) as f32 / self.scale } } /// An interface in a window, and the on-screen keyboard along its bottom /// while a text field has the focus: the interface is laid out above it, /// and scrolled so the field stays in view. A physical keyboard's first key /// sends it away, as in the apps drawn by hand (`pinnacle_ui::keyboard`). pub struct Screen { ui: Ui, /// The app's own rules ([`Screen::with_rules`]), kept for /// [`Screen::restyle`]. rules: Rules, faces: Faces, keyboard: Keyboard, scale: f32, width: i32, height: i32, /// The on-screen keyboard is up. typing: bool, /// The keyboard came or went: everything is drawn afresh, and the /// focused field brought into view. relaid: bool, /// A key of the on-screen keyboard changed (pressed, shift): it is /// drawn again. keys_changed: bool, /// A clipboard key was typed (ctrl+V, ctrl+C): for the app to act on. clipboard: Option, /// What [`Screen::announce`] said last: the prefix, and each named /// control's place. Said again where a place moved (a scroll, a list /// that grew), once no finger is down. announced: Option<(String, Places)>, /// A finger or a button is down. pressing: bool, } /// What a clipboard key asked of the app. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Clipboard { /// Ctrl+V: ask the display for what is on the clipboard /// (`App::paste`), and give the answer to [`Screen::paste`]. Paste, /// Ctrl+C: put what the app takes to be meant on it (`App::copy`). Copy, } /// A focused field: one line, several (`