dfui 0.2.0

Declarative UI framework that draws shapes, text and icons analytically in one wgpu shader
Documentation
//! Declarative UI framework that draws shapes, text and icons analytically in one wgpu shader.

// Proving wgpu's `Device` and `Queue` are `Sync` for `gpu()` recurses past the default.
#![recursion_limit = "256"]

pub mod anim;
pub mod icons;
pub mod kinetic;
mod layout;
pub mod model;
mod outline;
pub mod path;
mod render;
mod runtime;
pub mod source;
mod text;
mod theme;
mod widget;
pub mod widgets;

pub use anim::Animated;
pub use path::{Path, Shape};
#[cfg(not(target_arch = "wasm32"))]
pub use render::gpu;
pub use render::{Bitmap, Color, Pixels, Rect, Shadow, hdr};
pub use runtime::task::{Notifier, Sleep, sleep};
pub use source::{Cache, Latest, cached, polled};
#[cfg(not(target_arch = "wasm32"))]
pub use source::{Wanted, blocking};
pub use wgpu;
// Hooks for the benches and golden tests, outside the semver guarantee.
#[doc(hidden)]
pub use runtime::{Bench, GpuBench, insets, snapshot};
/// Layout styles. Its version is part of dfui's: a taffy upgrade is a breaking change.
pub use taffy;
pub use text::{Font, Text, TextLayout};
pub use theme::{Ink, Role, Theme};
pub use web_time::Instant;
pub use widget::{
    BuildCx, Element, Event, EventCx, Focus, Handle, Key, Keyboard, Layer, OnChange, PaintCx,
    Placement, Scope, State, Widget,
};

// Compiles the README's examples as doc tests without publishing it as docs.
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
struct ReadmeDoctests;

#[cfg(doctest)]
#[doc = include_str!("../docs/icons.md")]
struct IconsDoctests;

/// Root of the UI. State lives in `stateful` widgets and in external sources
/// that builders read; `view` runs again whenever any of it changes.
pub trait App: 'static {
    /// Builds the whole UI. Runs on every rebuild, so keep it to building
    /// elements; parse assets and start work elsewhere.
    fn view(&self) -> Element;
    /// Colors for the whole app, read every frame.
    fn theme(&self) -> Theme {
        Theme::default()
    }
    /// Window background, read every frame.
    fn background(&self) -> Color {
        self.theme().background
    }
    /// Desktop window title, read when the window opens.
    fn title(&self) -> String {
        "dfui".into()
    }
    /// Opens the desktop window borderless fullscreen, e.g. for a TV.
    fn fullscreen(&self) -> bool {
        false
    }
    /// Multiplies the window's scale factor, so everything grows or shrinks,
    /// e.g. for a user's size setting. Read every frame.
    fn zoom(&self) -> f32 {
        1.0
    }
    /// Shows bitmaps' values above 1.0 (from [`Bitmap::from_texture`] with
    /// `Rgba16Float`) brighter than SDR white, on screens that take HDR10.
    /// [`hdr()`] tells whether that's on. Read every frame.
    fn hdr(&self) -> bool {
        false
    }
}

/// A function returning the view is an app with the default theme:
/// `dfui::run(view)`.
impl<F: Fn() -> Element + 'static> App for F {
    fn view(&self) -> Element {
        self()
    }
}

/// Opens a window showing `app` and runs until it's closed. On the web it
/// returns at once and the browser drives the app; on Android use `run_android`.
pub fn run<A: App>(app: A) {
    // Without it a panic in wasm only reports "unreachable executed".
    #[cfg(target_arch = "wasm32")]
    std::panic::set_hook(Box::new(|p| {
        web_sys::console::error_1(&p.to_string().into())
    }));
    let event_loop = winit::event_loop::EventLoop::new()
        .unwrap_or_else(|e| runtime::startup_failed(&format!("can't start the event loop: {e}")));
    runtime::Runtime::new(app).run(event_loop);
}

#[cfg(target_os = "android")]
pub use runtime::android;
#[cfg(target_os = "android")]
pub use winit::platform::android::activity::AndroidApp;

/// Runs `app` until the activity is destroyed; call it from `android_main`.
/// The process may outlive the activity (a service, a download) and start
/// `android_main` again for a new one.
#[cfg(target_os = "android")]
pub fn run_android<A: App>(app: A, android: AndroidApp) {
    runtime::activity::run(runtime::Runtime::new(app), android);
}