Skip to content

Commit

Permalink
refactor: rename screenshot window
Browse files Browse the repository at this point in the history
  • Loading branch information
decipher3114 committed Sep 28, 2024
1 parent d507346 commit 8ec929a
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 104 deletions.
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},
crop::CropEvent,
capture::CaptureEvent,
window::WindowType,
};

Expand All @@ -20,12 +20,12 @@ pub enum AppEvent {
OpenConfigureWindow,
OpenDirectory,
UpdateDirectory(Id),
OpenCropWindow,
OpenCaptureWindow,
CaptureFullscreen,
CaptureWindow,
CloseWindow,
WindowClosed(Id),
ExitApp,
Config(Id, ConfigEvent),
Crop(Id, CropEvent),
Capture(Id, CaptureEvent),
}
8 changes: 4 additions & 4 deletions src/entities/crop.rs → src/entities/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ use iced::Point;
use xcap::image::RgbaImage;

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

#[derive(Debug, Clone)]
pub enum CropEvent {
pub enum CaptureEvent {
SetInitialPoint,
UpdateCurrentPosition(Point),
SetFinalPoint,
}

#[derive(Debug, Clone)]
pub struct SelectionArea {
pub struct Area {
pub initial_pos: Option<Point>,
pub final_pos: Option<Point>,
}
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 crop;
pub mod capture;
pub mod style;
pub mod theme;
pub mod window;
6 changes: 3 additions & 3 deletions src/entities/window.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::entities::{config::ConfigureWindow, crop::CropWindow};
use crate::entities::{config::ConfigureWindow, capture::CaptureWindow};

#[derive(Debug)]
pub enum WindowType {
ConfigureWindow(ConfigureWindow),
CropWindow(CropWindow),
Configure(ConfigureWindow),
Capture(CaptureWindow),
}
40 changes: 20 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use assets::{APPNAME, FONT_BOLD, FONT_MEDIUM, ICON, MEDIUM};
use entities::{
app::{App, AppEvent},
config::{Config, ConfigureWindow},
crop::CropWindow,
capture::CaptureWindow,
theme::Theme,
window::WindowType,
};
Expand Down Expand Up @@ -74,8 +74,8 @@ impl App {

pub fn title(&self, id: Id) -> String {
match self.windows.get(&id) {
Some(WindowType::ConfigureWindow(_)) => String::from("Capter"),
Some(WindowType::CropWindow(_)) => String::from("Capter: Crop"),
Some(WindowType::Configure(_)) => String::from("Capter"),
Some(WindowType::Capture(_)) => String::from("Capter: Capture"),
None => String::new(),
}
}
Expand Down Expand Up @@ -107,7 +107,7 @@ impl App {
});
self.windows.insert(
id,
WindowType::ConfigureWindow(ConfigureWindow::new(
WindowType::Configure(ConfigureWindow::new(
shorten_path(self.config.directory.clone()),
self.config.theme.clone(),
)),
Expand All @@ -120,13 +120,13 @@ impl App {
AppEvent::OpenDirectory => self.config.open_directory().into(),
AppEvent::UpdateDirectory(id) => {
self.config.update_directory();
if let Some(WindowType::ConfigureWindow(config_window)) = self.windows.get_mut(&id)
if let Some(WindowType::Configure(config_window)) = self.windows.get_mut(&id)
{
config_window.path = shorten_path(self.config.directory.clone());
}
Task::none()
}
AppEvent::OpenCropWindow => {
AppEvent::OpenCaptureWindow => {
if self.windows.len() <= 1 {
let (id, open_task) = window::open(window::Settings {
transparent: true,
Expand All @@ -139,8 +139,8 @@ impl App {
},
..Default::default()
});
let crop_window = CropWindow::new();
self.windows.insert(id, WindowType::CropWindow(crop_window));
let capture_window = CaptureWindow::new();
self.windows.insert(id, WindowType::Capture(capture_window));
open_task
.discard()
.chain(gain_focus(id))
Expand All @@ -160,10 +160,10 @@ impl App {
AppEvent::CloseWindow => window::get_latest().and_then::<Id>(close).discard(),
AppEvent::WindowClosed(id) => {
match self.windows.remove(&id) {
Some(WindowType::CropWindow(crop_window)) => {
crop_window.crop_screenshot(&self.config);
Some(WindowType::Capture(capture_window)) => {
capture_window.crop_screenshot(&self.config);
}
Some(WindowType::ConfigureWindow(config_window)) => {
Some(WindowType::Configure(config_window)) => {
self.config.theme = config_window.theme.target().clone();
self.config.update_config();
}
Expand All @@ -176,16 +176,16 @@ impl App {
iced::exit()
}
AppEvent::Config(id, message) => {
if let Some(WindowType::ConfigureWindow(config_window)) = self.windows.get_mut(&id)
if let Some(WindowType::Configure(config_window)) = self.windows.get_mut(&id)
{
config_window.update(id, message)
} else {
Task::none()
}
}
AppEvent::Crop(id, message) => {
if let Some(WindowType::CropWindow(crop_window)) = self.windows.get_mut(&id) {
crop_window.update(message)
AppEvent::Capture(id, message) => {
if let Some(WindowType::Capture(capture_window)) = self.windows.get_mut(&id) {
capture_window.update(message)
} else {
Task::none()
}
Expand All @@ -195,12 +195,12 @@ impl App {

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

Expand All @@ -209,7 +209,7 @@ impl App {

pub fn theme(&self, id: Id) -> Theme {
match self.windows.get(&id) {
Some(WindowType::ConfigureWindow(config_window)) => config_window.theme.value().clone(),
Some(WindowType::Configure(config_window)) => config_window.theme.value().clone(),
_ => self.config.theme.clone(),
}
}
Expand All @@ -229,7 +229,7 @@ impl App {
if m.contains(Modifiers::SHIFT) && m.contains(Modifiers::ALT) =>
{
match char.as_str() {
"s" => Some(AppEvent::OpenCropWindow),
"s" => Some(AppEvent::OpenCaptureWindow),
"f" => Some(AppEvent::CaptureFullscreen),
_ => None,
}
Expand Down
52 changes: 0 additions & 52 deletions src/utils/capture/crop.rs

This file was deleted.

52 changes: 49 additions & 3 deletions src/utils/capture/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,58 @@
use arboard::{Clipboard, ImageData};
use xcap::image::{ImageFormat, RgbaImage};
use display_info::DisplayInfo;
use iced::Point;
use xcap::image::{DynamicImage, ImageFormat, RgbaImage};

use crate::entities::config::Config;
use crate::{
entities::{
config::Config,
capture::{CaptureWindow, Area},
},
utils::evaluate_points,
};

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

impl CaptureWindow {
pub fn new() -> Self {
let image = fullscreen::get_fullscreen().unwrap();
CaptureWindow {
cursor_position: Point::ORIGIN,
image,
selection_area: Area {
initial_pos: None,
final_pos: None,
},
}
}

pub fn crop_screenshot(self, config: &Config) {
if let (Some(initial_pos), Some(final_pos)) = (
self.selection_area.initial_pos,
self.selection_area.final_pos,
) {
let scale_factor = DisplayInfo::all()
.unwrap()
.into_iter()
.find(|d| d.is_primary)
.unwrap()
.scale_factor;

let (initial_pos, final_pos) = evaluate_points(initial_pos, final_pos);

let cropped_image = DynamicImage::from(self.image).crop(
(initial_pos.x * scale_factor) as u32,
(initial_pos.y * scale_factor) as u32,
((final_pos.x - initial_pos.x) * scale_factor) as u32,
((final_pos.y - initial_pos.y) * scale_factor) as u32,
);

save_image(config, cropped_image.into_rgba8());
}
}
}

fn save_image(config: &Config, image: RgbaImage) {
let date = chrono::Local::now().format("%Y-%m-%d-%H-%M-%S");

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 @@ -28,7 +28,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::OpenCropWindow).await.unwrap();
output.send(AppEvent::OpenCaptureWindow).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 @@ -34,7 +34,7 @@ pub fn tray_icon() -> TrayIcon {
menu.append_items(&[
&MenuItem::with_id("open", "Open", true, None),
&PredefinedMenuItem::separator(),
&MenuItem::with_id("crop", "Capture & Crop", true, None),
&MenuItem::with_id("capture", "Capture", true, None),
&MenuItem::with_id("fullscreen", "Capture FullScreen", true, None),
&PredefinedMenuItem::separator(),
&MenuItem::with_id("exit", "Exit", true, None),
Expand Down Expand Up @@ -91,9 +91,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,
"crop" => {
"capture" => {
sleep(Duration::from_secs(1));
AppEvent::OpenCropWindow
AppEvent::OpenCaptureWindow
}
"fullscreen" => {
sleep(Duration::from_secs(1));
Expand Down
Loading

0 comments on commit 8ec929a

Please sign in to comment.