use serde_json::Value;
use waft_embedder::{Offset, ServiceExtensionError, ServiceExtensionParams, ViewId};
use waft_foundation::{App, AsyncApp, DiagnosticsNode};
use super::controller::{self, WAIT_LIMIT};
use super::selector::WidgetSelector;
use crate::widgets::widget_inspector::{
InspectorSerializationDelegate, element_named, from_inspector_id,
};
use crate::{AnyElement, WidgetsBinding};
pub(super) struct Target {
pub(super) element: AnyElement,
pub(super) at: Offset,
pub(super) view: ViewId,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub(super) enum Reach {
Touch,
Any,
}
pub(super) async fn target(
cx: &mut AsyncApp,
params: &ServiceExtensionParams,
reach: Reach,
) -> Result<Target, ServiceExtensionError> {
let selector = Selection::of(params)?;
controller::wait_until(cx, WAIT_LIMIT, |app| {
!candidates(app, &selector, reach).is_empty()
})
.await;
cx.update(|app| the_one(app, &selector, reach))
}
pub(super) enum Selection {
Css(WidgetSelector, String),
Id(String),
}
impl Selection {
pub(super) fn of(params: &ServiceExtensionParams) -> Result<Selection, ServiceExtensionError> {
match (params.get("selector"), params.get("id")) {
(Some(css), None) => WidgetSelector::parse(css)
.map(|selector| Selection::Css(selector, css.to_owned()))
.map_err(ServiceExtensionError::InvalidParams),
(None, Some(id)) => Ok(Selection::Id(id.to_owned())),
_ => Err(ServiceExtensionError::InvalidParams(
"a command names its widget by `selector` or by `id`, one of them".to_owned(),
)),
}
}
fn text(&self) -> &str {
match self {
Selection::Css(_, css) => css,
Selection::Id(id) => id,
}
}
pub(super) fn named(&self, app: &mut App) -> Vec<AnyElement> {
self.named_where(app, true)
}
fn named_where(&self, app: &mut App, onstage: bool) -> Vec<AnyElement> {
let Some(root) = WidgetsBinding::instance(app).root_element(app) else {
return Vec::new();
};
match self {
Selection::Css(selector, _) => selector.select(app, root, onstage),
Selection::Id(id) => from_inspector_id(id)
.and_then(|id| element_named(app, root, id))
.into_iter()
.collect(),
}
}
}
fn candidates(app: &mut App, selection: &Selection, reach: Reach) -> Vec<Target> {
selection
.named(app)
.into_iter()
.filter_map(|element| {
let (at, view) = controller::center_of(app, element)?;
let target = Target { element, at, view };
(reach == Reach::Any || reaches(app, &target)).then_some(target)
})
.collect()
}
fn reaches(app: &mut App, target: &Target) -> bool {
let Some(render) = target.element.find_render_object(app) else {
return false;
};
controller::hit_at(app, target.at, target.view)
.iter()
.any(|hit| hit.id() == render.id())
}
fn the_one(
app: &mut App,
selection: &Selection,
reach: Reach,
) -> Result<Target, ServiceExtensionError> {
let naming = selection.text();
let mut found = candidates(app, selection, reach);
match found.len() {
1 => Ok(found.remove(0)),
0 => {
let named = selection.named(app);
let missed = named.first().copied();
Err(ServiceExtensionError::Failed(match missed {
Some(element) if reach == Reach::Touch => missed_touch(app, naming, element),
_ => named_nothing(app, selection),
}))
}
count => {
let lines: Vec<String> = found
.iter()
.map(|target| line_of(app, target.element))
.collect();
Err(ServiceExtensionError::Failed(format!(
"`{naming}` names {count} widgets; name one by a narrower selector or its id:\n {}",
lines.join("\n ")
)))
}
}
}
fn named_nothing(app: &mut App, selection: &Selection) -> String {
let naming = selection.text();
let seconds = WAIT_LIMIT.as_secs();
match selection.named_where(app, false).len() {
0 => format!("`{naming}` names no widget after {seconds} seconds"),
offstage => format!(
"`{naming}` names no widget on screen after {seconds} seconds, though it names \
{offstage} built off screen, on a page behind this one or in a list outside its \
view; `cargo waft ext scroll-until-visible '{naming}' --in <list> --dy <step>` \
scrolls a list's item onto the screen"
),
}
}
fn missed_touch(app: &mut App, naming: &str, element: AnyElement) -> String {
let line = line_of(app, element);
let landed = controller::center_of(app, element)
.and_then(|(at, view)| controller::hit_at(app, at, view).first().copied())
.map_or("nothing".to_owned(), |hit| hit.to_string_short(app));
format!(
"`{naming}` names {line}, but a touch at its centre lands on {landed}: it is covered, \
outside its view, or ignores touches"
)
}
pub(super) fn line_of(app: &App, element: AnyElement) -> String {
let node = node_of(app, element);
let mut line = node["description"].as_str().unwrap_or("?").to_owned();
if let Some(location) = node["creationLocation"].as_object() {
let file = location.get("file").and_then(Value::as_str).unwrap_or("?");
let at = location.get("line").and_then(Value::as_u64).unwrap_or(0);
line.push_str(&format!(" {file}:{at}"));
}
if let Some(id) = node["valueId"].as_str() {
line.push_str(&format!(" {id}"));
}
line
}
pub(super) fn node_of(app: &App, element: AnyElement) -> Value {
let delegate = InspectorSerializationDelegate {
group_name: Some("driver".to_owned()),
subtree_depth: 0,
..InspectorSerializationDelegate::default()
};
Value::Object(
element
.to_diagnostics_node(app, None)
.to_json_map(&delegate),
)
}