AkurAI Build
Menu

Public repository / a0501370bec01f30f21f48fbebd95284b81aca77

AkurAI-Framework / crates/ui/src/platform.rs

Latest change 3aae9f4e85241bcb775c31f5c03094becd3da56a - Pure-Rust UI layer: akurai-ui, akurai-crypto, Tailwind-grade akurai-css by Ólafur Búi Ólafsson

//! Small platform services: timers, clock, storage, document.

use crate::callbacks;
use crate::host;

/// Run `f` once after `ms` milliseconds.
pub fn set_timeout(ms: u32, f: impl FnOnce() + 'static) {
    let mut f = Some(f);
    let mut run = crate::dom::in_scope(crate::reactive::current_scope(), move |()| {
        if let Some(f) = f.take() {
            f()
        }
    });
    let id = callbacks::once(move |_, _| run(()));
    host::set_timeout(ms, id);
}

/// Milliseconds since the Unix epoch.
pub fn now_ms() -> f64 {
    host::now_ms()
}

/// The browser's UTC offset in minutes (east positive; Iceland is 0).
pub fn tz_offset_minutes() -> i32 {
    host::tz_offset_minutes()
}

/// `localStorage` read (`None` when missing or unavailable).
pub fn storage_get(key: &str) -> Option<String> {
    host::storage_get(key)
}

/// `localStorage` write (silently ignored when unavailable).
pub fn storage_set(key: &str, value: &str) {
    host::storage_set(key, value)
}

/// Set `document.title`.
pub fn set_title(title: &str) {
    host::set_title(title)
}

/// Set an attribute on `<html>` (e.g. `data-theme`).
pub fn set_root_attr(name: &str, value: &str) {
    host::set_root_attr(name, value)
}

/// `console.log` (stderr off-browser).
pub fn log(message: &str) {
    host::log(message)
}

/// Focus the first focusable element inside `node`.
pub fn focus_first(node: host::Node) -> bool {
    host::focus_first(node)
}

/// The focused element, if it has a handle.
pub fn active_element() -> Option<host::Node> {
    host::active_element()
}

/// Focus `node`.
pub fn focus(node: host::Node) {
    host::focus(node)
}

/// Whether `node` is `ancestor` or inside it.
pub fn contains(ancestor: host::Node, node: host::Node) -> bool {
    host::contains(ancestor, node)
}

/// Read a DOM property as text (e.g. an input's current `value`).
pub fn prop(node: host::Node, name: &str) -> String {
    host::get_prop_str(node, name)
}