Skip to content

Commit 56a104e

Browse files
committed
Refactor screengrab module
1 parent 6e1e327 commit 56a104e

File tree

4 files changed

+165
-178
lines changed

4 files changed

+165
-178
lines changed

src/cropper.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ struct CropperPrograms {
4747
sub_quad_tex: Program,
4848
}
4949

50-
struct CroppingContext<T> {
50+
struct CroppingContext {
5151
started: Instant,
5252
delta: Duration,
5353

54-
snap: T,
54+
snap: Screenshot,
5555
snap_tex: SrgbTexture2d,
5656

5757
region: Option<Rectangle<f64>>,
@@ -126,15 +126,15 @@ impl Cropper {
126126
})
127127
}
128128

129-
pub fn apply(&mut self, snap: impl Screenshot) -> Result<bool, CropperError> {
129+
pub fn apply(&mut self, snap: Screenshot) -> Result<bool, CropperError> {
130130
self.display
131131
.gl_window()
132132
.window()
133-
.set_max_dimensions(Some(snap.dimensions().into()));
133+
.set_max_dimensions(Some(snap.dimensions.into()));
134134
self.display
135135
.gl_window()
136136
.window()
137-
.set_min_dimensions(Some(snap.dimensions().into()));
137+
.set_min_dimensions(Some(snap.dimensions.into()));
138138
self.display
139139
.gl_window()
140140
.window()
@@ -153,7 +153,7 @@ impl Cropper {
153153

154154
snap_tex: SrgbTexture2d::with_mipmaps(
155155
&self.display,
156-
RawImage2d::from_raw_rgb(snap.data().into(), snap.dimensions()),
156+
RawImage2d::from_raw_rgb(snap.data.clone(), snap.dimensions),
157157
MipmapsOption::NoMipmap,
158158
)?,
159159
snap: snap,
@@ -257,8 +257,8 @@ impl Cropper {
257257
ModifiersState { shift: true, .. } => {
258258
context.region = context
259259
.snap
260-
.windows()
261-
.into_iter()
260+
.windows
261+
.iter()
262262
.find(|w| w.content_bounds.contains(x as u32, y as u32))
263263
.map(|w| Rectangle {
264264
x: w.content_bounds.x as f64,
@@ -306,7 +306,7 @@ impl Cropper {
306306
fn render(
307307
&mut self,
308308
frame: &mut glium::Frame,
309-
ctx: &mut CroppingContext<impl Screenshot>,
309+
ctx: &mut CroppingContext,
310310
) -> Result<(), CropperError> {
311311
if let (Some(areg), Some(reg)) = (ctx.animated_region, ctx.region) {
312312
let delta_s = ctx.delta.as_millis() as f64 / 1000.0;
@@ -363,10 +363,10 @@ impl Cropper {
363363
200.0f32
364364
),
365365
bounds: [
366-
(areg.x as f32) / (ctx.snap.dimensions().0 as f32),
367-
1.0 - (areg.y as f32) / (ctx.snap.dimensions().1 as f32),
368-
(areg.w as f32) / (ctx.snap.dimensions().0 as f32),
369-
-(areg.h as f32) / (ctx.snap.dimensions().1 as f32)
366+
(areg.x as f32) / (ctx.snap.dimensions.0 as f32),
367+
1.0 - (areg.y as f32) / (ctx.snap.dimensions.1 as f32),
368+
(areg.w as f32) / (ctx.snap.dimensions.0 as f32),
369+
-(areg.h as f32) / (ctx.snap.dimensions.1 as f32)
370370
],
371371
};
372372

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod msgbox;
99
mod screengrab;
1010

1111
use cropper::Cropper;
12+
use screengrab::Screenshot;
1213

1314
custom_error! { ScreenshotError
1415
Cropping{source: cropper::CropperError} = "error while cropping: {source:?}",
@@ -20,7 +21,7 @@ fn main() -> Result<(), ScreenshotError> {
2021

2122
hotkey::register(true, || {
2223
// get screenshot
23-
match cropper.apply(screengrab::snap()) {
24+
match cropper.apply(Screenshot::take()) {
2425
Err(e) => {
2526
msgbox::error(&format!("{:?}", e));
2627
true

src/screengrab/mod.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,29 @@ use std::ops::Add;
33
#[cfg_attr(windows, path = "windows.rs")]
44
mod os;
55

6-
pub use os::snap;
6+
#[derive(Debug)]
7+
pub struct Screenshot {
8+
os: os::OsScreenshot,
79

8-
pub trait Screenshot {
9-
fn data(&self) -> &[u8];
10-
fn dimensions(&self) -> (u32, u32);
11-
fn windows(&self) -> &[Window];
12-
fn copy_to_clipboard(&self, region: Rectangle<u32>);
10+
pub data: Vec<u8>,
11+
pub dimensions: (u32, u32),
12+
pub windows: Vec<Window>,
13+
}
14+
15+
impl Screenshot {
16+
pub fn take() -> Self {
17+
os::take_screenshot()
18+
}
19+
20+
pub fn copy_to_clipboard(&self, region: Rectangle<u32>) {
21+
os::copy_to_clipboard(self, region);
22+
}
23+
}
24+
25+
#[derive(Debug, PartialEq, Copy, Clone)]
26+
pub struct Window {
27+
pub bounds: Rectangle<u32>,
28+
pub content_bounds: Rectangle<u32>,
1329
}
1430

1531
#[derive(Debug, PartialEq, Copy, Clone)]
@@ -25,9 +41,3 @@ impl<T: PartialEq + PartialOrd + Add<Output = T> + Copy + Clone> Rectangle<T> {
2541
x >= self.x && y >= self.y && x <= (self.x + self.w) && y <= (self.y + self.h)
2642
}
2743
}
28-
29-
#[derive(Debug, PartialEq, Copy, Clone)]
30-
pub struct Window {
31-
pub bounds: Rectangle<u32>,
32-
pub content_bounds: Rectangle<u32>,
33-
}

0 commit comments

Comments
 (0)