use std::time::Duration;
use tracing::{debug, instrument};
use viewpoint_cdp::protocol::input::{
DispatchKeyEventParams, DispatchMouseEventParams, MouseButton,
};
use super::Locator;
use crate::error::LocatorError;
#[derive(Debug)]
pub struct ClickBuilder<'l, 'a> {
locator: &'l Locator<'a>,
position: Option<(f64, f64)>,
button: MouseButton,
modifiers: i32,
force: bool,
click_count: i32,
}
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,
}
}
#[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
}
#[instrument(level = "debug", skip(self), fields(selector = ?self.locator.selector))]
pub async fn send(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())
}
}
#[derive(Debug)]
pub struct TypeBuilder<'l, 'a> {
locator: &'l Locator<'a>,
text: String,
delay: Option<Duration>,
}
impl<'l, 'a> TypeBuilder<'l, 'a> {
pub(crate) fn new(locator: &'l Locator<'a>, text: &str) -> Self {
Self {
locator,
text: text.to_string(),
delay: None,
}
}
#[must_use]
pub fn delay(mut self, delay: Duration) -> Self {
self.delay = Some(delay);
self
}
#[instrument(level = "debug", skip(self), fields(selector = ?self.locator.selector))]
pub async fn send(self) -> Result<(), LocatorError> {
self.locator.wait_for_actionable().await?;
debug!(text = %self.text, delay = ?self.delay, "Typing text");
self.locator.focus_element().await?;
for ch in self.text.chars() {
let char_str = ch.to_string();
self.locator
.dispatch_key_event(DispatchKeyEventParams::char(&char_str))
.await?;
if let Some(delay) = self.delay {
tokio::time::sleep(delay).await;
}
}
Ok(())
}
}
impl<'l> std::future::IntoFuture for TypeBuilder<'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())
}
}
#[derive(Debug)]
pub struct HoverBuilder<'l, 'a> {
locator: &'l Locator<'a>,
position: Option<(f64, f64)>,
modifiers: i32,
force: bool,
}
impl<'l, 'a> HoverBuilder<'l, 'a> {
pub(crate) fn new(locator: &'l Locator<'a>) -> Self {
Self {
locator,
position: None,
modifiers: 0,
force: false,
}
}
#[must_use]
pub fn position(mut self, x: f64, y: f64) -> Self {
self.position = Some((x, y));
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
}
#[instrument(level = "debug", skip(self), fields(selector = ?self.locator.selector))]
pub async fn send(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, modifiers = self.modifiers, "Hovering over 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?;
Ok(())
}
}
impl<'l> std::future::IntoFuture for HoverBuilder<'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())
}
}
#[derive(Debug)]
pub struct TapBuilder<'l, 'a> {
locator: &'l Locator<'a>,
position: Option<(f64, f64)>,
force: bool,
modifiers: i32,
}
impl<'l, 'a> TapBuilder<'l, 'a> {
pub(crate) fn new(locator: &'l Locator<'a>) -> Self {
Self {
locator,
position: None,
force: false,
modifiers: 0,
}
}
#[must_use]
pub fn position(mut self, x: f64, y: f64) -> Self {
self.position = Some((x, y));
self
}
#[must_use]
pub fn force(mut self, force: bool) -> Self {
self.force = force;
self
}
#[must_use]
pub fn modifiers(mut self, modifiers: i32) -> Self {
self.modifiers = modifiers;
self
}
#[instrument(level = "debug", skip(self), fields(selector = ?self.locator.selector))]
pub async fn send(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, modifiers = self.modifiers, "Tapping element");
if self.modifiers != 0 {
self.locator
.page
.touchscreen()
.tap_with_modifiers(x, y, self.modifiers)
.await
} else {
self.locator.page.touchscreen().tap(x, y).await
}
}
}