1use std::cell::Ref;
4
5use cosmic_text::{Buffer, LayoutGlyph};
6
7use crate::canvas::CanvasOp;
8use crate::geometry::{Affine, Point, Rect, Size};
9use crate::image::ImageTransition;
10use crate::region::Region;
11use crate::rich::Span;
12use crate::screencopy::CaptureFrame;
13use crate::select::{cursor_at, 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 pub fn loading_images(&self) -> Vec<ImageView<'_>> {
50 self
51 .tree
52 .images
53 .iter()
54 .filter_map(|&id| match NodeView::new(&self.tree, id)?.content() {
55 Content::Image(image) if crate::image::awaits_status(image.id) => Some(image),
56 _ => None,
57 })
58 .collect()
59 }
60}
61
62#[derive(Clone, Copy)]
66pub struct NodeView<'a> {
67 tree: &'a Tree,
68 id: NodeId,
69 node: &'a Node,
70}
71
72impl<'a> NodeView<'a> {
73 fn new(tree: &'a Tree, id: NodeId) -> Option<NodeView<'a>> {
74 let node = tree.node(id)?;
75 Some(NodeView { tree, id, node })
76 }
77
78 pub fn name(&self) -> &'static str {
80 self.node.name
81 }
82
83 pub fn path(&self) -> &'a str {
85 &self.node.path
86 }
87
88 pub fn rect(&self) -> Rect {
91 self.node.rect.unwrap_or_default()
92 }
93
94 pub fn bounds(&self) -> Rect {
100 self.node.bounds
101 }
102
103 pub fn inner_bounds(&self) -> Rect {
107 self.node.inner_bounds
108 }
109
110 pub fn transform(&self) -> Option<Affine> {
115 self.node.transform
116 }
117
118 pub fn style(&self) -> Style {
122 self.node.shown_style()
123 }
124
125 pub fn mask_region(&self) -> Option<&'a Region> {
129 self.node.mask_region.as_ref()
130 }
131
132 pub fn children(&self) -> impl Iterator<Item = NodeView<'a>> + 'a {
135 let tree = self.tree;
136 tree
137 .drawn_children(self.id)
138 .into_iter()
139 .filter_map(move |id| NodeView::new(tree, id))
140 }
141
142 pub fn clips(&self) -> bool {
145 self.node.clips()
146 }
147
148 pub fn scroll_bar(&self) -> Option<(Rect, Style)> {
152 self.scroll_bars().next()
153 }
154
155 pub fn scroll_bars(&self) -> impl Iterator<Item = (Rect, Style)> + 'a {
158 let scroll = match &self.node.kind {
159 NodeKind::Scroll(scroll) => Some(&**scroll),
160 _ => None,
161 };
162 scroll.into_iter().flat_map(|scroll| {
163 scroll
164 .axes
165 .iter()
166 .filter_map(|axis| Some((axis.handle?.1, scroll.bar?)))
167 })
168 }
169
170 pub fn content(&self) -> Content<'a> {
172 match &self.node.kind {
173 NodeKind::Text(text) => Content::Text(TextView {
174 text,
175 color: self.inherited_color(),
176 underline: self.inherited(|style| style.underline) == Some(true),
177 strikethrough: self.inherited(|style| style.strikethrough) == Some(true),
178 selected: None,
179 }),
180 NodeKind::Icon(icon) => match &icon.source {
181 Some(source) => Content::Icon(IconView {
182 source,
183 fallback: icon.fallback.as_ref(),
184 rect: icon_rect(self.node.content, icon.size),
185 tint: icon.tint,
186 }),
187 None => Content::None,
188 },
189 NodeKind::Input(input) => {
190 let color = self.inherited_color();
191 let color = if input.showing_placeholder() {
192 Color::rgba(color.r, color.g, color.b, color.a / 2)
193 } else {
194 color
195 };
196 let selected = match (input.focused, input.selected_text_color) {
198 (true, Some(selected)) => input.shown_selection().map(|range| {
199 let at = |index| {
200 let cursor = cursor_at(&input.text.buffer, index);
201 (cursor.line, cursor.index)
202 };
203 (at(range.start), at(range.end), selected)
204 }),
205 _ => None,
206 };
207 Content::Input(InputView {
208 text: TextView {
209 text: &input.text,
210 color,
211 underline: self.inherited(|style| style.underline) == Some(true),
212 strikethrough: self.inherited(|style| style.strikethrough) == Some(true),
213 selected,
214 },
215 selection_color: input.selection_color.unwrap_or(Color::rgba(
216 color.r,
217 color.g,
218 color.b,
219 color.a / 3,
220 )),
221 caret: (input.focused
223 && !matches!(&input.preedit, Some(preedit) if preedit.cursor.is_none()))
224 .then_some(input.caret),
225 selection: if input.focused { &input.selection } else { &[] },
226 area: input.area,
227 underline: &input.underline,
228 })
229 }
230 NodeKind::Image(image) => match &image.source {
231 Some(source) => Content::Image(ImageView {
232 id: image.id,
233 source,
234 rect: centered(self.node.content, image.width, image.height),
235 fit: image.fit,
236 filters: image.filters,
237 playing: image.playing,
238 transition: image.transition,
239 leaving: image
240 .leaving
241 .as_ref()
242 .map(|leaving| (&leaving.source, leaving.t)),
243 }),
244 None => Content::None,
245 },
246 NodeKind::Capture(capture) => match &capture.frame {
247 Some(frame) => Content::Capture(CaptureView {
248 id: capture.id,
249 frame,
250 rect: self.node.content,
251 fit: capture.fit,
252 crop: capture.crop,
253 }),
254 None => Content::None,
255 },
256 NodeKind::Canvas(canvas) => Content::Canvas(CanvasView {
257 ops: &canvas.ops,
258 rect: self.node.rect.unwrap_or_default(),
259 }),
260 _ => Content::None,
261 }
262 }
263
264 fn inherited_color(&self) -> Color {
267 self
268 .inherited(|style| style.color)
269 .unwrap_or(DEFAULT_TEXT_COLOR)
270 }
271
272 fn inherited<T>(&self, field: impl Fn(&Style) -> Option<T>) -> Option<T> {
275 let mut id = Some(self.id);
276 while let Some(current) = id {
277 if let Some(value) = self
278 .tree
279 .node(current)
280 .and_then(|node| field(&node.shown_style()))
281 {
282 return Some(value);
283 }
284 id = self.tree.taffy.parent(current);
285 }
286 None
287 }
288}
289
290pub enum Content<'a> {
292 None,
294 Text(TextView<'a>),
296 Icon(IconView<'a>),
298 Image(ImageView<'a>),
300 Capture(CaptureView<'a>),
302 Input(InputView<'a>),
304 Canvas(CanvasView<'a>),
306}
307
308pub struct CanvasView<'a> {
311 pub ops: &'a [CanvasOp],
313 pub rect: Rect,
315}
316
317pub struct InputView<'a> {
320 pub text: TextView<'a>,
322 pub caret: Option<Rect>,
324 pub selection: &'a [Rect],
327 pub selection_color: Color,
330 pub area: Rect,
332 pub underline: &'a [Rect],
334}
335
336pub struct TextView<'a> {
340 text: &'a TextNode,
341 color: Color,
342 underline: bool,
343 strikethrough: bool,
344 selected: Option<(Place, Place, Color)>,
347}
348
349type Place = (usize, usize);
351
352impl<'a> TextView<'a> {
353 pub fn content(&self) -> &'a str {
355 &self.text.content
356 }
357
358 pub fn buffer(&self) -> &'a Buffer {
360 &self.text.buffer
361 }
362
363 pub fn runs(&self) -> impl Iterator<Item = TextRun<'a>> + 'a {
366 self.text.runs()
367 }
368
369 pub fn origin(&self) -> Point {
371 self.text.origin
372 }
373
374 pub fn color(&self) -> Color {
377 self.color
378 }
379
380 pub fn span(&self, line: usize, glyph: &LayoutGlyph) -> Option<&'a Span> {
384 let index = self.text.glyph_span(line, glyph)?;
385 self.text.spans.get(index)
386 }
387
388 pub fn glyph_lit(&self, line: usize, glyph: &LayoutGlyph) -> bool {
393 let text = self.text;
394 (text.hovered_link.is_some() || text.focused_link.is_some())
395 && text
396 .glyph_span(line, glyph)
397 .is_some_and(|index| text.lit(index))
398 }
399
400 pub fn link_hover_color(&self) -> Option<Color> {
402 self.text.link_hover_color
403 }
404
405 pub fn selected_color(&self, line: usize, glyph: &LayoutGlyph) -> Option<Color> {
409 let (start, end, color) = self.selected?;
410 let at = (line, glyph.start);
411 (start <= at && at < end).then_some(color)
412 }
413
414 pub fn underline(&self) -> bool {
416 self.underline
417 }
418
419 pub fn strikethrough(&self) -> bool {
421 self.strikethrough
422 }
423
424 pub fn selection(&self) -> Vec<Rect> {
428 text_selection(self.text).map_or_else(Vec::new, |range| highlights(self.text, range))
429 }
430}
431
432pub struct IconView<'a> {
434 pub source: &'a IconSource,
436 pub fallback: Option<&'a IconSource>,
438 pub rect: Rect,
440 pub tint: Option<Color>,
442}
443
444pub struct ImageView<'a> {
446 pub id: u64,
449 pub source: &'a IconSource,
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 IconSource, 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}