Skip to content

Commit ed1e719

Browse files
committed
refactor: Margin and PlatformApi to own modules
1 parent e767bb9 commit ed1e719

File tree

3 files changed

+81
-77
lines changed

3 files changed

+81
-77
lines changed

src/common/margin.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#[derive(Debug, PartialEq)]
2+
pub struct Margin {
3+
pub top: u16,
4+
pub right: u16,
5+
pub bottom: u16,
6+
pub left: u16,
7+
}
8+
9+
impl Margin {
10+
pub fn new(top: u16, right: u16, bottom: u16, left: u16) -> Self {
11+
Self {
12+
top,
13+
right,
14+
bottom,
15+
left,
16+
}
17+
}
18+
19+
pub fn new_equal(margin: u16) -> Self {
20+
Self::new(margin, margin, margin, margin)
21+
}
22+
23+
pub fn zero() -> Self {
24+
Self::new_equal(0)
25+
}
26+
27+
pub fn is_zero(&self) -> bool {
28+
self.top == 0
29+
&& self.right == self.left
30+
&& self.left == self.bottom
31+
&& self.bottom == self.top
32+
}
33+
}
34+
35+
#[cfg(test)]
36+
mod test {
37+
use super::*;
38+
39+
#[test]
40+
fn margin_new() {
41+
let m = Margin::new(1, 2, 3, 4);
42+
assert_eq!(m.top, 1);
43+
assert_eq!(m.right, 2);
44+
assert_eq!(m.bottom, 3);
45+
assert_eq!(m.left, 4);
46+
}
47+
48+
#[test]
49+
fn margin_new_equal() {
50+
let m = Margin::new_equal(1);
51+
assert_eq!(m.top, 1);
52+
assert_eq!(m.right, 1);
53+
assert_eq!(m.bottom, 1);
54+
assert_eq!(m.left, 1);
55+
}
56+
57+
#[test]
58+
fn margin_zero() {
59+
let m = Margin::zero();
60+
assert_eq!(m.top, 0);
61+
assert_eq!(m.right, 0);
62+
assert_eq!(m.bottom, 0);
63+
assert_eq!(m.left, 0);
64+
assert!(m.is_zero());
65+
}
66+
}

src/common/mod.rs

Lines changed: 4 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,8 @@ pub mod identify_transparency;
22
pub mod image;
33
pub mod utils;
44

5-
use crate::{ImageOnHeap, Result, WindowId, WindowList};
5+
mod margin;
6+
mod platform_api;
67

7-
pub trait PlatformApi: Send {
8-
/// 1. it does check for the screenshot
9-
/// 2. it checks for transparent margins and configures the api
10-
/// to cut them away in further screenshots
11-
fn calibrate(&mut self, window_id: WindowId) -> Result<()>;
12-
fn window_list(&self) -> Result<WindowList>;
13-
fn capture_window_screenshot(&self, window_id: WindowId) -> Result<ImageOnHeap>;
14-
fn get_active_window(&self) -> Result<WindowId>;
15-
}
16-
17-
#[derive(Debug, PartialEq)]
18-
pub struct Margin {
19-
pub top: u16,
20-
pub right: u16,
21-
pub bottom: u16,
22-
pub left: u16,
23-
}
24-
25-
impl Margin {
26-
pub fn new(top: u16, right: u16, bottom: u16, left: u16) -> Self {
27-
Self {
28-
top,
29-
right,
30-
bottom,
31-
left,
32-
}
33-
}
34-
35-
pub fn new_equal(margin: u16) -> Self {
36-
Self::new(margin, margin, margin, margin)
37-
}
38-
39-
pub fn zero() -> Self {
40-
Self::new_equal(0)
41-
}
42-
43-
pub fn is_zero(&self) -> bool {
44-
self.top == 0
45-
&& self.right == self.left
46-
&& self.left == self.bottom
47-
&& self.bottom == self.top
48-
}
49-
}
50-
51-
#[cfg(test)]
52-
mod test {
53-
use super::*;
54-
55-
#[test]
56-
fn margin_new() {
57-
let m = Margin::new(1, 2, 3, 4);
58-
assert_eq!(m.top, 1);
59-
assert_eq!(m.right, 2);
60-
assert_eq!(m.bottom, 3);
61-
assert_eq!(m.left, 4);
62-
}
63-
64-
#[test]
65-
fn margin_new_equal() {
66-
let m = Margin::new_equal(1);
67-
assert_eq!(m.top, 1);
68-
assert_eq!(m.right, 1);
69-
assert_eq!(m.bottom, 1);
70-
assert_eq!(m.left, 1);
71-
}
72-
73-
#[test]
74-
fn margin_zero() {
75-
let m = Margin::zero();
76-
assert_eq!(m.top, 0);
77-
assert_eq!(m.right, 0);
78-
assert_eq!(m.bottom, 0);
79-
assert_eq!(m.left, 0);
80-
assert!(m.is_zero());
81-
}
82-
}
8+
pub use margin::*;
9+
pub use platform_api::*;

src/common/platform_api.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use crate::{ImageOnHeap, Result, WindowId, WindowList};
2+
3+
pub trait PlatformApi: Send {
4+
/// 1. it does check for the screenshot
5+
/// 2. it checks for transparent margins and configures the api
6+
/// to cut them away in further screenshots
7+
fn calibrate(&mut self, window_id: WindowId) -> Result<()>;
8+
fn window_list(&self) -> Result<WindowList>;
9+
fn capture_window_screenshot(&self, window_id: WindowId) -> Result<ImageOnHeap>;
10+
fn get_active_window(&self) -> Result<WindowId>;
11+
}

0 commit comments

Comments
 (0)