rusty_duplication/capturer/
vec.rs

1use super::CapturerBuffer;
2use crate::{Capturer, Error, Monitor, Result};
3
4impl CapturerBuffer for Vec<u8> {
5  #[inline]
6  fn as_bytes(&self) -> &[u8] {
7    self
8  }
9
10  #[inline]
11  fn as_bytes_mut(&mut self) -> &mut [u8] {
12    self
13  }
14}
15
16/// Capture screen to a `Vec<u8>`.
17/// # Examples
18/// ```
19/// use rusty_duplication::{Scanner, VecCapturer};
20///
21/// let monitor = Scanner::new().unwrap().next().unwrap();
22/// let mut capturer: VecCapturer = monitor.try_into().unwrap();
23/// ```
24pub type VecCapturer = Capturer<Vec<u8>>;
25
26impl TryFrom<Monitor> for VecCapturer {
27  type Error = Error;
28
29  fn try_from(monitor: Monitor) -> Result<Self> {
30    Capturer::new(monitor, |size| Ok(vec![0u8; size]))
31  }
32}
33
34#[cfg(test)]
35mod tests {
36  use super::*;
37  use crate::{FrameInfoExt, Scanner};
38  use serial_test::serial;
39  use std::{thread, time::Duration};
40
41  #[test]
42  #[serial]
43  fn vec_capturer() {
44    let monitor = Scanner::new().unwrap().next().unwrap();
45    let mut capturer: VecCapturer = monitor.try_into().unwrap();
46
47    // sleep for a while before capture to wait system to update the screen
48    thread::sleep(Duration::from_millis(50));
49
50    let info = capturer.capture().unwrap();
51    assert!(info.desktop_updated());
52
53    // ensure buffer not all zero
54    assert!(!capturer.buffer.as_bytes().iter().all(|&n| n == 0));
55
56    thread::sleep(Duration::from_millis(50));
57
58    // check mouse
59    let (frame_info, pointer_shape_info) = capturer.capture_with_pointer_shape().unwrap();
60    if frame_info.pointer_shape_updated() {
61      assert!(pointer_shape_info.is_some());
62      // make sure pointer shape buffer is not all zero
63      assert!(!capturer.pointer_shape_buffer.iter().all(|&n| n == 0));
64    } else {
65      panic!(
66        "Move your mouse to change the shape of the cursor during the test to check mouse capture"
67      );
68    }
69  }
70}