use std::{
cell::{Cell, RefCell},
ffi::{CStr, c_int},
ptr,
};
use nsh_core::{
Command, EventKind, NodeId, Op, ROOT_ID, Tag,
desktop::{
DesktopCommand, OpenWindow, WindowAttributes, WindowCloseCause, WindowEvent, WindowFrame,
WindowMode, WindowOptions, WindowPlacement, WindowRequest, WindowState,
},
element::{JsError, JsOwned, codes},
geometry::{Edges, Points},
};
use rquickjs::qjs;
use crate::{
dom,
reactive::{self, arg},
};
type JsValue = qjs::JSValue;
type JsContext = qjs::JSContext;
type DataFn = unsafe extern "C" fn(
*mut JsContext,
JsValue,
c_int,
*mut JsValue,
c_int,
*mut JsValue,
) -> JsValue;
const UNDEFINED: JsValue = qjs::JS_UNDEFINED;
#[derive(Clone, Copy, PartialEq, Eq)]
enum Event {
ClosePress,
Close,
FocusChange,
ModeChange,
BoundsChange,
DisplayChange,
}
impl Event {
fn parse(name: &str) -> Option<Event> {
Some(match name {
"closePress" => Event::ClosePress,
"close" => Event::Close,
"focusChange" => Event::FocusChange,
"modeChange" => Event::ModeChange,
"boundsChange" => Event::BoundsChange,
"displayChange" => Event::DisplayChange,
_ => return None,
})
}
}
struct Listener {
id: u32,
window: NodeId,
event: Event,
f: JsValue,
}
enum How {
Opened { node: JsValue, owner: u32 },
Element { close: JsValue, owner: u32 },
}
struct Own {
key: String,
id: NodeId,
handle: JsValue,
state: JsValue,
last: Option<WindowState>,
title_bar: JsValue,
how: How,
}
pub(crate) struct Windows {
key: Cell<JsValue>,
version: Cell<JsValue>,
surface: Cell<JsValue>,
pending: RefCell<Option<(NodeId, WindowAttributes, Vec<WindowRequest>)>>,
unopened: RefCell<Vec<JsValue>>,
list: RefCell<Vec<Own>>,
listeners: RefCell<Vec<Listener>>,
next: Cell<u32>,
}
impl Default for Windows {
fn default() -> Self {
Windows {
key: Cell::new(UNDEFINED),
version: Cell::new(UNDEFINED),
surface: Cell::new(UNDEFINED),
pending: RefCell::default(),
unopened: RefCell::default(),
list: RefCell::default(),
listeners: RefCell::default(),
next: Cell::new(0),
}
}
}
impl Windows {
pub(crate) fn free_all(&mut self, raw: *mut JsContext) {
unsafe {
for c in [&self.key, &self.version, &self.surface] {
qjs::JS_FreeValue(raw, c.replace(UNDEFINED));
}
for v in self.unopened.get_mut().drain(..) {
qjs::JS_FreeValue(raw, v);
}
for w in self.list.get_mut().drain(..) {
qjs::JS_FreeValue(raw, w.handle);
qjs::JS_FreeValue(raw, w.state);
qjs::JS_FreeValue(raw, w.title_bar);
match w.how {
How::Opened { node, .. } => qjs::JS_FreeValue(raw, node),
How::Element { close, .. } => qjs::JS_FreeValue(raw, close),
}
}
for l in self.listeners.get_mut().drain(..) {
qjs::JS_FreeValue(raw, l.f);
}
}
}
}
fn windows<'a>(cx: *mut JsContext) -> &'a Windows {
unsafe { &(*crate::state::state(cx)).windows }
}
fn free(cx: *mut JsContext, v: JsValue) {
unsafe { qjs::JS_FreeValue(cx, v) };
}
fn dup(cx: *mut JsContext, v: JsValue) -> JsValue {
unsafe { qjs::JS_DupValue(cx, v) }
}
fn nullish(v: JsValue) -> bool {
unsafe { qjs::JS_IsUndefined(v) || qjs::JS_IsNull(v) }
}
fn is_exception(v: JsValue) -> bool {
unsafe { qjs::JS_IsException(v) }
}
fn get(cx: *mut JsContext, object: JsValue, key: &CStr) -> JsValue {
unsafe {
if !qjs::JS_IsObject(object) {
return UNDEFINED;
}
qjs::JS_GetPropertyStr(cx, object, key.as_ptr())
}
}
fn set(cx: *mut JsContext, object: JsValue, key: &CStr, value: JsValue) {
unsafe { qjs::JS_SetPropertyStr(cx, object, key.as_ptr(), value) };
}
fn string(cx: *mut JsContext, s: &str) -> JsValue {
unsafe { qjs::JS_NewStringLen(cx, s.as_ptr().cast(), s.len() as _) }
}
fn boolean(b: bool) -> JsValue {
if b { qjs::JS_TRUE } else { qjs::JS_FALSE }
}
fn number(n: f64) -> JsValue {
qjs::JS_NewFloat64(n)
}
fn new_object(cx: *mut JsContext) -> JsValue {
unsafe { qjs::JS_NewObject(cx) }
}
fn as_number(cx: *mut JsContext, v: JsValue) -> Option<f64> {
unsafe {
if !qjs::JS_IsNumber(v) {
return None;
}
let mut n = 0f64;
qjs::JS_ToFloat64(cx, &mut n, v);
Some(n)
}
}
fn truthy(cx: *mut JsContext, v: JsValue) -> bool {
unsafe { qjs::JS_ToBool(cx, v) > 0 }
}
fn call(cx: *mut JsContext, f: JsValue, args: &mut [JsValue]) -> bool {
let r = unsafe { qjs::JS_Call(cx, f, UNDEFINED, args.len() as c_int, args.as_mut_ptr()) };
let ok = !is_exception(r);
free(cx, r);
ok
}
fn read(cx: *mut JsContext, f: JsValue) -> JsValue {
unsafe { qjs::JS_Call(cx, f, UNDEFINED, 0, ptr::null_mut()) }
}
fn native(
cx: *mut JsContext,
f: DataFn,
length: c_int,
magic: c_int,
data: &mut [JsValue],
) -> JsValue {
unsafe {
qjs::JS_NewCFunctionData(
cx,
Some(f),
length,
magic,
data.len() as c_int,
data.as_mut_ptr(),
)
}
}
fn type_error(cx: *mut JsContext, message: &str) -> JsValue {
let _ = crate::web::js::type_error(cx, message);
qjs::JS_EXCEPTION
}
fn throw(cx: *mut JsContext, error: JsError) -> JsValue {
use nsh_core::element::IntoThrown as _;
let value = crate::classes::error_value(cx, &error.into_thrown());
let _ = crate::web::js::throw(cx, value);
qjs::JS_EXCEPTION
}
fn decode<T: nsh_core::element::JsValue>(cx: *mut JsContext, v: JsValue) -> Result<T, String> {
let host = crate::classes::host_outside_calls(cx);
let owned = JsOwned::new(&host, crate::classes::to_raw(dup(cx, v)));
let decoded = T::from_js(owned.get()).map_err(|e| e.0);
drop(owned);
host.clear_pending();
decoded
}
fn encode<T: nsh_core::element::JsValue>(cx: *mut JsContext, value: &T) -> JsValue {
crate::env::to_js(cx, value)
}
fn send(cx: *mut JsContext, id: NodeId, command: DesktopCommand) -> Result<(), JsValue> {
dom::push_op(
cx,
Op::Command {
id,
command: Command::Desktop(Box::new(command)),
},
)
}
fn context_key(cx: *mut JsContext) -> JsValue {
let w = windows(cx);
if nullish(w.key.get()) {
w.key.set(new_object(cx));
}
w.key.get()
}
fn version(cx: *mut JsContext) -> JsValue {
let w = windows(cx);
if nullish(w.version.get()) {
w.version.set(reactive::new_signal(cx, number(0.0)));
}
w.version.get()
}
fn bump(cx: *mut JsContext) {
let v = version(cx);
let n = reactive::peek_number(cx, v).unwrap_or(0.0);
reactive::set_signal(cx, v, number(n + 1.0));
}
fn main_ref(cx: *mut JsContext) -> JsValue {
let open = windows(cx)
.list
.borrow()
.iter()
.find(|w| w.key == "main")
.map(|w| dup(cx, w.handle));
open.unwrap_or_else(|| surface_ref(cx))
}
fn surface_ref(cx: *mut JsContext) -> JsValue {
let w = windows(cx);
if nullish(w.surface.get()) {
w.surface
.set(make_handle(cx, "main", ROOT_ID, qjs::JS_NULL));
}
dup(cx, w.surface.get())
}
fn node_of_key(cx: *mut JsContext, key: &str) -> Option<NodeId> {
windows(cx)
.list
.borrow()
.iter()
.find(|w| w.key == key)
.map(|w| w.id)
}
fn handle_of(cx: *mut JsContext, id: NodeId) -> Option<JsValue> {
windows(cx)
.list
.borrow()
.iter()
.find(|w| w.id == id)
.map(|w| dup(cx, w.handle))
}
pub(crate) fn state_value(cx: *mut JsContext, state: Option<&WindowState>) -> JsValue {
let Some(state) = state else {
return qjs::JS_NULL;
};
let value = encode(cx, state);
if nullish(value) {
return value;
}
let display = state.display.and_then(|id| {
crate::env::display_list(cx)
.into_iter()
.find(|d| d.id == id)
});
let display = match display {
Some(d) => encode(cx, &d),
None => qjs::JS_NULL,
};
set(cx, value, c"display", display);
value
}
const METHODS: &[(&CStr, c_int)] = &[
(c"state", 0),
(c"titleBar", 0),
(c"set", 1),
(c"show", 0),
(c"hide", 0),
(c"focus", 0),
(c"close", 0),
(c"minimize", 0),
(c"restore", 0),
(c"setMode", 1),
(c"setBounds", 1),
(c"setPlacement", 1),
(c"center", 1),
(c"requestAttention", 1),
(c"on", 2),
];
fn make_handle(cx: *mut JsContext, key: &str, id: NodeId, parent: JsValue) -> JsValue {
let handle = new_object(cx);
set(cx, handle, c"id", string(cx, key));
set(cx, handle, c"main", boolean(key == "main"));
set(cx, handle, c"parent", dup(cx, parent));
let mut data = [number(id as f64)];
for (magic, (name, length)) in METHODS.iter().enumerate() {
let f = native(cx, ref_call, *length, magic as c_int, &mut data);
set(cx, handle, name, f);
}
handle
}
fn is_open(cx: *mut JsContext, id: NodeId) -> bool {
(id == ROOT_ID && !windows_supported(cx))
|| windows(cx).list.borrow().iter().any(|w| w.id == id)
|| windows(cx)
.pending
.borrow()
.as_ref()
.is_some_and(|(pending, _, _)| *pending == id)
}
unsafe extern "C" fn ref_call(
cx: *mut JsContext,
_this: JsValue,
argc: c_int,
argv: *mut JsValue,
magic: c_int,
data: *mut JsValue,
) -> JsValue {
let id = as_number(cx, unsafe { *data }).unwrap_or(0.0) as NodeId;
let a0 = arg(argc, argv, 0);
let request = |r: WindowRequest| -> JsValue {
if !is_open(cx, id) {
return UNDEFINED;
}
if let Some((pending, _, requests)) = windows(cx).pending.borrow_mut().as_mut()
&& *pending == id
{
requests.push(r);
return UNDEFINED;
}
match send(cx, id, DesktopCommand::Request(r)) {
Ok(()) => UNDEFINED,
Err(e) => e,
}
};
let name = METHODS[magic as usize].0.to_str().unwrap_or("");
match name {
"state" => state_of(cx, id),
"titleBar" => title_bar_of(cx, id),
"set" => {
let attributes = match decode::<WindowAttributes>(cx, a0) {
Ok(a) => a,
Err(e) => return type_error(cx, &format!("WindowRef.set: {e}")),
};
if attributes.is_empty() || !is_open(cx, id) {
return UNDEFINED;
}
if let Some((pending, pending_attributes, _)) =
windows(cx).pending.borrow_mut().as_mut()
&& *pending == id
{
pending_attributes.merge(&attributes);
return UNDEFINED;
}
match send(cx, id, DesktopCommand::SetAttributes(Box::new(attributes))) {
Ok(()) => UNDEFINED,
Err(e) => e,
}
}
"show" => request(WindowRequest::Show),
"hide" => request(WindowRequest::Hide),
"focus" => request(WindowRequest::Focus),
"close" => close_own(cx, id, None),
"minimize" => request(WindowRequest::Minimize),
"restore" => request(WindowRequest::Restore),
"setMode" => match decode::<WindowMode>(cx, a0) {
Ok(mode) => request(WindowRequest::SetMode(mode)),
Err(e) => type_error(cx, &format!("WindowRef.setMode: {e}")),
},
"setBounds" => match bounds_arg(cx, a0) {
Ok(frame) => request(WindowRequest::SetBounds(frame)),
Err(e) => type_error(cx, &format!("WindowRef.setBounds: {e}")),
},
"setPlacement" => match decode::<WindowPlacement>(cx, a0) {
Ok(p) => request(WindowRequest::SetPlacement(p)),
Err(e) => type_error(cx, &format!("WindowRef.setPlacement: {e}")),
},
"center" => {
let display = get(cx, a0, c"display");
let display = if nullish(display) {
Ok(None)
} else {
decode(cx, display).map(Some)
};
match display {
Ok(display) => request(WindowRequest::Center { display }),
Err(e) => type_error(cx, &format!("WindowRef.center: {e}")),
}
}
"requestAttention" => {
let critical = get(cx, a0, c"critical");
let critical = truthy(cx, critical);
request(WindowRequest::RequestAttention { critical })
}
"on" => on(cx, id, a0, arg(argc, argv, 1)),
_ => UNDEFINED,
}
}
fn bounds_arg(cx: *mut JsContext, v: JsValue) -> Result<WindowFrame, String> {
if !unsafe { qjs::JS_IsObject(v) } {
return Err("takes { origin?, size?, animated? }".into());
}
let field = |object: JsValue, key: &CStr| -> Option<f64> {
let x = get(cx, object, key);
let n = as_number(cx, x);
free(cx, x);
n.filter(|n| n.is_finite())
};
let origin = get(cx, v, c"origin");
let size = get(cx, v, c"size");
let animated = get(cx, v, c"animated");
let frame = WindowFrame {
x: field(origin, c"x").map(|x| Points(x as f32)),
y: field(origin, c"y").map(|y| Points(y as f32)),
width: field(size, c"width")
.filter(|w| *w > 0.0)
.map(|w| Points(w as f32)),
height: field(size, c"height")
.filter(|h| *h > 0.0)
.map(|h| Points(h as f32)),
animated: (!nullish(animated)).then(|| truthy(cx, animated)),
};
free(cx, origin);
free(cx, size);
free(cx, animated);
Ok(frame)
}
fn state_of(cx: *mut JsContext, id: NodeId) -> JsValue {
let signal = windows(cx)
.list
.borrow()
.iter()
.find(|w| w.id == id)
.map(|w| dup(cx, w.state));
match signal {
Some(signal) => {
let v = read(cx, signal);
free(cx, signal);
v
}
None => {
free(cx, read(cx, version(cx)));
qjs::JS_NULL
}
}
}
fn title_bar_of(cx: *mut JsContext, id: NodeId) -> JsValue {
let signal = windows(cx)
.list
.borrow()
.iter()
.find(|w| w.id == id)
.map(|w| dup(cx, w.title_bar));
match signal {
Some(signal) => {
let v = read(cx, signal);
free(cx, signal);
v
}
None => {
free(cx, read(cx, version(cx)));
title_bar_value(cx, &Edges::default())
}
}
}
fn title_bar_value(cx: *mut JsContext, area: &Edges<Points>) -> JsValue {
let o = new_object(cx);
set(cx, o, c"height", number(area.top.0.into()));
set(cx, o, c"left", number(area.left.0.into()));
set(cx, o, c"right", number(area.right.0.into()));
o
}
fn on(cx: *mut JsContext, window: NodeId, event: JsValue, f: JsValue) -> JsValue {
let name = dom::string_arg(cx, event);
let Some(event) = name.as_deref().and_then(Event::parse) else {
return type_error(
cx,
"WindowRef.on: the event is \"closePress\", \"close\", \"focusChange\", \"modeChange\", \"boundsChange\" or \"displayChange\"",
);
};
if !unsafe { qjs::JS_IsFunction(cx, f) } {
return type_error(cx, "WindowRef.on: the listener is a function");
}
let id = add_listener(cx, window, event, dup(cx, f));
let stop = native(cx, stop_listener, 0, 0, &mut [number(id as f64)]);
if reactive::has_owner(cx) {
reactive::add_cleanup(cx, dup(cx, stop));
}
stop
}
fn add_listener(cx: *mut JsContext, window: NodeId, event: Event, f: JsValue) -> u32 {
let w = windows(cx);
let id = w.next.get().wrapping_add(1);
w.next.set(id);
w.listeners.borrow_mut().push(Listener {
id,
window,
event,
f,
});
id
}
unsafe extern "C" fn stop_listener(
cx: *mut JsContext,
_this: JsValue,
_argc: c_int,
_argv: *mut JsValue,
_magic: c_int,
data: *mut JsValue,
) -> JsValue {
let id = as_number(cx, unsafe { *data }).unwrap_or(0.0) as u32;
let removed = {
let mut listeners = windows(cx).listeners.borrow_mut();
listeners
.iter()
.position(|l| l.id == id)
.map(|i| listeners.remove(i))
};
if let Some(l) = removed {
free(cx, l.f);
}
UNDEFINED
}
fn fire(cx: *mut JsContext, window: NodeId, event: Event, payload: JsValue) {
let fs: Vec<JsValue> = windows(cx)
.listeners
.borrow()
.iter()
.filter(|l| l.window == window && l.event == event)
.map(|l| dup(cx, l.f))
.collect();
for f in fs {
let mut args = [payload];
let ok = reactive::untracked_with(cx, || call(cx, f, &mut args));
if !ok {
crate::web::js::report_exception(cx);
}
free(cx, f);
}
free(cx, payload);
}
fn state_changed(
cx: *mut JsContext,
window: NodeId,
old: Option<&WindowState>,
new: Option<&WindowState>,
) {
let (Some(old), Some(new)) = (old, new) else {
return;
};
if old.focused != new.focused {
let o = new_object(cx);
set(cx, o, c"focused", boolean(new.focused));
fire(cx, window, Event::FocusChange, o);
}
if old.mode != new.mode || old.minimized != new.minimized {
let o = new_object(cx);
set(cx, o, c"mode", encode(cx, &new.mode));
set(cx, o, c"minimized", boolean(new.minimized));
fire(cx, window, Event::ModeChange, o);
}
if old.bounds != new.bounds {
let o = new_object(cx);
set(cx, o, c"bounds", encode(cx, &new.bounds));
fire(cx, window, Event::BoundsChange, o);
}
if old.display != new.display
&& let Some(id) = new.display
&& let Some(d) = crate::env::display_list(cx)
.into_iter()
.find(|d| d.id == id)
{
let o = new_object(cx);
set(cx, o, c"display", encode(cx, &d));
fire(cx, window, Event::DisplayChange, o);
}
}
pub(crate) fn displays_changed(cx: *mut JsContext) {
let states: Vec<(JsValue, WindowState)> = windows(cx)
.list
.borrow()
.iter()
.filter_map(|w| w.last.map(|state| (dup(cx, w.state), state)))
.collect();
for (signal, state) in states {
reactive::set_signal(cx, signal, state_value(cx, Some(&state)));
free(cx, signal);
}
}
pub(crate) fn platform_event(cx: *mut JsContext, id: NodeId, event: &WindowEvent) {
match event {
WindowEvent::State(state) => {
let state = **state;
let (signal, previous) = {
let mut list = windows(cx).list.borrow_mut();
let Some(w) = list.iter_mut().find(|w| w.id == id) else {
return;
};
(dup(cx, w.state), w.last.replace(state))
};
reactive::set_signal(cx, signal, state_value(cx, Some(&state)));
free(cx, signal);
state_changed(cx, id, previous.as_ref(), Some(&state));
}
WindowEvent::TitleBar(area) => {
let signal = windows(cx)
.list
.borrow()
.iter()
.find(|w| w.id == id)
.map(|w| dup(cx, w.title_bar));
let Some(signal) = signal else {
return;
};
reactive::set_signal(cx, signal, title_bar_value(cx, area));
free(cx, signal);
}
WindowEvent::ClosePress { reason } => {
let o = new_object(cx);
set(cx, o, c"reason", encode(cx, reason));
fire(cx, id, Event::ClosePress, o);
}
WindowEvent::Closed { cause } => {
if id != ROOT_ID {
let r = close_own(cx, id, Some(*cause));
if is_exception(r) {
crate::web::js::report_exception(cx);
}
}
}
WindowEvent::QuitRequest | WindowEvent::Reopen => {}
}
let r = reactive::finish_value(cx, UNDEFINED);
if is_exception(r) {
crate::web::js::report_exception(cx);
}
}
fn close_own(cx: *mut JsContext, id: NodeId, cause: Option<WindowCloseCause>) -> JsValue {
enum Then {
Opened(JsValue),
Element(JsValue),
}
let then = {
let list = windows(cx).list.borrow();
let Some(w) = list.iter().find(|w| w.id == id) else {
return UNDEFINED;
};
match &w.how {
How::Opened { node, .. } => Then::Opened(dup(cx, *node)),
How::Element { close, .. } => Then::Element(dup(cx, *close)),
}
};
match then {
Then::Opened(node) => {
unregister(cx, id);
let r = dom::remove_node(cx, dom::root_value(cx), node);
free(cx, node);
match r {
Ok(()) => reactive::finish_value(cx, UNDEFINED),
Err(e) => e,
}
}
Then::Element(close) => {
let reason = encode(cx, &cause.unwrap_or(WindowCloseCause::Close));
let ok = call(cx, close, &mut [reason]);
free(cx, reason);
free(cx, close);
if ok {
reactive::finish_value(cx, UNDEFINED)
} else {
qjs::JS_EXCEPTION
}
}
}
}
fn unregister(cx: *mut JsContext, id: NodeId) {
let removed = {
let mut list = windows(cx).list.borrow_mut();
list.iter().position(|w| w.id == id).map(|i| list.remove(i))
};
let Some(w) = removed else {
return;
};
reactive::set_signal(cx, w.state, qjs::JS_NULL);
bump(cx);
fire(cx, id, Event::Close, UNDEFINED);
let gone: Vec<Listener> = {
let mut listeners = windows(cx).listeners.borrow_mut();
let (gone, kept) = listeners.drain(..).partition(|l| l.window == id);
*listeners = kept;
gone
};
for l in gone {
free(cx, l.f);
}
free(cx, w.handle);
free(cx, w.state);
free(cx, w.title_bar);
match w.how {
How::Opened { node, owner } => {
free(cx, node);
reactive::dispose_owner(cx, owner);
}
How::Element { close, owner } => {
free(cx, close);
reactive::dispose_owner(cx, owner);
}
}
}
fn checked_options(cx: *mut JsContext, what: &str, o: JsValue) -> Result<WindowOptions, JsValue> {
if !windows_supported(cx) {
let error = JsError::Error(format!(
"{what}: windows of the app's own open on the desktops (`isAvailable`)"
))
.with_code(codes::UNAVAILABLE);
return Err(throw(cx, error));
}
let options =
decode::<WindowOptions>(cx, o).map_err(|e| type_error(cx, &format!("{what}: {e}")))?;
let open = windows(cx).list.borrow().len();
let checked = crate::dom::shared(cx).system_access.borrow().check_window(
options.kind,
&options.attributes,
open,
);
if let Err(message) = checked {
let error =
JsError::access_denied(format!("{what}: {message}"), "windows", options.id.clone());
return Err(throw(cx, error));
}
Ok(options)
}
fn open_node(
cx: *mut JsContext,
options: WindowOptions,
how: impl FnOnce(JsValue) -> How,
) -> Result<(JsValue, NodeId, JsValue), JsValue> {
let node = dom::create_element(cx, Tag::Modal)?;
let Some(id) = dom::node_id(cx, node) else {
free(cx, node);
return Err(type_error(cx, "Window: no node"));
};
let parent = options
.parent
.as_deref()
.and_then(|p| node_of_key(cx, p))
.and_then(|p| handle_of(cx, p))
.unwrap_or(qjs::JS_NULL);
let key = options.id.clone();
let mut open = OpenWindow::default();
open.options = options;
if let Err(e) = send(cx, id, DesktopCommand::OpenWindow(Box::new(open))) {
free(cx, parent);
free(cx, node);
return Err(e);
}
let handle = make_handle(cx, &key, id, parent);
free(cx, parent);
let dismissed = native(cx, on_dismiss, 0, 0, &mut [number(id as f64)]);
let r = dom::set_handler(cx, id, EventKind::Dismiss, dismissed);
free(cx, dismissed);
if let Err(e) = r {
free(cx, handle);
free(cx, node);
return Err(e);
}
windows(cx).list.borrow_mut().push(Own {
key,
id,
handle: dup(cx, handle),
state: reactive::new_signal(cx, qjs::JS_NULL),
last: None,
title_bar: {
let zeros = title_bar_value(cx, &Edges::default());
let signal = reactive::new_signal(cx, zeros);
free(cx, zeros);
signal
},
how: how(node),
});
bump(cx);
Ok((node, id, handle))
}
pub(crate) struct Implicit {
pub(crate) node: JsValue,
handle: JsValue,
id: NodeId,
}
pub(crate) fn begin_implicit(cx: *mut JsContext) -> Result<Implicit, JsValue> {
let node = dom::create_element(cx, Tag::Modal)?;
let Some(id) = dom::node_id(cx, node) else {
free(cx, node);
return Err(type_error(cx, "mount: no node for the first window"));
};
let handle = make_handle(cx, "main", id, qjs::JS_NULL);
set(cx, handle, c"implicit", qjs::JS_TRUE);
*windows(cx).pending.borrow_mut() = Some((id, WindowAttributes::default(), Vec::new()));
reactive::provide(cx, context_key(cx), handle);
Ok(Implicit { node, handle, id })
}
pub(crate) fn discard_implicit(cx: *mut JsContext, implicit: Implicit) {
windows(cx).pending.borrow_mut().take();
let _ = dom::remove_node(cx, dom::root_value(cx), implicit.node);
free(cx, implicit.node);
windows(cx).unopened.borrow_mut().push(implicit.handle);
}
pub(crate) fn forget_implicit(cx: *mut JsContext, implicit: Implicit) {
windows(cx).pending.borrow_mut().take();
windows(cx).unopened.borrow_mut().push(implicit.handle);
free(cx, implicit.node);
}
pub(crate) fn finish_implicit(cx: *mut JsContext, implicit: Implicit, mount_owner: u32) {
let (attributes, requests) = windows(cx).pending.borrow_mut().take().map_or_else(
|| (WindowAttributes::default(), Vec::new()),
|(_, attributes, requests)| (attributes, requests),
);
let mut options = WindowOptions::default();
options.id = "main".to_owned();
options.attributes = attributes;
let mut open = OpenWindow::default();
open.implicit = true;
open.options = options;
if let Err(e) = send(cx, implicit.id, DesktopCommand::OpenWindow(Box::new(open))) {
windows(cx).unopened.borrow_mut().push(implicit.handle);
free(cx, e);
return;
}
for request in requests {
if let Err(e) = send(cx, implicit.id, DesktopCommand::Request(request)) {
free(cx, e);
}
}
let (owner, ()) = reactive::with_root(cx, || ());
windows(cx).list.borrow_mut().push(Own {
key: "main".to_owned(),
id: implicit.id,
handle: implicit.handle,
state: reactive::new_signal(cx, qjs::JS_NULL),
last: None,
title_bar: {
let zeros = title_bar_value(cx, &Edges::default());
let signal = reactive::new_signal(cx, zeros);
free(cx, zeros);
signal
},
how: How::Opened {
node: dup(cx, implicit.node),
owner,
},
});
reactive::with_root_under(cx, mount_owner, || {
reactive::add_cleanup(
cx,
native(cx, element_gone, 0, 0, &mut [number(implicit.id as f64)]),
);
});
bump(cx);
free(cx, implicit.node);
}
unsafe extern "C" fn on_dismiss(
cx: *mut JsContext,
_this: JsValue,
_argc: c_int,
_argv: *mut JsValue,
_magic: c_int,
data: *mut JsValue,
) -> JsValue {
let id = as_number(cx, unsafe { *data }).unwrap_or(0.0) as NodeId;
close_own(cx, id, Some(WindowCloseCause::ClosePress))
}
unsafe extern "C" fn open_window(
cx: *mut JsContext,
_this: JsValue,
argc: c_int,
argv: *mut JsValue,
) -> JsValue {
let (component, o) = (arg(argc, argv, 0), arg(argc, argv, 1));
if !unsafe { qjs::JS_IsFunction(cx, component) } {
return type_error(cx, "Window.open: the first argument is a component");
}
if !unsafe { qjs::JS_IsObject(o) } {
return type_error(cx, "Window.open: options with an id");
}
let id = get(cx, o, c"id");
let key = dom::string_arg(cx, id);
free(cx, id);
if let Some(open) = key.as_deref().and_then(|k| node_of_key(cx, k))
&& let Some(handle) = handle_of(cx, open)
{
if let Err(e) = send(cx, open, DesktopCommand::Request(WindowRequest::Focus)) {
free(cx, handle);
return e;
}
return handle;
}
let options = match checked_options(cx, "Window.open", o) {
Ok(options) => options,
Err(e) => return e,
};
let (node, id, handle) = match open_node(cx, options, |node| How::Opened {
node: dup(cx, node),
owner: 0,
}) {
Ok(opened) => opened,
Err(e) => return e,
};
let (owner, rendered) = reactive::with_root(cx, || {
reactive::provide(cx, context_key(cx), handle);
let mut args = [new_object(cx)];
let out = reactive::untracked_with(cx, || {
unsafe { qjs::JS_Call(cx, component, UNDEFINED, 1, args.as_mut_ptr()) }
});
free(cx, args[0]);
if is_exception(out) {
return Err(out);
}
let r = dom::insert_value_at(cx, node, out, None);
free(cx, out);
r
});
if let Some(w) = windows(cx)
.list
.borrow_mut()
.iter_mut()
.find(|w| w.id == id)
&& let How::Opened { owner: o, .. } = &mut w.how
{
*o = owner;
}
let inserted = rendered.and_then(|()| dom::insert_node(cx, dom::root_value(cx), node, None));
free(cx, node);
if let Err(e) = inserted {
reactive::dispose_owner(cx, owner);
unregister(cx, id);
free(cx, handle);
return e;
}
reactive::finish_value(cx, handle)
}
unsafe extern "C" fn get_window(
cx: *mut JsContext,
_this: JsValue,
argc: c_int,
argv: *mut JsValue,
) -> JsValue {
let Some(key) = dom::string_arg(cx, arg(argc, argv, 0)) else {
return type_error(cx, "Window.get: the id is a string");
};
free(cx, read(cx, version(cx)));
node_of_key(cx, &key)
.and_then(|id| handle_of(cx, id))
.unwrap_or(qjs::JS_NULL)
}
unsafe extern "C" fn window_component(
cx: *mut JsContext,
_this: JsValue,
argc: c_int,
argv: *mut JsValue,
) -> JsValue {
let props = arg(argc, argv, 0);
dom::note_declared_window(cx);
if !windows_supported(cx) {
let run = native(cx, mobile_window_run, 0, 0, &mut [props]);
let shown = reactive::memo(cx, run, false);
return reactive::finish_value(cx, shown);
}
let state = new_object(cx);
let tick = reactive::new_signal(cx, number(0.0));
let run = native(cx, window_run, 0, 0, &mut [props, state, tick]);
free(cx, state);
free(cx, tick);
let shown = reactive::memo(cx, run, false);
reactive::finish_value(cx, shown)
}
unsafe extern "C" fn mobile_window_run(
cx: *mut JsContext,
_this: JsValue,
_argc: c_int,
_argv: *mut JsValue,
_magic: c_int,
data: *mut JsValue,
) -> JsValue {
let props = unsafe { *data };
if !window_is_open(cx, props) {
return qjs::JS_NULL;
}
get(cx, props, c"children")
}
fn window_is_open(cx: *mut JsContext, props: JsValue) -> bool {
let open = get(cx, props, c"open");
if nullish(open) {
let default = get(cx, props, c"defaultOpen");
let closed = unsafe { qjs::JS_IsBool(default) } && !truthy(cx, default);
free(cx, default);
!closed
} else {
let on = truthy(cx, open);
free(cx, open);
on
}
}
fn flag(cx: *mut JsContext, state: JsValue, key: &CStr) -> bool {
let v = get(cx, state, key);
let on = truthy(cx, v);
free(cx, v);
on
}
unsafe extern "C" fn window_run(
cx: *mut JsContext,
_this: JsValue,
_argc: c_int,
_argv: *mut JsValue,
_magic: c_int,
data: *mut JsValue,
) -> JsValue {
let (props, state, tick) = unsafe { (*data, *data.add(1), *data.add(2)) };
free(cx, read(cx, tick));
let open = window_is_open(cx, props);
if !open {
set(cx, state, c"closed", qjs::JS_FALSE);
return qjs::JS_NULL;
}
if flag(cx, state, c"closed") {
return qjs::JS_NULL;
}
reactive::untracked_with(cx, || {
let options = match checked_options(cx, "<Window>", props) {
Ok(options) => options,
Err(e) => return e,
};
if node_of_key(cx, &options.id).is_some() {
log::warn!(
target: "js",
"<Window>: a window with id {:?} is open already; one window per id",
options.id
);
return qjs::JS_NULL;
}
let close = native(cx, element_close, 1, 0, &mut [props, state, tick]);
let (owner, ()) = reactive::with_root(cx, || ());
let opened = open_node(cx, options, |_| How::Element { close, owner });
let (node, id, handle) = match opened {
Ok(opened) => opened,
Err(e) => {
reactive::dispose_owner(cx, owner);
return e;
}
};
reactive::add_cleanup(cx, native(cx, element_gone, 0, 0, &mut [number(id as f64)]));
let (_, result) = reactive::with_root_under(cx, owner, || -> Result<(), JsValue> {
reactive::provide(cx, context_key(cx), handle);
let target = get(cx, props, c"ref");
let r = dom::apply_ref(cx, target, handle);
free(cx, target);
r?;
for (prop, event) in [
(c"onFocusChange", Event::FocusChange),
(c"onModeChange", Event::ModeChange),
(c"onBoundsChange", Event::BoundsChange),
(c"onDisplayChange", Event::DisplayChange),
(c"onClosePress", Event::ClosePress),
] {
let name = string(cx, prop.to_str().unwrap_or(""));
let f = native(cx, call_prop, 1, 0, &mut [props, name]);
free(cx, name);
add_listener(cx, id, event, f);
}
let effect = native(cx, attributes_effect, 0, 0, &mut [props, number(id as f64)]);
free(cx, reactive::render_effect(cx, effect));
let children = get(cx, props, c"children");
let r = dom::insert_value_at(cx, node, children, None);
free(cx, children);
r
});
free(cx, handle);
match result {
Ok(()) => node,
Err(e) => {
unregister(cx, id);
free(cx, node);
e
}
}
})
}
unsafe extern "C" fn call_prop(
cx: *mut JsContext,
_this: JsValue,
argc: c_int,
argv: *mut JsValue,
_magic: c_int,
data: *mut JsValue,
) -> JsValue {
let (props, name) = unsafe { (*data, *data.add(1)) };
let Some(name) = dom::string_arg(cx, name) else {
return UNDEFINED;
};
let Ok(name) = std::ffi::CString::new(name) else {
return UNDEFINED;
};
let f = get(cx, props, &name);
let r = if unsafe { qjs::JS_IsFunction(cx, f) } {
let mut args = [arg(argc, argv, 0)];
unsafe { qjs::JS_Call(cx, f, UNDEFINED, 1, args.as_mut_ptr()) }
} else {
UNDEFINED
};
free(cx, f);
r
}
unsafe extern "C" fn attributes_effect(
cx: *mut JsContext,
_this: JsValue,
_argc: c_int,
_argv: *mut JsValue,
_magic: c_int,
data: *mut JsValue,
) -> JsValue {
let (props, id) = unsafe { (*data, *data.add(1)) };
let id = as_number(cx, id).unwrap_or(0.0) as NodeId;
match decode::<WindowAttributes>(cx, props) {
Ok(attributes) if !attributes.is_empty() => {
match send(cx, id, DesktopCommand::SetAttributes(Box::new(attributes))) {
Ok(()) => UNDEFINED,
Err(e) => e,
}
}
Ok(_) => UNDEFINED,
Err(e) => {
log::warn!(target: "js", "<Window>: {e}");
UNDEFINED
}
}
}
unsafe extern "C" fn element_close(
cx: *mut JsContext,
_this: JsValue,
argc: c_int,
argv: *mut JsValue,
_magic: c_int,
data: *mut JsValue,
) -> JsValue {
let (props, state, tick) = unsafe { (*data, *data.add(1), *data.add(2)) };
set(cx, state, c"closed", qjs::JS_TRUE);
let n = reactive::peek_number(cx, tick).unwrap_or(0.0);
reactive::set_signal(cx, tick, number(n + 1.0));
let f = get(cx, props, c"onOpenChange");
let r = if unsafe { qjs::JS_IsFunction(cx, f) } {
let event = new_object(cx);
set(cx, event, c"open", qjs::JS_FALSE);
set(cx, event, c"reason", dup(cx, arg(argc, argv, 0)));
let mut args = [event];
let r = unsafe { qjs::JS_Call(cx, f, UNDEFINED, 1, args.as_mut_ptr()) };
free(cx, event);
r
} else {
UNDEFINED
};
free(cx, f);
r
}
unsafe extern "C" fn element_gone(
cx: *mut JsContext,
_this: JsValue,
_argc: c_int,
_argv: *mut JsValue,
_magic: c_int,
data: *mut JsValue,
) -> JsValue {
let id = as_number(cx, unsafe { *data }).unwrap_or(0.0) as NodeId;
unregister(cx, id);
UNDEFINED
}
pub(crate) fn window_export(cx: *mut JsContext) -> JsValue {
let window = reactive::new_function(cx, c"Window", window_component, 1);
let open = reactive::new_function(cx, c"open", open_window, 2);
set(cx, window, c"open", open);
let get = reactive::new_function(cx, c"get", get_window, 1);
set(cx, window, c"get", get);
window
}
fn current(cx: *mut JsContext) -> Option<JsValue> {
if !reactive::has_owner(cx) {
return None;
}
Some(reactive::lookup(cx, context_key(cx)).unwrap_or_else(|| {
if dom::inline_root(cx) {
surface_ref(cx)
} else {
main_ref(cx)
}
}))
}
pub(crate) fn focused_node(cx: *mut JsContext) -> Option<NodeId> {
windows(cx)
.list
.borrow()
.iter()
.find(|w| w.last.as_ref().is_some_and(|state| state.focused))
.map(|w| w.id)
}
pub(crate) fn windows_supported(cx: *mut JsContext) -> bool {
let env = crate::env::current(cx);
env.os.is_desktop() && env.idiom != nsh_core::platform::Idiom::Terminal
}
pub(crate) unsafe extern "C" fn current_window(
cx: *mut JsContext,
_this: JsValue,
_argc: c_int,
_argv: *mut JsValue,
) -> JsValue {
current(cx).unwrap_or_else(|| {
type_error(
cx,
"currentWindow() reads the window a component renders in: call it while rendering and keep the WindowRef it returns",
)
})
}
pub(crate) unsafe extern "C" fn all_windows(
cx: *mut JsContext,
_this: JsValue,
_argc: c_int,
_argv: *mut JsValue,
) -> JsValue {
free(cx, read(cx, version(cx)));
let mut handles: Vec<JsValue> = Vec::new();
if !windows_supported(cx) {
handles.push(main_ref(cx));
}
handles.extend(windows(cx).list.borrow().iter().map(|w| dup(cx, w.handle)));
unsafe { qjs::JS_NewArrayFrom(cx, handles.len() as c_int, handles.as_mut_ptr()) }
}
pub(crate) unsafe extern "C" fn focused_window(
cx: *mut JsContext,
_this: JsValue,
_argc: c_int,
_argv: *mut JsValue,
) -> JsValue {
free(cx, read(cx, version(cx)));
let ids: Vec<NodeId> = windows(cx).list.borrow().iter().map(|w| w.id).collect();
let mut focused = None;
for id in ids {
let state = state_of(cx, id);
let f = get(cx, state, c"focused");
if focused.is_none() && truthy(cx, f) {
focused = Some(id);
}
free(cx, f);
free(cx, state);
}
focused
.and_then(|id| handle_of(cx, id))
.unwrap_or(qjs::JS_NULL)
}
pub(crate) unsafe extern "C" fn window_state(
cx: *mut JsContext,
_this: JsValue,
_argc: c_int,
_argv: *mut JsValue,
) -> JsValue {
if !windows_supported(cx) {
return qjs::JS_NULL;
}
let Some(id) = current_ref_id(cx) else {
return type_error(
cx,
"windowState() reads the current window: call it inside <Window>, or use focusedWindow()/windows() in the app root",
);
};
state_of(cx, id)
}
pub(crate) unsafe extern "C" fn title_bar(
cx: *mut JsContext,
_this: JsValue,
_argc: c_int,
_argv: *mut JsValue,
) -> JsValue {
if !windows_supported(cx) {
return title_bar_value(cx, &Edges::default());
}
let Some(id) = current_ref_id(cx) else {
return type_error(
cx,
"titleBar() reads the current window: call it inside <Window>, or use focusedWindow()/windows() in the app root",
);
};
title_bar_of(cx, id)
}
pub(crate) fn current_node(cx: *mut JsContext) -> Option<NodeId> {
current_id(cx)
}
fn current_id(cx: *mut JsContext) -> Option<NodeId> {
if !reactive::has_owner(cx) {
return None;
}
let handle = reactive::lookup(cx, context_key(cx))?;
let implicit = get(cx, handle, c"implicit");
let is_implicit = truthy(cx, implicit);
free(cx, implicit);
if is_implicit {
free(cx, handle);
return None;
}
let id = key_node(cx, handle);
free(cx, handle);
id
}
fn current_ref_id(cx: *mut JsContext) -> Option<NodeId> {
if !reactive::has_owner(cx) {
return None;
}
let handle = reactive::lookup(cx, context_key(cx))?;
let pending = {
let implicit = get(cx, handle, c"implicit");
let is_implicit = truthy(cx, implicit);
free(cx, implicit);
if is_implicit {
windows(cx).pending.borrow().as_ref().map(|(id, _, _)| *id)
} else {
None
}
};
let id = pending.or_else(|| key_node(cx, handle));
free(cx, handle);
id
}
fn key_node(cx: *mut JsContext, handle: JsValue) -> Option<NodeId> {
let id = get(cx, handle, c"id");
let key = dom::string_arg(cx, id);
free(cx, id);
key.and_then(|k| node_of_key(cx, &k))
}