on master help sign in
idle timer wip
commitf99bea404f43ea0e373fd9fe54616d3282e8364b authorBlaž Hrastnik <blaz@mxxn.io> dateSun 22 Aug 2021, 15:28 (5 years ago) parent8925fdd
3 files changed+25−1
helix-term/src/application.rs+5 helix-term/src/ui/editor.rs+1 helix-view/src/editor.rs+19−1
helix-term/src/application.rs+5
@@ -199,6 +199,11 @@ impl Application {
199199 self.jobs.handle_callback(&mut self.editor, &mut self.compositor, callback);
200200 self.render();
201201 }
202+ _ = &mut self.editor.idle_timer => {
203+ self.editor.clear_idle_timer();
204+ println!("idle!")
205+ // idle timeout
206+ }
202207 }
203208 }
204209 }
helix-term/src/ui/editor.rs+1
@@ -901,6 +901,7 @@ impl Component for EditorView {
901901 EventResult::Consumed(None)
902902 }
903903 Event::Key(key) => {
904+ cxt.editor.reset_idle_timer();
904905 let mut key = KeyEvent::from(key);
905906 canonicalize_key(&mut key);
906907 // clear status
helix-view/src/editor.rs+19−1
@@ -9,10 +9,12 @@ use crate::{
99use futures_util::future;
1010use std::{
1111 path::{Path, PathBuf},
12+ pin::Pin,
1213 sync::Arc,
13− time::Duration,
1414};
1515
16+use tokio::time::{sleep, Duration, Instant, Sleep};
17+
1618use slotmap::SlotMap;
1719
1820use anyhow::Error;
@@ -91,6 +93,8 @@ pub struct Editor {
9193 pub status_msg: Option<(String, Severity)>,
9294
9395 pub config: Config,
96+
97+ pub idle_timer: Pin<Box<Sleep>>,
9498}
9599
96100#[derive(Debug, Copy, Clone)]
@@ -125,10 +129,24 @@ impl Editor {
125129 registers: Registers::default(),
126130 clipboard_provider: get_clipboard_provider(),
127131 status_msg: None,
132+ idle_timer: Box::pin(sleep(Duration::from_millis(500))),
128133 config,
129134 }
130135 }
131136
137+ pub fn clear_idle_timer(&mut self) {
138+ // equivalent to internal Instant::far_future() (30 years)
139+ self.idle_timer
140+ .as_mut()
141+ .reset(Instant::now() + Duration::from_secs(86400 * 365 * 30));
142+ }
143+
144+ pub fn reset_idle_timer(&mut self) {
145+ self.idle_timer
146+ .as_mut()
147+ .reset(Instant::now() + Duration::from_millis(500));
148+ }
149+
132150 pub fn clear_status(&mut self) {
133151 self.status_msg = None;
134152 }