From 70a089d452c0ea8b815724fa5c0a9c355e053d3d Mon Sep 17 00:00:00 2001 From: Dlurak <84224239+Dlurak@users.noreply.github.com> Date: Fri, 18 Apr 2025 15:59:08 +0200 Subject: [PATCH 1/2] Earlier cli parsing and refactors --- .gitignore | 1 + src/app.rs | 5 ++++ src/canvas.rs | 74 +++++++++++++++++++++++++++++---------------------- src/config.rs | 5 ---- src/lib.rs | 1 - 5 files changed, 48 insertions(+), 38 deletions(-) diff --git a/.gitignore b/.gitignore index f756e1b..aca13a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ docs/docs target/ +.direnv/ diff --git a/src/app.rs b/src/app.rs index be3e62b..cd3db09 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,5 +1,6 @@ //! Main logic for the application, handling of events and mutation of the state +use crate::config::Config; use crate::selection::Speed; use std::borrow::Cow; use std::time::Instant; @@ -8,6 +9,7 @@ use crate::message::Message; use crate::screenshot::RgbaHandle; use crate::selection::selection_lock::OptionalSelectionExt; use crate::theme::THEME; +use clap::Parser; use iced::alignment::Vertical; use iced::mouse::Cursor; use iced::widget::canvas::Path; @@ -87,6 +89,8 @@ pub struct App { pub screenshot: RgbaHandle, /// Area of the screen that is selected for capture pub selection: Option, + /// The user specified configuration + pub config: Config, /// Errors to display to the user pub errors: Vec, } @@ -101,6 +105,7 @@ impl Default for App { selection: None, selections_created: 0, errors: vec![], + config: Config::parse() } } } diff --git a/src/canvas.rs b/src/canvas.rs index 37bc93f..7cf2583 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -1,19 +1,20 @@ //! The canvas handles drawing the selection frame -use iced::Event::{Keyboard, Mouse}; +use iced::advanced::debug::core::SmolStr; +use iced::keyboard::key::Named::F11; +use iced::keyboard::key::Named::{Enter, Escape, Shift}; use iced::keyboard::Event::KeyPressed; use iced::keyboard::Event::KeyReleased; use iced::keyboard::Key::{Character, Named}; use iced::keyboard::Modifiers as Mods; -use iced::keyboard::key::Named::F11; -use iced::keyboard::key::Named::{Enter, Escape, Shift}; use iced::mouse::Button::{Left, Middle, Right}; use iced::mouse::Event::ButtonPressed; use iced::mouse::Event::ButtonReleased; use iced::mouse::Event::CursorMoved; +use iced::Event::{Keyboard, Mouse}; use iced::{ - Rectangle, Renderer, Theme, mouse::{self, Interaction}, - widget::{self, Action, canvas}, + widget::{self, canvas, Action}, + Rectangle, Renderer, Theme, }; /// Holds information about the mouse @@ -29,11 +30,11 @@ pub struct MouseState { use crate::selection::Speed; use crate::{ - App, CONFIG, corners::SideOrCorner, message::Message, - selection::{Selection, SelectionStatus, selection_lock::OptionalSelectionExt as _}, + selection::{selection_lock::OptionalSelectionExt as _, Selection, SelectionStatus}, theme::THEME, + App, }; impl canvas::Program for App { @@ -129,7 +130,7 @@ impl canvas::Program for App { } Mouse(ButtonReleased(Left)) => { state.is_left_down = false; - if CONFIG.instant && self.selections_created == 1 { + if self.config.instant && self.selections_created == 1 { // we have created 1 selections in total, (the current one), // in which case we want to copy it to the clipboard as the // --instant flag was provided @@ -138,27 +139,38 @@ impl canvas::Program for App { Message::EnterIdle } } - Keyboard(KeyReleased { key, .. }) if *key == Named(Shift) => { + Keyboard(KeyReleased { + key: Named(Shift), .. + }) => { state.is_shift_down = false; Message::NoOp } // Esc - Keyboard(KeyPressed { key, .. }) if *key == Named(Escape) => Message::Exit, - // Ctrl + C or Enter - Keyboard(KeyPressed { key, modifiers, .. }) - if (*key == Named(Enter)) - || (*modifiers == Mods::CTRL && *key == Character("c".into())) => - { - Message::CopyToClipboard - } + Keyboard(KeyPressed { + key: Named(Escape), .. + }) => Message::Exit, + // Ctrl + C + Keyboard(KeyPressed { + key: Character(c), + modifiers: Mods::CTRL, + .. + }) if *c == "c" => Message::CopyToClipboard, + // Enter + Keyboard(KeyPressed { + key: Named(Enter), .. + }) => Message::CopyToClipboard, // Ctrl + S - Keyboard(KeyPressed { key, modifiers, .. }) - if (*modifiers == Mods::CTRL && *key == Character("s".into())) => - { - Message::SaveScreenshot - } - Keyboard(KeyPressed { key, .. }) if *key == Named(F11) => Message::SelectFullScreen, - Keyboard(KeyPressed { key, .. }) if *key == Named(Shift) => { + Keyboard(KeyPressed { + key: Character(c), + modifiers: Mods::CTRL, + .. + }) if *c == "s" => Message::SaveScreenshot, + Keyboard(KeyPressed { + key: Named(F11), .. + }) => Message::SelectFullScreen, + Keyboard(KeyPressed { + key: Named(Shift), .. + }) => { state.is_shift_down = true; // If we are already resizing a side, and we press shift, we @@ -168,8 +180,8 @@ impl canvas::Program for App { cursor .position() .map_or(Message::NoOp, |current_cursor_pos| { - if let SelectionStatus::Resize { resize_side, .. } = selection.status { - Message::Resize { + match selection.status { + SelectionStatus::Resize { resize_side, .. } => Message::Resize { resize_side, // start resizing from this point on current_cursor_pos, @@ -180,9 +192,8 @@ impl canvas::Program for App { speed: Speed::Slow { has_speed_changed: true, }, - } - } else if let SelectionStatus::Move { .. } = selection.status { - Message::MoveSelection { + }, + SelectionStatus::Move { .. } => Message::MoveSelection { current_cursor_pos, initial_cursor_pos: current_cursor_pos, current_selection: selection, @@ -190,9 +201,8 @@ impl canvas::Program for App { speed: Speed::Slow { has_speed_changed: true, }, - } - } else { - Message::NoOp + }, + _ => Message::NoOp, } }) } else { diff --git a/src/config.rs b/src/config.rs index 9303054..c45061d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,11 +1,6 @@ //! Command line arguments to configure the program -use std::sync::LazyLock; - use clap::Parser; -/// Configuration of the app -pub static CONFIG: LazyLock = LazyLock::new(Config::parse); - /// Configuration for the program #[derive(Parser, Debug)] #[command(version, about, author = "Nik Revenco")] diff --git a/src/lib.rs b/src/lib.rs index 0b91459..51cd142 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,4 +36,3 @@ pub use clipboard::{CLIPBOARD_DAEMON_ID, run_clipboard_daemon}; pub use app::App; pub use app::SAVED_IMAGE; -pub use config::CONFIG; From d9591439be218ddfae2103e898d32bc5bf6a6a4b Mon Sep 17 00:00:00 2001 From: Dlurak <84224239+Dlurak@users.noreply.github.com> Date: Fri, 18 Apr 2025 16:08:32 +0200 Subject: [PATCH 2/2] fix linting problems --- src/app.rs | 2 +- src/canvas.rs | 19 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/app.rs b/src/app.rs index cd3db09..5db6151 100644 --- a/src/app.rs +++ b/src/app.rs @@ -105,7 +105,7 @@ impl Default for App { selection: None, selections_created: 0, errors: vec![], - config: Config::parse() + config: Config::parse(), } } } diff --git a/src/canvas.rs b/src/canvas.rs index 7cf2583..c5b45fa 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -1,20 +1,19 @@ //! The canvas handles drawing the selection frame -use iced::advanced::debug::core::SmolStr; -use iced::keyboard::key::Named::F11; -use iced::keyboard::key::Named::{Enter, Escape, Shift}; +use iced::Event::{Keyboard, Mouse}; use iced::keyboard::Event::KeyPressed; use iced::keyboard::Event::KeyReleased; use iced::keyboard::Key::{Character, Named}; use iced::keyboard::Modifiers as Mods; +use iced::keyboard::key::Named::F11; +use iced::keyboard::key::Named::{Enter, Escape, Shift}; use iced::mouse::Button::{Left, Middle, Right}; use iced::mouse::Event::ButtonPressed; use iced::mouse::Event::ButtonReleased; use iced::mouse::Event::CursorMoved; -use iced::Event::{Keyboard, Mouse}; use iced::{ - mouse::{self, Interaction}, - widget::{self, canvas, Action}, Rectangle, Renderer, Theme, + mouse::{self, Interaction}, + widget::{self, Action, canvas}, }; /// Holds information about the mouse @@ -30,11 +29,11 @@ pub struct MouseState { use crate::selection::Speed; use crate::{ + App, corners::SideOrCorner, message::Message, - selection::{selection_lock::OptionalSelectionExt as _, Selection, SelectionStatus}, + selection::{Selection, SelectionStatus, selection_lock::OptionalSelectionExt as _}, theme::THEME, - App, }; impl canvas::Program for App { @@ -167,7 +166,8 @@ impl canvas::Program for App { }) if *c == "s" => Message::SaveScreenshot, Keyboard(KeyPressed { key: Named(F11), .. - }) => Message::SelectFullScreen, + }) + | Mouse(ButtonPressed(Middle)) => Message::SelectFullScreen, Keyboard(KeyPressed { key: Named(Shift), .. }) => { @@ -269,7 +269,6 @@ impl canvas::Program for App { Mouse(CursorMoved { position }) if self.selection.is_some_and(Selection::is_create) => { Message::ExtendNewSelection(*position) } - Mouse(ButtonPressed(Middle)) => Message::SelectFullScreen, _ => return None, };