Skip to content

Earlier cli parsing and refactors #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
docs/docs
target/
.direnv/
5 changes: 5 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -87,6 +89,8 @@ pub struct App {
pub screenshot: RgbaHandle,
/// Area of the screen that is selected for capture
pub selection: Option<Selection>,
/// The user specified configuration
pub config: Config,
/// Errors to display to the user
pub errors: Vec<ErrorMessage>,
}
Expand All @@ -101,6 +105,7 @@ impl Default for App {
selection: None,
selections_created: 0,
errors: vec![],
config: Config::parse(),
}
}
}
Expand Down
63 changes: 36 additions & 27 deletions src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _},
Expand Down Expand Up @@ -129,7 +129,7 @@ impl canvas::Program<Message> 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
Expand All @@ -138,27 +138,39 @@ impl canvas::Program<Message> 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
Expand All @@ -168,8 +180,8 @@ impl canvas::Program<Message> 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,
Expand All @@ -180,19 +192,17 @@ impl canvas::Program<Message> 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,
initial_rect_pos: selection.pos(),
speed: Speed::Slow {
has_speed_changed: true,
},
}
} else {
Message::NoOp
},
_ => Message::NoOp,
}
})
} else {
Expand Down Expand Up @@ -259,7 +269,6 @@ impl canvas::Program<Message> for App {
Mouse(CursorMoved { position }) if self.selection.is_some_and(Selection::is_create) => {
Message::ExtendNewSelection(*position)
}
Mouse(ButtonPressed(Middle)) => Message::SelectFullScreen,
_ => return None,
};

Expand Down
5 changes: 0 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -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<Config> = LazyLock::new(Config::parse);

/// Configuration for the program
#[derive(Parser, Debug)]
#[command(version, about, author = "Nik Revenco")]
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;