use crate::TypeInfo;
use std::cell::Cell;
use std::rc::Rc;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum RelativeSourceMode {
DataContext,
TemplatedParent,
SelfMode,
FindAncestor,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TreeType {
Visual,
Logical,
}
#[derive(Debug)]
pub struct RelativeSource {
ancestor_level: Cell<i32>,
ancestor_type: Cell<Option<&'static TypeInfo>>,
mode: Cell<RelativeSourceMode>,
tree: Cell<TreeType>,
}
impl Default for RelativeSource {
fn default() -> Self {
Self {
ancestor_level: Cell::new(1),
ancestor_type: Cell::new(None),
mode: Cell::new(RelativeSourceMode::FindAncestor),
tree: Cell::new(TreeType::Visual),
}
}
}
impl PartialEq for RelativeSource {
#[inline]
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(self, other)
}
}
impl RelativeSource {
pub fn empty() -> Rc<Self> {
Rc::new(Self::default())
}
pub fn new(mode: RelativeSourceMode) -> Rc<Self> {
Self::empty().with_mode(mode)
}
pub fn ancestor_level(&self) -> i32 {
self.ancestor_level.get()
}
pub fn set_ancestor_level(&self, value: i32) {
if value <= 0 {
panic!("AncestorLevel may not be set to less than 1.");
}
self.ancestor_level.set(value);
}
pub fn with_ancestor_level(self: Rc<Self>, value: i32) -> Rc<Self> {
self.set_ancestor_level(value);
self
}
pub fn ancestor_type(&self) -> Option<&'static TypeInfo> {
self.ancestor_type.get()
}
pub fn set_ancestor_type(&self, value: Option<&'static TypeInfo>) {
self.ancestor_type.set(value)
}
pub fn with_ancestor_type(self: Rc<Self>, value: Option<&'static TypeInfo>) -> Rc<Self> {
self.set_ancestor_type(value);
self
}
pub fn mode(&self) -> RelativeSourceMode {
self.mode.get()
}
pub fn set_mode(&self, value: RelativeSourceMode) {
self.mode.set(value)
}
pub fn with_mode(self: Rc<Self>, value: RelativeSourceMode) -> Rc<Self> {
self.set_mode(value);
self
}
pub fn tree(&self) -> TreeType {
self.tree.get()
}
pub fn set_tree(&self, value: TreeType) {
self.tree.set(value)
}
pub fn with_tree(self: Rc<Self>, value: TreeType) -> Rc<Self> {
self.set_tree(value);
self
}
}