Skip to content

Commit

Permalink
Add animation frame notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Sep 24, 2023
1 parent ea2faf2 commit 3add274
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 8 deletions.
162 changes: 162 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ raw-window-handle = { version = "0.5.2", optional = true }
skia-safe = { version = "0.64.0", features = ["gl"], optional = true }
winit = { version = "0.28.6", optional = true }
interpolation = "0.3.0"
tokio = { version = "1.32.0", features = ["full"] }

13 changes: 6 additions & 7 deletions examples/animation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
thread::{self, sleep},
time::{Duration, Instant},
};
use std::time::Instant;

use skia_safe::Color4f;
use taffy::{
Expand All @@ -10,7 +7,8 @@ use taffy::{
};
use viewbuilder::{render::UserEvent, Element, Renderer, Tree};

fn main() {
#[tokio::main]
async fn main() {
// TODO this is really early stage, an animation frame should be requested

let mut tree = Tree::default();
Expand All @@ -23,8 +21,9 @@ fn main() {

let renderer = Renderer::new();
let tx = renderer.tx.clone();
let notify = renderer.notify.clone();

thread::spawn(move || {
tokio::spawn(async move {
let start = Instant::now();
loop {
let t = (Instant::now() - start).as_millis() as f32;
Expand All @@ -36,7 +35,7 @@ fn main() {
})))
.unwrap();

sleep(Duration::from_millis(5))
notify.notified().await;
}
});

Expand Down
8 changes: 7 additions & 1 deletion src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ use slotmap::DefaultKey;
use std::{
ffi::CString,
num::NonZeroU32,
time::{Duration, Instant}, sync::mpsc,
sync::{mpsc, Arc},
time::{Duration, Instant},
};
use tokio::sync::Notify;
use winit::{
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::{ControlFlow, EventLoop, EventLoopBuilder},
Expand All @@ -48,6 +50,7 @@ pub struct Renderer {
fb_info: FramebufferInfo,
pub tx: mpsc::Sender<UserEvent>,
rx: mpsc::Receiver<UserEvent>,
pub notify: Arc<Notify>,
}

impl Renderer {
Expand Down Expand Up @@ -172,6 +175,7 @@ impl Renderer {
fb_info,
tx,
rx,
notify: Arc::new(Notify::default()),
}
}

Expand Down Expand Up @@ -322,6 +326,8 @@ impl Renderer {
previous_frame_start = frame_start;
}
if draw_frame {
self.notify.notify_waiters();

let canvas = self.surface.canvas();
canvas.clear(Color::WHITE);

Expand Down

0 comments on commit 3add274

Please sign in to comment.