use tracing::{debug, instrument, trace};
use viewpoint_cdp::protocol::input::{DispatchMouseEventParams, MouseButton};
use super::super::Locator;
use crate::error::LocatorError;
use crate::wait::NavigationWaiter;
#[derive(Debug)]
pub struct ClickBuilder<'l, 'a> {
locator: &'l Locator<'a>,
position: Option<(f64, f64)>,
button: MouseButton,
modifiers: i32,
force: bool,
click_count: i32,
no_wait_after: bool,
}
impl<'l, 'a> ClickBuilder<'l, 'a> {
pub(crate) fn new(locator: &'l Locator<'a>) -> Self {
Self {
locator,
position: None,
button: MouseButton::Left,
modifiers: 0,
force: false,
click_count: 1,
no_wait_after: false,
}
}
#[must_use]
pub fn position(mut self, x: f64, y: f64) -> Self {
self.position = Some((x, y));
self
}
#[must_use]
pub fn button(mut self, button: MouseButton) -> Self {
self.button = button;
self
}
#[must_use]
pub fn modifiers(mut self, modifiers: i32) -> Self {
self.modifiers = modifiers;
self
}
#[must_use]
pub fn force(mut self, force: bool) -> Self {
self.force = force;
self
}
#[must_use]
pub(crate) fn click_count(mut self, count: i32) -> Self {
self.click_count = count;
self
}
#[must_use]
pub fn no_wait_after(mut self, no_wait_after: bool) -> Self {
self.no_wait_after = no_wait_after;
self
}
#[instrument(level = "debug", skip(self), fields(selector = ?self.locator.selector))]
pub async fn send(self) -> Result<(), LocatorError> {
let navigation_waiter = if self.no_wait_after {
None
} else {
Some(NavigationWaiter::new(
self.locator.page.connection().subscribe_events(),
self.locator.page.session_id().to_string(),
self.locator.page.frame_id().to_string(),
))
};
self.perform_click().await?;
if let Some(waiter) = navigation_waiter {
match waiter.wait_for_navigation_if_triggered().await {
Ok(navigated) => {
if navigated {
trace!("Navigation completed after click");
}
}
Err(e) => {
debug!(error = ?e, "Navigation wait failed after click");
return Err(LocatorError::WaitError(e));
}
}
}
Ok(())
}
async fn perform_click(&self) -> Result<(), LocatorError> {
let (x, y) = if self.force {
let info = self.locator.query_element_info().await?;
if !info.found {
return Err(LocatorError::NotFound(format!(
"{:?}",
self.locator.selector
)));
}
if let Some((offset_x, offset_y)) = self.position {
(
info.x.unwrap_or(0.0) + offset_x,
info.y.unwrap_or(0.0) + offset_y,
)
} else {
(
info.x.unwrap_or(0.0) + info.width.unwrap_or(0.0) / 2.0,
info.y.unwrap_or(0.0) + info.height.unwrap_or(0.0) / 2.0,
)
}
} else {
let info = self.locator.wait_for_actionable().await?;
if let Some((offset_x, offset_y)) = self.position {
(
info.x.expect("visible element has x") + offset_x,
info.y.expect("visible element has y") + offset_y,
)
} else {
(
info.x.expect("visible element has x")
+ info.width.expect("visible element has width") / 2.0,
info.y.expect("visible element has y")
+ info.height.expect("visible element has height") / 2.0,
)
}
};
debug!(x, y, button = ?self.button, modifiers = self.modifiers, click_count = self.click_count, "Clicking element");
let mut move_event = DispatchMouseEventParams::mouse_move(x, y);
if self.modifiers != 0 {
move_event.modifiers = Some(self.modifiers);
}
self.locator.dispatch_mouse_event(move_event).await?;
let mut down_event = DispatchMouseEventParams::mouse_down(x, y, self.button);
if self.modifiers != 0 {
down_event.modifiers = Some(self.modifiers);
}
down_event.click_count = Some(self.click_count);
self.locator.dispatch_mouse_event(down_event).await?;
let mut up_event = DispatchMouseEventParams::mouse_up(x, y, self.button);
if self.modifiers != 0 {
up_event.modifiers = Some(self.modifiers);
}
up_event.click_count = Some(self.click_count);
self.locator.dispatch_mouse_event(up_event).await?;
Ok(())
}
}
impl<'l> std::future::IntoFuture for ClickBuilder<'l, '_> {
type Output = Result<(), LocatorError>;
type IntoFuture =
std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + Send + 'l>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(self.send())
}
}