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..5db6151 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..c5b45fa 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -29,7 +29,7 @@ pub struct MouseState { use crate::selection::Speed; use crate::{ - App, CONFIG, + App, corners::SideOrCorner, message::Message, selection::{Selection, SelectionStatus, selection_lock::OptionalSelectionExt as _}, @@ -129,7 +129,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 +138,39 @@ 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), .. + }) + | Mouse(ButtonPressed(Middle)) => 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 { @@ -259,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, }; 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;