1use crate::{parsers::convert_percent_to_decimal, screen::get_screen_size};
2use enigo::{Enigo, Key, KeyboardControllable, MouseButton, MouseControllable};
3
4pub fn keyboard_type(text: &str) -> String {
5 let mut enigo = Enigo::new();
6 for c in text.chars() {
7 match c {
8 '/' => {
9 enigo.key_sequence("/");
10 }
11 _ => {
12 enigo.key_click(Key::Layout(c));
13 }
14 }
15 }
16
17 enigo.key_down(Key::Return);
18
19 format!("Type: {}", text)
20}
21
22pub fn search(text: &str) -> String {
23 let mut enigo = Enigo::new();
24 enigo.key_down(Key::Meta);
26 enigo.key_click(Key::Layout(' '));
27 enigo.key_up(Key::Meta);
28
29 std::thread::sleep(std::time::Duration::from_secs(1));
30
31 for c in text.chars() {
32 enigo.key_click(Key::Layout(c));
33 }
34 enigo.key_down(Key::Return);
35 return format!("Open program: {}", text);
36}
37
38pub fn click_at_percentage(x_percentage: &str, y_percentage: &str) -> String {
39 let x_decimal = match convert_percent_to_decimal(x_percentage) {
40 Ok(x_decimal) => x_decimal,
41 Err(_) => 0.0,
42 };
43
44 let y_decimal = match convert_percent_to_decimal(y_percentage) {
45 Ok(y_decimal) => y_decimal,
46 Err(_) => 0.0,
47 };
48
49 let (screen_width, screen_height) = match get_screen_size() {
50 Ok((screen_width, screen_height)) => (screen_width, screen_height),
51 Err(_) => (0, 0),
52 };
53
54 let x_pixel = (x_decimal * screen_width as f32).round() as i32;
55 let y_pixel = (y_decimal * screen_height as f32).round() as i32;
56
57 let mut enigo = Enigo::new();
58 enigo.mouse_move_to(x_pixel, y_pixel);
59 enigo.mouse_click(MouseButton::Left);
60
61 format!("Click: x: {}, y: {}", x_pixel, y_pixel)
62}
63
64pub fn mouse_click(click_detail: &serde_json::Value) -> String {
65 match (click_detail["x"].as_str(), click_detail["y"].as_str()) {
66 (Some(x), Some(y)) if !x.is_empty() && !y.is_empty() => {
67 click_at_percentage(x, y);
68 format!(
69 "Click: x: {}, y: {}, description: {}, reason: {}",
70 x, y, click_detail["description"], click_detail["reason"]
71 )
72 }
73 _ => "We failed to click".to_string(),
74 }
75}