Skip to content

Commit

Permalink
fix: Add custom SendAnyMap for torin to not depend on freya-native-co…
Browse files Browse the repository at this point in the history
…re (#1038)

* fix: Add custom SendAnyMap for torin to not depend on freya-native-core always

* clean up
  • Loading branch information
marc2332 authored Jan 11, 2025
1 parent d96f0b4 commit 0f83403
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 60 deletions.
2 changes: 1 addition & 1 deletion crates/core/src/render/skia_measurer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use freya_native_core::{
prelude::{
ElementNode,
NodeType,
SendAnyMap,
},
real_dom::NodeImmutable,
tags::TagName,
Expand All @@ -20,6 +19,7 @@ use torin::prelude::{
Area,
LayoutMeasurer,
Node,
SendAnyMap,
Size2D,
};

Expand Down
1 change: 0 additions & 1 deletion crates/torin/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use criterion::{
criterion_main,
Criterion,
};
use freya_native_core::SendAnyMap;
use torin::prelude::*;

struct TestingMeasurer;
Expand Down
29 changes: 3 additions & 26 deletions crates/torin/examples/demo.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
use std::{
collections::HashMap,
sync::Arc,
};
use std::collections::HashMap;

use freya_native_core::prelude::SendAnyMap;
use torin::prelude::*;

// Custom measurer, useful to measure certain elements such as text with other libraries
pub struct CustomMeasurer;

impl LayoutMeasurer<usize> for CustomMeasurer {
fn measure(
&mut self,
_node_id: usize,
_node: &Node,
_size: &Size2D,
) -> Option<(Size2D, Arc<SendAnyMap>)> {
None
}

fn should_measure_inner_children(&mut self, _node_id: usize) -> bool {
true
}
}

#[derive(Clone)]
struct DemoNode {
parent: Option<usize>,
Expand Down Expand Up @@ -112,7 +90,6 @@ impl DOMAdapter<usize> for DemoDOM {

fn main() {
let mut layout = Torin::<usize>::new();
let mut measurer = Some(CustomMeasurer);

let mut demo_dom = DemoDOM::default();

Expand Down Expand Up @@ -158,7 +135,7 @@ fn main() {
layout.measure(
0, // Root ID
Rect::new(Point2D::new(0.0, 0.0), Size2D::new(1000.0, 1000.0)), // Available Area
&mut measurer,
&mut None::<NoopMeasurer>,
&mut demo_dom,
);

Expand All @@ -185,7 +162,7 @@ fn main() {
layout.measure(
0, // Fallback Root ID
Rect::new(Point2D::new(0.0, 0.0), Size2D::new(1000.0, 1000.0)), // Available Area
&mut measurer,
&mut None::<NoopMeasurer>,
&mut demo_dom,
);

Expand Down
25 changes: 22 additions & 3 deletions crates/torin/src/custom_measurer.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::sync::Arc;

use freya_native_core::prelude::SendAnyMap;

use crate::{
dom_adapter::NodeKey,
geometry::Size2D,
node::Node,
prelude::Area,
prelude::{
Area,
SendAnyMap,
},
};

pub trait LayoutMeasurer<Key: NodeKey> {
Expand All @@ -21,3 +22,21 @@ pub trait LayoutMeasurer<Key: NodeKey> {

fn notify_layout_references(&self, _node_id: Key, _area: Area, _inner_sizes: Size2D) {}
}

// No-op measurer, use it when you don't need one.
pub struct NoopMeasurer;

impl LayoutMeasurer<usize> for NoopMeasurer {
fn measure(
&mut self,
_node_id: usize,
_node: &Node,
_size: &Size2D,
) -> Option<(Size2D, Arc<SendAnyMap>)> {
None
}

fn should_measure_inner_children(&mut self, _node_id: usize) -> bool {
false
}
}
2 changes: 1 addition & 1 deletion crates/torin/src/dom_adapter.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::sync::Arc;

pub use euclid::Rect;
use freya_native_core::SendAnyMap;

use crate::{
geometry::Area,
node::Node,
prelude::{
AreaModel,
Gaps,
SendAnyMap,
},
};

Expand Down
2 changes: 2 additions & 0 deletions crates/torin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod geometry;
mod measure;
pub mod node;
pub mod scaled;
pub mod sendanymap;
pub mod torin;
pub mod values;

Expand All @@ -17,6 +18,7 @@ pub mod prelude {
geometry::*,
node::*,
scaled::*,
sendanymap::*,
torin::*,
values::prelude::*,
};
Expand Down
36 changes: 36 additions & 0 deletions crates/torin/src/sendanymap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::any::{
Any,
TypeId,
};

use rustc_hash::FxHashMap;

/// A map of types that can be sent between threads
#[derive(Debug)]
pub struct SendAnyMap {
map: FxHashMap<TypeId, Box<dyn Any + Send + Sync + 'static>>,
}

impl Default for SendAnyMap {
fn default() -> Self {
Self::new()
}
}

impl SendAnyMap {
pub fn new() -> Self {
Self {
map: FxHashMap::default(),
}
}

pub fn get<T: 'static>(&self) -> Option<&T> {
self.map
.get(&TypeId::of::<T>())
.and_then(|any| any.downcast_ref::<T>())
}

pub fn insert<T: Send + Sync + 'static>(&mut self, value: T) {
self.map.insert(TypeId::of::<T>(), Box::new(value));
}
}
32 changes: 4 additions & 28 deletions crates/torin/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
use std::{
collections::HashMap,
sync::Arc,
};

use freya_native_core::SendAnyMap;
use std::collections::HashMap;

use crate::prelude::*;

pub struct TestingMeasurer;

impl LayoutMeasurer<usize> for TestingMeasurer {
fn measure(
&mut self,
_node_id: usize,
_node: &Node,
_area_size: &Size2D,
) -> Option<(Size2D, Arc<SendAnyMap>)> {
None
}

fn should_measure_inner_children(&mut self, _node_id: usize) -> bool {
true
}
}

#[derive(Default)]
pub struct TestingDOM {
mapper: HashMap<usize, (Option<usize>, Vec<usize>, u16, Node)>,
Expand All @@ -40,14 +18,12 @@ impl TestingDOM {
}

pub fn remove(&mut self, node_id: usize) {
let node = self.mapper.get(&node_id).unwrap().clone();
let node = self.mapper.remove(&node_id).unwrap();

if let Some((_, parent_children, _, _)) = node.0.and_then(|p| self.mapper.get_mut(&p)) {
parent_children.retain(|c| *c != node_id);
}

self.mapper.remove(&node_id);

for child in node.1 {
self.remove(child);
}
Expand Down Expand Up @@ -83,9 +59,9 @@ impl DOMAdapter<usize> for TestingDOM {
}
}

pub fn test_utils() -> (Torin<usize>, Option<TestingMeasurer>) {
pub fn test_utils() -> (Torin<usize>, Option<NoopMeasurer>) {
let layout = Torin::<usize>::new();
let measurer = Some(TestingMeasurer);
let measurer = None::<NoopMeasurer>;

(layout, measurer)
}

0 comments on commit 0f83403

Please sign in to comment.