Debian Codegraph @

helix-event/src/redraw.rs

raw symbols SCIP
1 //! Signals that control when/if the editor redraws
2
3 use std::future::Future;
4
5 use parking_lot::{RwLock, RwLockReadGuard};
6 use tokio::sync::Notify;
7
8 use crate::runtime_local;
9
10 runtime_local! {
11 /// A `Notify` instance that can be used to (asynchronously) request
12 /// the editor to render a new frame.
13 static REDRAW_NOTIFY: Notify = Notify::const_new();
14
15 /// A `RwLock` that prevents the next frame from being
16 /// drawn until an exclusive (write) lock can be acquired.
17 /// This allows asynchronous tasks to acquire `non-exclusive`
18 /// locks (read) to prevent the next frame from being drawn
19 /// until a certain computation has finished.
20 static RENDER_LOCK: RwLock<()> = RwLock::new(());
21 }
22
23 pub type RenderLockGuard = RwLockReadGuard<'static, ()>;
24
25 /// Requests that the editor is redrawn. The redraws are debounced (currently to
26 /// 30FPS) so this can be called many times without causing a ton of frames to
27 /// be rendered.
28 pub fn request_redraw() {
29 REDRAW_NOTIFY.notify_one();
30 }
31
32 /// Returns a future that will yield once a redraw has been asynchronously
33 /// requested using [`request_redraw`].
34 pub fn redraw_requested() -> impl Future<Output = ()> {
35 REDRAW_NOTIFY.notified()
36 }
37
38 /// Wait until all locks acquired with [`lock_frame`] have been released.
39 /// This function is called before rendering and is intended to allow the frame
40 /// to wait for async computations that should be included in the current frame.
41 pub fn start_frame() {
42 drop(RENDER_LOCK.write());
43 // exhaust any leftover redraw notifications
44 let notify = REDRAW_NOTIFY.notified();
45 tokio::pin!(notify);
46 notify.enable();
47 }
48
49 /// Acquires the render lock which will prevent the next frame from being drawn
50 /// until the returned guard is dropped.
51 pub fn lock_frame() -> RenderLockGuard {
52 RENDER_LOCK.read()
53 }
54
55 /// A zero sized type that requests a redraw via [request_redraw] when the type [Drop]s.
56 pub struct RequestRedrawOnDrop;
57
58 impl Drop for RequestRedrawOnDrop {
59 fn drop(&mut self) {
60 request_redraw();
61 }
62 }