use std::{
sync::{
Arc, Mutex,
atomic::{AtomicUsize, Ordering},
},
time::{Duration, Instant},
};
use crossbeam_channel::{Receiver, Sender};
use nsh_core::{
ApproxTextEngine, AttributedText, EventKind, EventPayload, MountBatch, MountItem, Size,
TextEngine, TextLayout,
platform::{Idiom, Locale, Os},
ui::{AssetFile, AssetKind, Registry, Root, prelude::*},
};
use nsh_engine::{App, Assets, Engine, EngineConfig, Environment, MountSink};
struct FrameSink {
batches: Sender<MountBatch>,
frames: Arc<AtomicUsize>,
}
impl MountSink for FrameSink {
fn submit(&self, batch: MountBatch) {
let _ = self.batches.send(batch);
}
fn request_frame(&self) {
self.frames.fetch_add(1, Ordering::SeqCst);
}
}
fn config(app: App) -> EngineConfig {
EngineConfig {
app,
viewport: Size::new(390.0, 844.0),
trace: false,
network: None,
assets: None,
environment: Environment::default(),
header_heights: (44.0, 96.0),
limits: nsh_engine::Limits::default(),
ui_sets: Vec::new(),
runtime: None,
}
}
fn start<C: Component>(environment: Environment) -> (Engine, Receiver<MountBatch>, Arc<AtomicUsize>)
where
C::Props: Default,
{
let (tx, rx) = crossbeam_channel::unbounded();
let frames = Arc::new(AtomicUsize::new(0));
let config = EngineConfig {
environment,
..config(App::Rust(Root::of::<C>()))
};
let sink = FrameSink {
batches: tx,
frames: frames.clone(),
};
let engine = Engine::start(config, || ApproxTextEngine, sink).unwrap();
(engine, rx, frames)
}
fn until(rx: &Receiver<MountBatch>, until: &str) -> (Vec<String>, Vec<bool>, Option<u32>) {
let deadline = Instant::now() + Duration::from_secs(5);
let (mut texts, mut directions, mut host) = (Vec::new(), Vec::new(), None);
while let Ok(batch) = rx.recv_deadline(deadline) {
for item in &batch.items {
match item {
MountItem::Text { text, .. } => texts.push(text.text.clone()),
MountItem::LayoutDirection { rtl } => directions.push(*rtl),
MountItem::Insert {
parent: nsh_core::ROOT_ID,
child,
..
} => host = Some(*child),
_ => {}
}
}
if texts.iter().any(|t| t == until) {
break;
}
}
(texts, directions, host)
}
struct Fonts(ApproxTextEngine, Arc<Mutex<Vec<Vec<u8>>>>);
impl TextEngine for Fonts {
type Prepared = <ApproxTextEngine as TextEngine>::Prepared;
fn prepare(&self, text: &AttributedText) -> Self::Prepared {
self.0.prepare(text)
}
fn measure(&self, prepared: &Self::Prepared, max_width: Option<f32>) -> Size {
self.0.measure(prepared, max_width)
}
fn layout(&self, prepared: &Self::Prepared, size: Size) -> TextLayout {
self.0.layout(prepared, size)
}
fn register_font(&self, data: &[u8]) -> Result<(), String> {
self.1.lock().unwrap().push(data.to_vec());
Ok(())
}
}
static FILES: &[AssetFile] = &[
AssetFile {
uri: "asset:/@app/fonts/Script.ttf",
source: "/nowhere/fonts/Script.ttf",
kind: AssetKind::Font,
},
AssetFile {
uri: "asset:/@app/logo.png",
source: "/nowhere/logo.png",
kind: AssetKind::Image,
},
];
struct CrateAssets(Arc<Mutex<Vec<String>>>);
impl Assets for CrateAssets {
fn read(&self, uri: &str) -> Result<Vec<u8>, String> {
self.0.lock().unwrap().push(uri.to_owned());
Ok(uri.as_bytes().to_vec())
}
}
#[derive(Default)]
struct Label;
impl Component for Label {
type Props = ();
type Event = NoEvent;
fn render(&self, _: &(), _: &mut RenderCx<'_, Self>) -> impl IntoElement {
view().child(text("ready").font_family("Script"))
}
}
#[test]
fn the_fonts_ui_sets_ship_are_registered_before_the_first_render() {
let (tx, rx) = crossbeam_channel::unbounded();
let read = Arc::new(Mutex::new(Vec::new()));
let fonts = Arc::new(Mutex::new(Vec::new()));
let mut set = Registry::ui_set("app/native");
set.assets(FILES);
let config = EngineConfig {
assets: Some(Arc::new(CrateAssets(read.clone()))),
ui_sets: vec![set],
..config(App::Rust(Root::of::<Label>()))
};
let registered = fonts.clone();
let _engine = Engine::start(
config,
move || Fonts(ApproxTextEngine, registered),
FrameSink {
batches: tx,
frames: Arc::default(),
},
)
.unwrap();
until(&rx, "ready");
assert_eq!(*read.lock().unwrap(), ["asset:/@app/fonts/Script.ttf"]);
assert_eq!(
*fonts.lock().unwrap(),
[b"asset:/@app/fonts/Script.ttf".to_vec()]
);
}
#[derive(Default)]
struct Frames {
frames: u32,
called: bool,
_loop: Option<Task<()>>,
_once: Option<Task<()>>,
}
impl Component for Frames {
type Props = ();
type Event = NoEvent;
fn setup(&mut self, _: &(), cx: &mut Context<'_, Self>) {
let start = Instant::now();
self._once = Some(cx.request_animation_frame(move |this, at, cx| {
this.called = at >= start;
cx.notify();
}));
self._loop = Some(cx.spawn(|this, cx| async move {
for _ in 0..3 {
cx.next_frame().await;
this.update(|this, cx| {
this.frames += 1;
cx.notify();
})
.await;
}
}));
}
fn render(&self, _: &(), _: &mut RenderCx<'_, Self>) -> impl IntoElement {
view().child(text(format!(
"{} frames, called {}",
self.frames, self.called
)))
}
}
#[test]
fn tasks_wait_for_display_frames() {
let (engine, rx, frames) = start::<Frames>(Environment::default());
until(&rx, "0 frames, called false");
let deadline = Instant::now() + Duration::from_secs(5);
while frames.load(Ordering::SeqCst) == 0 && Instant::now() < deadline {
std::thread::sleep(Duration::from_millis(5));
}
assert_eq!(frames.load(Ordering::SeqCst), 1);
for n in 1..=3 {
engine.control().frame();
let (texts, ..) = until(&rx, &format!("{n} frames, called true"));
assert_eq!(
texts.last().map(String::as_str),
Some(format!("{n} frames, called true").as_str())
);
}
std::thread::sleep(Duration::from_millis(100));
assert_eq!(frames.load(Ordering::SeqCst), 3);
}
#[derive(Default)]
struct Direction {
presses: u32,
}
impl Component for Direction {
type Props = ();
type Event = NoEvent;
fn render(&self, _: &(), cx: &mut RenderCx<'_, Self>) -> impl IntoElement {
view()
.flex_1()
.on_press(cx.listener(|this: &mut Self, _, cx| {
this.presses += 1;
match this.presses {
1 => cx.force_rtl(true),
2 => cx.force_rtl(false),
_ => cx.allow_rtl(false),
}
}))
.child(text(if cx.rtl() { "rtl" } else { "ltr" }))
}
}
#[test]
fn components_set_the_layout_direction() {
let (engine, rx, _) = start::<Direction>(Environment::default());
let (_, directions, host) = until(&rx, "ltr");
assert_eq!(directions, [false]);
let host = host.expect("the root component");
let press = || {
engine
.control()
.dispatch_event(host, EventKind::Press, EventPayload::None)
};
press();
let (_, directions, _) = until(&rx, "rtl");
assert_eq!(directions, [true]);
press();
let (_, directions, _) = until(&rx, "ltr");
assert_eq!(directions, [false]);
engine.control().set_environment(Environment {
rtl: true,
..Environment::default()
});
let (_, directions, _) = until(&rx, "rtl");
assert_eq!(directions, [true]);
press();
let (_, directions, _) = until(&rx, "ltr");
assert_eq!(directions, [false]);
}
#[derive(Default)]
struct Device;
impl Component for Device {
type Props = ();
type Event = NoEvent;
fn render(&self, _: &(), cx: &mut RenderCx<'_, Self>) -> impl IntoElement {
let (major, minor) = cx.os_version();
view().child(text(format!(
"{} {major}.{minor} {:?} ×{} {} {}",
cx.os().name(),
cx.idiom(),
cx.pixel_ratio(),
cx.round_to_nearest_pixel(10.3),
cx.locale(),
)))
}
}
#[test]
fn components_read_the_pixel_ratio_the_device_and_the_locale() {
let environment = Environment {
pixel_ratio: 3.0,
os: Os::Android,
os_version: (34, 0),
idiom: Idiom::Pad,
locale: Locale::parse("de_DE").unwrap(),
..Environment::default()
};
let (engine, rx, _) = start::<Device>(environment);
let shown = "android 34.0 Pad ×3 10.333333 de-DE";
let (texts, ..) = until(&rx, shown);
assert!(texts.iter().any(|t| t == shown), "{texts:?}");
engine.control().set_environment(Environment {
pixel_ratio: 2.0,
..environment
});
let shown = "android 34.0 Pad ×2 10.5 de-DE";
let (texts, ..) = until(&rx, shown);
assert!(texts.iter().any(|t| t == shown), "{texts:?}");
engine.control().set_environment(Environment {
pixel_ratio: 2.0,
locale: Locale::parse("zh-Hans_CN").unwrap(),
..environment
});
let shown = "android 34.0 Pad ×2 10.5 zh-Hans-CN";
let (texts, ..) = until(&rx, shown);
assert!(texts.iter().any(|t| t == shown), "{texts:?}");
}