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
use std::any::Any;
use std::fmt::Debug;
use std::fmt::Display;
use std::sync::Arc;
use std::sync::Mutex;

use accesskit::NodeId as AccessibilityId;
use bytes::Bytes;
use dioxus_core::AttributeValue;
use dioxus_native_core::node::FromAnyValue;
use freya_common::{CursorLayoutResponse, NodeReferenceLayout};
use freya_engine::prelude::*;
use tokio::sync::mpsc::UnboundedSender;
use torin::geometry::{Area, CursorPoint};
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 UnboundedSender<NodeReferenceLayout>);

impl PartialEq for NodeReference {
    fn eq(&self, other: &Self) -> bool {
        self.0.same_channel(&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 type CanvasRunner = dyn Fn(&Canvas, &FontCollection, Area) + 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,
    #[allow(clippy::type_complexity)]
    pub cursor_selections: Arc<Mutex<Option<(CursorPoint, CursorPoint)>>>,
    pub cursor_position: Arc<Mutex<Option<CursorPoint>>>,
    pub cursor_sender: UnboundedSender<CursorLayoutResponse>,
    pub cursor_id: Arc<Mutex<Option<usize>>>,
}

impl CursorReference {
    pub fn set_cursor_selections(&self, cursor_selections: Option<(CursorPoint, CursorPoint)>) {
        *self.cursor_selections.lock().unwrap() = cursor_selections;
    }

    pub fn set_cursor_position(&self, cursor_position: Option<CursorPoint>) {
        *self.cursor_position.lock().unwrap() = cursor_position;
    }

    pub fn set_id(&self, id: Option<usize>) {
        *self.cursor_id.lock().unwrap() = id;
    }
}

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()
    }
}

/// Group all the custom attribute types
#[derive(Clone, PartialEq)]
pub enum CustomAttributeValues {
    Reference(NodeReference),
    CursorReference(CursorReference),
    Bytes(Vec<u8>),
    ImageReference(ImageReference),
    FocusId(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::FocusId(_) => f.debug_tuple("FocusId").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 bytes (e.g: raw image, raw svg) into attribute data
pub fn bytes_to_data(bytes: &[u8]) -> AttributeValue {
    AttributeValue::any_value(CustomAttributeValues::Bytes(bytes.to_vec()))
}