use std::sync::Arc;
pub use nsh_macros::HostProps;
use crate::{EdgeInsets, EventKind, element::TsTypes, geometry::Points, style::Color};
pub trait HostInput: Sized {
fn is_nullish(&self) -> bool;
fn is_object(&self) -> bool;
fn as_bool(&self) -> Option<bool>;
fn as_number(&self) -> Option<f64>;
fn as_string(&self) -> Option<String>;
fn field(&self, key: &str) -> Option<Self>;
fn items(&self) -> Vec<Self> {
match self.as_list() {
Some(list) => (0..list.len()).filter_map(|i| list.item(i)).collect(),
None => Vec::new(),
}
}
fn as_list(&self) -> Option<Self::List>;
type List: HostList<Item = Self>;
fn entries(&self) -> Vec<(String, Self)>;
fn truthy(&self) -> bool;
fn node(&self) -> Option<crate::NodeId> {
None
}
fn animated_node(&self) -> Option<crate::animation::NodeId> {
self.field("__av")
.and_then(|v| v.as_number())
.map(|n| n as crate::animation::NodeId)
}
}
pub trait HostList {
type Item;
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn item(&self, index: usize) -> Option<Self::Item>;
}
#[derive(Debug, Default)]
pub struct HostCx {
pub warnings: Vec<String>,
pub element: u32,
}
impl HostCx {
pub fn for_element(tag: crate::Tag) -> Self {
Self {
element: element_bits(tag),
..Self::default()
}
}
pub fn warn(&mut self, message: impl Into<String>) {
self.warnings.push(message.into());
}
pub fn on_element(&self, on: u32) -> bool {
self.element == 0 || self.element & on != 0
}
}
pub fn element_bits(tag: crate::Tag) -> u32 {
use crate::Tag;
on::ALL
| match tag {
Tag::Text => on::TEXT,
Tag::Image => on::IMAGE,
Tag::Scroll => on::SCROLLING | on::SCROLL,
Tag::List => on::SCROLLING | on::LIST,
Tag::Input => on::INPUT,
Tag::Screen => on::SCREEN,
Tag::Modal => on::MODAL,
Tag::WebView => on::WEBVIEW,
Tag::Video => on::VIDEO,
Tag::InputAccessory => on::INPUT_ACCESSORY,
Tag::View => on::VIEW,
Tag::Cell | Tag::Stack | Tag::HeaderItem | Tag::Canvas | Tag::Native | Tag::Overlay => {
0
}
}
}
pub trait HostValue: Sized {
fn decode<I: HostInput>(input: &I, cx: &mut HostCx) -> Option<Self>;
fn ts_type(types: &mut TsTypes) -> String;
}
pub mod on {
pub const ALL: u32 = 1;
pub const TEXT: u32 = 1 << 1;
pub const IMAGE: u32 = 1 << 2;
pub const SCROLLING: u32 = 1 << 3;
pub const SCROLL: u32 = 1 << 4;
pub const LIST: u32 = 1 << 5;
pub const INPUT: u32 = 1 << 6;
pub const SCREEN: u32 = 1 << 7;
pub const MODAL: u32 = 1 << 8;
pub const WEBVIEW: u32 = 1 << 9;
pub const VIDEO: u32 = 1 << 10;
pub const INPUT_ACCESSORY: u32 = 1 << 11;
pub const OVERLAY: u32 = 1 << 12;
pub const VIEW: u32 = 1 << 13;
pub const INTERNAL: u32 = 1 << 31;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Platform {
All,
Ios,
Android,
Desktop,
Web,
}
#[derive(Debug, Clone)]
pub struct HostPropSpec {
pub name: &'static str,
pub ts: String,
pub doc: &'static str,
pub on: u32,
pub platform: Platform,
}
pub trait HostProps: Sized {
fn decode<I: HostInput>(name: &str, input: &I, cx: &mut HostCx) -> Option<Option<Self>>;
fn reset(&self) -> Option<Self>;
fn same_kind(&self, other: &Self) -> bool;
fn specs(types: &mut TsTypes, out: &mut Vec<HostPropSpec>);
}
pub trait Clamped {
fn clamped(self, min: Option<f64>, max: Option<f64>) -> Self;
}
impl Clamped for f32 {
fn clamped(self, min: Option<f64>, max: Option<f64>) -> Self {
let v = min.map_or(self, |m| self.max(m as f32));
max.map_or(v, |m| v.min(m as f32))
}
}
impl Clamped for Points {
fn clamped(self, min: Option<f64>, max: Option<f64>) -> Self {
Points(self.0.clamped(min, max))
}
}
impl Clamped for u32 {
fn clamped(self, min: Option<f64>, max: Option<f64>) -> Self {
let v = min.map_or(self, |m| self.max(m as u32));
max.map_or(v, |m| v.min(m as u32))
}
}
impl<T: Clamped> Clamped for Option<T> {
fn clamped(self, min: Option<f64>, max: Option<f64>) -> Self {
self.map(|v| v.clamped(min, max))
}
}
impl HostValue for bool {
fn decode<I: HostInput>(input: &I, _: &mut HostCx) -> Option<Self> {
input.as_bool()
}
fn ts_type(_: &mut TsTypes) -> String {
"boolean".into()
}
}
impl HostValue for f32 {
fn decode<I: HostInput>(input: &I, _: &mut HostCx) -> Option<Self> {
input.as_number().map(|n| n as f32)
}
fn ts_type(_: &mut TsTypes) -> String {
"number".into()
}
}
impl HostValue for Points {
fn decode<I: HostInput>(input: &I, cx: &mut HostCx) -> Option<Self> {
f32::decode(input, cx).map(Points)
}
fn ts_type(_: &mut TsTypes) -> String {
"number".into()
}
}
impl HostValue for u32 {
fn decode<I: HostInput>(input: &I, _: &mut HostCx) -> Option<Self> {
input.as_number().map(|n| n.max(0.0) as u32)
}
fn ts_type(_: &mut TsTypes) -> String {
"number".into()
}
}
impl HostValue for String {
fn decode<I: HostInput>(input: &I, _: &mut HostCx) -> Option<Self> {
input.as_string()
}
fn ts_type(_: &mut TsTypes) -> String {
"string".into()
}
}
impl HostValue for Arc<str> {
fn decode<I: HostInput>(input: &I, _: &mut HostCx) -> Option<Self> {
input.as_string().filter(|s| !s.is_empty()).map(Into::into)
}
fn ts_type(_: &mut TsTypes) -> String {
"string".into()
}
}
impl HostValue for Box<str> {
fn decode<I: HostInput>(input: &I, _: &mut HostCx) -> Option<Self> {
input.as_string().filter(|s| !s.is_empty()).map(Into::into)
}
fn ts_type(_: &mut TsTypes) -> String {
"string".into()
}
}
impl HostValue for Color {
fn decode<I: HostInput>(input: &I, _: &mut HostCx) -> Option<Self> {
input.as_string().and_then(|s| Color::parse(&s))
}
fn ts_type(_: &mut TsTypes) -> String {
"ColorValue".into()
}
}
impl<T: HostValue> HostValue for Option<T> {
fn decode<I: HostInput>(input: &I, cx: &mut HostCx) -> Option<Self> {
Some(T::decode(input, cx))
}
fn ts_type(types: &mut TsTypes) -> String {
T::ts_type(types)
}
}
impl<T: HostValue> HostValue for Vec<T> {
fn decode<I: HostInput>(input: &I, cx: &mut HostCx) -> Option<Self> {
Some(
input
.items()
.iter()
.filter_map(|i| T::decode(i, cx))
.collect(),
)
}
fn ts_type(types: &mut TsTypes) -> String {
format!("{}[]", T::ts_type(types))
}
}
impl HostValue for EdgeInsets {
fn decode<I: HostInput>(input: &I, _: &mut HostCx) -> Option<Self> {
let side = |key| Points(input.field(key).and_then(|v| v.as_number()).unwrap_or(0.0) as f32);
Some(EdgeInsets {
top: side("top"),
right: side("right"),
bottom: side("bottom"),
left: side("left"),
})
}
fn ts_type(_: &mut TsTypes) -> String {
"Partial<Edges>".into()
}
}
pub fn field<T: HostValue, I: HostInput>(input: &I, key: &str, cx: &mut HostCx) -> Option<T> {
input.field(key).and_then(|v| T::decode(&v, cx))
}
const EVENT_TYPES: &str = r#"/** A position, in points (`nsh_core::geometry::Point`). */
export interface Point {
x: number;
y: number;
}
/** An extent, in points (`nsh_core::geometry::Size`). */
export interface Size {
width: number;
height: number;
}
/** A position and an extent, in points (`nsh_core::geometry::Bounds`). */
export interface Bounds {
/** The top left corner. */
origin: Point;
size: Size;
}
/** A length on each side, in points (`nsh_core::geometry::Edges`): insets, a safe area. */
export interface Edges {
top: number;
right: number;
bottom: number;
left: number;
}
/** The modifier keys held (GPUI's `Modifiers`). */
export interface Modifiers {
control: boolean;
/** Option on Apple platforms, Alt elsewhere. */
alt: boolean;
shift: boolean;
/** Command on Apple platforms, the Windows / Super key elsewhere. */
platform: boolean;
}
/** Where the core placed an overlay: the payload of `onPosition`. */
export interface OverlayPosition {
/** The side of the anchor it went on: the one asked for, or the other one when only that one has room. */
placement: "top" | "bottom" | "left" | "right";
/** Its frame in the root view (the surface, or the dialog it is in). */
bounds: Bounds;
/** The room on that side, inside the safe area less `margin`: what a menu may grow to. */
available: Size;
}
/** A drag: translation since the start and velocity (points / second). */
export interface PanEvent {
/** The system cancelled the drag rather than releasing it. */
cancelled: boolean;
translationX: number;
translationY: number;
velocityX: number;
velocityY: number;
/** The `pan.x` value now (with one). */
valueX?: number;
/** The `pan.y` value now (with one). */
valueY?: number;
}
/** Where the pointer was at a press, the modifier keys held when it began and its click count: the payload of `onPress`, `onLongPress`, `onPressDown` and `onPressUp` on backends that know it. */
export interface PressEvent {
/** Position in the pressed view (its transforms undone; a pressed span's text view). */
position: Point;
/** Position in the root view the view is in (the surface or its modal), as touches' `pageX` / `pageY`. */
pagePosition: Point;
modifiers: Modifiers;
/** Consecutive clicks at the same place: 1 for a click, 2 for a double click. */
clickCount: number;
}
/** Where the mouse is over a hovered view: the payload of `onHoverMove` (GPUI's `MouseMoveEvent`). */
export interface HoverEvent {
/** Position in the view (its transforms undone). */
position: Point;
/** Position in the root view the view is in (the surface or its modal), as touches' `pageX` / `pageY`. */
pagePosition: Point;
modifiers: Modifiers;
}
/** Where a context menu was asked for: the payload of `onContextMenu`. */
export interface ContextMenuEvent {
/** Position in the view (its transforms undone). */
position: Point;
/** Position in the root view the view is in: where `Menu.popup` opens a menu (`Menu.popup(items, e.pagePosition)`). */
pagePosition: Point;
modifiers: Modifiers;
}
/** Where a scroll view is: the payload of `onScroll`. */
export interface ScrollEvent {
/** The content offset. */
offset: Point;
/** The content's size (zoomed). */
contentSize: Size;
/** The scroll view's size. */
size: Size;
/** The zoom scale (1 unless the scroll view zooms). */
zoomScale: number;
}
/** Where an input's caret is: the payload of `onCaretMove`. */
export interface CaretEvent {
/** The caret in the input (its border box), the input's own scrolling applied: as wide as the platform draws it, as tall as its line. Empty until the input is laid out. */
bounds: Bounds;
/** The caret in the root view the input is in (the surface or its dialog), as of the event: an `Overlay`'s `anchor`. */
pageBounds: Bounds;
}
/** What the user pasted into an input: the payload of `onPaste`. The platform read the clipboard as the user pasted; the app never reads it for this. */
export interface PasteEvent {
/** The plain text, `""` when there was none. */
text: string;
/** Files with their bytes: images (a screenshot, an image copied from a page or an app), as `File`s an app can upload. */
files: File[];
/** `file:` URLs of local files copied in a file manager (as `onFileDrop`'s). */
uris: string[];
}
/** A line of a laid-out text (in the text's content box). */
export interface TextLayoutLine {
text: string;
bounds: Bounds;
ascender: number;
descender: number;
capHeight: number;
xHeight: number;
}
/** Where a web view is: the payload of its navigation events. */
export interface WebViewNavigation {
url: string;
title: string;
loading: boolean;
canGoBack: boolean;
canGoForward: boolean;
}
"#;
pub const EVENTS: &[(EventKind, u32, &str, &str)] = &[
(
EventKind::Press,
on::ALL,
"(e?: PressEvent) => void",
"A tap. `e` is where the pointer was, with the modifier keys and the click count, on backends that know it.",
),
(
EventKind::LongPress,
on::ALL,
"(e?: PressEvent) => void",
"A press held for about half a second. `e` is where the pointer was, with the modifier keys and the click count, on backends that know it.",
),
(
EventKind::PressDown,
on::ALL,
"(e?: PressEvent) => void",
"A press went down. `e` is where the pointer was, with the modifier keys and the click count, on backends that know it.",
),
(
EventKind::PressUp,
on::ALL,
"(e?: PressEvent) => void",
"The press ended. `e` is where the pointer was, with the modifier keys and the click count, on backends that know it.",
),
(
EventKind::HoverIn,
on::ALL,
"() => void",
"The mouse came over the view or a view inside it (never on touches).",
),
(
EventKind::HoverOut,
on::ALL,
"() => void",
"The mouse left the view and the views inside it, or the window.",
),
(
EventKind::HoverMove,
on::ALL,
"(e: HoverEvent) => void",
"The mouse moved over the view (or a view inside it) with no button down, at most once per frame.",
),
(
EventKind::ContextMenu,
on::ALL,
"(e: ContextMenuEvent) => void",
"The secondary button (a right click, Control-click on Apple platforms) was pressed over the view: open a menu with `Menu.popup(items, e.pagePosition)`. The nearest listener hears it; a text input or selectable text nearer shows its own menu instead.",
),
(
EventKind::MouseMove,
on::ALL,
"(e: Point) => void",
"The mouse moved inside the view, including with a button down (desktops and web); `x`/`y` are local coordinates in points, delivered at most once per frame.",
),
(
EventKind::FileDragEnter,
on::ALL,
"() => void",
"A local file drag entered this desktop destination.",
),
(
EventKind::FileDragLeave,
on::ALL,
"() => void",
"A local file drag left this desktop destination.",
),
(
EventKind::FileDrop,
on::ALL,
"(e: { uris: string[] }) => void",
"Local file URLs dropped on this desktop destination.",
),
(
EventKind::Layout,
on::ALL,
"(bounds: Bounds) => void",
"The frame changed: its bounds in the parent.",
),
(EventKind::PanStart, on::ALL, "(e: PanEvent) => void", ""),
(
EventKind::PanEnd,
on::ALL,
"(e: PanEvent) => void",
"With the release velocity, e.g. to start a `decay`.",
),
(
EventKind::PinchStart,
on::ALL,
"(e: { scale: number; velocity: number }) => void",
"",
),
(
EventKind::PinchEnd,
on::ALL,
"(e: { scale: number; velocity: number }) => void",
"",
),
(
EventKind::RotationStart,
on::ALL,
"(e: { rotation: number; velocity: number }) => void",
"",
),
(
EventKind::RotationEnd,
on::ALL,
"(e: { rotation: number; velocity: number }) => void",
"",
),
(
EventKind::AccessibilityAction,
on::ALL,
"(e: { actionName: string }) => void",
"",
),
(
EventKind::TextLayout,
on::TEXT,
"(e: { lines: TextLayoutLine[] }) => void",
"The laid-out lines (in the text's content box), after each layout.",
),
(
EventKind::LoadStart,
on::IMAGE | on::VIDEO,
"() => void",
"",
),
(
EventKind::LoadStart,
on::WEBVIEW,
"(e: WebViewNavigation) => void",
"",
),
(
EventKind::Progress,
on::IMAGE,
"(e: { loaded: number; total: number }) => void",
"Download progress in bytes (`total` 0 when unknown); at most every 100 ms.",
),
(
EventKind::Progress,
on::VIDEO,
"(e: { currentTime: number; duration: number }) => void",
"",
),
(
EventKind::Load,
on::IMAGE,
"(e: Size) => void",
"The source image's size in pixels (not the decoded, downsampled one).",
),
(
EventKind::Load,
on::WEBVIEW,
"(e: WebViewNavigation) => void",
"",
),
(
EventKind::Load,
on::VIDEO,
"(e: { duration: number; size: Size }) => void",
"",
),
(
EventKind::Error,
on::IMAGE | on::WEBVIEW | on::VIDEO,
"(e: { message: string }) => void",
"",
),
(
EventKind::LoadEnd,
on::IMAGE,
"() => void",
"After `onLoad` or `onError`.",
),
(
EventKind::Scroll,
on::SCROLLING,
"(e: ScrollEvent) => void",
"",
),
(
EventKind::ScrollDragStart,
on::SCROLLING,
"(e: Point) => void",
"",
),
(
EventKind::ScrollDragEnd,
on::SCROLLING,
"(e: Point) => void",
"",
),
(
EventKind::MomentumStart,
on::SCROLLING,
"(e: Point) => void",
"",
),
(
EventKind::MomentumEnd,
on::SCROLLING,
"(e: Point) => void",
"",
),
(
EventKind::ScrollEnd,
on::SCROLL | on::LIST,
"(e: Point) => void",
"",
),
(EventKind::Refresh, on::SCROLL | on::LIST, "() => void", ""),
(EventKind::LoadMore, on::LIST, "() => void", ""),
(
EventKind::LoadMoreAtStart,
on::LIST,
"() => void",
"Within `loadMoreAtStartThreshold` viewports of the start, once the user or a scroll command moved the list: load what comes before the first item.",
),
(
EventKind::ChangeText,
on::INPUT,
"(text: string) => void",
"",
),
(
EventKind::SubmitEditing,
on::INPUT,
"(e: { text: string }) => void",
"",
),
(
EventKind::Focus,
on::ALL,
"() => void",
"It took the keyboard focus: an input, or a view that takes presses reached by Tab on the platforms that move the focus between pressables (macOS, the web).",
),
(
EventKind::Blur,
on::ALL,
"() => void",
"It lost the keyboard focus.",
),
(
EventKind::SelectionChange,
on::INPUT,
"(e: { start: number; end: number }) => void",
"",
),
(
EventKind::Paste,
on::INPUT,
"(e: PasteEvent) => void",
"The user pasted (the keyboard's shortcut, a menu's Paste, a keyboard's image): the text, images as `File`s, local files' `file:` URLs. It comes before the `onChange` of the text it inserts; `pasteBehavior` decides whether the input inserts the text (images and files never are).",
),
(
EventKind::CaretMove,
on::INPUT,
"(e: CaretEvent) => void",
"The caret moved: with the selection, or as the text scrolled or wrapped again. `bounds` is where it is in the input, `pageBounds` in the root view (an `Overlay`'s `anchor`): an `@` mention's suggestions open there.",
),
(
EventKind::KeyPress,
on::INPUT,
"(e: { key: string }) => void",
"Before the text changes: `key` is the typed text, `Backspace` or `Enter`.",
),
(
EventKind::CompositionChange,
on::INPUT,
"(e: { composing: boolean; text: string }) => void",
"An input method's composition started, changed or ended: `text` is what is being composed (pinyin before a character is chosen), empty once it ends. The committed text reaches `onChange` before the composition's end; while composing, the platform holds the `value` the app sets and shows it once the composition ends.",
),
(
EventKind::ContentSizeChange,
on::INPUT,
"(e: Size) => void",
"The size of the text (at the input's width when `multiline`; one empty line's while it is empty, whatever its placeholder): grow a multiline input with it.",
),
(
EventKind::Message,
on::WEBVIEW,
"(e: { data: string }) => void",
"The page called `window.nsh.postMessage(data)`.",
),
(
EventKind::NavigationStateChange,
on::WEBVIEW,
"(e: WebViewNavigation) => void",
"",
),
(EventKind::End, on::VIDEO, "() => void", ""),
(
EventKind::Buffer,
on::VIDEO,
"(e: { isBuffering: boolean }) => void",
"",
),
(
EventKind::PressOutside,
on::OVERLAY,
"(e: { pagePosition: Point }) => void",
"A press went down outside the overlay, its anchor and the overlays inside it (where, in the root view). Closing is the app's: leave the overlay out.",
),
(
EventKind::Cancel,
on::OVERLAY,
"() => void",
"Escape or the Android back button, while this is the top overlay listening to it (it takes the key from the overlays below, the pages and `BackHandler`). Closing is the app's.",
),
(
EventKind::Position,
on::OVERLAY,
"(e: OverlayPosition) => void",
"The core placed the overlay (again: the anchor moved, the overlay changed size, it went to the other side).",
),
];
pub const INTERNAL_EVENTS: &[EventKind] = &[
EventKind::Touch,
EventKind::Insets,
EventKind::NativeEvent,
EventKind::Dialog,
EventKind::BackPress,
EventKind::ImageInfo,
EventKind::Clipboard,
EventKind::Host,
EventKind::Pick,
EventKind::OpenUrl,
EventKind::KeyCommand,
EventKind::Menu,
EventKind::ViewableItemsChange,
EventKind::BackRequest,
EventKind::Dismiss,
EventKind::RequestClose,
EventKind::Show,
];
const GROUPS: &[(u32, &str, &str)] = &[
(on::ALL, "", "every element"),
(on::VIEW, "View", "`<View>`"),
(on::TEXT, "Text", "`<Text>`"),
(on::IMAGE, "Image", "`<Image>`"),
(
on::SCROLLING,
"Scrolling",
"scroll views and lists (how they scroll)",
),
(on::SCROLL, "Scroll", "`<Scroll>`"),
(on::LIST, "List", "lists"),
(on::INPUT, "Input", "`<Input>`"),
(on::SCREEN, "NavPage", "the pages of a `NavStack`"),
(on::MODAL, "Dialog", "dialogs"),
(on::WEBVIEW, "WebView", "web views"),
(on::VIDEO, "Video", "videos"),
(on::INPUT_ACCESSORY, "InputAccessory", "input accessories"),
(on::OVERLAY, "Overlay", "`<Overlay>`"),
];
pub fn declarations() -> String {
use std::fmt::Write as _;
let mut types = TsTypes::default();
let mut specs = Vec::new();
<crate::Prop as HostProps>::specs(&mut types, &mut specs);
let mut out = String::from(EVENT_TYPES);
for declaration in types.declarations() {
let _ = write!(out, "\n{declaration}\n");
}
for (bit, group, what) in GROUPS {
for (platform, prefix, inside) in [
(Platform::All, "", ""),
(Platform::Ios, "IOS", " inside `ios={{…}}`"),
(Platform::Android, "Android", " inside `android={{…}}`"),
(Platform::Desktop, "Desktop", " inside `desktop={{…}}`"),
(Platform::Web, "Web", " inside `web={{…}}`"),
] {
let props: Vec<&HostPropSpec> = specs
.iter()
.filter(|s| s.on & bit != 0 && s.platform == platform)
.collect();
let events: Vec<_> = EVENTS
.iter()
.filter(|e| e.1 & bit != 0 && platform == Platform::All)
.collect();
if props.is_empty() && events.is_empty() {
continue;
}
let _ = write!(
out,
"\n/** The props of {what}{inside}. */\nexport interface {prefix}{group}HostProps {{\n"
);
for p in props {
if !p.doc.is_empty() {
let _ = writeln!(out, " /** {} */", p.doc);
}
let _ = writeln!(out, " {}?: {};", p.name, p.ts);
}
for (kind, _, handler, doc) in events {
if !doc.is_empty() {
let _ = writeln!(out, " /** {doc} */");
}
let _ = writeln!(out, " {}?: {handler};", kind.handler_name());
}
out.push_str("}\n");
}
}
out
}