Skip to content

Commit

Permalink
Use the lock crate instead of keyboard shortcuts to lock the screen
Browse files Browse the repository at this point in the history
  • Loading branch information
rameloni committed Mar 24, 2024
1 parent 0d6cdc7 commit 1324be0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 94 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
.idea
82 changes: 7 additions & 75 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ dirs = "5.0.1"
clap = { version = "4.4.16", features = ["derive", "cargo"] }
rodio = "0.17.3"
crossterm = "0.27.0"
enigo = "0.1.3"

lock = "0.1.0"

[profile.release]
strip = true # Automatically strip symbols from the binary.
Expand Down
30 changes: 13 additions & 17 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::utils::*;
use crossterm::cursor::{MoveToColumn, MoveToPreviousLine};
use crossterm::execute;
use crossterm::terminal::{Clear, ClearType};
use enigo::*;
use lock::FailureReason;
use notify_rust::{Notification, Timeout};
use rodio::{Decoder, OutputStream, Sink};
use std::thread;
Expand Down Expand Up @@ -128,25 +128,21 @@ pub fn stop_timer() -> AppResult<()> {
Ok(())
}

/// Lock the screen.
fn lock_screen() -> AppResult<()> {
println!("Locking screen...");
let mut enigo = Enigo::new();

enigo.key_down(Key::Meta); // The meta key is meta key (also known as “windows”, “super”, and “command”)

#[cfg(any(target_os = "linux", target_os = "windows"))]
enigo.key_click(Key::Layout('l'));

#[cfg(target_os = "macos")]
{
enigo.key_down(Key::Control);
enigo.key_click(Key::Layout('q'));
enigo.key_up(Key::Control);
}

enigo.key_up(Key::Meta);

Ok(())
lock::lock().map_err(|fail| {
AppError::new(match fail {
FailureReason::CannotExecute => "Cannot execute the lock command.",
FailureReason::LinuxCommandNotFound => {
"Linux command not found. The following commands are supported\
\n- xdg-screensaver\
\n- gnome-screensaver\
\n- dm-tool"
}
})
})
}

/// Trigger the alarm sound and/or the system notification.
Expand Down

0 comments on commit 1324be0

Please sign in to comment.