use crate::Editor;
use std::any::Any;
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::rc::Rc;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ContextKind {
Import,
Render,
}
thread_local! {
static NEXT_CONFIG_ID: Cell<u64> = const { Cell::new(1) };
static ACTIVE_CONTEXT: RefCell<Option<EditorContext>> = const { RefCell::new(None) };
}
struct ConfigInner<V> {
id: u64,
name: String,
default_value: V,
}
pub struct ContextConfig<V>(Rc<ConfigInner<V>>);
impl<V> Clone for ContextConfig<V> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<V: Clone + PartialEq + 'static> ContextConfig<V> {
pub fn name(&self) -> &str {
&self.0.name
}
pub fn default_value(&self) -> V {
self.0.default_value.clone()
}
}
pub(crate) fn create_context_state<V: Clone + PartialEq + 'static>(
_kind: ContextKind,
name: &str,
get_default_value: impl FnOnce() -> V,
) -> ContextConfig<V> {
let id = NEXT_CONFIG_ID.with(|n| {
let id = n.get();
n.set(id + 1);
id
});
ContextConfig(Rc::new(ConfigInner {
id,
name: name.to_owned(),
default_value: get_default_value(),
}))
}
#[derive(Default)]
pub struct ContextRecord {
parent: Option<Rc<ContextRecord>>,
values: RefCell<HashMap<u64, Rc<dyn Any>>>,
}
impl ContextRecord {
pub(crate) fn child_of(parent: Option<Rc<ContextRecord>>) -> Rc<ContextRecord> {
Rc::new(ContextRecord {
parent,
values: RefCell::default(),
})
}
fn lookup(&self, id: u64) -> Option<Rc<dyn Any>> {
if let Some(value) = self.values.borrow().get(&id) {
return Some(value.clone());
}
self.parent.as_ref().and_then(|p| p.lookup(id))
}
pub(crate) fn set<V: Clone + PartialEq + 'static>(&self, cfg: &ContextConfig<V>, value: V) {
self.values.borrow_mut().insert(cfg.0.id, Rc::new(value));
}
pub(crate) fn has_own<V>(&self, cfg: &ContextConfig<V>) -> bool {
self.values.borrow().contains_key(&cfg.0.id)
}
}
pub fn get_context_value<V: Clone + PartialEq + 'static>(
record: Option<&Rc<ContextRecord>>,
cfg: &ContextConfig<V>,
) -> V {
record
.and_then(|r| r.lookup(cfg.0.id))
.and_then(|v| v.downcast_ref::<V>().cloned())
.unwrap_or_else(|| cfg.default_value())
}
type Resolve = Rc<dyn Fn(Option<&Rc<ContextRecord>>) -> Rc<dyn Any>>;
type SameAs = Rc<dyn Fn(Option<&Rc<ContextRecord>>, &dyn Any) -> bool>;
#[derive(Clone)]
pub struct ContextPairOrUpdater {
id: u64,
resolve: Resolve,
same_as: SameAs,
}
fn pair_of<V: Clone + PartialEq + 'static>(
cfg: &ContextConfig<V>,
resolve: Resolve,
) -> ContextPairOrUpdater {
let same = cfg.clone();
ContextPairOrUpdater {
id: cfg.0.id,
resolve,
same_as: Rc::new(move |record, value| {
value.downcast_ref::<V>() == Some(&get_context_value(record, &same))
}),
}
}
pub fn context_value<V: Clone + PartialEq + 'static>(
cfg: &ContextConfig<V>,
value: V,
) -> ContextPairOrUpdater {
pair_of(cfg, Rc::new(move |_| Rc::new(value.clone())))
}
pub fn context_updater<V: Clone + PartialEq + 'static>(
cfg: &ContextConfig<V>,
updater: impl Fn(V) -> V + 'static,
) -> ContextPairOrUpdater {
let cfg2 = cfg.clone();
pair_of(
cfg,
Rc::new(move |record| Rc::new(updater(get_context_value(record, &cfg2)))),
)
}
pub(crate) fn context_from_pairs(
pairs: &[ContextPairOrUpdater],
parent: Option<Rc<ContextRecord>>,
) -> Option<Rc<ContextRecord>> {
let mut rval = parent.clone();
for pair in pairs {
let value = (pair.resolve)(rval.as_ref());
let is_parent = ptr_eq(&rval, &parent);
if is_parent && (pair.same_as)(rval.as_ref(), value.as_ref()) {
continue;
}
let ctx = match &rval {
Some(r) if !is_parent => r.clone(),
_ => ContextRecord::child_of(parent.clone()),
};
ctx.values.borrow_mut().insert(pair.id, value);
rval = Some(ctx);
}
rval
}
fn ptr_eq(a: &Option<Rc<ContextRecord>>, b: &Option<Rc<ContextRecord>>) -> bool {
match (a, b) {
(Some(a), Some(b)) => Rc::ptr_eq(a, b),
(None, None) => true,
_ => false,
}
}
#[derive(Clone)]
struct EditorContext {
editor: Editor,
records: HashMap<ContextKind, Rc<ContextRecord>>,
}
fn get_editor_context(editor: &Editor) -> Option<EditorContext> {
ACTIVE_CONTEXT.with(|active| {
active
.borrow()
.as_ref()
.filter(|c| Rc::ptr_eq(&c.editor.0, &editor.0))
.cloned()
})
}
pub(crate) fn get_context_record(kind: ContextKind, editor: &Editor) -> Option<Rc<ContextRecord>> {
get_editor_context(editor).and_then(|c| c.records.get(&kind).cloned())
}
pub(crate) fn with_full_context<T>(
kind: ContextKind,
record: Rc<ContextRecord>,
editor: &Editor,
f: impl FnOnce() -> T,
) -> T {
let mut records = get_editor_context(editor)
.map(|c| c.records)
.unwrap_or_default();
records.insert(kind, record);
let prev = ACTIVE_CONTEXT.with(|active| {
active.replace(Some(EditorContext {
editor: editor.clone(),
records,
}))
});
struct Restore(Option<EditorContext>);
impl Drop for Restore {
fn drop(&mut self) {
let prev = self.0.take();
ACTIVE_CONTEXT.with(|active| *active.borrow_mut() = prev);
}
}
let _restore = Restore(prev);
f()
}
pub(crate) fn with_context<T>(
kind: ContextKind,
defaults: impl FnOnce(&Editor) -> Option<Rc<ContextRecord>>,
cfg: &[ContextPairOrUpdater],
editor: &Editor,
f: impl FnOnce() -> T,
) -> T {
let parent = get_context_record(kind, editor);
let base = parent.clone().or_else(|| defaults(editor));
match context_from_pairs(cfg, base) {
Some(record) if !ptr_eq(&Some(record.clone()), &parent) => {
with_full_context(kind, record, editor, f)
}
_ => f(),
}
}