1use iced_core::Element;
4use icedwright_types::role::{AnyRole, RoleKind, RoleType, Untyped};
5
6use crate::annotation::Annotation;
7use crate::test_id::IntoTestId;
8
9#[must_use = "a Semantic does nothing until it is converted into an Element"]
15pub struct Semantic<'a, Message, Theme, Renderer, R: RoleKind = AnyRole> {
16 pub(crate) content: Element<'a, Message, Theme, Renderer>,
17 pub(crate) annotation: Annotation<'a, R>,
18 #[cfg(feature = "semantics")]
19 pub(crate) cursor: Option<icedwright_types::CursorSeen>,
20}
21
22impl<Message, Theme, Renderer, R: RoleKind> std::fmt::Debug
23 for Semantic<'_, Message, Theme, Renderer, R>
24{
25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26 f.debug_struct("Semantic")
27 .field("annotation", &self.annotation)
28 .finish_non_exhaustive()
29 }
30}
31
32pub fn semantic<'a, Message, Theme, Renderer>(
34 content: impl Into<Element<'a, Message, Theme, Renderer>>,
35) -> Semantic<'a, Message, Theme, Renderer, AnyRole> {
36 Semantic {
37 content: content.into(),
38 annotation: Annotation::new(),
39 #[cfg(feature = "semantics")]
40 cursor: None,
41 }
42}
43
44pub fn test_id<'a, Message, Theme, Renderer>(
46 id: impl IntoTestId<'a>,
47 content: impl Into<Element<'a, Message, Theme, Renderer>>,
48) -> Semantic<'a, Message, Theme, Renderer, AnyRole> {
49 semantic(content).test_id(id)
50}
51
52pub trait SemanticExt<'a, Message, Theme, Renderer>:
54 Into<Element<'a, Message, Theme, Renderer>>
55{
56 fn semantic(self) -> Semantic<'a, Message, Theme, Renderer, AnyRole> {
58 semantic(self)
59 }
60}
61
62impl<'a, Message, Theme, Renderer, T> SemanticExt<'a, Message, Theme, Renderer> for T where
63 T: Into<Element<'a, Message, Theme, Renderer>>
64{
65}
66
67impl<'a, Message, Theme, Renderer, R: RoleKind> Semantic<'a, Message, Theme, Renderer, R> {
68 #[inline]
70 pub fn role<R2: RoleType>(self, role: R2) -> Semantic<'a, Message, Theme, Renderer, R2>
71 where
72 R: Untyped,
73 {
74 Semantic {
75 content: self.content,
76 annotation: self.annotation.role(role),
77 #[cfg(feature = "semantics")]
78 cursor: self.cursor,
79 }
80 }
81
82 #[inline]
83 pub(crate) fn map_annotation(
84 self,
85 change: impl FnOnce(Annotation<'a, R>) -> Annotation<'a, R>,
86 ) -> Self {
87 Self {
88 content: self.content,
89 annotation: change(self.annotation),
90 #[cfg(feature = "semantics")]
91 cursor: self.cursor,
92 }
93 }
94}
95
96impl<'a, Message, Theme, Renderer, R> From<Semantic<'a, Message, Theme, Renderer, R>>
97 for Element<'a, Message, Theme, Renderer>
98where
99 Message: 'a,
100 Theme: 'a,
101 Renderer: iced_core::Renderer + 'a,
102 R: RoleKind,
103{
104 #[inline]
105 fn from(semantic: Semantic<'a, Message, Theme, Renderer, R>) -> Self {
106 #[cfg(feature = "semantics")]
107 {
108 Element::new(semantic)
109 }
110 #[cfg(not(feature = "semantics"))]
111 {
112 semantic.content
113 }
114 }
115}
116
117#[cfg(feature = "semantics")]
118mod widget {
119 use iced_core::layout::{self, Layout};
120 use iced_core::widget::{Operation, Tree, tree};
121 use iced_core::{
122 Clipboard, Event, Length, Rectangle, Shell, Size, Vector, mouse, overlay, renderer,
123 };
124 use icedwright_types::CursorSeen;
125 use icedwright_types::role::RoleKind;
126
127 use super::Semantic;
128 use crate::annotation::report;
129
130 fn seen(cursor: mouse::Cursor) -> CursorSeen {
131 match cursor {
132 mouse::Cursor::Available(_) => CursorSeen::Available,
133 mouse::Cursor::Levitating(_) => CursorSeen::Levitating,
134 mouse::Cursor::Unavailable => CursorSeen::Unavailable,
135 }
136 }
137
138 impl<Message, Theme, Renderer, R> iced_core::Widget<Message, Theme, Renderer>
139 for Semantic<'_, Message, Theme, Renderer, R>
140 where
141 Renderer: iced_core::Renderer,
142 R: RoleKind,
143 {
144 fn size(&self) -> Size<Length> {
145 self.content.as_widget().size()
146 }
147
148 fn size_hint(&self) -> Size<Length> {
149 self.content.as_widget().size_hint()
150 }
151
152 fn layout(
153 &mut self,
154 tree: &mut Tree,
155 renderer: &Renderer,
156 limits: &layout::Limits,
157 ) -> layout::Node {
158 self.content.as_widget_mut().layout(tree, renderer, limits)
159 }
160
161 fn draw(
162 &self,
163 tree: &Tree,
164 renderer: &mut Renderer,
165 theme: &Theme,
166 style: &renderer::Style,
167 layout: Layout<'_>,
168 cursor: mouse::Cursor,
169 viewport: &Rectangle,
170 ) {
171 self.content
172 .as_widget()
173 .draw(tree, renderer, theme, style, layout, cursor, viewport);
174 }
175
176 fn tag(&self) -> tree::Tag {
177 self.content.as_widget().tag()
178 }
179
180 fn state(&self) -> tree::State {
181 self.content.as_widget().state()
182 }
183
184 fn children(&self) -> Vec<Tree> {
185 self.content.as_widget().children()
186 }
187
188 fn diff(&self, tree: &mut Tree) {
189 self.content.as_widget().diff(tree);
190 }
191
192 fn operate(
193 &mut self,
194 tree: &mut Tree,
195 layout: Layout<'_>,
196 renderer: &Renderer,
197 operation: &mut dyn Operation,
198 ) {
199 let cursor = self.cursor;
200 let annotation = &self.annotation;
201 report(operation, layout.bounds(), || {
202 annotation.to_static().cursor_seen(cursor)
203 });
204 let content = &mut self.content;
205 operation.traverse(&mut |operation| {
206 content
207 .as_widget_mut()
208 .operate(tree, layout, renderer, operation);
209 });
210 }
211
212 fn update(
213 &mut self,
214 tree: &mut Tree,
215 event: &Event,
216 layout: Layout<'_>,
217 cursor: mouse::Cursor,
218 renderer: &Renderer,
219 clipboard: &mut dyn Clipboard,
220 shell: &mut Shell<'_, Message>,
221 viewport: &Rectangle,
222 ) {
223 if matches!(event, Event::Mouse(_)) {
224 self.cursor = Some(seen(cursor));
225 }
226 self.content.as_widget_mut().update(
227 tree, event, layout, cursor, renderer, clipboard, shell, viewport,
228 );
229 }
230
231 fn mouse_interaction(
232 &self,
233 tree: &Tree,
234 layout: Layout<'_>,
235 cursor: mouse::Cursor,
236 viewport: &Rectangle,
237 renderer: &Renderer,
238 ) -> mouse::Interaction {
239 self.content
240 .as_widget()
241 .mouse_interaction(tree, layout, cursor, viewport, renderer)
242 }
243
244 fn overlay<'b>(
245 &'b mut self,
246 tree: &'b mut Tree,
247 layout: Layout<'b>,
248 renderer: &Renderer,
249 viewport: &Rectangle,
250 translation: Vector,
251 ) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
252 self.content
253 .as_widget_mut()
254 .overlay(tree, layout, renderer, viewport, translation)
255 }
256 }
257}