Add a slight delay between screen off and locking

This commit is contained in:
Ian Douglas Scott 2024-12-24 13:15:37 -08:00
parent eaa09a6fef
commit e3f2e505bb

View file

@ -1,12 +1,12 @@
#![allow(clippy::single_match)] #![allow(clippy::single_match)]
use calloop::{channel, EventLoop}; use calloop::{channel, timer, EventLoop};
use calloop_wayland_source::WaylandSource; use calloop_wayland_source::WaylandSource;
use cosmic_config::{calloop::ConfigWatchSource, CosmicConfigEntry}; use cosmic_config::{calloop::ConfigWatchSource, CosmicConfigEntry};
use cosmic_idle_config::CosmicIdleConfig; use cosmic_idle_config::CosmicIdleConfig;
use cosmic_settings_config::shortcuts; use cosmic_settings_config::shortcuts;
use futures_lite::stream::StreamExt; use futures_lite::stream::StreamExt;
use std::process::Command; use std::{process::Command, time::Duration};
use upower_dbus::UPowerProxy; use upower_dbus::UPowerProxy;
use wayland_client::{ use wayland_client::{
delegate_noop, delegate_noop,
@ -30,6 +30,9 @@ mod fade_black;
use fade_black::FadeBlackSurface; use fade_black::FadeBlackSurface;
mod freedesktop_screensaver; mod freedesktop_screensaver;
// Delay between screen off and locking
const LOCK_SCREEN_DELAY: Duration = Duration::from_millis(500);
#[derive(Debug)] #[derive(Debug)]
enum Event { enum Event {
OnBattery(bool), OnBattery(bool),
@ -99,6 +102,7 @@ struct State {
on_battery: bool, on_battery: bool,
screensaver_inhibit: bool, screensaver_inhibit: bool,
system_actions: shortcuts::SystemActions, system_actions: shortcuts::SystemActions,
loop_handle: calloop::LoopHandle<'static, Self>,
} }
fn run_command(command: String) { fn run_command(command: String) {
@ -157,6 +161,16 @@ impl State {
output.fade_surface = None; output.fade_surface = None;
} }
let timer = timer::Timer::from_duration(LOCK_SCREEN_DELAY);
self.loop_handle
.insert_source(timer, |_, _, state| {
state.lock_screen();
timer::TimeoutAction::Drop
})
.unwrap();
}
fn lock_screen(&self) {
if let Some(command) = self if let Some(command) = self
.system_actions .system_actions
.get(&shortcuts::action::System::LockScreen) .get(&shortcuts::action::System::LockScreen)
@ -267,6 +281,8 @@ fn main() {
let shortcuts_config = shortcuts::context().unwrap(); let shortcuts_config = shortcuts::context().unwrap();
let system_actions = shortcuts::system_actions(&shortcuts_config); let system_actions = shortcuts::system_actions(&shortcuts_config);
let mut event_loop: EventLoop<State> = EventLoop::try_new().unwrap();
let mut state = State { let mut state = State {
inner: StateInner { inner: StateInner {
registry: globals.registry().clone(), registry: globals.registry().clone(),
@ -286,6 +302,7 @@ fn main() {
on_battery: false, on_battery: false,
screensaver_inhibit: false, screensaver_inhibit: false,
system_actions, system_actions,
loop_handle: event_loop.handle(),
}; };
globals.contents().with_list(|list| { globals.contents().with_list(|list| {
for global in list { for global in list {
@ -296,8 +313,6 @@ fn main() {
}); });
state.recreate_notifications(); state.recreate_notifications();
let mut event_loop: EventLoop<State> = EventLoop::try_new().unwrap();
WaylandSource::new(connection, event_queue) WaylandSource::new(connection, event_queue)
.insert(event_loop.handle()) .insert(event_loop.handle())
.unwrap(); .unwrap();