Skip to content

Commit

Permalink
Refactor screengrab module
Browse files Browse the repository at this point in the history
  • Loading branch information
nasso committed Jul 25, 2019
1 parent 6e1e327 commit 56a104e
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 178 deletions.
26 changes: 13 additions & 13 deletions src/cropper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ struct CropperPrograms {
sub_quad_tex: Program,
}

struct CroppingContext<T> {
struct CroppingContext {
started: Instant,
delta: Duration,

snap: T,
snap: Screenshot,
snap_tex: SrgbTexture2d,

region: Option<Rectangle<f64>>,
Expand Down Expand Up @@ -126,15 +126,15 @@ impl Cropper {
})
}

pub fn apply(&mut self, snap: impl Screenshot) -> Result<bool, CropperError> {
pub fn apply(&mut self, snap: Screenshot) -> Result<bool, CropperError> {
self.display
.gl_window()
.window()
.set_max_dimensions(Some(snap.dimensions().into()));
.set_max_dimensions(Some(snap.dimensions.into()));
self.display
.gl_window()
.window()
.set_min_dimensions(Some(snap.dimensions().into()));
.set_min_dimensions(Some(snap.dimensions.into()));
self.display
.gl_window()
.window()
Expand All @@ -153,7 +153,7 @@ impl Cropper {

snap_tex: SrgbTexture2d::with_mipmaps(
&self.display,
RawImage2d::from_raw_rgb(snap.data().into(), snap.dimensions()),
RawImage2d::from_raw_rgb(snap.data.clone(), snap.dimensions),
MipmapsOption::NoMipmap,
)?,
snap: snap,
Expand Down Expand Up @@ -257,8 +257,8 @@ impl Cropper {
ModifiersState { shift: true, .. } => {
context.region = context
.snap
.windows()
.into_iter()
.windows
.iter()
.find(|w| w.content_bounds.contains(x as u32, y as u32))
.map(|w| Rectangle {
x: w.content_bounds.x as f64,
Expand Down Expand Up @@ -306,7 +306,7 @@ impl Cropper {
fn render(
&mut self,
frame: &mut glium::Frame,
ctx: &mut CroppingContext<impl Screenshot>,
ctx: &mut CroppingContext,
) -> Result<(), CropperError> {
if let (Some(areg), Some(reg)) = (ctx.animated_region, ctx.region) {
let delta_s = ctx.delta.as_millis() as f64 / 1000.0;
Expand Down Expand Up @@ -363,10 +363,10 @@ impl Cropper {
200.0f32
),
bounds: [
(areg.x as f32) / (ctx.snap.dimensions().0 as f32),
1.0 - (areg.y as f32) / (ctx.snap.dimensions().1 as f32),
(areg.w as f32) / (ctx.snap.dimensions().0 as f32),
-(areg.h as f32) / (ctx.snap.dimensions().1 as f32)
(areg.x as f32) / (ctx.snap.dimensions.0 as f32),
1.0 - (areg.y as f32) / (ctx.snap.dimensions.1 as f32),
(areg.w as f32) / (ctx.snap.dimensions.0 as f32),
-(areg.h as f32) / (ctx.snap.dimensions.1 as f32)
],
};

Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod msgbox;
mod screengrab;

use cropper::Cropper;
use screengrab::Screenshot;

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

hotkey::register(true, || {
// get screenshot
match cropper.apply(screengrab::snap()) {
match cropper.apply(Screenshot::take()) {
Err(e) => {
msgbox::error(&format!("{:?}", e));
true
Expand Down
34 changes: 22 additions & 12 deletions src/screengrab/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,29 @@ use std::ops::Add;
#[cfg_attr(windows, path = "windows.rs")]
mod os;

pub use os::snap;
#[derive(Debug)]
pub struct Screenshot {
os: os::OsScreenshot,

pub trait Screenshot {
fn data(&self) -> &[u8];
fn dimensions(&self) -> (u32, u32);
fn windows(&self) -> &[Window];
fn copy_to_clipboard(&self, region: Rectangle<u32>);
pub data: Vec<u8>,
pub dimensions: (u32, u32),
pub windows: Vec<Window>,
}

impl Screenshot {
pub fn take() -> Self {
os::take_screenshot()
}

pub fn copy_to_clipboard(&self, region: Rectangle<u32>) {
os::copy_to_clipboard(self, region);
}
}

#[derive(Debug, PartialEq, Copy, Clone)]
pub struct Window {
pub bounds: Rectangle<u32>,
pub content_bounds: Rectangle<u32>,
}

#[derive(Debug, PartialEq, Copy, Clone)]
Expand All @@ -25,9 +41,3 @@ impl<T: PartialEq + PartialOrd + Add<Output = T> + Copy + Clone> Rectangle<T> {
x >= self.x && y >= self.y && x <= (self.x + self.w) && y <= (self.y + self.h)
}
}

#[derive(Debug, PartialEq, Copy, Clone)]
pub struct Window {
pub bounds: Rectangle<u32>,
pub content_bounds: Rectangle<u32>,
}
Loading

0 comments on commit 56a104e

Please sign in to comment.