RepaintSignal now implements Sync so it can be sent to another thread

Fixes https://github.com/emilk/egui/issues/82
This commit is contained in:
Emil Ernerfeldt 2020-12-27 10:49:26 +01:00
parent af1df8d339
commit 958fc2753a
3 changed files with 14 additions and 5 deletions

View file

@ -9,6 +9,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## Unreleased
### Fixed 🐛
* `RepaintSignal` now implements `Sync` so it can be sent to a background thread.
## 0.6.0 - 2020-12-26
### Added ⭐

View file

@ -55,7 +55,7 @@ pub struct IntegrationContext<'a> {
pub tex_allocator: Option<&'a mut dyn TextureAllocator>,
/// Where the app can issue commands back to the integration.
pub output: AppOutput,
/// If you need to request a repaint from another thread, clone this and give to that other thread
/// If you need to request a repaint from another thread, clone this and send it to that other thread.
pub repaint_signal: std::sync::Arc<dyn RepaintSignal>,
}
@ -113,7 +113,7 @@ pub trait TextureAllocator {
fn free(&mut self, id: crate::TextureId);
}
pub trait RepaintSignal: Send {
pub trait RepaintSignal: Send + Sync {
/// This signals the Egui integration that a repaint is required.
/// This is meant to be called when a background process finishes in an async context and/or background thread.
fn request_repaint(&self);

View file

@ -31,11 +31,13 @@ impl egui::app::TextureAllocator for Painter {
struct RequestRepaintEvent;
struct GliumRepaintSignal(glutin::event_loop::EventLoopProxy<RequestRepaintEvent>);
struct GliumRepaintSignal(
std::sync::Mutex<glutin::event_loop::EventLoopProxy<RequestRepaintEvent>>,
);
impl egui::app::RepaintSignal for GliumRepaintSignal {
fn request_repaint(&self) {
self.0.send_event(RequestRepaintEvent).ok();
self.0.lock().unwrap().send_event(RequestRepaintEvent).ok();
}
}
@ -105,7 +107,9 @@ pub fn run(mut app: Box<dyn App>) -> ! {
let event_loop = glutin::event_loop::EventLoop::with_user_event();
let display = create_display(app.name(), window_settings, app.is_resizable(), &event_loop);
let repaint_signal = std::sync::Arc::new(GliumRepaintSignal(event_loop.create_proxy()));
let repaint_signal = std::sync::Arc::new(GliumRepaintSignal(std::sync::Mutex::new(
event_loop.create_proxy(),
)));
let mut ctx = egui::CtxRef::default();
*ctx.memory() = storage