Skip to content

Commit

Permalink
chore: rustfmt and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
decipher3114 committed Dec 18, 2024
1 parent e1357c8 commit 904fcdd
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 57 deletions.
3 changes: 2 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use iced::{
keyboard::{key, on_key_press, Modifiers},
widget::horizontal_space,
window::{
self, change_mode, close, close_events, gain_focus, get_scale_factor, icon, settings::PlatformSpecific, Id, Level, Mode, Position
self, change_mode, close, close_events, gain_focus, get_scale_factor, icon,
settings::PlatformSpecific, Id, Level, Mode, Position,
},
Point, Size, Subscription, Task,
};
Expand Down
4 changes: 2 additions & 2 deletions src/config/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ pub fn open_config() -> Result<File, std::io::Error> {
.create(Path::new(&path).parent().unwrap())
.unwrap();
}
return File::options()
File::options()
.read(true)
.create(true)
.truncate(false)
.write(true)
.open(path);
.open(path)
}

pub fn default_path() -> String {
Expand Down
26 changes: 13 additions & 13 deletions src/windows/capture_window/annotate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ use iced::{

use crate::{
theme::Theme,
windows::capture_window::models::{SelectionMode, Mode},
windows::capture_window::models::{Mode, SelectionMode},
};

use super::{
models::{Shape, ShapeType}, utils::{normalize, resolve_arrow_points}, CaptureEvent, CaptureWindow
models::{Shape, ShapeType},
utils::{normalize, resolve_arrow_points},
CaptureEvent, CaptureWindow,
};

impl Program<CaptureEvent, Theme> for CaptureWindow {
Expand Down Expand Up @@ -46,7 +48,7 @@ impl Program<CaptureEvent, Theme> for CaptureWindow {
SelectionMode::FullScreen => {
let (x, y) = self.image.dimensions();
(Point::ORIGIN, Point::new(x as f32, y as f32))
},
}
SelectionMode::Window(id) => {
let window = self.windows.get(&id).unwrap();
let x = if window.x < 0 { 0u32 } else { window.x as u32 };
Expand All @@ -55,16 +57,14 @@ impl Program<CaptureEvent, Theme> for CaptureWindow {
Point::new(x as f32, y as f32),
Point::new(
(window.x + window.width as i32) as f32,
(window.y + window.height as i32) as f32
)
(window.y + window.height as i32) as f32,
),
)
},
}
SelectionMode::InProgress(initial_pt) => {
normalize(initial_pt, self.cursor_position)
},
SelectionMode::Area(endpoints) => {
(endpoints.initial_pt, endpoints.final_pt)
},
}
SelectionMode::Area(endpoints) => (endpoints.initial_pt, endpoints.final_pt),
};

