pub mod inspect;
pub mod mcp;
pub mod script;
use aylis_core::platform::LocaleInfo;
use aylis_core::{ComponentId, EventArgs, Key, ModalResult, PointerButton, PropEnum, PropValue, Theme, Ui};
use aylis_data::json::{json_to_value, value_to_json};
use aylis_data::{DataSet, data_set};
use serde_json::{Map, Value, json};
thread_local! {
static CURRENT_ID: std::cell::RefCell<Option<Value>> = const { std::cell::RefCell::new(None) };
static PROFILE_MARKS: std::cell::RefCell<Vec<(String, f64)>> = const { std::cell::RefCell::new(Vec::new()) };
}
pub fn register_all(ui: &mut Ui) {
aylis_controls::register(ui);
aylis_data::register(ui);
aylis_dbcontrols::register(ui);
aylis_providers::register(ui);
aylis_media::register(ui);
aylis_vector::register(ui);
aylis_markdown::register(ui);
aylis_webview::register(ui);
mcp::register(ui);
}
pub const PROTOCOL_VERSION: u64 = 1;
pub const CAPABILITIES: &[&str] = &["wake", "exception", "deflate", "ask"];
pub const COMPRESS_ABOVE: usize = 1024;
pub fn compress(text: &str) -> Vec<u8> {
miniz_oxide::deflate::compress_to_vec(text.as_bytes(), 6)
}
pub fn decompress(bytes: &[u8]) -> Option<String> {
let raw = miniz_oxide::inflate::decompress_to_vec(bytes).ok()?;
String::from_utf8(raw).ok()
}
pub fn announces_deflate(line: &str) -> bool {
line.contains("\"capabilities\"")
&& serde_json::from_str::<Value>(line).ok().is_some_and(|m| {
m["result"]["capabilities"].as_array().is_some_and(|c| c.iter().any(|v| v == "deflate"))
})
}
#[derive(Default)]
pub struct Handshake {
greeted: bool,
}
impl Handshake {
pub fn check(&mut self, message: &Value) -> Option<Value> {
if message.get("cmd").and_then(Value::as_str) == Some("hello") {
self.greeted = true;
return None;
}
if self.greeted {
return None;
}
let error = "o backend não se apresentou: a primeira mensagem tem de ser {\"cmd\": \"hello\", \"protocol\": 1} (atualize o cliente do aylis)";
Some(match message.get("id") {
Some(id) => json!({ "id": id, "error": error }),
None => json!({ "event": "error", "cmd": message.get("cmd").cloned().unwrap_or(Value::Null), "message": error }),
})
}
}
fn hello(ui: &mut Ui, obj: &Map<String, Value>) -> Result<Value, String> {
let theirs = obj.get("protocol").and_then(Value::as_u64).ok_or("hello: falta \"protocol\" (a versão do protocolo do cliente)")?;
let done = obj.get("features").and_then(Value::as_array).is_some_and(|f| f.iter().any(|v| v == "done"));
ui.extension::<Remote>().busy.tracking = done;
if theirs == PROTOCOL_VERSION {
return Ok(json!({ "protocol": PROTOCOL_VERSION, "capabilities": CAPABILITIES }));
}
let update_url = obj.get("update_url").and_then(Value::as_str).unwrap_or_default();
let (user, backend) = if theirs > PROTOCOL_VERSION {
let mut text = aylis_core::messages::t("Há uma versão nova do aplicativo. Atualize para continuar.").to_string();
if !update_url.is_empty() {
text = format!("{text}\n{update_url}");
}
(text, format!("a tela é do protocolo {PROTOCOL_VERSION} e o backend do {theirs}: atualize o aplicativo (a tela)"))
} else {
(
aylis_core::messages::t("O servidor do aplicativo é de uma versão anterior. Tente de novo mais tarde.").to_string(),
format!("a tela é do protocolo {PROTOCOL_VERSION} e o backend do {theirs}: atualize o backend (o cliente do aylis)"),
)
};
ui.raise_exception(aylis_core::Exception { message: user, path: String::new(), event: "hello".into() });
Err(backend)
}
pub fn handle_message(ui: &mut Ui, message: &Value) -> Option<Value> {
if message.get("cmd").and_then(Value::as_str) == Some("wake") {
return Some(json!({ "event": "__post" }));
}
let id = message.get("id").cloned();
CURRENT_ID.with(|c| *c.borrow_mut() = id.clone());
let result = execute(ui, message);
let deferred = id.is_some() && CURRENT_ID.with(|c| c.borrow_mut().take()).is_none();
if deferred {
return result.err().map(|error| json!({ "event": "error", "cmd": message.get("cmd").cloned().unwrap_or(Value::Null), "message": error }));
}
match (id, result) {
(Some(id), Ok(result)) => Some(json!({ "id": id, "result": result })),
(Some(id), Err(error)) => Some(json!({ "id": id, "error": error })),
(None, Ok(_)) => None,
(None, Err(error)) => {
let cmd = message.get("cmd").cloned().unwrap_or(Value::Null);
Some(json!({ "event": "error", "cmd": cmd, "message": error }))
}
}
}
pub fn execute(ui: &mut Ui, command: &Value) -> Result<Value, String> {
let obj = command.as_object().ok_or("o comando deve ser um objeto JSON")?;
let cmd = obj.get("cmd").and_then(Value::as_str).ok_or("falta \"cmd\"")?;
match cmd {
"load" => {
let layout = obj.get("layout").ok_or("falta \"layout\"")?;
let document = if layout.get("root").is_some() { layout.clone() } else { json!({ "root": layout }) };
let parent = optional_target(ui, obj.get("parent"))?;
let id = aylis_json::load_value(ui, &document, parent).map_err(|e| e.to_string())?;
aylis_data::open_active(ui);
Ok(json!({ "id": id.to_u64(), "name": ui.name(id) }))
}
"create" => {
let class = string(obj, "class")?;
let parent = optional_target(ui, obj.get("parent"))?;
let id = ui.create(class, parent).map_err(|e| e.to_string())?;
let setup = (|| {
if let Some(name) = obj.get("name").and_then(Value::as_str) {
ui.set_name(id, name).map_err(|e| e.to_string())?;
}
if let Some(props) = obj.get("props") {
let props = props.as_object().ok_or("\"props\" deve ser um objeto")?;
ui.without_change_events(|ui| aylis_json::set_props(ui, id, props))?;
}
Ok::<_, String>(())
})();
if let Err(e) = setup {
ui.free(id);
return Err(e);
}
aylis_data::open_active(ui);
Ok(json!({ "id": id.to_u64() }))
}
"set" => {
let id = target(ui, obj)?;
if let Some(name) = obj.get("prop").and_then(Value::as_str) {
let value = obj.get("value").ok_or("falta \"value\" (com \"prop\")")?;
let mut one = Map::new();
one.insert(name.to_string(), value.clone());
aylis_json::set_props(ui, id, &one)?;
return Ok(Value::Null);
}
let props = obj.get("props").and_then(Value::as_object).ok_or("informe \"prop\" e \"value\", ou \"props\" (objeto)")?;
aylis_json::set_props(ui, id, props)?;
Ok(Value::Null)
}
"get" => {
let id = target(ui, obj)?;
if let Some(name) = obj.get("prop").and_then(Value::as_str) {
if obj.get("ref").and_then(Value::as_bool) == Some(true) {
let value = ui.get_prop(id, name).map_err(|e| e.to_string())?;
return Ok(match value.to_component() {
Ok(Some(c)) => json!({ "id": c.to_u64(), "name": ui.name(c).unwrap_or_default(), "class": ui.class_name(c).unwrap_or_default() }),
_ => Value::Null,
});
}
return get(ui, id, name);
}
let names = obj.get("props").and_then(Value::as_array).ok_or("informe \"prop\" ou \"props\"")?;
let mut out = Map::new();
for name in names {
let name = name.as_str().ok_or("\"props\" deve ser uma lista de nomes")?;
out.insert(name.to_string(), get(ui, id, name)?);
}
Ok(Value::Object(out))
}
"free" => {
let id = target(ui, obj)?;
ui.free(id);
Ok(Value::Null)
}
"invoke" => {
let id = target(ui, obj)?;
let method = string(obj, "method")?;
let args: Vec<PropValue> = obj.get("args").and_then(Value::as_array).map(|a| a.iter().map(json_arg).collect()).unwrap_or_default();
let result = ui.invoke(id, method, &args)?;
if obj.get("ref").and_then(Value::as_bool) == Some(true) {
return Ok(match result.to_component() {
Ok(Some(c)) => json!({ "id": c.to_u64(), "name": ui.name(c).unwrap_or_default(), "class": ui.class_name(c).unwrap_or_default() }),
_ => Value::Null,
});
}
Ok(aylis_json::prop_to_json(ui, &result))
}
"click" => {
let id = target(ui, obj)?;
if !["modifiers", "x", "y", "button", "double"].iter().any(|k| obj.contains_key(*k)) {
ui.click(id);
return Ok(Value::Null);
}
let (root, at) = point_in(ui, id, obj.get("x"), obj.get("y"))?;
let mut event = aylis_core::PointerEvent { position: at, button: button(obj)?, modifiers: modifiers(obj)?, click_count: 1, ..Default::default() };
ui.pointer_move(root, event);
ui.pointer_down(root, event);
ui.pointer_up(root, event);
if obj.get("double").and_then(Value::as_bool) == Some(true) {
event.click_count = 2;
ui.pointer_down(root, event);
ui.pointer_up(root, event);
}
ui.process_events();
Ok(Value::Null)
}
"drag" => {
let from = resolve(ui, obj.get("from").ok_or("falta \"from\"")?)?;
let (root, start) = point_in(ui, from, obj.get("from_x"), obj.get("from_y"))?;
let steps = obj.get("steps").and_then(Value::as_u64).unwrap_or(10).clamp(1, 200) as usize;
let event = aylis_core::PointerEvent { position: start, button: button(obj)?, modifiers: modifiers(obj)?, click_count: 1, ..Default::default() };
let Some(to) = obj.get("to") else {
let screen = |k: &str| obj.get(k).and_then(Value::as_i64).map(|v| v as i32).ok_or(format!("drag sem \"to\": falta \"{k}\""));
let (x, y) = (screen("screen_x")?, screen("screen_y")?);
start_drag_between_forms(ui, root, start, event)?;
ui.drag_over_form(root, None, aylis_core::PointF::default());
ui.set_screen_pointer(Some(aylis_core::ScreenPointer { x, y, scale: 1.0 }));
ui.pointer_up(root, aylis_core::PointerEvent { position: aylis_core::PointF::new(-1.0e6, -1.0e6), ..event });
ui.process_events();
return Ok(Value::Null);
};
let to = resolve(ui, to)?;
let (end_root, end) = point_in(ui, to, obj.get("to_x"), obj.get("to_y"))?;
if end_root != root {
start_drag_between_forms(ui, root, start, event)?;
for _ in 0..steps.min(3) {
ui.drag_over_form(root, Some(end_root), end);
ui.process_events();
}
ui.pointer_up(root, aylis_core::PointerEvent { position: aylis_core::PointF::new(-1.0e6, -1.0e6), ..event });
ui.process_events();
return Ok(Value::Null);
}
ui.pointer_move(root, event);
ui.pointer_down(root, event);
for i in 1..=steps {
let t = i as f32 / steps as f32;
let at = aylis_core::PointF::new(start.x + (end.x - start.x) * t, start.y + (end.y - start.y) * t);
ui.pointer_move(root, aylis_core::PointerEvent { position: at, ..event });
ui.process_events();
}
ui.pointer_up(root, aylis_core::PointerEvent { position: end, ..event });
ui.process_events();
Ok(Value::Null)
}
"drag_window" => {
let form = resolve(ui, obj.get("form").ok_or("falta \"form\"")?)?;
let form = ui.root_of(form);
let to = resolve(ui, obj.get("to").ok_or("falta \"to\"")?)?;
let (end_root, end) = point_in(ui, to, obj.get("to_x"), obj.get("to_y"))?;
if end_root == form {
return Err("drag_window: \"to\" está no próprio form".into());
}
ui.window_drag_over(form, Some((end_root, end)));
let received = ui.window_drag_end(form);
ui.process_events();
Ok(json!({ "received": received }))
}
"items" => {
let id = target(ui, obj)?;
let items: Vec<Value> = ui
.access_items(id)
.into_iter()
.map(|it| {
serde_json::json!({
"role": format!("{:?}", it.role).to_lowercase(),
"name": it.name,
"selected": it.selected,
"checked": it.checked,
"disabled": it.disabled,
"parent": it.parent,
"row": it.row,
"column": it.column,
"level": it.level,
"expanded": it.expanded,
})
})
.collect();
Ok(Value::Array(items))
}
"click_item" => {
use aylis_core::automation::ItemAction;
let id = target(ui, obj)?;
let action = match obj.get("action").and_then(Value::as_str).unwrap_or("click") {
"select" => ItemAction::Select,
"expand" => ItemAction::Expand,
"collapse" => ItemAction::Collapse,
_ => ItemAction::Click,
};
let named = obj.get("item").and_then(Value::as_str).and_then(|name| ui.access_items(id).iter().position(|i| i.name.trim().eq_ignore_ascii_case(name.trim())));
let done = match (named, obj.get("index").and_then(Value::as_u64)) {
(Some(i), _) => ui.access_action(id, i, action),
(None, Some(i)) => ui.access_action(id, i as usize, action),
_ => return Err("click_item: informe \"item\" (o texto) ou \"index\"".into()),
};
if done { Ok(Value::Null) } else { Err("item não encontrado ou sem ação".into()) }
}
"focus" => {
let id = target(ui, obj)?;
Ok(Value::Bool(ui.set_focus(id)))
}
"focused" => {
let form = target(ui, obj)?;
let root = ui.root_of(form);
Ok(ui.focused(root).and_then(|f| ui.name(f).map(|n| Value::String(n.to_string()))).unwrap_or(Value::Null))
}
"key" => {
let form = target(ui, obj)?;
let root = ui.root_of(form);
let (mut modifiers, key) = aylis_core::parse_shortcut(string(obj, "key")?).ok_or("tecla inválida (ex.: \"Enter\", \"Shift+Tab\", \"Ctrl+A\", \"Alt+S\")")?;
let extra = self::modifiers(obj)?;
modifiers.shift |= extra.shift;
modifiers.ctrl |= extra.ctrl;
modifiers.alt |= extra.alt;
modifiers.meta |= extra.meta;
let event = aylis_core::KeyEvent { key, modifiers, repeat: false };
ui.key_down(root, event);
ui.key_up(root, event);
Ok(Value::Null)
}
"type" => {
let form = target(ui, obj)?;
let root = ui.root_of(form);
ui.text_input(root, string(obj, "text")?);
Ok(Value::Null)
}
"focus_next" | "focus_prev" => {
let form = target(ui, obj)?;
let root = ui.root_of(form);
Ok(Value::Bool(ui.focus_next(root, cmd == "focus_next")))
}
"find" => {
let name = string(obj, "name")?;
Ok(ui.find(name).map_or(Value::Null, |id| Value::from(id.to_u64())))
}
"save" => {
let id = target(ui, obj)?;
aylis_json::save_value(ui, id).map_err(|e| e.to_string())
}
"theme" => {
let theme = match (obj.get("theme"), obj.get("name").and_then(Value::as_str)) {
(Some(theme), _) => aylis_json::theme_from_value(theme).map_err(|e| e.to_string())?,
(None, Some("dark")) => Theme::dark(),
(None, Some("light")) => Theme::light(),
_ => return Err("informe \"theme\" (JSON) ou \"name\": \"light\"/\"dark\"".into()),
};
ui.set_theme(theme);
Ok(Value::Null)
}
"locale" => {
let locale = match string(obj, "locale")? {
"pt-BR" | "pt_BR" | "pt" => LocaleInfo::pt_br(),
"en-US" | "en_US" | "en" => LocaleInfo::en_us(),
other => return Err(format!("idioma '{other}' não suportado (use pt-BR ou en-US)")),
};
ui.services_mut().locale = Some(Box::new(locale));
Ok(Value::Null)
}
"data" => data_command(ui, obj),
"batch" => {
let commands = obj.get("commands").and_then(Value::as_array).ok_or("falta \"commands\" (lista)")?;
Ok(Value::Array(
commands
.iter()
.map(|c| execute(ui, c).unwrap_or_else(|e| json!({ "error": e })))
.collect(),
))
}
"create_form" => {
let name = string(obj, "name")?;
let fresh = obj.get("new").and_then(Value::as_bool).unwrap_or(false);
let id = if fresh { ui.create_form_instance(name) } else { ui.create_form(name) }.map_err(|e| e.to_string())?;
aylis_data::open_active(ui);
Ok(json!({ "id": id.to_u64(), "name": ui.name(id) }))
}
"show" => {
let id = target(ui, obj)?;
ui.show(id).map_err(|e| e.to_string())?;
Ok(Value::Null)
}
"show_modal" => {
let id = target(ui, obj)?;
let handler = obj.get("handler").and_then(Value::as_str).map(str::to_string);
let reply = if obj.get("wait").and_then(Value::as_bool) == Some(true) { defer_reply(ui) } else { None };
let waiting = ui.backend_busy();
if waiting {
ui.backend_waiting_user(true);
}
ui.show_modal(id, move |ui, result| {
if waiting {
ui.backend_waiting_user(false);
}
if let Some(reply) = reply {
send_reply(ui, reply, Ok(json!(result.name())));
}
if let Some(handler) = handler {
let args = EventArgs::Value(PropValue::Enum(result.name().to_string()));
ui.call_handler(id, "on_modal_result", &handler, &args);
}
})
.map_err(|e| e.to_string())?;
Ok(Value::Null)
}
"components" => {
let form = target(ui, obj)?;
let mut out = Vec::new();
let mut stack = ui.children(form).to_vec();
while let Some(id) = stack.pop() {
if ui.form_of(id) != form {
continue;
}
if let Some(name) = ui.name(id).filter(|n| !n.is_empty()) {
out.push(json!({ "name": name, "id": id.to_u64(), "class": ui.class_name(id).unwrap_or_default() }));
}
stack.extend(ui.children(id).iter().copied());
}
Ok(Value::Array(out))
}
"close" => match obj.get("target") {
Some(t) => {
let id = resolve(ui, t)?;
Ok(Value::Bool(ui.close(id)))
}
None => {
ui.request_close();
Ok(Value::Null)
}
},
"media" => media(ui, obj),
"tree" => {
let root = optional_target(ui, obj.get("target"))?;
let depth = obj.get("depth").and_then(Value::as_u64).map_or(usize::MAX, |d| d as usize);
let visible_only = obj.get("visible_only").and_then(Value::as_bool).unwrap_or(false);
Ok(inspect::tree_with(ui, root, depth, visible_only))
}
"props" => {
let id = target(ui, obj)?;
Ok(inspect::props(ui, id))
}
"rect" => {
let id = target(ui, obj)?;
ui.layout();
let abs = ui.absolute_rect(id).ok_or("componente sem posição")?;
let own = ui.bounds(id).ok_or("componente sem posição")?;
Ok(serde_json::json!({ "x": abs.x, "y": abs.y, "width": abs.width, "height": abs.height, "in_parent": [own.x, own.y, own.width, own.height] }))
}
"highlight" => {
let id = target(ui, obj)?;
inspect::highlight(ui, id)?;
Ok(Value::Null)
}
"tr" => Ok(Value::String(ui.tr(string(obj, "text")?))),
"set_language" => {
aylis_project::i18n::set_language(ui, string(obj, "language")?);
Ok(Value::Null)
}
"toast" => {
aylis_controls::toast(ui, string(obj, "text")?);
Ok(Value::Null)
}
"profile_begin" => {
let name = string(obj, "name")?.to_string();
PROFILE_MARKS.with(|m| m.borrow_mut().push((name, aylis_core::profile::now())));
Ok(Value::Null)
}
"profile_end" => {
if let Some((name, start)) = PROFILE_MARKS.with(|m| m.borrow_mut().pop()) {
aylis_core::profile::record(aylis_core::profile::Lane::Mark, &name, start, (aylis_core::profile::now() - start).max(0.0));
}
Ok(Value::Null)
}
"message_dlg" => {
use aylis_controls::DlgKind;
let text = string(obj, "text")?.to_string();
let kind = match obj.get("kind").and_then(Value::as_str).unwrap_or("information") {
"warning" => DlgKind::Warning,
"error" => DlgKind::Error,
"confirmation" => DlgKind::Confirmation,
_ => DlgKind::Information,
};
let buttons: Vec<ModalResult> = obj
.get("buttons")
.and_then(Value::as_array)
.map(|a| a.iter().filter_map(|b| b.as_str().and_then(ModalResult::from_name)).collect())
.unwrap_or_default();
let handler = obj.get("handler").and_then(Value::as_str).map(str::to_string);
let reply = if obj.get("wait").and_then(Value::as_bool) == Some(true) { defer_reply(ui) } else { None };
let waiting = ui.backend_busy();
if waiting {
ui.backend_waiting_user(true);
}
let form = aylis_controls::message_dlg(ui, &text, kind, &buttons, move |ui, result| {
if waiting {
ui.backend_waiting_user(false);
}
if let Some(reply) = reply {
send_reply(ui, reply, Ok(json!(result.name())));
}
if let (Some(handler), Some(main)) = (handler, ui.main_form()) {
let args = EventArgs::Value(PropValue::Enum(result.name().to_string()));
ui.call_handler(main, "on_modal_result", &handler, &args);
}
});
Ok(json!(ui.path_of(form)))
}
"execute" => {
let id = target(ui, obj)?;
aylis_controls::OpenDialog::execute(ui, id);
Ok(Value::Null)
}
"hello" => hello(ui, obj),
"exception" => {
let message = string(obj, "message")?.to_string();
let path = obj.get("path").and_then(Value::as_str).unwrap_or_default().to_string();
let event = obj.get("handler").and_then(Value::as_str).unwrap_or_default().to_string();
ui.raise_exception(aylis_core::Exception { message, path, event });
Ok(Value::Null)
}
"done" => {
let seq = obj.get("seq").and_then(Value::as_u64).ok_or("falta 'seq'")?;
backend_done(ui, seq);
Ok(Value::Null)
}
"answer" => {
let token = obj.get("ask").and_then(Value::as_u64).ok_or("falta 'ask' (o número da pergunta)")?;
let answer = match obj.get("error").and_then(Value::as_str) {
Some(message) => aylis_core::Answer::Refused(message.to_string()),
None => {
let values = obj.get("values").and_then(Value::as_object).map(|m| m.iter().map(|(k, v)| (k.clone(), answer_value(v))).collect()).unwrap_or_default();
aylis_core::Answer::Accepted(values)
}
};
ui.answer(token, answer);
flush_waiting_data(ui);
Ok(Value::Null)
}
"modal_result" => {
let id = target(ui, obj)?;
let name = string(obj, "result")?;
let result = ModalResult::from_name(name).ok_or_else(|| format!("resultado '{name}' inválido (use {})", ModalResult::NAMES.join(", ")))?;
ui.set_modal_result(id, result);
Ok(Value::Null)
}
other => Err(format!("comando desconhecido '{other}'")),
}
}
fn media(ui: &mut Ui, obj: &Map<String, Value>) -> Result<Value, String> {
use aylis_media::{MediaPlayer, VideoPlayer};
let id = target(ui, obj)?;
let op = string(obj, "op")?;
let video = ui.get::<VideoPlayer>(id).is_some();
if !video && ui.get::<MediaPlayer>(id).is_none() {
return Err("o alvo não é um MediaPlayer nem um VideoPlayer".into());
}
let seconds = obj.get("seconds").and_then(Value::as_f64).unwrap_or(0.0);
Ok(match (op, video) {
("play", true) => VideoPlayer::play(ui, id).map(|_| Value::Null)?,
("play", false) => MediaPlayer::play(ui, id).map(|_| Value::Null)?,
("pause", true) => {
VideoPlayer::pause(ui, id);
Value::Null
}
("pause", false) => {
MediaPlayer::pause(ui, id);
Value::Null
}
("stop", true) => {
VideoPlayer::stop(ui, id);
Value::Null
}
("stop", false) => {
MediaPlayer::stop(ui, id);
Value::Null
}
("seek", true) => {
VideoPlayer::seek(ui, id, seconds);
Value::Null
}
("seek", false) => {
MediaPlayer::seek(ui, id, seconds);
Value::Null
}
("position", true) => json!(VideoPlayer::position(ui, id)),
("position", false) => json!(MediaPlayer::position(ui, id)),
("duration", true) => json!(VideoPlayer::duration(ui, id)),
("duration", false) => json!(MediaPlayer::duration(ui, id)),
("playing", true) => json!(VideoPlayer::is_playing(ui, id)),
("playing", false) => json!(MediaPlayer::is_playing(ui, id)),
(other, _) => return Err(format!("operação de mídia desconhecida '{other}'")),
})
}
fn string<'a>(obj: &'a Map<String, Value>, key: &str) -> Result<&'a str, String> {
obj.get(key).and_then(Value::as_str).ok_or_else(|| format!("falta \"{key}\" (texto)"))
}
pub fn resolve(ui: &Ui, value: &Value) -> Result<ComponentId, String> {
match value {
Value::String(name) if !name.is_empty() && !name.contains('.') => {
let found = named_everywhere(ui, name);
match found.as_slice() {
[one] => Ok(*one),
[] => ui.find_path(name).ok_or_else(|| format!("componente '{name}' não encontrado")),
many => Err(format!(
"o nome '{name}' está em {} lugares; use o caminho ou o id: {}",
many.len(),
many.iter().map(|&id| format!("{} (id {})", ui.path_of(id), id.to_u64())).collect::<Vec<_>>().join(", ")
)),
}
}
Value::String(name) => ui.find_path(name).ok_or_else(|| format!("componente '{name}' não encontrado")),
Value::Number(n) => {
let id = ComponentId::from_u64(n.as_u64().ok_or("id inválido")?);
if ui.contains(id) { Ok(id) } else { Err(format!("componente {n} não existe (ou foi liberado)")) }
}
other => Err(format!("alvo inválido: {other} (use o nome ou o id)")),
}
}
fn named_everywhere(ui: &Ui, name: &str) -> Vec<ComponentId> {
let mut found = Vec::new();
let mut stack: Vec<ComponentId> = ui.roots().to_vec();
while let Some(id) = stack.pop() {
if ui.name(id).is_some_and(|n| n.eq_ignore_ascii_case(name)) {
found.push(id);
}
stack.extend(ui.children(id).iter().copied());
}
found
}
fn start_drag_between_forms(ui: &mut Ui, root: ComponentId, start: aylis_core::PointF, event: aylis_core::PointerEvent) -> Result<(), String> {
ui.pointer_move(root, event);
ui.pointer_down(root, event);
for i in 1..=6 {
let at = aylis_core::PointF::new(start.x + 6.0 * i as f32, start.y + 12.0 * i as f32);
ui.pointer_move(root, aylis_core::PointerEvent { position: at, ..event });
ui.process_events();
}
if !ui.dragging(root) {
ui.pointer_up(root, event);
return Err("drag: o arrasto não começou (o componente em \"from\" não arrasta)".into());
}
Ok(())
}
fn point_in(ui: &mut Ui, id: ComponentId, x: Option<&Value>, y: Option<&Value>) -> Result<(ComponentId, aylis_core::PointF), String> {
ui.update_layout();
let rect = ui.absolute_rect(id).ok_or("o componente não está numa tela")?;
let coord = |v: Option<&Value>, size: f32, name: &str| -> Result<f32, String> {
match v {
None | Some(Value::Null) => Ok(size / 2.0),
Some(v) => v.as_f64().map(|n| n as f32).ok_or(format!("\"{name}\" tem de ser um número")),
}
};
let at = aylis_core::PointF::new(rect.x + coord(x, rect.width, "x")?, rect.y + coord(y, rect.height, "y")?);
Ok((ui.root_of(id), at))
}
fn modifiers(obj: &Map<String, Value>) -> Result<aylis_core::Modifiers, String> {
let names: Vec<String> = match obj.get("modifiers") {
None | Some(Value::Null) => return Ok(aylis_core::Modifiers::default()),
Some(Value::String(s)) => s.split('+').map(|p| p.trim().to_lowercase()).filter(|p| !p.is_empty()).collect(),
Some(Value::Array(a)) => a.iter().map(|v| v.as_str().map(|s| s.trim().to_lowercase()).ok_or("\"modifiers\": uma lista de nomes")).collect::<Result<_, _>>()?,
Some(_) => return Err("\"modifiers\": \"Ctrl+Shift\" ou [\"ctrl\", \"shift\"]".into()),
};
let mut m = aylis_core::Modifiers::default();
for name in names {
match name.as_str() {
"ctrl" | "control" => m.ctrl = true,
"shift" => m.shift = true,
"alt" => m.alt = true,
"meta" | "cmd" | "win" => m.meta = true,
other => return Err(format!("tecla modificadora desconhecida: {other} (use ctrl, shift, alt, meta)")),
}
}
Ok(m)
}
fn button(obj: &Map<String, Value>) -> Result<PointerButton, String> {
match obj.get("button").and_then(Value::as_str).unwrap_or("left") {
"left" | "primary" => Ok(PointerButton::Primary),
"right" | "secondary" => Ok(PointerButton::Secondary),
"middle" => Ok(PointerButton::Middle),
other => Err(format!("botão desconhecido: {other} (use left, right, middle)")),
}
}
fn target(ui: &Ui, obj: &Map<String, Value>) -> Result<ComponentId, String> {
resolve(ui, obj.get("target").ok_or("falta \"target\"")?)
}
fn optional_target(ui: &Ui, value: Option<&Value>) -> Result<Option<ComponentId>, String> {
match value {
None | Some(Value::Null) => Ok(None),
Some(v) => resolve(ui, v).map(Some),
}
}
fn json_arg(v: &Value) -> PropValue {
match v {
Value::Bool(b) => PropValue::Bool(*b),
Value::Number(n) => n.as_i64().map(PropValue::Int).unwrap_or_else(|| PropValue::Float(n.as_f64().unwrap_or(0.0))),
Value::String(s) => PropValue::String(s.clone()),
Value::Null => PropValue::Null,
other => PropValue::String(other.to_string()),
}
}
fn get(ui: &Ui, id: ComponentId, name: &str) -> Result<Value, String> {
let value = ui.get_prop(id, name).map_err(|e| e.to_string())?;
Ok(aylis_json::prop_to_json(ui, &value))
}
fn json_key(json: &Value) -> aylis_data::Value {
match json {
Value::Null => aylis_data::Value::Null,
Value::Bool(b) => aylis_data::Value::Bool(*b),
Value::Number(n) => n.as_i64().map(aylis_data::Value::Int).unwrap_or_else(|| aylis_data::Value::Float(n.as_f64().unwrap_or(0.0))),
Value::String(s) => aylis_data::Value::String(s.clone()),
other => aylis_data::Value::String(other.to_string()),
}
}
fn data(ui: &mut Ui, obj: &Map<String, Value>) -> Result<Value, String> {
let id = target(ui, obj)?;
if ui.get::<aylis_data::ClientDataSet>(id).is_none() {
return Err(format!("'{}' não é um ClientDataSet", ui.name(id).unwrap_or("?")));
}
let op = string(obj, "op")?;
let mut ds = data_set(ui, id);
let ok = |r: aylis_data::DataResult| r.map(|_| Value::Null).map_err(|e| e.message);
match op {
"load" => ok(ds.load_json(obj.get("rows").ok_or("falta \"rows\"")?)),
"load_columns" => ok(ds.load_columns(obj.get("fields").ok_or("falta \"fields\"")?, obj.get("columns").ok_or("falta \"columns\"")?)),
"append" => {
let more = obj.get("has_more").and_then(Value::as_bool).unwrap_or(false);
ok(ds.append_json(obj.get("rows").ok_or("falta \"rows\"")?, more))
}
"reconcile" => {
let results = obj.get("results").ok_or("falta \"results\"")?;
let results = ds.parse_results(&json!({ "results": results }).to_string()).map_err(|e| e.message)?;
let rejected: Vec<&str> = results.iter().filter_map(|r| r.error.as_deref()).collect();
let rejected = json!({ "rejected": rejected });
let _ = ds.reconcile(&results);
Ok(rejected)
}
"first" => ok(ds.first()),
"prior" => ok(ds.prior()),
"next" => ok(ds.next()),
"last" => ok(ds.last()),
"move_to" => ok(ds.move_to(obj.get("index").and_then(Value::as_u64).ok_or("falta \"index\"")? as usize)),
"edit" => ok(ds.edit()),
"insert" => ok(ds.insert()),
"append_record" => ok(ds.append()),
"post" => {
ok(ds.post())?;
if ds.is_asking() || ds.awaiting_text() {
if let Some(reply) = defer_reply(ds.ui()) {
ds.ui().extension::<Remote>().waiting.push(Waiting { reply: Some(reply), ds: id, op: "post".into(), replay: None });
}
}
Ok(Value::Null)
}
"cancel" => ok(ds.cancel()),
"delete" => {
ok(ds.delete())?;
if ds.is_asking() {
if let Some(reply) = defer_reply(ds.ui()) {
ds.ui().extension::<Remote>().waiting.push(Waiting { reply: Some(reply), ds: id, op: "delete".into(), replay: None });
}
}
Ok(Value::Null)
}
"apply" => {
let result = ds.apply_updates();
if ds.is_applying() {
if let Some(reply) = defer_reply(ds.ui()) {
ds.ui().extension::<Remote>().waiting.push(Waiting { reply: Some(reply), ds: id, op: "apply".into(), replay: None });
}
return Ok(Value::Null);
}
match result {
Err(e) if ds.apply_errors() == 0 => Err(e.message),
_ => Ok(json!(ds.apply_errors())),
}
}
"cancel_updates" => ok(ds.cancel_updates()),
"refresh" => {
ds.refresh_links();
Ok(Value::Null)
}
"set_has_more" => ok(ds.set_has_more(obj.get("value").and_then(Value::as_bool).unwrap_or(false))),
"set_value" => {
let field = string(obj, "field")?;
let json = obj.get("value").cloned().unwrap_or(Value::Null);
let value = json_to_value(&json, ds.view().field(field).map_err(|e| e.message)?)?;
ok(ds.set_value(field, value))
}
"set_text" => {
let field = string(obj, "field")?;
ok(ds.set_text(field, string(obj, "text")?))
}
"set_values" => {
let values = obj.get("values").and_then(Value::as_object).ok_or("falta \"values\" (objeto)")?;
if !ds.state().is_editing() {
ds.edit().map_err(|e| e.message)?;
}
for (field, json) in values {
let value = json_to_value(json, ds.view().field(field).map_err(|e| e.message)?)?;
ds.set_value(field, value).map_err(|e| format!("{field}: {}", e.message))?;
}
Ok(Value::Null)
}
"locate" => {
let field = string(obj, "field")?;
let json = obj.get("value").cloned().unwrap_or(Value::Null);
let value = json_to_value(&json, ds.view().field(field).map_err(|e| e.message)?)?;
ds.locate(field, value).map(Value::Bool).map_err(|e| e.message)
}
"set_filter" => ok(ds.set_filter(obj.get("filter").and_then(Value::as_str).unwrap_or_default())),
"set_index" => ok(ds.set_index(obj.get("index").and_then(Value::as_str).unwrap_or_default())),
"set_index_name" => ok(ds.set_index_name(obj.get("name").and_then(Value::as_str).unwrap_or_default())),
"set_param" => {
let name = string(obj, "name")?;
ok(ds.set_param(name, json_key(obj.get("value").unwrap_or(&Value::Null))))
}
"revert_record" => ok(ds.revert_record()),
"field_texts" => {
let field = string(obj, "field")?;
let pairs = obj.get("texts").and_then(Value::as_array).ok_or("falta \"texts\" (lista de [valor, texto])")?;
let texts = pairs
.iter()
.filter_map(|p| {
let p = p.as_array()?;
Some((json_key(p.first()?), p.get(1)?.as_str().unwrap_or_default().to_string()))
})
.collect();
ok(ds.set_field_texts(field, texts))
}
"field_error" => {
let field = string(obj, "field")?;
let message = obj.get("message").and_then(Value::as_str).map(str::to_string);
ok(ds.set_field_error(field, message))
}
"lookup" => {
let key_fields = string(obj, "key_fields")?;
let result = string(obj, "result_field")?;
let keys: Vec<aylis_data::Value> = match obj.get("keys") {
Some(Value::Array(list)) => list.iter().map(json_key).collect(),
Some(k) => vec![json_key(k)],
None => return Err("falta \"keys\"".into()),
};
Ok(ds.view().lookup(key_fields, &keys, result).map(|v| value_to_json(&v)).unwrap_or(Value::Null))
}
"value" => ds.value(string(obj, "field")?).map(|v| value_to_json(&v)).map_err(|e| e.message),
"text" => ds.text(string(obj, "field")?).map(Value::String).map_err(|e| e.message),
"record" => record(&ds),
"info" => {
let v = ds.view();
Ok(json!({
"state": format!("{:?}", v.state()).to_lowercase(),
"rec_no": v.rec_no(),
"count": v.record_count(),
"changes": v.change_count(),
"has_more": v.has_more(),
"bof": v.bof(),
"eof": v.eof(),
"status": v.update_status().map(|s| format!("{s:?}").to_lowercase()),
}))
}
"delta" => {
let changes = ds.delta_with_details().map_err(|e| e.message)?;
let name = ds.ui().name(id).unwrap_or_default().to_string();
serde_json::from_str(&aylis_data::json::delta_to_json(&name, &changes)).map_err(|e| e.to_string())
}
other => Err(format!("operação de dados desconhecida '{other}'")),
}
}
fn answer_value(v: &Value) -> PropValue {
match v {
Value::Bool(b) => PropValue::Bool(*b),
Value::Number(n) if n.is_i64() => PropValue::Int(n.as_i64().unwrap_or_default()),
Value::Number(n) => PropValue::Float(n.as_f64().unwrap_or_default()),
Value::String(s) => PropValue::String(s.clone()),
Value::Null => PropValue::Null,
other => PropValue::String(other.to_string()),
}
}
fn record(ds: &DataSet<'_>) -> Result<Value, String> {
let v = ds.view();
let mut out = Map::new();
for name in v.field_names() {
out.insert(name.clone(), value_to_json(&v.value(&name).map_err(|e| e.message)?));
}
Ok(Value::Object(out))
}
pub fn event_to_json(ui: &Ui, sender: ComponentId, event: &str, handler: &str, args: &EventArgs) -> Value {
let mut message = json!({
"event": event,
"handler": handler,
"form": ui.form_class(ui.form_of(sender)).or_else(|| ui.name(ui.form_of(sender))).unwrap_or_default(),
"form_id": ui.form_of(sender).to_u64(),
"sender": {
"id": sender.to_u64(),
"name": ui.name(sender).unwrap_or_default(),
"path": ui.path_of(sender),
"class": ui.class_name(sender).unwrap_or_default(),
},
"args": args_to_json(ui, args),
});
if event == "on_click"
&& let Some(p) = ui.click_pointer()
{
message["pointer"] = pointer_to_json(&p);
}
let info = ui.component(sender).map(|c| c.event_info(event)).unwrap_or_default();
if !info.is_empty() {
message["info"] = Value::Object(info.into_iter().map(|(k, v)| (k.to_string(), Value::String(v))).collect());
}
message
}
fn button_name(button: PointerButton) -> &'static str {
match button {
PointerButton::Primary => "primary",
PointerButton::Secondary => "secondary",
PointerButton::Middle => "middle",
PointerButton::Back => "back",
PointerButton::Forward => "forward",
}
}
fn pointer_to_json(p: &aylis_core::ClickPointer) -> Value {
let m = p.modifiers;
let modifiers: Vec<&str> =
[(m.ctrl, "ctrl"), (m.shift, "shift"), (m.alt, "alt"), (m.meta, "meta")].into_iter().filter(|(on, _)| *on).map(|(_, n)| n).collect();
json!({
"x": p.position.x,
"y": p.position.y,
"form_x": p.form_position.x,
"form_y": p.form_position.y,
"button": button_name(p.button),
"modifiers": modifiers,
"kind": match p.kind {
aylis_core::PointerKind::Mouse => "mouse",
aylis_core::PointerKind::Touch => "touch",
aylis_core::PointerKind::Pen => "pen",
},
"clicks": p.click_count,
})
}
fn args_to_json(ui: &Ui, args: &EventArgs) -> Value {
match args {
EventArgs::None => Value::Null,
EventArgs::Pointer(p) => json!({
"x": p.position.x,
"y": p.position.y,
"button": button_name(p.button),
"clicks": p.click_count,
}),
EventArgs::Wheel(w) => json!({ "x": w.position.x, "y": w.position.y, "dx": w.delta.x, "dy": w.delta.y }),
EventArgs::Gesture(g) => json!({
"phase": match g.phase { aylis_core::GesturePhase::Begin => "begin", aylis_core::GesturePhase::Update => "update", aylis_core::GesturePhase::End => "end" },
"x": g.center.x, "y": g.center.y, "scale": g.scale, "delta_scale": g.delta_scale,
"rotation": g.rotation, "delta_rotation": g.delta_rotation, "pan_x": g.pan.x, "pan_y": g.pan.y,
}),
EventArgs::Swipe(s) => json!({
"direction": s.direction.name(), "x": s.start.x, "y": s.start.y,
"dx": s.distance.x, "dy": s.distance.y, "vx": s.velocity.x, "vy": s.velocity.y,
}),
EventArgs::Key(k) => json!({
"key": key_name(k.key),
"ctrl": k.modifiers.ctrl,
"shift": k.modifiers.shift,
"alt": k.modifiers.alt,
}),
EventArgs::Text(text) => match serde_json::from_str::<Value>(text) {
Ok(v @ (Value::Object(_) | Value::Array(_))) => v,
_ => Value::String(text.clone()),
},
EventArgs::Value(PropValue::Component(c)) => c.map_or(Value::Null, |id| Value::from(id.to_u64())),
EventArgs::Value(v) => aylis_json::prop_to_json(ui, v),
EventArgs::Intercept(i) => {
let sender = i.sender.map_or(Value::Null, |id| {
json!({
"id": id.to_u64(),
"name": ui.name(id).unwrap_or_default(),
"path": ui.path_of(id),
"class": ui.class_name(id).unwrap_or_default(),
})
});
json!({
"form": i.form.and_then(|f| ui.name(f)).unwrap_or_default(),
"form_id": i.form.map_or(Value::Null, |f| Value::from(f.to_u64())),
"sender": sender,
"event": i.event,
"handler": i.handler,
"args": args_to_json(ui, &i.args),
})
}
}
}
fn key_name(key: Key) -> String {
match key {
Key::Char(c) => c.to_string(),
Key::F(n) => format!("f{n}"),
other => format!("{other:?}").to_lowercase(),
}
}
pub fn forward_events(ui: &mut Ui, send: impl Fn(&mut Ui, Value) + 'static) {
let send: std::rc::Rc<dyn Fn(&mut Ui, Value)> = std::rc::Rc::new(send);
ui.extension::<Remote>().sink = Some(send.clone());
ui.on_forms_freed(|ui, forms| {
let ids: Vec<Value> = forms.iter().map(|f| Value::from(f.to_u64())).collect();
send_event(ui, None, json!({ "event": "__freed", "forms": ids }));
});
ui.set_event_sink(move |ui, sender, event, handler, args| {
let mut json = event_to_json(ui, sender, event, handler, args);
if ui.asking() {
json["ask"] = Value::from(ui.defer_question());
}
let key = (sender.to_u64(), event.to_string());
let joined = {
let coalesce = coalesces(ui, sender, event);
let b = &mut ui.extension::<Remote>().busy;
if b.tracking && json.get("ask").is_none() && b.running.values().any(|k| *k == key) && coalesce {
b.latest.insert(key.clone(), json.clone());
true
} else {
false
}
};
if !joined {
send_event(ui, Some(sender), json);
}
});
}
fn defer_reply(ui: &Ui) -> Option<Value> {
if ui.extension_ref::<Remote>().is_none_or(|r| r.sink.is_none()) {
return None;
}
CURRENT_ID.with(|c| c.borrow_mut().take())
}
fn send_reply(ui: &mut Ui, id: Value, result: Result<Value, String>) {
let message = match result {
Ok(result) => json!({ "id": id, "result": result }),
Err(error) => json!({ "id": id, "error": error }),
};
if let Some(sink) = ui.extension::<Remote>().sink.clone() {
sink(ui, message);
}
}
fn flush_waiting_data(ui: &mut Ui) {
let waiting = std::mem::take(&mut ui.extension::<Remote>().waiting);
let mut still = Vec::new();
for w in waiting {
let ds = data_set(ui, w.ds);
if ds.is_asking() || ds.awaiting_text() {
still.push(w);
continue;
}
let editing = ds.state().is_editing();
let result = match w.replay {
Some(_) if editing => Err("a gravação foi recusada".to_string()),
Some(command) => {
let had_id = w.reply.is_some();
CURRENT_ID.with(|c| *c.borrow_mut() = w.reply.clone());
let result = data_command(ui, &command);
if had_id && CURRENT_ID.with(|c| c.borrow_mut().take()).is_none() {
continue;
}
result
}
None if w.op == "post" && editing => Err("a gravação foi recusada".to_string()),
None if w.op == "apply" => match ds.apply_errors() {
usize::MAX => Err("a gravação foi recusada".to_string()),
n => Ok(json!(n)),
},
None => Ok(Value::Null),
};
match (w.reply, result) {
(Some(reply), result) => send_reply(ui, reply, result),
(None, Err(error)) => {
if let Some(sink) = ui.extension::<Remote>().sink.clone() {
sink(ui, json!({ "event": "error", "cmd": "data", "message": error }));
}
}
(None, Ok(_)) => {}
}
}
ui.extension::<Remote>().waiting.extend(still);
}
struct Waiting {
reply: Option<Value>,
ds: ComponentId,
op: String,
replay: Option<Map<String, Value>>,
}
fn data_command(ui: &mut Ui, obj: &Map<String, Value>) -> Result<Value, String> {
match data(ui, obj) {
Err(error) if error == aylis_data::DataError::waiting().message => {
let ds = target(ui, obj)?;
let reply = defer_reply(ui);
let op = string(obj, "op")?.to_string();
ui.extension::<Remote>().waiting.push(Waiting { reply, ds, op, replay: Some(obj.clone()) });
Ok(Value::Null)
}
other => other,
}
}
#[derive(Default)]
struct BusyTrack {
tracking: bool,
next: u64,
running: std::collections::HashMap<u64, (u64, String)>,
latest: std::collections::HashMap<(u64, String), Value>,
}
pub fn coalesces(ui: &Ui, sender: ComponentId, event: &str) -> bool {
aylis_core::question::COALESCED_EVENTS.contains(&event)
|| (event == "on_change" && matches!(ui.class_name(sender), Some("TrackBar" | "Slider" | "RangeSlider" | "ScrollBar" | "Knob" | "Gauge")))
}
fn backend_done(ui: &mut Ui, seq: u64) {
let next = {
let b = &mut ui.extension::<Remote>().busy;
b.running.remove(&seq).and_then(|key| b.latest.remove(&key))
};
ui.backend_done(seq);
if let Some(json) = next {
let sender = json["sender"]["id"].as_u64().map(ComponentId::from_u64);
send_event(ui, sender, json);
}
}
fn send_event(ui: &mut Ui, sender: Option<ComponentId>, mut json: Value) {
if ui.extension::<Remote>().busy.tracking {
let seq = {
let b = &mut ui.extension::<Remote>().busy;
b.next += 1;
b.next
};
json["seq"] = Value::from(seq);
if let Some(sender) = sender {
ui.backend_event_sent(seq, sender);
let event = json["event"].as_str().unwrap_or_default().to_string();
if coalesces(ui, sender, &event) {
ui.extension::<Remote>().busy.running.insert(seq, (sender.to_u64(), event));
}
}
}
if let Some(sink) = ui.extension::<Remote>().sink.clone() {
sink(ui, json);
}
}
#[derive(Default)]
struct Remote {
sink: Option<std::rc::Rc<dyn Fn(&mut Ui, Value)>>,
busy: BusyTrack,
waiting: Vec<Waiting>,
}