1use std::cell::Ref;
4use std::path::Path;
5
6use cosmic_text::{Buffer, LayoutGlyph};
7
8use crate::canvas::CanvasOp;
9use crate::geometry::{Affine, Point, Rect, Size};
10use crate::image::ImageTransition;
11use crate::rich::Span;
12use crate::screencopy::CaptureFrame;
13use crate::select::{highlights, text_selection};
14use crate::style::{Color, Style};
15use crate::text::{TextNode, TextRun};
16use crate::tree::{Node, NodeId, NodeKind, Tree, centered, icon_rect};
17use crate::widgets::{Fit, IconSource, ImageFilters};
18
19const DEFAULT_TEXT_COLOR: Color = Color::rgb(0, 0, 0);
21
22pub struct WindowView<'w> {
25 tree: Ref<'w, Tree>,
26}
27
28impl<'w> WindowView<'w> {
29 pub(crate) fn new(tree: Ref<'w, Tree>) -> WindowView<'w> {
30 WindowView { tree }
31 }
32
33 pub fn size(&self) -> Size {
35 self.tree.size
36 }
37
38 pub fn root(&self) -> NodeView<'_> {
40 NodeView::new(&self.tree, self.tree.root)
41 .expect("invariant: the root node exists while the tree does")
42 }
43}
44
45#[derive(Clone, Copy)]
49pub struct NodeView<'a> {
50 tree: &'a Tree,
51 id: NodeId,
52 node: &'a Node,
53}
54
55impl<'a> NodeView<'a> {
56 fn new(tree: &'a Tree, id: NodeId) -> Option<NodeView<'a>> {
57 let node = tree.node(id)?;
58 Some(NodeView { tree, id, node })
59 }
60
61 pub fn name(&self) -> &'static str {
63 self.node.name
64 }
65
66 pub fn path(&self) -> &'a str {
68 &self.node.path
69 }
70
71 pub fn rect(&self) -> Rect {
74 self.node.rect.unwrap_or_default()
75 }
76
77 pub fn bounds(&self) -> Rect {
83 self.node.bounds
84 }
85
86 pub fn inner_bounds(&self) -> Rect {
90 self.node.inner_bounds
91 }
92
93 pub fn transform(&self) -> Option<Affine> {
98 self.node.transform
99 }
100
101 pub fn style(&self) -> Style {
105 self.node.shown_style()
106 }
107
108 pub fn children(&self) -> impl Iterator<Item = NodeView<'a>> + 'a {
111 let tree = self.tree;
112 tree
113 .drawn_children(self.id)
114 .into_iter()
115 .filter_map(move |id| NodeView::new(tree, id))
116 }
117
118 pub fn clips(&self) -> bool {
121 self.node.clips()
122 }
123
124 pub fn scroll_bar(&self) -> Option<(Rect, Style)> {
128 self.scroll_bars().next()
129 }
130
131 pub fn scroll_bars(&self) -> impl Iterator<Item = (Rect, Style)> + 'a {
134 let scroll = match &self.node.kind {
135 NodeKind::Scroll(scroll) => Some(&**scroll),
136 _ => None,
137 };
138 scroll.into_iter().flat_map(|scroll| {
139 scroll
140 .axes
141 .iter()
142 .filter_map(|axis| Some((axis.handle?.1, scroll.bar?)))
143 })
144 }
145
146 pub fn content(&self) -> Content<'a> {
148 match &self.node.kind {
149 NodeKind::Text(text) => Content::Text(TextView {
150 text,
151 color: self.inherited_color(),
152 underline: self.inherited(|style| style.underline) == Some(true),
153 strikethrough: self.inherited(|style| style.strikethrough) == Some(true),
154 }),
155 NodeKind::Icon(icon) => match &icon.source {
156 Some(source) => Content::Icon(IconView {
157 source,
158 rect: icon_rect(self.node.content, icon.size),
159 tint: icon.tint,
160 }),
161 None => Content::None,
162 },
163 NodeKind::Input(input) => {
164 let color = self.inherited_color();
165 let color = if input.showing_placeholder() {
166 Color::rgba(color.r, color.g, color.b, color.a / 2)
167 } else {
168 color
169 };
170 Content::Input(InputView {
171 text: TextView {
172 text: &input.text,
173 color,
174 underline: self.inherited(|style| style.underline) == Some(true),
175 strikethrough: self.inherited(|style| style.strikethrough) == Some(true),
176 },
177 caret: (input.focused
179 && !matches!(&input.preedit, Some(preedit) if preedit.cursor.is_none()))
180 .then_some(input.caret),
181 selection: if input.focused { &input.selection } else { &[] },
182 area: input.area,
183 underline: &input.underline,
184 })
185 }
186 NodeKind::Image(image) => match &image.source {
187 Some(path) => Content::Image(ImageView {
188 id: image.id,
189 path,
190 rect: centered(self.node.content, image.width, image.height),
191 fit: image.fit,
192 filters: image.filters,
193 playing: image.playing,
194 transition: image.transition,
195 leaving: image
196 .leaving
197 .as_ref()
198 .map(|leaving| (leaving.source.as_path(), leaving.t)),
199 }),
200 None => Content::None,
201 },
202 NodeKind::Capture(capture) => match &capture.frame {
203 Some(frame) => Content::Capture(CaptureView {
204 id: capture.id,
205 frame,
206 rect: self.node.content,
207 fit: capture.fit,
208 }),
209 None => Content::None,
210 },
211 NodeKind::Canvas(canvas) => Content::Canvas(CanvasView {
212 ops: &canvas.ops,
213 rect: self.node.rect.unwrap_or_default(),
214 }),
215 _ => Content::None,
216 }
217 }
218
219 fn inherited_color(&self) -> Color {
222 self
223 .inherited(|style| style.color)
224 .unwrap_or(DEFAULT_TEXT_COLOR)
225 }
226
227 fn inherited<T>(&self, field: impl Fn(&Style) -> Option<T>) -> Option<T> {
230 let mut id = Some(self.id);
231 while let Some(current) = id {
232 if let Some(value) = self
233 .tree
234 .node(current)
235 .and_then(|node| field(&node.shown_style()))
236 {
237 return Some(value);
238 }
239 id = self.tree.taffy.parent(current);
240 }
241 None
242 }
243}
244
245pub enum Content<'a> {
247 None,
249 Text(TextView<'a>),
251 Icon(IconView<'a>),
253 Image(ImageView<'a>),
255 Capture(CaptureView<'a>),
257 Input(InputView<'a>),
259 Canvas(CanvasView<'a>),
261}
262
263pub struct CanvasView<'a> {
266 pub ops: &'a [CanvasOp],
268 pub rect: Rect,
270}
271
272pub struct InputView<'a> {
275 pub text: TextView<'a>,
277 pub caret: Option<Rect>,
279 pub selection: &'a [Rect],
282 pub area: Rect,
284 pub underline: &'a [Rect],
286}
287
288pub struct TextView<'a> {
292 text: &'a TextNode,
293 color: Color,
294 underline: bool,
295 strikethrough: bool,
296}
297
298impl<'a> TextView<'a> {
299 pub fn content(&self) -> &'a str {
301 &self.text.content
302 }
303
304 pub fn buffer(&self) -> &'a Buffer {
306 &self.text.buffer
307 }
308
309 pub fn runs(&self) -> impl Iterator<Item = TextRun<'a>> + 'a {
312 self.text.runs()
313 }
314
315 pub fn origin(&self) -> Point {
317 self.text.origin
318 }
319
320 pub fn color(&self) -> Color {
323 self.color
324 }
325
326 pub fn span(&self, line: usize, glyph: &LayoutGlyph) -> Option<&'a Span> {
330 let index = self.text.glyph_span(line, glyph)?;
331 self.text.spans.get(index)
332 }
333
334 pub fn glyph_lit(&self, line: usize, glyph: &LayoutGlyph) -> bool {
339 let text = self.text;
340 (text.hovered_link.is_some() || text.focused_link.is_some())
341 && text
342 .glyph_span(line, glyph)
343 .is_some_and(|index| text.lit(index))
344 }
345
346 pub fn link_hover_color(&self) -> Option<Color> {
348 self.text.link_hover_color
349 }
350
351 pub fn underline(&self) -> bool {
353 self.underline
354 }
355
356 pub fn strikethrough(&self) -> bool {
358 self.strikethrough
359 }
360
361 pub fn selection(&self) -> Vec<Rect> {
365 text_selection(self.text).map_or_else(Vec::new, |range| highlights(self.text, range))
366 }
367}
368
369pub struct IconView<'a> {
371 pub source: &'a IconSource,
373 pub rect: Rect,
375 pub tint: Option<Color>,
377}
378
379pub struct ImageView<'a> {
381 pub id: u64,
384 pub path: &'a Path,
386 pub rect: Rect,
388 pub fit: Fit,
390 pub filters: ImageFilters,
392 pub playing: bool,
394 pub transition: ImageTransition,
397 pub leaving: Option<(&'a Path, f32)>,
401}
402
403pub struct CaptureView<'a> {
405 pub id: u64,
407 pub frame: &'a CaptureFrame,
409 pub rect: Rect,
411 pub fit: Fit,
413}