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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::{
    any::Any,
    fmt::{
        Debug,
        Display,
    },
    sync::{
        Arc,
        Mutex,
    },
};

use accesskit::NodeId as AccessibilityId;
use bytes::Bytes;
use dioxus_core::AttributeValue;
use freya_common::{
    CursorLayoutResponse,
    NodeReferenceLayout,
};
use freya_engine::prelude::*;
use freya_native_core::node::FromAnyValue;
use tokio::sync::{
    mpsc::UnboundedSender,
    watch,
};
use torin::geometry::Area;
use uuid::Uuid;

/// Image Reference
#[derive(Clone, Debug)]
pub struct ImageReference(pub Arc<Mutex<Option<Bytes>>>);

impl PartialEq for ImageReference {
    fn eq(&self, _other: &Self) -> bool {
        true
    }
}

impl Display for ImageReference {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ImageReference").finish_non_exhaustive()
    }
}

/// Node Reference
#[derive(Debug, Clone)]
pub struct NodeReference(pub Arc<watch::Sender<NodeReferenceLayout>>);

impl PartialEq for NodeReference {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }
}

impl Display for NodeReference {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("NodeReference").finish_non_exhaustive()
    }
}

pub struct CanvasRunnerContext<'a> {
    pub canvas: &'a Canvas,
    pub font_collection: &'a mut FontCollection,
    pub area: Area,
    pub scale_factor: f32,
}

pub type CanvasRunner = dyn Fn(&mut CanvasRunnerContext) + Sync + Send + 'static;

/// Canvas Reference
#[derive(Clone)]
pub struct CanvasReference {
    pub runner: Arc<Box<CanvasRunner>>,
}

impl PartialEq for CanvasReference {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.runner, &other.runner)
    }
}

impl Debug for CanvasReference {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CanvasReference").finish_non_exhaustive()
    }
}

/// Cursor reference
#[derive(Clone, Debug)]
pub struct CursorReference {
    pub text_id: Uuid,
    pub cursor_sender: UnboundedSender<CursorLayoutResponse>,
}

impl PartialEq for CursorReference {
    fn eq(&self, other: &Self) -> bool {
        self.text_id == other.text_id
    }
}

impl Display for CursorReference {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CursorReference").finish_non_exhaustive()
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum AttributesBytes {
    Dynamic(Bytes),
    Static(&'static [u8]),
}

impl AttributesBytes {
    pub fn as_slice(&self) -> &[u8] {
        match self {
            Self::Dynamic(bytes) => bytes.as_ref(),
            Self::Static(bytes) => bytes,
        }
    }
}

/// Group all the custom attribute types
#[derive(Clone, PartialEq)]
pub enum CustomAttributeValues {
    Reference(NodeReference),
    CursorReference(CursorReference),
    Bytes(AttributesBytes),
    ImageReference(ImageReference),
    AccessibilityId(AccessibilityId),
    TextHighlights(Vec<(usize, usize)>),
    Canvas(CanvasReference),
}

impl Debug for CustomAttributeValues {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Reference(_) => f.debug_tuple("Reference").finish(),
            Self::CursorReference(_) => f.debug_tuple("CursorReference").finish(),
            Self::Bytes(_) => f.debug_tuple("Bytes").finish(),
            Self::ImageReference(_) => f.debug_tuple("ImageReference").finish(),
            Self::AccessibilityId(_) => f.debug_tuple("AccessibilityId").finish(),
            Self::TextHighlights(_) => f.debug_tuple("TextHighlights").finish(),
            Self::Canvas(_) => f.debug_tuple("Canvas").finish(),
        }
    }
}

impl FromAnyValue for CustomAttributeValues {
    fn from_any_value(b: &dyn Any) -> Self {
        b.downcast_ref::<CustomAttributeValues>().unwrap().clone()
    }
}

/// Transform some dynamic bytes (e.g: remote image fetched at runtime) into an attribute
pub fn dynamic_bytes(bytes: impl Into<Bytes>) -> AttributeValue {
    AttributeValue::any_value(CustomAttributeValues::Bytes(AttributesBytes::Dynamic(
        bytes.into(),
    )))
}

/// Transform some static bytes (e.g: statically linked images or SVGs) into an attribute
pub fn static_bytes(bytes: &'static [u8]) -> AttributeValue {
    AttributeValue::any_value(CustomAttributeValues::Bytes(AttributesBytes::Static(bytes)))
}