From 958fc2753aef7e35b615a13c54324860ee7f1427 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 27 Dec 2020 10:49:26 +0100 Subject: [PATCH] RepaintSignal now implements Sync so it can be sent to another thread Fixes https://github.com/emilk/egui/issues/82 --- CHANGELOG.md | 5 +++++ egui/src/app.rs | 4 ++-- egui_glium/src/backend.rs | 10 +++++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc02d024..d11ca6f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ⭐ diff --git a/egui/src/app.rs b/egui/src/app.rs index 9d3c4cc0..b5dbd75c 100644 --- a/egui/src/app.rs +++ b/egui/src/app.rs @@ -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, } @@ -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); diff --git a/egui_glium/src/backend.rs b/egui_glium/src/backend.rs index d722e246..7783eb86 100644 --- a/egui_glium/src/backend.rs +++ b/egui_glium/src/backend.rs @@ -31,11 +31,13 @@ impl egui::app::TextureAllocator for Painter { struct RequestRepaintEvent; -struct GliumRepaintSignal(glutin::event_loop::EventLoopProxy); +struct GliumRepaintSignal( + std::sync::Mutex>, +); 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) -> ! { 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