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::region::Region;
12use crate::rich::Span;
13use crate::screencopy::CaptureFrame;
14use crate::select::{cursor_at, highlights, text_selection};
15use crate::style::{Color, Style};
16use crate::text::{TextNode, TextRun};
17use crate::tree::{Node, NodeId, NodeKind, Tree, centered, icon_rect};
18use crate::widgets::{Fit, IconSource, ImageFilters};
19
20const DEFAULT_TEXT_COLOR: Color = Color::rgb(0, 0, 0);
22
23pub struct WindowView<'w> {
26 tree: Ref<'w, Tree>,
27}
28
29impl<'w> WindowView<'w> {
30 pub(crate) fn new(tree: Ref<'w, Tree>) -> WindowView<'w> {
31 WindowView { tree }
32 }
33
34 pub fn size(&self) -> Size {
36 self.tree.size
37 }
38
39 pub fn root(&self) -> NodeView<'_> {
41 NodeView::new(&self.tree, self.tree.root)
42 .expect("invariant: the root node exists while the tree does")
43 }
44
45 pub fn loading_images(&self) -> Vec<ImageView<'_>> {
51 self
52 .tree
53 .images
54 .iter()
55 .filter_map(|&id| match NodeView::new(&self.tree, id)?.content() {
56 Content::Image(image) if crate::image::awaits_status(image.id) => Some(image),
57 _ => None,
58 })
59 .collect()
60 }
61}
62
63#[derive(Clone, Copy)]
67pub struct NodeView<'a> {
68 tree: &'a Tree,
69 id: NodeId,
70 node: &'a Node,
71}
72
73impl<'a> NodeView<'a> {
74 fn new(tree: &'a Tree, id: NodeId) -> Option<NodeView<'a>> {
75 let node = tree.node(id)?;
76 Some(NodeView { tree, id, node })
77 }
78
79 pub fn name(&self) -> &'static str {
81 self.node.name
82 }
83
84 pub fn path(&self) -> &'a str {
86 &self.node.path
87 }
88
89 pub fn rect(&self) -> Rect {
92 self.node.rect.unwrap_or_default()
93 }
94
95 pub fn bounds(&self) -> Rect {
101 self.node.bounds
102 }
103
104 pub fn inner_bounds(&self) -> Rect {
108 self.node.inner_bounds
109 }
110
111 pub fn transform(&self) -> Option<Affine> {
116 self.node.transform
117 }
118
119 pub fn style(&self) -> Style {
123 self.node.shown_style()
124 }
125
126 pub fn mask_region(&self) -> Option<&'a Region> {
130 self.node.mask_region.as_ref()
131 }
132
133 pub fn children(&self) -> impl Iterator<Item = NodeView<'a>> + 'a {
136 let tree = self.tree;
137 tree
138 .drawn_children(self.id)
139 .into_iter()
140 .filter_map(move |id| NodeView::new(tree, id))
141 }
142
143 pub fn clips(&self) -> bool {
146 self.node.clips()
147 }
148
149 pub fn scroll_bar(&self) -> Option<(Rect, Style)> {
153 self.scroll_bars().next()
154 }
155
156 pub fn scroll_bars(&self) -> impl Iterator<Item = (Rect, Style)> + 'a {
159 let scroll = match &self.node.kind {
160 NodeKind::Scroll(scroll) => Some(&**scroll),
161 _ => None,
162 };
163 scroll.into_iter().flat_map(|scroll| {
164 scroll
165 .axes
166 .iter()
167 .filter_map(|axis| Some((axis.handle?.1, scroll.bar?)))
168 })
169 }
170
171 pub fn content(&self) -> Content<'a> {
173 match &self.node.kind {
174 NodeKind::Text(text) => Content::Text(TextView {
175 text,
176 color: self.inherited_color(),
177 underline: self.inherited(|style| style.underline) == Some(true),
178 strikethrough: self.inherited(|style| style.strikethrough) == Some(true),
179 selected: None,
180 }),
181 NodeKind::Icon(icon) => match &icon.source {
182 Some(source) => Content::Icon(IconView {
183 source,
184 fallback: icon.fallback.as_ref(),
185 rect: icon_rect(self.node.content, icon.size),
186 tint: icon.tint,
187 }),
188 None => Content::None,
189 },
190 NodeKind::Input(input) => {
191 let color = self.inherited_color();
192 let color = if input.showing_placeholder() {
193 Color::rgba(color.r, color.g, color.b, color.a / 2)
194 } else {
195 color
196 };
197 let selected = match (input.focused, input.selected_text_color) {
199 (true, Some(selected)) => input.shown_selection().map(|range| {
200 let at = |index| {
201 let cursor = cursor_at(&input.text.buffer, index);
202 (cursor.line, cursor.index)
203 };
204 (at(range.start), at(range.end), selected)
205 }),
206 _ => None,
207 };
208 Content::Input(InputView {
209 text: TextView {
210 text: &input.text,
211 color,
212 underline: self.inherited(|style| style.underline) == Some(true),
213 strikethrough: self.inherited(|style| style.strikethrough) == Some(true),
214 selected,
215 },
216 selection_color: input.selection_color.unwrap_or(Color::rgba(
217 color.r,
218 color.g,
219 color.b,
220 color.a / 3,
221 )),
222 caret: (input.focused
224 && !matches!(&input.preedit, Some(preedit) if preedit.cursor.is_none()))
225 .then_some(input.caret),
226 selection: if input.focused { &input.selection } else { &[] },
227 area: input.area,
228 underline: &input.underline,
229 })
230 }
231 NodeKind::Image(image) => match &image.source {
232 Some(path) => Content::Image(ImageView {
233 id: image.id,
234 path,
235 rect: centered(self.node.content, image.width, image.height),
236 fit: image.fit,
237 filters: image.filters,
238 playing: image.playing,
239 transition: image.transition,
240 leaving: image
241 .leaving
242 .as_ref()
243 .map(|leaving| (leaving.source.as_path(), leaving.t)),
244 }),
245 None => Content::None,
246 },
247 NodeKind::Capture(capture) => match &capture.frame {
248 Some(frame) => Content::Capture(CaptureView {
249 id: capture.id,
250 frame,
251 rect: self.node.content,
252 fit: capture.fit,
253 crop: capture.crop,
254 }),
255 None => Content::None,
256 },
257 NodeKind::Canvas(canvas) => Content::Canvas(CanvasView {
258 ops: &canvas.ops,
259 rect: self.node.rect.unwrap_or_default(),
260 }),
261 _ => Content::None,
262 }
263 }
264
265 fn inherited_color(&self) -> Color {
268 self
269 .inherited(|style| style.color)
270 .unwrap_or(DEFAULT_TEXT_COLOR)
271 }
272
273 fn inherited<T>(&self, field: impl Fn(&Style) -> Option<T>) -> Option<T> {
276 let mut id = Some(self.id);
277 while let Some(current) = id {
278 if let Some(value) = self
279 .tree
280 .node(current)
281 .and_then(|node| field(&node.shown_style()))
282 {
283 return Some(value);
284 }
285 id = self.tree.taffy.parent(current);
286 }
287 None
288 }
289}
290
291pub enum Content<'a> {
293 None,
295 Text(TextView<'a>),
297 Icon(IconView<'a>),
299 Image(ImageView<'a>),
301 Capture(CaptureView<'a>),
303 Input(InputView<'a>),
305 Canvas(CanvasView<'a>),
307}
308
309pub struct CanvasView<'a> {
312 pub ops: &'a [CanvasOp],
314 pub rect: Rect,
316}
317
318pub struct InputView<'a> {
321 pub text: TextView<'a>,
323 pub caret: Option<Rect>,
325 pub selection: &'a [Rect],
328 pub selection_color: Color,
331 pub area: Rect,
333 pub underline: &'a [Rect],
335}
336
337pub struct TextView<'a> {
341 text: &'a TextNode,
342 color: Color,
343 underline: bool,
344 strikethrough: bool,
345 selected: Option<(Place, Place, Color)>,
348}
349
350type Place = (usize, usize);
352
353impl<'a> TextView<'a> {
354 pub fn content(&self) -> &'a str {
356 &self.text.content
357 }
358
359 pub fn buffer(&self) -> &'a Buffer {
361 &self.text.buffer
362 }
363
364 pub fn runs(&self) -> impl Iterator<Item = TextRun<'a>> + 'a {
367 self.text.runs()
368 }
369
370 pub fn origin(&self) -> Point {
372 self.text.origin
373 }
374
375 pub fn color(&self) -> Color {
378 self.color
379 }
380
381 pub fn span(&self, line: usize, glyph: &LayoutGlyph) -> Option<&'a Span> {
385 let index = self.text.glyph_span(line, glyph)?;
386 self.text.spans.get(index)
387 }
388
389 pub fn glyph_lit(&self, line: usize, glyph: &LayoutGlyph) -> bool {
394 let text = self.text;
395 (text.hovered_link.is_some() || text.focused_link.is_some())
396 && text
397 .glyph_span(line, glyph)
398 .is_some_and(|index| text.lit(index))
399 }
400
401 pub fn link_hover_color(&self) -> Option<Color> {
403 self.text.link_hover_color
404 }
405
406 pub fn selected_color(&self, line: usize, glyph: &LayoutGlyph) -> Option<Color> {
410 let (start, end, color) = self.selected?;
411 let at = (line, glyph.start);
412 (start <= at && at < end).then_some(color)
413 }
414
415 pub fn underline(&self) -> bool {
417 self.underline
418 }
419
420 pub fn strikethrough(&self) -> bool {
422 self.strikethrough
423 }
424
425 pub fn selection(&self) -> Vec<Rect> {
429 text_selection(self.text).map_or_else(Vec::new, |range| highlights(self.text, range))
430 }
431}
432
433pub struct IconView<'a> {
435 pub source: &'a IconSource,
437 pub fallback: Option<&'a IconSource>,
439 pub rect: Rect,
441 pub tint: Option<Color>,
443}
444
445pub struct ImageView<'a> {
447 pub id: u64,
450 pub path: &'a Path,
452 pub rect: Rect,
454 pub fit: Fit,
456 pub filters: ImageFilters,
458 pub playing: bool,
460 pub transition: ImageTransition,
463 pub leaving: Option<(&'a Path, f32)>,
467}
468
469pub struct CaptureView<'a> {
471 pub id: u64,
473 pub frame: &'a CaptureFrame,
475 pub rect: Rect,
477 pub fit: Fit,
479 pub crop: Option<Rect>,
482}