use crate::common::relative_rect::RelativeRect;
use crate::{Droid, Result, Target};
use std::time::Duration;
pub struct TouchBuilder<'a> {
droid: &'a mut Droid,
target: Target,
duration: Duration,
times: u32,
threshold: Option<f32>,
search_rect: Option<RelativeRect>,
}
impl<'a> TouchBuilder<'a> {
pub fn new(droid: &'a mut Droid, target: Target) -> Self {
Self {
droid,
target,
duration: Duration::from_millis(100),
times: 1,
threshold: None,
search_rect: None,
}
}
pub fn times(mut self, count: u32) -> Self {
self.times = count;
self
}
pub fn duration(mut self, duration: Duration) -> Self {
self.duration = duration;
self
}
pub fn threshold(mut self, value: f32) -> Self {
self.threshold = Some(value);
self
}
pub fn search_in(mut self, rect: RelativeRect) -> Self {
self.search_rect = Some(rect);
self
}
pub fn execute(self) -> Result<()> {
let threshold = self
.threshold
.unwrap_or(self.droid.config.default_confidence);
let point = self
.droid
.resolve_target(&self.target, threshold, self.search_rect)?;
log::info!(
"Executing touch action at {:?} for {} times",
point,
self.times
);
for i in 0..self.times {
if self.duration <= Duration::from_millis(200) {
self.droid.controller.tap(point)?;
} else {
self.droid.controller.swipe(point, point, self.duration)?;
}
if self.times > 1 && i < self.times - 1 {
std::thread::sleep(Duration::from_millis(50));
}
}
Ok(())
}
}