-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements RuntimeExt::exec for UI (#1179)
- Loading branch information
1 parent
0996ba7
commit 48b7a04
Showing
8 changed files
with
150 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,49 @@ | ||
use super::event::WindowEvent; | ||
use std::cell::Cell; | ||
use std::future::Future; | ||
use std::mem::transmute; | ||
use std::ptr::null; | ||
use std::ptr::null_mut; | ||
use winit::event_loop::ActiveEventLoop; | ||
use winit::window::WindowId; | ||
|
||
/// Execution context of the runtime. | ||
pub struct RuntimeContext<'a> { | ||
el: &'a ActiveEventLoop, | ||
pub(super) el: &'a ActiveEventLoop, | ||
pub(super) on_close: &'a mut WindowEvent<()>, | ||
} | ||
|
||
impl<'a> RuntimeContext<'a> { | ||
pub(super) fn new(el: &'a ActiveEventLoop) -> Self { | ||
Self { el } | ||
} | ||
|
||
/// # Panics | ||
/// If called from the other thread than main thread. | ||
pub fn with<R>(f: impl FnOnce(&Self) -> R) -> R { | ||
let cx = CONTEXT.get(); | ||
/// - If called from the other thread than main thread. | ||
/// - If this call has been nested. | ||
pub fn with<R>(f: impl FnOnce(&mut RuntimeContext) -> R) -> R { | ||
// Take context to achieve exclusive access. | ||
let cx = CONTEXT.replace(null_mut()); | ||
|
||
assert!(!cx.is_null()); | ||
unsafe { f(&*cx) } | ||
|
||
// Execute action then put context back. | ||
let r = unsafe { f(&mut *cx) }; | ||
|
||
CONTEXT.set(cx); | ||
r | ||
} | ||
|
||
pub fn event_loop(&self) -> &ActiveEventLoop { | ||
self.el | ||
} | ||
|
||
pub(super) fn run(&self, f: impl FnOnce()) { | ||
pub fn on_close(&mut self, win: WindowId) -> impl Future<Output = ()> { | ||
self.on_close.wait(win) | ||
} | ||
|
||
pub(super) fn run(&mut self, f: impl FnOnce()) { | ||
assert!(CONTEXT.replace(unsafe { transmute(self) }).is_null()); | ||
f(); | ||
CONTEXT.set(null()); | ||
CONTEXT.set(null_mut()); | ||
} | ||
} | ||
|
||
thread_local! { | ||
static CONTEXT: Cell<*const RuntimeContext<'static>> = Cell::new(null()); | ||
static CONTEXT: Cell<*mut RuntimeContext<'static>> = Cell::new(null_mut()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use futures::channel::oneshot::{Receiver, Sender}; | ||
use futures::FutureExt; | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
use winit::window::WindowId; | ||
|
||
/// List of one-shot channel waiting for a window event. | ||
#[derive(Default)] | ||
pub struct WindowEvent<T>(Vec<(WindowId, Sender<T>)>); | ||
|
||
impl<T: Clone> WindowEvent<T> { | ||
pub fn wait(&mut self, win: WindowId) -> impl Future<Output = T> { | ||
let (tx, rx) = futures::channel::oneshot::channel(); | ||
|
||
self.0.push((win, tx)); | ||
|
||
Wait(rx) | ||
} | ||
|
||
pub fn raise(&mut self, win: WindowId, data: T) { | ||
// TODO: https://github.com/rust-lang/rust/issues/43244 | ||
let mut i = 0; | ||
|
||
while i < self.0.len() { | ||
let s = if self.0[i].0 == win { | ||
self.0.remove(i).1 | ||
} else { | ||
i += 1; | ||
continue; | ||
}; | ||
|
||
s.send(data.clone()).ok(); | ||
} | ||
} | ||
} | ||
|
||
/// Result of [`WindowEvent::wait()`]. | ||
struct Wait<T>(Receiver<T>); | ||
|
||
impl<T> Future for Wait<T> { | ||
type Output = T; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let r = match self.0.poll_unpin(cx) { | ||
Poll::Ready(v) => v, | ||
Poll::Pending => return Poll::Pending, | ||
}; | ||
|
||
// The future only driven when the event loop is running so this should never panic. | ||
Poll::Ready(r.unwrap()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters