Skip to content

Commit

Permalink
refactor: rename FreeForm to Crop
Browse files Browse the repository at this point in the history
  • Loading branch information
decipher3114 committed Sep 12, 2024
1 parent ef1c353 commit 1663959
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 45 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
A simple cross-platform screenshot tool made in Rust

## ✨ Features
- Captures FreeForm, FullScreen and Window
- Captures Cropped, FullScreen and Window
- Keybindings support
- Better usablity
> FullScreen and Window Screenshots are immediate.
Expand Down
6 changes: 3 additions & 3 deletions src/entities/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tray_icon::TrayIcon;

use crate::entities::{
config::{Config, ConfigEvent},
freeform::FreeFormEvent,
crop::CropEvent,
window::WindowType,
};

Expand All @@ -19,12 +19,12 @@ pub struct App {
pub enum AppEvent {
OpenConfigureWindow,
UpdateConfig(Id),
InitiateFreeForm,
OpenCropWindow,
CaptureFullscreen,
CaptureWindow,
CloseWindow,
WindowClosed(Id),
ExitApp,
Config(Id, ConfigEvent),
FreeForm(Id, FreeFormEvent),
Crop(Id, CropEvent),
}
4 changes: 2 additions & 2 deletions src/entities/freeform.rs → src/entities/crop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use iced::Point;
use xcap::image::RgbaImage;

#[derive(Debug)]
pub struct FreeFormWindow {
pub struct CropWindow {
pub cursor_position: Point,
pub image: RgbaImage,
pub selection_area: SelectionArea,
}

#[derive(Debug, Clone)]
pub enum FreeFormEvent {
pub enum CropEvent {
SetInitialPoint,
UpdateCurrentPosition(Point),
SetFinalPoint,
Expand Down
2 changes: 1 addition & 1 deletion src/entities/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod app;
pub mod config;
pub mod freeform;
pub mod crop;
pub mod style;
pub mod theme;
pub mod window;
4 changes: 2 additions & 2 deletions src/entities/window.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::entities::{config::ConfigureWindow, freeform::FreeFormWindow};
use crate::entities::{config::ConfigureWindow, crop::CropWindow};

#[derive(Debug)]
pub enum WindowType {
ConfigureWindow(ConfigureWindow),
FreeFormWindow(FreeFormWindow),
CropWindow(CropWindow),
}
28 changes: 14 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use assets::{FONT_BOLD, FONT_MEDIUM, ICON, MEDIUM};
use entities::{
app::{App, AppEvent},
config::{Config, ConfigureWindow},
freeform::FreeFormWindow,
crop::CropWindow,
theme::Theme,
window::WindowType,
};
Expand Down Expand Up @@ -103,7 +103,7 @@ impl App {
}
Task::none()
}
AppEvent::InitiateFreeForm => {
AppEvent::OpenCropWindow => {
if self.windows.len() <= 1 {
let (id, open_task) = window::open(window::Settings {
transparent: true,
Expand All @@ -116,9 +116,9 @@ impl App {
},
..Default::default()
});
let freeform = FreeFormWindow::new();
let crop_window = CropWindow::new();
self.windows
.insert(id, WindowType::FreeFormWindow(freeform));
.insert(id, WindowType::CropWindow(crop_window));
open_task
.discard()
.chain(gain_focus(id))
Expand All @@ -138,8 +138,8 @@ impl App {
AppEvent::CloseWindow => window::get_latest().and_then::<Id>(close).discard(),
AppEvent::WindowClosed(id) => {
match self.windows.remove(&id) {
Some(WindowType::FreeFormWindow(freeform)) => {
freeform.capture_freeform(&self.config);
Some(WindowType::CropWindow(crop_window)) => {
crop_window.crop_screenshot(&self.config);
}
Some(WindowType::ConfigureWindow(_)) => {
self.config.update_config()
Expand All @@ -153,15 +153,15 @@ impl App {
iced::exit()
}
AppEvent::Config(id, message) => {
if let Some(WindowType::ConfigureWindow(config)) = self.windows.get_mut(&id) {
config.update(id, message)
if let Some(WindowType::ConfigureWindow(config_window)) = self.windows.get_mut(&id) {
config_window.update(id, message)
} else {
Task::none()
}
}
AppEvent::FreeForm(id, message) => {
if let Some(WindowType::FreeFormWindow(freeform)) = self.windows.get_mut(&id) {
freeform.update(message)
AppEvent::Crop(id, message) => {
if let Some(WindowType::CropWindow(crop_window)) = self.windows.get_mut(&id) {
crop_window.update(message)
} else {
Task::none()
}
Expand All @@ -171,12 +171,12 @@ impl App {

pub fn view(&self, id: Id) -> Element<AppEvent> {
let content = match &self.windows.get(&id) {
Some(WindowType::ConfigureWindow(config)) => config
Some(WindowType::ConfigureWindow(config_window)) => config_window
.view()
.map(move |message| AppEvent::Config(id, message)),
Some(WindowType::FreeFormWindow(freeform)) => freeform
Some(WindowType::CropWindow(crop_window)) => crop_window
.view()
.map(move |message| AppEvent::FreeForm(id, message)),
.map(move |message| AppEvent::Crop(id, message)),
None => horizontal_space().into(),
};

Expand Down
8 changes: 4 additions & 4 deletions src/utils/capture/freeform.rs → src/utils/capture/crop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ use xcap::image::DynamicImage;
use crate::{
entities::{
config::Config,
freeform::{FreeFormWindow, SelectionArea},
crop::{CropWindow, SelectionArea},
},
utils::evaluate_points,
};

use crate::utils::capture::{fullscreen, save_image};

impl FreeFormWindow {
impl CropWindow {
pub fn new() -> Self {
let image = fullscreen::get_fullscreen().unwrap();
FreeFormWindow {
CropWindow {
cursor_position: Point::ORIGIN,
image,
selection_area: SelectionArea {
Expand All @@ -25,7 +25,7 @@ impl FreeFormWindow {
}
}

pub fn capture_freeform(self, config: &Config) {
pub fn crop_screenshot(self, config: &Config) {

if let (Some(initial_pos), Some(final_pos)) = (
self.selection_area.initial_pos,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/capture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use xcap::image::{ImageFormat, RgbaImage};

use crate::entities::config::Config;

pub mod freeform;
pub mod crop;
pub mod fullscreen;
pub mod window;

Expand Down
2 changes: 1 addition & 1 deletion src/utils/key_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn global_key_listener() -> impl Stream<Item = AppEvent> {
Key::Alt => alt_pressed = true,
Key::ShiftLeft | Key::ShiftRight => shift_pressed = true,
Key::KeyS if alt_pressed && shift_pressed => {
output.send(AppEvent::InitiateFreeForm).await.unwrap();
output.send(AppEvent::OpenCropWindow).await.unwrap();
}
Key::KeyF if alt_pressed && shift_pressed => {
output.send(AppEvent::CaptureFullscreen).await.unwrap();
Expand Down
6 changes: 3 additions & 3 deletions src/utils/tray_icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn tray_icon() -> TrayIcon {
&[
&MenuItem::with_id("open", "Open", true, None),
&PredefinedMenuItem::separator(),
&MenuItem::with_id("freeform", "Capture FreeForm", true, None),
&MenuItem::with_id("crop", "Capture & Crop", true, None),
&MenuItem::with_id("fullscreen", "Capture FullScreen", true, None),
&PredefinedMenuItem::separator(),
&MenuItem::with_id("exit", "Exit", true, None),
Expand Down Expand Up @@ -89,9 +89,9 @@ pub fn tray_menu_listener() -> impl Stream<Item = AppEvent> {
if let Some(MenuEvent { id: MenuId(id)} ) = reciever.recv().await {
let event = match id.as_str() {
"open" => AppEvent::OpenConfigureWindow,
"freeform" => {
"crop" => {
sleep(Duration::from_secs(1));
AppEvent::InitiateFreeForm
AppEvent::OpenCropWindow
},
"fullscreen" => {
sleep(Duration::from_secs(1));
Expand Down
2 changes: 1 addition & 1 deletion src/windows/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl ConfigureWindow {
column![
text("Keybindings:").size(22).font(BOLD),
vertical_space().height(10),
footer_row("Alt+Shift+S", "Captures FreeForm Screenshot"),
footer_row("Alt+Shift+S", "Capture and Crop Screenshot"),
footer_row("Alt+Shift+F", "Captures Full Screenshot"),
footer_row("Alt+Shift+W", "Captures Focused Window"),
]
Expand Down
22 changes: 11 additions & 11 deletions src/windows/freeform.rs → src/windows/crop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ use iced::{

use crate::{
entities::{
freeform::{FreeFormWindow, FreeFormEvent},
crop::{CropWindow, CropEvent},
theme::Theme,
},
style::Element,
utils::evaluate_points,
AppEvent,
};

impl FreeFormWindow {
pub fn update(&mut self, message: FreeFormEvent) -> Task<AppEvent> {
impl CropWindow {
pub fn update(&mut self, message: CropEvent) -> Task<AppEvent> {
match message {
FreeFormEvent::SetInitialPoint => {
CropEvent::SetInitialPoint => {
self.selection_area.final_pos = None;
self.selection_area.initial_pos = Some(self.cursor_position);
}
FreeFormEvent::UpdateCurrentPosition(point) => {
CropEvent::UpdateCurrentPosition(point) => {
self.cursor_position = point;
}
FreeFormEvent::SetFinalPoint => {
CropEvent::SetFinalPoint => {
if Some(self.cursor_position) != self.selection_area.initial_pos {
self.selection_area.final_pos = Some(self.cursor_position);
} else {
Expand All @@ -41,7 +41,7 @@ impl FreeFormWindow {
Task::none()
}

pub fn view(&self) -> Element<FreeFormEvent> {
pub fn view(&self) -> Element<CropEvent> {

let background: Image<Handle> = Image::new(
Handle::from_rgba(
Expand Down Expand Up @@ -72,17 +72,17 @@ impl FreeFormWindow {
]
],
mouse_area(Canvas::new(self).height(Fill).width(Fill))
.on_move(FreeFormEvent::UpdateCurrentPosition)
.on_press(FreeFormEvent::SetInitialPoint)
.on_release(FreeFormEvent::SetFinalPoint)
.on_move(CropEvent::UpdateCurrentPosition)
.on_press(CropEvent::SetInitialPoint)
.on_release(CropEvent::SetFinalPoint)
]
.height(Fill)
.width(Fill)
.into()
}
}

impl Program<FreeFormEvent, Theme> for FreeFormWindow {
impl Program<CropEvent, Theme> for CropWindow {
type State = ();

fn draw(
Expand Down
2 changes: 1 addition & 1 deletion src/windows/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod config;
pub mod freeform;
pub mod crop;

0 comments on commit 1663959

Please sign in to comment.