1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::{
    ops::{
        Deref,
        Div,
    },
    sync::Arc,
};

use freya_engine::prelude::Paragraph;
use freya_native_core::SendAnyMap;
use torin::{
    geometry::{
        Area,
        Size2D,
    },
    prelude::NodeData,
};

/// Layout info of a certain Node, used by `use_node`.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct NodeReferenceLayout {
    pub area: Area,
    pub inner: Size2D,
}

impl NodeReferenceLayout {
    pub fn div(&mut self, rhs: f32) {
        self.area = self.area.div(rhs);
        self.inner = self.inner.div(rhs);
    }
}

/// Messages emitted from the layout library to the Nodes. Used in `use_editable`.
#[derive(Debug)]
pub enum CursorLayoutResponse {
    CursorPosition { position: usize, id: usize },
    TextSelection { from: usize, to: usize, id: usize },
}

pub struct CachedParagraph(pub Paragraph, pub f32);

/// # Safety
/// Skia `Paragraph` are neither Sync or Send, but in order to store them in the Associated
/// data of the Nodes in Torin (which will be used across threads when making the attributes diffing),
/// we must manually mark the Paragraph as Send and Sync, this is fine because `Paragraph`s will only be accessed and modified
/// In the main thread when measuring the layout and painting.
unsafe impl Send for CachedParagraph {}
unsafe impl Sync for CachedParagraph {}

#[derive(Clone)]
pub struct LayoutNodeData(pub Arc<SendAnyMap>);

impl Deref for LayoutNodeData {
    type Target = Arc<SendAnyMap>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl NodeData for LayoutNodeData {}