Fix web redraw requested (#1181)
* Keep track of what windows have requested redraw Instead of using request_animation_frame and sending redraw request events, just keep track of all windows that have asked for a redraw. This doesn't handle dispatching the events * Issue redraw events to windows that request it * Cargo fmt
This commit is contained in:
parent
2c47c43f47
commit
28a50817af
4 changed files with 34 additions and 36 deletions
|
|
@ -1,9 +1,15 @@
|
|||
use super::{backend, state::State};
|
||||
use crate::event::{Event, StartCause};
|
||||
use crate::event::{Event, StartCause, WindowEvent};
|
||||
use crate::event_loop as root;
|
||||
use crate::window::WindowId;
|
||||
|
||||
use instant::{Duration, Instant};
|
||||
use std::{cell::RefCell, clone::Clone, collections::VecDeque, rc::Rc};
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
clone::Clone,
|
||||
collections::{HashSet, VecDeque},
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
pub struct Shared<T>(Rc<Execution<T>>);
|
||||
|
||||
|
|
@ -17,6 +23,7 @@ pub struct Execution<T> {
|
|||
runner: RefCell<Option<Runner<T>>>,
|
||||
events: RefCell<VecDeque<Event<T>>>,
|
||||
id: RefCell<u32>,
|
||||
redraw_pending: RefCell<HashSet<WindowId>>,
|
||||
}
|
||||
|
||||
struct Runner<T> {
|
||||
|
|
@ -41,6 +48,7 @@ impl<T: 'static> Shared<T> {
|
|||
runner: RefCell::new(None),
|
||||
events: RefCell::new(VecDeque::new()),
|
||||
id: RefCell::new(0),
|
||||
redraw_pending: RefCell::new(HashSet::new()),
|
||||
}))
|
||||
}
|
||||
|
||||
|
|
@ -64,6 +72,10 @@ impl<T: 'static> Shared<T> {
|
|||
*id
|
||||
}
|
||||
|
||||
pub fn request_redraw(&self, id: WindowId) {
|
||||
self.0.redraw_pending.borrow_mut().insert(id);
|
||||
}
|
||||
|
||||
// Add an event to the event loop runner
|
||||
//
|
||||
// It will determine if the event should be immediately sent to the user or buffered for later
|
||||
|
|
@ -115,6 +127,17 @@ impl<T: 'static> Shared<T> {
|
|||
if !event_is_start {
|
||||
self.handle_event(event, &mut control);
|
||||
}
|
||||
// Collect all of the redraw events to avoid double-locking the RefCell
|
||||
let redraw_events: Vec<WindowId> = self.0.redraw_pending.borrow_mut().drain().collect();
|
||||
for window_id in redraw_events {
|
||||
self.handle_event(
|
||||
Event::WindowEvent {
|
||||
window_id,
|
||||
event: WindowEvent::RedrawRequested,
|
||||
},
|
||||
&mut control,
|
||||
);
|
||||
}
|
||||
self.handle_event(Event::EventsCleared, &mut control);
|
||||
self.apply_control_flow(control);
|
||||
// If the event loop is closed, it has been closed this iteration and now the closing
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue