rust_droid/action/
swipe.rs1use crate::common::relative_rect::RelativeRect;
2use crate::{Droid, Result, Target};
3use std::time::Duration;
4
5pub struct SwipeBuilder<'a> {
6 droid: &'a mut Droid,
7 start: Target,
8 end: Target,
9 duration: Duration,
10 threshold: Option<f32>,
11 start_search_rect: Option<RelativeRect>,
12 end_search_rect: Option<RelativeRect>,
13}
14
15impl<'a> SwipeBuilder<'a> {
16 pub fn new(droid: &'a mut Droid, start: Target, end: Target) -> Self {
17 Self {
18 droid,
19 start,
20 end,
21 duration: Duration::from_millis(300),
22 threshold: None,
23 start_search_rect: None,
24 end_search_rect: None,
25 }
26 }
27
28 pub fn duration(mut self, duration: Duration) -> Self {
29 self.duration = duration;
30 self
31 }
32
33 pub fn threshold(mut self, value: f32) -> Self {
34 self.threshold = Some(value);
35 self
36 }
37
38 pub fn search_start_in(mut self, rect: RelativeRect) -> Self {
39 self.start_search_rect = Some(rect);
40 self
41 }
42
43 pub fn search_end_in(mut self, rect: RelativeRect) -> Self {
44 self.end_search_rect = Some(rect);
45 self
46 }
47
48 pub fn execute(self) -> Result<()> {
49 let threshold = self
50 .threshold
51 .unwrap_or(self.droid.config.default_confidence);
52 let start_point =
53 self.droid
54 .resolve_target(&self.start, threshold, self.start_search_rect)?;
55 let end_point = self
56 .droid
57 .resolve_target(&self.end, threshold, self.end_search_rect)?;
58
59 log::info!(
60 "Executing swipe from {:?} to {:?} over {:?}",
61 start_point,
62 end_point,
63 self.duration
64 );
65
66 self.droid
67 .controller
68 .swipe(start_point, end_point, self.duration)?;
69
70 Ok(())
71 }
72}