use std::{collections::HashMap, mem};
pub use euclid::Rect;
use rustc_hash::{FxHashMap, FxHashSet};
use tracing::info;
use crate::{
custom_measurer::LayoutMeasurer,
dom_adapter::{DOMAdapter, NodeAreas, NodeKey},
geometry::{Area, Size2D},
measure::measure_node,
prelude::Gaps,
};
#[derive(PartialEq, Debug, Clone)]
pub enum RootNodeCandidate<Key: NodeKey> {
Valid(Key),
None,
}
impl<Key: NodeKey> RootNodeCandidate<Key> {
pub fn take(&mut self) -> Self {
mem::replace(self, Self::None)
}
}
pub struct Torin<Key: NodeKey> {
pub results: FxHashMap<Key, NodeAreas>,
pub dirty: FxHashSet<Key>,
pub root_node_candidate: RootNodeCandidate<Key>,
}
impl<Key: NodeKey> Default for Torin<Key> {
fn default() -> Self {
Self::new()
}
}
#[cfg(feature = "dioxus")]
use dioxus_core::Mutations;
#[cfg(feature = "dioxus")]
use dioxus_native_core::prelude::*;
#[cfg(feature = "dioxus")]
impl Torin<NodeId> {
pub fn apply_mutations(
&mut self,
mutations: &Mutations,
dioxus_integration_state: &DioxusState,
dom_adapter: &mut impl DOMAdapter<NodeId>,
) {
use dioxus_core::Mutation;
for mutation in &mutations.edits {
match mutation {
Mutation::SetText { id, .. } => {
self.invalidate(dioxus_integration_state.element_to_node_id(*id));
}
Mutation::InsertAfter { id, m } => {
if *m > 0 {
self.invalidate(dioxus_integration_state.element_to_node_id(*id));
}
}
Mutation::InsertBefore { id, m } => {
if *m > 0 {
self.invalidate(dioxus_integration_state.element_to_node_id(*id));
}
}
Mutation::Remove { id } => {
self.remove(
dioxus_integration_state.element_to_node_id(*id),
dom_adapter,
true,
);
}
Mutation::ReplaceWith { id, m } => {
if *m > 0 {
self.remove(
dioxus_integration_state.element_to_node_id(*id),
dom_adapter,
true,
);
}
}
_ => {}
}
}
}
}
impl<Key: NodeKey> Torin<Key> {
pub fn new() -> Self {
Self {
results: HashMap::default(),
dirty: FxHashSet::default(),
root_node_candidate: RootNodeCandidate::None,
}
}
pub fn reset(&mut self) {
self.root_node_candidate = RootNodeCandidate::None;
self.results.clear();
self.dirty.clear();
}
pub fn get_dirty_nodes(&self) -> &FxHashSet<Key> {
&self.dirty
}
pub fn raw_remove(&mut self, node_id: Key) {
self.results.remove(&node_id);
self.dirty.remove(&node_id);
if let RootNodeCandidate::Valid(id) = self.root_node_candidate {
if id == node_id {
self.root_node_candidate = RootNodeCandidate::None
}
}
}
pub fn remove(
&mut self,
node_id: Key,
dom_adapter: &mut impl DOMAdapter<Key>,
invalidate_parent: bool,
) {
self.raw_remove(node_id);
if invalidate_parent {
self.invalidate(dom_adapter.parent_of(&node_id).unwrap());
}
for child_id in dom_adapter.children_of(&node_id) {
self.remove(child_id, dom_adapter, false);
}
}
pub fn invalidate(&mut self, node_id: Key) {
self.dirty.insert(node_id);
}
pub fn safe_invalidate(&mut self, node_id: Key, dom_adapter: &mut impl DOMAdapter<Key>) {
if dom_adapter.is_node_valid(&node_id) {
self.invalidate(node_id)
}
}
pub fn check_dirty_dependants(
&mut self,
node_id: Key,
dom_adapter: &mut impl DOMAdapter<Key>,
ignore: bool,
) {
if (self.dirty.contains(&node_id) && ignore) || !dom_adapter.is_node_valid(&node_id) {
return;
}
self.invalidate(node_id);
if RootNodeCandidate::None == self.root_node_candidate {
self.root_node_candidate = RootNodeCandidate::Valid(node_id);
} else if let RootNodeCandidate::Valid(root_candidate) = &mut self.root_node_candidate {
if node_id != *root_candidate {
let closest_parent = dom_adapter.closest_common_parent(&node_id, root_candidate);
if let Some(closest_parent) = closest_parent {
*root_candidate = closest_parent;
}
}
}
for child in dom_adapter.children_of(&node_id) {
self.check_dirty_dependants(child, dom_adapter, true)
}
let parent_id = dom_adapter.parent_of(&node_id);
if let Some(parent_id) = parent_id {
let parent = dom_adapter.get_node(&parent_id);
if let Some(parent) = parent {
if parent.does_depend_on_inner() {
self.check_dirty_dependants(parent_id, dom_adapter, true);
}
else {
let mut found_node = false;
let mut multiple_children = false;
for child_id in dom_adapter.children_of(&parent_id) {
if found_node {
self.check_dirty_dependants(child_id, dom_adapter, true);
}
if child_id == node_id {
found_node = true;
} else {
multiple_children = true;
}
}
if multiple_children {
if let RootNodeCandidate::Valid(root_candidate) = self.root_node_candidate {
let closest_parent =
dom_adapter.closest_common_parent(&parent_id, &root_candidate);
if let Some(closest_parent) = closest_parent {
self.root_node_candidate = RootNodeCandidate::Valid(closest_parent);
}
}
}
}
}
}
}
pub fn get_root_candidate(&self) -> RootNodeCandidate<Key> {
self.root_node_candidate.clone()
}
pub fn find_best_root(&mut self, dom_adapter: &mut impl DOMAdapter<Key>) {
if self.results.is_empty() {
return;
}
for dirty in self.dirty.clone() {
self.check_dirty_dependants(dirty, dom_adapter, false);
}
}
pub fn measure(
&mut self,
suggested_root_id: Key,
suggested_root_area: Area,
measurer: &mut Option<impl LayoutMeasurer<Key>>,
dom_adapter: &mut impl DOMAdapter<Key>,
) {
if self.dirty.is_empty() && !self.results.is_empty() {
return;
}
let root_id = if let RootNodeCandidate::Valid(id) = self.root_node_candidate.take() {
id
} else {
suggested_root_id
};
let root_parent = dom_adapter.parent_of(&root_id);
let areas = root_parent
.and_then(|root_parent| self.get(root_parent).cloned())
.unwrap_or(NodeAreas {
area: suggested_root_area,
inner_area: suggested_root_area,
inner_sizes: Size2D::default(),
margin: Gaps::default(),
});
let root = dom_adapter.get_node(&root_id).unwrap();
let root_height = dom_adapter.height(&root_id).unwrap();
info!(
"Processing {} dirty nodes and {} cached nodes from a height of {}",
self.dirty.len(),
self.results.len(),
root_height
);
let (root_revalidated, root_areas) = measure_node(
root_id,
&root,
self,
&areas.inner_area,
&areas.inner_area,
measurer,
true,
dom_adapter,
);
if root_revalidated {
self.cache_node(root_id, root_areas);
}
self.dirty.clear();
self.root_node_candidate = RootNodeCandidate::None;
}
pub fn get(&self, node_id: Key) -> Option<&NodeAreas> {
self.results.get(&node_id)
}
pub fn cache_node(&mut self, node_id: Key, areas: NodeAreas) {
self.results.insert(node_id, areas);
}
}