use crate::id::ElementId;
use crate::theme::{Direction, ThemeData, ThemeWidget};
use std::any::Any;
pub trait InheritedWidget: Any {
fn update_should_notify(&self, _old: &dyn InheritedWidget) -> bool {
true
}
fn as_any(&self) -> &dyn Any;
}
pub struct BuildContext<'a> {
pub element_id: ElementId,
pub(crate) tree: &'a mut crate::element::ElementTree,
pub(crate) in_build_scope: bool,
}
impl<'a> BuildContext<'a> {
pub fn depend_on<T: InheritedWidget>(&mut self) -> &T {
assert!(self.in_build_scope, "depend_on() 只能在 state.build(ctx) 期间调用");
self.tree
.depend_on_internal::<T>(self.element_id)
.expect("no InheritedElement of requested type in ancestor chain")
}
pub fn try_theme_rc(&mut self) -> Option<std::rc::Rc<ThemeData>> {
self.tree.depend_on_internal::<ThemeWidget>(self.element_id).map(|tw| std::rc::Rc::clone(&tw.theme))
}
pub fn try_theme(&mut self) -> Option<&ThemeData> {
self.tree.depend_on_internal::<ThemeWidget>(self.element_id).map(|tw| tw.theme())
}
pub fn theme(&mut self) -> &ThemeData {
self.depend_on::<ThemeWidget>().theme()
}
pub fn direction(&mut self) -> Direction {
self.depend_on::<ThemeWidget>().direction
}
}