Skip to content

Commit

Permalink
refactor: clippy suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
decipher3114 committed Oct 3, 2024
1 parent e43d698 commit 4a5a96c
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 30 deletions.
10 changes: 4 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,10 @@ impl App {
_ => None,
}
}
(key::Key::Character(char), Modifiers::CTRL) => {
match char.as_str() {
"z" => Some(AppEvent::Undo),
_ => None
}
}
(key::Key::Character(char), Modifiers::CTRL) => match char.as_str() {
"z" => Some(AppEvent::Undo),
_ => None,
},
_ => None,
});

Expand Down
2 changes: 1 addition & 1 deletion src/utils/capture/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::entities::capture::Endpoints;

impl Endpoints {
pub fn normalize(self) -> (Point, Point) {
let (initial_pt, final_pt) = (self.initial_pt.clone(), self.final_pt.clone());
let (initial_pt, final_pt) = (self.initial_pt, self.final_pt);
let (mut start, mut end) = (initial_pt, final_pt);
if initial_pt.x > final_pt.x {
(start.x, end.x) = (final_pt.x, initial_pt.x)
Expand Down
30 changes: 17 additions & 13 deletions src/utils/capture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use xcap::{
use crate::entities::{
capture::{
shape::{Shape, ShapeType},
CaptureWindow, CapturedMonitor, CapturedWindow, Endpoints, Mode, CropMode,
CaptureWindow, CapturedMonitor, CapturedWindow, CropMode, Endpoints, Mode,
},
config::Config,
};
Expand Down Expand Up @@ -77,8 +77,14 @@ impl CaptureWindow {
}
ShapeType::Line => {
let mut builder = PathBuilder::new();
builder.move_to(endpoints.initial_pt.x * scale_factor, endpoints.initial_pt.y * scale_factor);
builder.line_to(endpoints.final_pt.x * scale_factor, endpoints.final_pt.y * scale_factor);
builder.move_to(
endpoints.initial_pt.x * scale_factor,
endpoints.initial_pt.y * scale_factor,
);
builder.line_to(
endpoints.final_pt.x * scale_factor,
endpoints.final_pt.y * scale_factor,
);
let path = builder.finish().unwrap();
pixmap.stroke_path(&path, &paint, &stroke, transform, None);
}
Expand All @@ -87,7 +93,7 @@ impl CaptureWindow {
RgbaImage::from_vec(width, height, pixmap.take()).unwrap()
}

pub fn take_screenshot(mut self, config: &Config) {
pub fn take_screenshot(self, config: &Config) {
let (img_width, img_height) = self.display.image.dimensions();
let top = self.draw_shapes();

Expand All @@ -98,23 +104,22 @@ impl CaptureWindow {
base
}
CropMode::SpecificWindow(id) => {
let window = self.windows.swap_remove(&id).unwrap();
let window = self.windows.get(&id).unwrap();

let x = window.x;
let y = window.y;
let width = window.width;
let height = window.height;
let window_img = window.image.clone();
let window_img = &window.image;

let mut base = RgbaImage::new(img_width, img_height);
overlay(&mut base, &window_img, x as i64, y as i64);
overlay(&mut base, window_img, x as i64, y as i64);
overlay(&mut base, &top, 0, 0);
let x = if x < 0 { 0u32 } else { x as u32 };
let y = if y < 0 { 0u32 } else { y as u32 };
let final_image = DynamicImage::from(base)
DynamicImage::from(base)
.crop_imm(x, y, width, height)
.into_rgba8();
final_image
.into_rgba8()
}
CropMode::ManualSelection | CropMode::SelectionInProgress => {
let (top_left, bottom_right) = self.endpoints.normalize();
Expand All @@ -125,10 +130,9 @@ impl CaptureWindow {
let height = size.y * self.scale_factor;
let mut base = self.display.image;
overlay(&mut base, &top, 0, 0);
let final_image = DynamicImage::from(base)
DynamicImage::from(base)
.crop_imm(x as u32, y as u32, width as u32, height as u32)
.into_rgba8();
final_image
.into_rgba8()
}
};

Expand Down
15 changes: 13 additions & 2 deletions src/utils/key_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ use iced::{
futures::{SinkExt, Stream},
stream,
};
use rdev::{grab, EventType, Key};
use rdev::{EventType, Key};
#[cfg(target_os = "linux")]
use rdev::start_grab_listen;
#[cfg(not(target_os = "linux"))]
use rdev::grab;
use tokio::sync::mpsc::channel;

use crate::entities::app::AppEvent;
Expand All @@ -12,11 +16,18 @@ pub fn global_key_listener() -> impl Stream<Item = AppEvent> {
let (sender, mut receiver) = channel(10);

std::thread::spawn(move || {
#[cfg(target_os = "linux")]
start_grab_listen(move |event| {
sender.blocking_send(event.clone()).ok();
Some(event)
})
.unwrap();
#[cfg(not(target_os = "linux"))]
grab(move |event| {
sender.blocking_send(event.clone()).ok();
Some(event)
})
.unwrap()
.unwrap();
});

let mut alt_pressed = false;
Expand Down
7 changes: 3 additions & 4 deletions src/utils/tray_icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,16 @@ pub fn tray_icon_listener() -> impl Stream<Item = AppEvent> {
});

loop {
match reciever.recv().await {
Some(event) => match event {
if let Some(event) = reciever.recv().await {
match event {
TrayIconEvent::Click { button: Left, .. } => {
output.send(AppEvent::OpenConfigureWindow).await.unwrap()
}
TrayIconEvent::DoubleClick { button: Left, .. } => {
output.send(AppEvent::OpenCaptureWindow).await.unwrap()
}
_ => (),
},
None => (),
}
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion src/windows/capture/annotate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use iced::{
use crate::entities::{
capture::{
shape::{Shape, ShapeType},
CaptureEvent, CaptureWindow, Mode, CropMode,
CaptureEvent, CaptureWindow, CropMode, Mode,
},
theme::Theme,
};
Expand Down
6 changes: 3 additions & 3 deletions src/windows/capture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
entities::{
capture::{
shape::{ShapeColor, ShapeStroke, ShapeType},
CaptureEvent, CaptureWindow, Endpoints, Mode, CropMode,
CaptureEvent, CaptureWindow, CropMode, Endpoints, Mode,
},
style::{ButtonClass, TextClass},
},
Expand Down Expand Up @@ -97,9 +97,9 @@ impl CaptureWindow {
(window.x + window.width as i32) as f32 / scale,
(window.y + window.height as i32) as f32 / scale,
);
if (top_left.0..bottom_right.0).contains(&(self.cursor_position.x as f32))
if (top_left.0..bottom_right.0).contains(&(self.cursor_position.x))
&& (top_left.1..bottom_right.1)
.contains(&(self.cursor_position.y as f32))
.contains(&(self.cursor_position.y))
{
Some((id, window.name.clone(), top_left, bottom_right))
} else {
Expand Down

0 comments on commit 4a5a96c

Please sign in to comment.