Skip to content

Commit

Permalink
#53. Add with_context
Browse files Browse the repository at this point in the history
  • Loading branch information
wtholliday committed Aug 19, 2023
1 parent a9e707b commit f74b700
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::any::TypeId;
use std::collections::{HashMap, HashSet};
use std::iter::FromIterator;
use std::ops;
use std::cell::RefCell;

pub type LocalSpace = vger::defs::LocalSpace;
pub type WorldSpace = vger::defs::WorldSpace;
Expand Down Expand Up @@ -122,6 +123,14 @@ pub struct Context {
pub(crate) prev_grab_cursor: bool,
}

thread_local! {
pub(crate) static CONTEXT: RefCell<Context> = RefCell::new(Context::new());
}

pub(crate) fn with_context<T>(f: impl FnOnce(&mut Context) -> T) -> T {
CONTEXT.with(|cx| f(&mut cx.borrow_mut()))
}

impl Default for Context {
fn default() -> Self {
Self::new()
Expand Down

0 comments on commit f74b700

Please sign in to comment.