let selection = Path::rectangle(top_left, (bottom_right - top_left).into());
Expand Down Expand Up @@ -108,8 +108,7 @@ impl Program<CaptureEvent, Theme> for CaptureWindow {

frame.stroke(&selection, stroke);

let (width, height) =
(bottom_right.x - top_left.x, bottom_right.y - top_left.y);
let (width, height) = (bottom_right.x - top_left.x, bottom_right.y - top_left.y);

let horizontal_segment_len = if width > 80.0 { 20.0 } else { width / 4.0 };

Expand Down Expand Up @@ -242,7 +241,8 @@ fn draw_shape(frame: &mut Frame, shape: &Shape) {
frame.stroke(&path, stroke);
}
ShapeType::Arrow => {
let (right_pt, left_pt) = resolve_arrow_points(endpoints.initial_pt, endpoints.final_pt);
let (right_pt, left_pt) =
resolve_arrow_points(endpoints.initial_pt, endpoints.final_pt);
let mut builder = Builder::new();
builder.move_to(endpoints.initial_pt);
builder.line_to(endpoints.final_pt);
Expand Down
36 changes: 18 additions & 18 deletions src/windows/capture_window/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use xcap::{
};

use super::{
models::{SelectionMode, CapturedWindow, Mode, Shape, ShapeType}, utils::{normalize, resolve_arrow_points}, CaptureWindow
models::{CapturedWindow, Mode, SelectionMode, Shape, ShapeType},
utils::{normalize, resolve_arrow_points},
CaptureWindow,
};

impl CaptureWindow {
Expand Down Expand Up @@ -76,23 +78,20 @@ impl CaptureWindow {
}

pub fn auto_detect_area(&mut self) {
if let Some(id) = self.windows
.iter()
.find_map(|(id, window)| {
let top_left = Point::new(window.x as f32,window.y as f32);
let bottom_right = Point::new(
(window.x + window.width as i32) as f32,
(window.y + window.height as i32) as f32,
);
if (top_left.x..bottom_right.x).contains(&(self.cursor_position.x)) &&
(top_left.y..bottom_right.y).contains(&(self.cursor_position.y))
{
Some(id)
} else {
None
}
if let Some(id) = self.windows.iter().find_map(|(id, window)| {
let top_left = Point::new(window.x as f32, window.y as f32);
let bottom_right = Point::new(
(window.x + window.width as i32) as f32,
(window.y + window.height as i32) as f32,
);
if (top_left.x..bottom_right.x).contains(&(self.cursor_position.x))
&& (top_left.y..bottom_right.y).contains(&(self.cursor_position.y))
{
Some(id)
} else {
None
}
) {
}) {
self.selection_mode = SelectionMode::Window(*id);
} else {
self.selection_mode = SelectionMode::FullScreen;
Expand Down Expand Up @@ -145,7 +144,8 @@ pub fn draw_shapes(image: &RgbaImage, shapes: Vec<Shape>) -> RgbaImage {
pixmap.stroke_path(&path, &paint, &stroke, transform, None);
}
ShapeType::Arrow => {
let (right_pt, left_pt) = resolve_arrow_points(endpoints.initial_pt, endpoints.final_pt);
let (right_pt, left_pt) =
resolve_arrow_points(endpoints.initial_pt, endpoints.final_pt);
let mut builder = PathBuilder::new();
builder.move_to(endpoints.initial_pt.x, endpoints.initial_pt.y);
builder.line_to(endpoints.final_pt.x, endpoints.final_pt.y);
Expand Down
34 changes: 15 additions & 19 deletions src/windows/capture_window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use iced::{
};
use indexmap::IndexMap;
use models::{
SelectionMode, CapturedWindow, Endpoints, Mode, Shape, ShapeColor, ShapeStroke, ShapeType
CapturedWindow, Endpoints, Mode, SelectionMode, Shape, ShapeColor, ShapeStroke, ShapeType,
};
use utils::normalize;
use xcap::image::RgbaImage;
Expand Down Expand Up @@ -111,14 +111,12 @@ impl CaptureWindow {
endpoints.final_pt = final_pt;
}
}
Mode::Crop => {
match self.selection_mode {
SelectionMode::FullScreen | SelectionMode::Window(_) => {
self.auto_detect_area();
}
_ => ()
Mode::Crop => match self.selection_mode {
SelectionMode::FullScreen | SelectionMode::Window(_) => {
self.auto_detect_area();
}
}
_ => (),
},
}
}
CaptureEvent::SetFinalPoint => {
Expand All @@ -133,13 +131,12 @@ impl CaptureWindow {
Mode::Crop => {
if let SelectionMode::InProgress(initial_pt) = self.selection_mode {
if self.cursor_position != initial_pt {
let (top_left, bottom_right) = normalize(initial_pt, self.cursor_position);
self.selection_mode = SelectionMode::Area(
Endpoints {
initial_pt: top_left,
final_pt: bottom_right,
}
)
let (top_left, bottom_right) =
normalize(initial_pt, self.cursor_position);
self.selection_mode = SelectionMode::Area(Endpoints {
initial_pt: top_left,
final_pt: bottom_right,
})
} else {
self.auto_detect_area();
}
Expand Down Expand Up @@ -272,8 +269,7 @@ impl CaptureWindow {

let mut overlay = column![vertical_space().height(5)];

if self.shape.endpoints.is_none()
{
if self.shape.endpoints.is_none() {
overlay = overlay.push(toolbar);
};

Expand All @@ -284,11 +280,11 @@ impl CaptureWindow {
let (top_left, bottom_right) = normalize(initial_pt, self.cursor_position);
let area = bottom_right - top_left;
format!("Area: {} x {}", area.x as u32, area.y as u32)
},
}
SelectionMode::Area(endpoints) => {
let area = endpoints.final_pt - endpoints.initial_pt;
format!("Area: {} x {}", area.x as u32, area.y as u32)
},
}
};

overlay = overlay.push(vertical_space().height(Fill));
Expand Down
2 changes: 1 addition & 1 deletion src/windows/capture_window/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub enum SelectionMode {
FullScreen,
Window(u32),
InProgress(Point),
Area(Endpoints)
Area(Endpoints),
}

pub struct CapturedWindow {
Expand Down
6 changes: 3 additions & 3 deletions src/windows/capture_window/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn normalize(initial_pt: Point, final_pt: Point) -> (Point, Point) {

(start, end)
}

pub fn resolve_arrow_points(initial_pt: Point, final_pt: Point) -> (Point, Point) {
let line = final_pt - initial_pt;
let length = f32::sqrt(line.x.powf(2.0) + line.y.powf(2.0));
Expand All @@ -28,5 +28,5 @@ pub fn resolve_arrow_points(initial_pt: Point, final_pt: Point) -> (Point, Point
final_pt.x - size * (rad + PI / 5.0).cos(),
final_pt.y - size * (rad + PI / 5.0).sin(),
);
(right_pt , left_pt)
}
(right_pt, left_pt)
}

0 comments on commit 904fcdd

Please sign in to comment.