#![allow(unused_imports)]
use crate::*;
use pax_engine::api::{
cursor::CursorStyle, Click, Duration, EasingCurve, Event, MouseDown, MouseMove, MouseOut,
MouseOver, MouseUp, Store,
};
use pax_engine::*;
use pax_runtime::api::NodeContext;
const DESKTOP_DRAWER_WIDTH_PX: f64 = 430.0;
const MIN_PREVIEW_WIDTH_PX: f64 = 320.0;
const MIN_DRAWER_WIDTH_PX: f64 = 280.0;
const RESIZE_MIN_DELTA_PX: f64 = 2.0;
const DRAWER_EASE_MS: u64 = 250;
#[pax]
#[engine_import_path("pax_engine")]
#[file("reference/example_host.pax")]
#[custom(Default)]
pub struct ExampleHost {
pub title: Property<String>,
pub sources: Property<Vec<ExampleSource>>,
pub selected_source: Property<usize>,
pub drawer_open: Property<bool>,
pub _active_source: Property<ExampleSource>,
pub _source_markups: Property<Vec<String>>,
pub _active_source_markup: Property<String>,
pub _drawer_width_px: Property<f64>,
pub _drawer_progress: Property<f64>,
pub _drawer_target_open: Property<bool>,
pub _is_resizing_drawer: Property<bool>,
pub _compact_drawer: Property<bool>,
}
impl Default for ExampleHost {
fn default() -> Self {
Self {
title: Property::new("Example".to_string()),
sources: Property::new(vec![]),
selected_source: Property::new(0),
drawer_open: Property::new(false),
_active_source: Property::new(fallback_source()),
_source_markups: Property::new(vec![highlighted_code_markup(&fallback_source())]),
_active_source_markup: Property::new(highlighted_code_markup(&fallback_source())),
_drawer_width_px: Property::new(DESKTOP_DRAWER_WIDTH_PX),
_drawer_progress: Property::new(0.0),
_drawer_target_open: Property::new(false),
_is_resizing_drawer: Property::new(false),
_compact_drawer: Property::new(false),
}
}
}
impl ExampleHost {
pub fn on_mount(&mut self, ctx: &NodeContext) {
ctx.push_local_store(SelectedSourceStore(self.selected_source.clone()));
let drawer_open = self.drawer_open.get();
self._drawer_target_open.set(drawer_open);
self._drawer_progress
.set(if drawer_open { 1.0 } else { 0.0 });
self.clamp_drawer_to_host(ctx.bounds_self.get().0);
let sources = self.sources.clone();
let selected_source = self.selected_source.clone();
let deps = [sources.untyped(), selected_source.untyped()];
self._active_source.replace_with(Property::computed(
move || active_source(&sources.get(), selected_source.get()),
&deps,
));
let sources_for_markup = self.sources.clone();
let deps = [sources_for_markup.untyped()];
self._source_markups.replace_with(Property::computed(
move || source_markups(&sources_for_markup.get()),
&deps,
));
let source_markups = self._source_markups.clone();
let selected_source = self.selected_source.clone();
let deps = [source_markups.untyped(), selected_source.untyped()];
self._active_source_markup.replace_with(Property::computed(
move || {
let source_markups = source_markups.get();
if source_markups.is_empty() {
return highlighted_code_markup(&fallback_source());
}
source_markups
.get(selected_source.get().min(source_markups.len() - 1))
.cloned()
.unwrap_or_else(|| highlighted_code_markup(&fallback_source()))
},
&deps,
));
}
pub fn on_pre_render(&mut self, ctx: &NodeContext) {
self.clamp_drawer_to_host(ctx.bounds_self.get().0);
let drawer_open = self.drawer_open.get();
if drawer_open != self._drawer_target_open.get() {
self._drawer_target_open.set(drawer_open);
self.animate_drawer_progress(drawer_open);
}
}
pub fn toggle_drawer(&mut self, _ctx: &NodeContext, _event: Event<Click>) {
let next_open = !self.drawer_open.get();
self.drawer_open.set(next_open);
self._drawer_target_open.set(next_open);
self.animate_drawer_progress(next_open);
}
pub fn on_resize_mouse_down(&mut self, ctx: &NodeContext, event: Event<MouseDown>) {
if !self.drawer_open.get() {
return;
}
let bounds = ctx.bounds_self.get();
let divider_px = bounds.0 - self._drawer_width_px.get();
if (event.mouse.x - divider_px).abs() < 12.0 {
self._is_resizing_drawer.set(true);
ctx.set_cursor(CursorStyle::EwResize);
}
}
pub fn on_split_mouse_down(&mut self, ctx: &NodeContext, _event: Event<MouseDown>) {
if !self.drawer_open.get() {
return;
}
self._is_resizing_drawer.set(true);
ctx.set_cursor(CursorStyle::EwResize);
}
pub fn on_resize_mouse_over(&mut self, ctx: &NodeContext, _event: Event<MouseOver>) {
ctx.set_cursor(CursorStyle::EwResize);
}
pub fn on_resize_mouse_out(&mut self, ctx: &NodeContext, _event: Event<MouseOut>) {
if !self._is_resizing_drawer.get() {
ctx.set_cursor(CursorStyle::Auto);
}
}
pub fn on_resize_mouse_move(&mut self, ctx: &NodeContext, event: Event<MouseMove>) {
if !self._is_resizing_drawer.get() {
return;
}
let bounds = ctx.bounds_self.get();
if bounds.0 < MIN_PREVIEW_WIDTH_PX + MIN_DRAWER_WIDTH_PX {
self._drawer_width_px.set_if_neq(bounds.0.max(0.0));
return;
}
let max_drawer_width = (bounds.0 - MIN_PREVIEW_WIDTH_PX).max(MIN_DRAWER_WIDTH_PX);
let drawer_width = (bounds.0 - event.mouse.x).clamp(MIN_DRAWER_WIDTH_PX, max_drawer_width);
if (drawer_width - self._drawer_width_px.get()).abs() >= RESIZE_MIN_DELTA_PX {
self._drawer_width_px.set(drawer_width);
}
}
pub fn on_resize_mouse_up(&mut self, ctx: &NodeContext, _event: Event<MouseUp>) {
self._is_resizing_drawer.set(false);
ctx.set_cursor(CursorStyle::Auto);
}
fn animate_drawer_progress(&self, open: bool) {
let target = if open { 1.0 } else { 0.0 };
self._drawer_progress.ease_to(
target,
Duration::Milliseconds(DRAWER_EASE_MS.into()),
EasingCurve::InQuad,
);
}
fn clamp_drawer_to_host(&self, host_width: f64) {
let Some((compact_drawer, next_width)) =
clamped_drawer_layout(host_width, self._drawer_width_px.get())
else {
return;
};
self._compact_drawer.set_if_neq(compact_drawer);
self._drawer_width_px.set_if_neq(next_width);
}
}
fn clamped_drawer_layout(host_width: f64, current_width: f64) -> Option<(bool, f64)> {
if host_width <= 0.0 {
return None;
}
let compact_drawer = host_width < MIN_PREVIEW_WIDTH_PX + MIN_DRAWER_WIDTH_PX;
let width = if compact_drawer {
host_width
} else {
current_width.clamp(MIN_DRAWER_WIDTH_PX, host_width - MIN_PREVIEW_WIDTH_PX)
};
Some((compact_drawer, width))
}
#[pax]
#[engine_import_path("pax_engine")]
pub struct ExampleSource {
pub label: String,
pub language: String,
pub code: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn compact_drawer_replaces_preview_at_narrow_widths() {
assert_eq!(clamped_drawer_layout(390.0, 430.0), Some((true, 390.0)));
}
#[test]
fn wide_drawer_preserves_preview_and_clamps_to_available_space() {
assert_eq!(clamped_drawer_layout(1_280.0, 430.0), Some((false, 430.0)));
assert_eq!(
clamped_drawer_layout(1_280.0, 1_100.0),
Some((false, 960.0))
);
}
}
#[pax]
#[engine_import_path("pax_engine")]
#[inlined(
<Group width=100% height=100% @click=self.select_source>
<Text class="example_host_tab_label" x=50% y=50% width={100% - 16px} height=18px text={source.label} selectable=false clip=true />
if self.index == self.selected {
<Path class="example_host_tab_active" width=100% height=100% />
}
if self.index != self.selected {
<Path class="example_host_tab_idle" width=100% height=100% />
}
</Group>
@settings {
.example_host_tab_label {
style: TextStyle {
font: Font::Web("Space Grotesk", "https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;500;600&display=swap", FontStyle::Normal, FontWeight::Medium)
font_size: 11px
fill: WHITE
align_vertical: TextAlignVertical::Center
align_horizontal: TextAlignHorizontal::Center
align_multiline: TextAlignHorizontal::Center
underline: false
}
}
.example_host_tab_active {
fill: rgba(61, 103, 180, 255)
elements: [
PathElement::Point(0%, 100%),
PathElement::Line,
PathElement::Point(2.5%, 17%),
PathElement::Quadratic(2.5%, 0%),
PathElement::Point(5%, 0%),
PathElement::Line,
PathElement::Point(95%, 0%),
PathElement::Quadratic(97.5%, 0%),
PathElement::Point(97.5%, 17%),
PathElement::Line,
PathElement::Point(100%, 100%),
PathElement::Close
]
}
.example_host_tab_idle {
fill: rgba(20, 24, 32, 255)
elements: [
PathElement::Point(0%, 100%),
PathElement::Line,
PathElement::Point(2.5%, 17%),
PathElement::Quadratic(2.5%, 0%),
PathElement::Point(5%, 0%),
PathElement::Line,
PathElement::Point(95%, 0%),
PathElement::Quadratic(97.5%, 0%),
PathElement::Point(97.5%, 17%),
PathElement::Line,
PathElement::Point(100%, 100%),
PathElement::Close
]
}
}
)]
pub struct ExampleHostSourceTab {
pub source: Property<ExampleSource>,
pub index: Property<usize>,
pub selected: Property<usize>,
}
impl ExampleHostSourceTab {
pub fn select_source(&mut self, ctx: &NodeContext, _event: Event<Click>) {
let index = self.index.get();
let _ = ctx.peek_local_store(|SelectedSourceStore(selected): &mut SelectedSourceStore| {
selected.set(index);
});
}
}
struct SelectedSourceStore(Property<usize>);
impl Store for SelectedSourceStore {}
fn active_source(sources: &[ExampleSource], selected: usize) -> ExampleSource {
if sources.is_empty() {
return fallback_source();
}
sources
.get(selected.min(sources.len() - 1))
.cloned()
.unwrap_or_else(fallback_source)
}
fn fallback_source() -> ExampleSource {
ExampleSource {
label: "No source".to_string(),
language: "text".to_string(),
code: "No source files were provided.".to_string(),
}
}
fn source_markups(sources: &[ExampleSource]) -> Vec<String> {
if sources.is_empty() {
return vec![highlighted_code_markup(&fallback_source())];
}
sources.iter().map(highlighted_code_markup).collect()
}
fn highlighted_code_markup(source: &ExampleSource) -> String {
let mut out = String::from(
"<pre data-pax-code-markup=\"example-host\" style=\"margin:0; white-space:pre; line-height:18px; color:#e0eafa;\">",
);
for line in source.code.lines() {
highlight_line(line, source.language.as_str(), &mut out);
out.push('\n');
}
out.push_str("</pre>");
out
}
fn highlight_line(line: &str, language: &str, out: &mut String) {
let bytes = line.as_bytes();
let mut index = 0;
while index < bytes.len() {
let rest = &line[index..];
if rest.starts_with("//") {
push_span(out, "#7f8da8", rest);
return;
}
let ch = rest.chars().next().unwrap_or_default();
if ch == '"' {
let end = string_end(rest, '"');
push_span(out, "#ffd166", &rest[..end]);
index += end;
continue;
}
if ch == '\'' {
let end = string_end(rest, '\'');
push_span(out, "#ffd166", &rest[..end]);
index += end;
continue;
}
if ch.is_ascii_digit() {
let len = token_len(rest, |c| c.is_ascii_digit() || c == '.');
push_span(out, "#c792ea", &rest[..len]);
index += len;
continue;
}
if is_ident_start(ch) {
let len = token_len(rest, is_ident_continue);
let token = &rest[..len];
if is_keyword(token, language) {
push_span(out, "#82aaff", token);
} else if token.chars().next().is_some_and(|c| c.is_ascii_uppercase()) {
push_span(out, "#7ee787", token);
} else {
escape_html_into(out, token);
}
index += len;
continue;
}
if "{}[]()<>=+-*/.,:;!&|?%@".contains(ch) {
push_span(out, "#89ddff", &rest[..ch.len_utf8()]);
index += ch.len_utf8();
continue;
}
escape_html_into(out, &rest[..ch.len_utf8()]);
index += ch.len_utf8();
}
}
fn string_end(input: &str, quote: char) -> usize {
let mut escaped = false;
for (offset, ch) in input.char_indices().skip(1) {
if escaped {
escaped = false;
continue;
}
if ch == '\\' {
escaped = true;
continue;
}
if ch == quote {
return offset + ch.len_utf8();
}
}
input.len()
}
fn token_len(input: &str, predicate: impl Fn(char) -> bool) -> usize {
input
.char_indices()
.find_map(|(offset, ch)| (!predicate(ch)).then_some(offset))
.unwrap_or(input.len())
}
fn is_ident_start(ch: char) -> bool {
ch == '_' || ch.is_ascii_alphabetic()
}
fn is_ident_continue(ch: char) -> bool {
is_ident_start(ch) || ch.is_ascii_digit()
}
fn is_keyword(token: &str, language: &str) -> bool {
matches!(
token,
"as" | "bind"
| "break"
| "const"
| "continue"
| "else"
| "enum"
| "false"
| "fn"
| "for"
| "if"
| "impl"
| "in"
| "let"
| "match"
| "mod"
| "move"
| "mut"
| "pub"
| "return"
| "self"
| "Self"
| "slot"
| "struct"
| "true"
| "type"
| "use"
| "where"
| "while"
) || (language == "pax" && matches!(token, "import" | "settings"))
}
fn push_span(out: &mut String, color: &str, text: &str) {
out.push_str("<span style=\"color:");
out.push_str(color);
out.push_str("\">");
escape_html_into(out, text);
out.push_str("</span>");
}
fn escape_html_into(out: &mut String, text: &str) {
for ch in text.chars() {
match ch {
'&' => out.push_str("&"),
'<' => out.push_str("<"),
'>' => out.push_str(">"),
'"' => out.push_str("""),
'\'' => out.push_str("'"),
_ => out.push(ch),
}
}
}