use gpui::{Context, Pixels, Task, px};
use instant::Duration;
static INTERVAL: Duration = Duration::from_millis(500);
static PAUSE_DELAY: Duration = Duration::from_millis(300);
#[cfg(not(target_os = "macos"))]
pub(super) const CURSOR_WIDTH: Pixels = px(2.);
#[cfg(target_os = "macos")]
pub(super) const CURSOR_WIDTH: Pixels = px(1.5);
pub(crate) struct BlinkCursor {
visible: bool,
paused: bool,
epoch: usize,
_task: Task<()>,
}
impl BlinkCursor {
pub(crate) fn new() -> Self {
Self {
visible: false,
paused: false,
epoch: 0,
_task: Task::ready(()),
}
}
pub(crate) fn start(&mut self, cx: &mut Context<Self>) {
self.blink(self.epoch, cx);
}
pub(crate) fn stop(&mut self, cx: &mut Context<Self>) {
self.epoch = 0;
self.paused = false;
self.visible = false;
self._task = Task::ready(());
cx.notify();
}
fn next_epoch(&mut self) -> usize {
self.epoch += 1;
self.epoch
}
fn blink(&mut self, epoch: usize, cx: &mut Context<Self>) {
if epoch != self.epoch {
return;
}
if self.paused {
self.visible = true;
return;
}
self.visible = !self.visible;
cx.notify();
let epoch = self.next_epoch();
self._task = cx.spawn(async move |this, cx| {
cx.background_executor().timer(INTERVAL).await;
if let Some(this) = this.upgrade() {
this.update(cx, |this, cx| this.blink(epoch, cx));
}
});
}
pub(crate) fn visible(&self) -> bool {
self.paused || self.visible
}
pub(crate) fn pause(&mut self, cx: &mut Context<Self>) {
if self.epoch == 0 {
return;
}
self.paused = true;
self.visible = true;
cx.notify();
let epoch = self.next_epoch();
self._task = cx.spawn(async move |this, cx| {
cx.background_executor().timer(PAUSE_DELAY).await;
if let Some(this) = this.upgrade() {
this.update(cx, |this, cx| {
if epoch != this.epoch {
return;
}
this.paused = false;
this.blink(epoch, cx);
});
}
});
}
}
#[cfg(test)]
mod tests {
use super::*;
use gpui::{AppContext as _, TestAppContext};
use std::{cell::Cell, rc::Rc};
#[gpui::test]
fn repeated_pauses_keep_cursor_visible_until_idle(cx: &mut TestAppContext) {
let cursor = cx.new(|_| BlinkCursor::new());
assert!(!cursor.read_with(cx, |cursor, _| cursor.visible()));
cursor.update(cx, |cursor, cx| cursor.start(cx));
cx.run_until_parked();
for _ in 0..5 {
cursor.update(cx, |cursor, cx| cursor.pause(cx));
cx.run_until_parked();
cx.executor().advance_clock(Duration::from_millis(200));
cx.run_until_parked();
assert!(cursor.read_with(cx, |cursor, _| cursor.visible()));
}
cx.executor().advance_clock(Duration::from_millis(100));
cx.run_until_parked();
assert!(!cursor.read_with(cx, |cursor, _| cursor.visible()));
cx.executor().advance_clock(INTERVAL);
cx.run_until_parked();
assert!(cursor.read_with(cx, |cursor, _| cursor.visible()));
}
#[gpui::test]
fn pausing_a_cursor_that_is_not_blinking_does_not_start_it(cx: &mut TestAppContext) {
let cursor = cx.new(|_| BlinkCursor::new());
cursor.update(cx, |cursor, cx| cursor.pause(cx));
cx.run_until_parked();
cx.executor().advance_clock(PAUSE_DELAY);
cx.run_until_parked();
assert!(!cursor.read_with(cx, |cursor, _| cursor.visible()));
let notifies = Rc::new(Cell::new(0usize));
let counter = notifies.clone();
let _observer =
cx.update(|cx| cx.observe(&cursor, move |_, _| counter.set(counter.get() + 1)));
cx.run_until_parked();
cx.executor().advance_clock(INTERVAL * 6);
cx.run_until_parked();
assert_eq!(
notifies.get(),
0,
"a cursor that was never started is blinking, and every blink repaints the view"
);
}
#[gpui::test]
fn blurring_a_paused_cursor_leaves_the_next_focus_blinking(cx: &mut TestAppContext) {
let cursor = cx.new(|_| BlinkCursor::new());
cursor.update(cx, |cursor, cx| cursor.start(cx));
cx.run_until_parked();
cursor.update(cx, |cursor, cx| cursor.pause(cx));
cx.run_until_parked();
cursor.update(cx, |cursor, cx| cursor.stop(cx));
cx.run_until_parked();
assert!(!cursor.read_with(cx, |cursor, _| cursor.visible()));
cursor.update(cx, |cursor, cx| cursor.start(cx));
cx.run_until_parked();
assert!(cursor.read_with(cx, |cursor, _| cursor.visible()));
cx.executor().advance_clock(INTERVAL);
cx.run_until_parked();
assert!(!cursor.read_with(cx, |cursor, _| cursor.visible()));
}
#[gpui::test]
fn stopping_a_paused_cursor_ends_the_blink_loop(cx: &mut TestAppContext) {
let cursor = cx.new(|_| BlinkCursor::new());
cursor.update(cx, |cursor, cx| cursor.start(cx));
cx.run_until_parked();
cursor.update(cx, |cursor, cx| cursor.pause(cx));
cx.run_until_parked();
cursor.update(cx, |cursor, cx| cursor.stop(cx));
cx.run_until_parked();
let notifications = Rc::new(Cell::new(0usize));
let count = notifications.clone();
let _observer = cx.update(|cx| cx.observe(&cursor, move |_, _| count.set(count.get() + 1)));
cx.run_until_parked();
cx.executor().advance_clock(Duration::from_secs(3));
cx.run_until_parked();
assert!(!cursor.read_with(cx, |cursor, _| cursor.visible()));
assert_eq!(notifications.get(), 0, "a stopped cursor kept blinking");
cursor.update(cx, |cursor, cx| cursor.start(cx));
cx.run_until_parked();
assert!(cursor.read_with(cx, |cursor, _| cursor.visible()));
}
#[gpui::test]
fn stopping_a_blinking_cursor_ends_the_blink_loop(cx: &mut TestAppContext) {
let cursor = cx.new(|_| BlinkCursor::new());
cursor.update(cx, |cursor, cx| cursor.start(cx));
cx.run_until_parked();
cursor.update(cx, |cursor, cx| cursor.stop(cx));
cx.run_until_parked();
let notifications = Rc::new(Cell::new(0usize));
let count = notifications.clone();
let _observer = cx.update(|cx| cx.observe(&cursor, move |_, _| count.set(count.get() + 1)));
cx.run_until_parked();
cx.executor().advance_clock(Duration::from_secs(3));
cx.run_until_parked();
assert!(!cursor.read_with(cx, |cursor, _| cursor.visible()));
assert_eq!(notifications.get(), 0, "a stopped cursor kept blinking");
}
}