Function add_idle

Source
pub fn add_idle<F: FnMut(IdleHandle) + 'static>(cb: F) -> IdleHandle
Expand description

Add an idle callback to run within the event loop. This function returns a handle that can be used for future interaction with the callback. Calls to WidgetExt::redraw within the callback require an explicit sleep

Examples found in repository?
examples/fb.rs (lines 31-38)
14fn main() -> Result<(), Box<dyn std::error::Error>> {
15    let app = app::App::default();
16    let mut win = Window::default()
17        .with_size(WIDTH, HEIGHT)
18        .with_label("Framebuffer");
19    let mut frame = frame::Frame::default_fill();
20    win.end();
21    win.make_resizable(true);
22    win.show();
23
24    let mut framebuf: Vec<u8> = vec![0; (WIDTH * HEIGHT * 4) as usize];
25    let mut world = World::new();
26
27    unsafe {
28        utils::blit_rgba_nocopy(&mut frame, &framebuf);
29    }
30
31    app::add_idle(move |_| {
32        world.update();
33        world.draw(&mut framebuf);
34        // utils::blit_rgba(&mut frame, &framebuf).unwrap(); // A safe variant of draw_rgba_nocopy
35        win.redraw();
36        // sleeps are necessary when calling redraw in the event loop
37        app::sleep(0.016);
38    });
39
40    app.run()?;
41    Ok(())
42}
More examples
Hide additional examples
examples/pong.rs (lines 82-108)
34fn main() {
35    let app = app::App::default();
36    let mut wind = window::Window::default()
37        .with_size(800, 600)
38        .with_label("Pong!");
39    wind.set_center_screen();
40    let mut ball = Ball::new(40, 40);
41    ball.wid.set_color(Color::White);
42    wind.set_color(Color::Black);
43    wind.end();
44    wind.show();
45
46    let paddle_pos = Rc::from(RefCell::from(320)); // paddle's starting x position
47
48    // This is called whenever the window is drawn and redrawn (in the event loop)
49    wind.draw({
50        let paddle_pos = paddle_pos.clone();
51        move |_| {
52            draw::set_draw_color(Color::White);
53            draw::draw_rectf(*paddle_pos.borrow(), 540, 160, 20);
54        }
55    });
56
57    wind.handle({
58        let paddle_pos = paddle_pos.clone();
59        move |_, ev| {
60            match ev {
61                // we handle focus to be able to accept KeyDown events
62                Event::Focus => true,
63                Event::KeyDown => {
64                    let key = app::event_key();
65                    match key {
66                        Key::Left | KEY_A => *paddle_pos.borrow_mut() -= 30,
67                        Key::Right | KEY_D => *paddle_pos.borrow_mut() += 30,
68                        _ => return false,
69                    }
70                    true
71                }
72                Event::Move => {
73                    // Mouse's x position relative to the paddle's center
74                    *paddle_pos.borrow_mut() = app::event_coords().0 - 80;
75                    true
76                }
77                _ => false,
78            }
79        }
80    });
81
82    app::add_idle(move |_| {
83        ball.pos.0 += 10 * ball.dir.0 as i32; // The increment in x position
84        ball.pos.1 += 10 * ball.dir.1 as i32; // The increment in y position
85        if ball.pos.1 == 540 - 40
86            && (ball.pos.0 > *paddle_pos.borrow() - 40 && ball.pos.0 < *paddle_pos.borrow() + 160)
87        {
88            ball.dir.1 = Direction::Negative; // Reversal of motion when hitting the paddle
89        }
90        if ball.pos.1 == 0 {
91            ball.dir.1 = Direction::Positive; // Reversal of motion when hitting the top border
92        }
93        if ball.pos.0 == 800 - 40 {
94            ball.dir.0 = Direction::Negative; // Reversal of motion when hitting the right border
95        }
96        if ball.pos.0 == 0 {
97            ball.dir.0 = Direction::Positive; // Reversal of motion when hitting the left border
98        }
99        if ball.pos.1 > 600 {
100            // Resetting the ball position after it bypasses the paddle
101            ball.pos = (0, 0);
102            ball.dir = (Direction::Positive, Direction::Positive);
103        }
104        ball.wid.resize(ball.pos.0, ball.pos.1, 40, 40); // Moves the ball
105        wind.redraw();
106        // sleeps are necessary when calling redraw in the event loop
107        app::sleep(0.016);
108    });
109    app.run().unwrap();
110}