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 has_images(&self) -> bool {
46 !self.tree.images.is_empty()
47 }
48
49 pub fn loading_images(&self) -> Vec<ImageView<'_>> {
55 self
56 .tree
57 .images
58 .iter()
59 .filter_map(|&id| match NodeView::new(&self.tree, id)?.content() {
60 Content::Image(image) if crate::image::awaits_status(image.id) => Some(image),
61 _ => None,
62 })
63 .collect()
64 }
65}
66
67#[derive(Clone, Copy)]
71pub struct NodeView<'a> {
72 tree: &'a Tree,
73 id: NodeId,
74 node: &'a Node,
75}
76
77impl<'a> NodeView<'a> {
78 fn new(tree: &'a Tree, id: NodeId) -> Option<NodeView<'a>> {
79 let node = tree.node(id)?;
80 Some(NodeView { tree, id, node })
81 }
82
83 pub fn name(&self) -> &'static str {
85 self.node.name
86 }
87
88 pub fn path(&self) -> &'a str {
90 &self.node.path
91 }
92
93 pub fn rect(&self) -> Rect {
96 self.node.rect.unwrap_or_default()
97 }
98
99 pub fn bounds(&self) -> Rect {
105 self.node.bounds
106 }
107
108 pub fn inner_bounds(&self) -> Rect {
112 self.node.inner_bounds
113 }
114
115 pub fn transform(&self) -> Option<Affine> {
120 self.node.transform
121 }
122
123 pub fn style(&self) -> Style {
127 self.node.shown_style()
128 }
129
130 pub fn mask_region(&self) -> Option<&'a Region> {
134 self.node.mask_region.as_ref()
135 }
136
137 pub fn children(&self) -> impl Iterator<Item = NodeView<'a>> + 'a {
140 let tree = self.tree;
141 tree
142 .drawn_children(self.id)
143 .into_iter()
144 .filter_map(move |id| NodeView::new(tree, id))
145 }
146
147 pub fn clips(&self) -> bool {
150 self.node.clips()
151 }
152
153 pub fn scroll_bar(&self) -> Option<(Rect, Style)> {
157 self.scroll_bars().next()
158 }
159
160 pub fn scroll_bars(&self) -> impl Iterator<Item = (Rect, Style)> + 'a {
163 let scroll = match &self.node.kind {
164 NodeKind::Scroll(scroll) => Some(&**scroll),
165 _ => None,
166 };
167 scroll.into_iter().flat_map(|scroll| {
168 scroll
169 .axes
170 .iter()
171 .filter_map(|axis| Some((axis.handle?.1, scroll.bar?)))
172 })
173 }
174
175 pub fn content(&self) -> Content<'a> {
177 match &self.node.kind {
178 NodeKind::Text(text) => Content::Text(TextView {
179 text,
180 color: self.inherited_color(),
181 underline: self.inherited(|style| style.underline) == Some(true),
182 strikethrough: self.inherited(|style| style.strikethrough) == Some(true),
183 selected: None,
184 }),
185 NodeKind::Icon(icon) => match &icon.source {
186 Some(source) => Content::Icon(IconView {
187 source,
188 fallback: icon.fallback.as_ref(),
189 rect: icon_rect(self.node.content, icon.size),
190 tint: icon.tint,
191 }),
192 None => Content::None,
193 },
194 NodeKind::Input(input) => {
195 let color = self.inherited_color();
196 let color = if input.showing_placeholder() {
197 Color::rgba(color.r, color.g, color.b, color.a / 2)
198 } else {
199 color
200 };
201 let selected = match (input.focused, input.selected_text_color) {
203 (true, Some(selected)) => input.shown_selection().map(|range| {
204 let at = |index| {
205 let cursor = cursor_at(&input.text.buffer, index);
206 (cursor.line, cursor.index)
207 };
208 (at(range.start), at(range.end), selected)
209 }),
210 _ => None,
211 };
212 Content::Input(InputView {
213 text: TextView {
214 text: &input.text,
215 color,
216 underline: self.inherited(|style| style.underline) == Some(true),
217 strikethrough: self.inherited(|style| style.strikethrough) == Some(true),
218 selected,
219 },
220 selection_color: input.selection_color.unwrap_or(Color::rgba(
221 color.r,
222 color.g,
223 color.b,
224 color.a / 3,
225 )),
226 caret: (input.focused
228 && !matches!(&input.preedit, Some(preedit) if preedit.cursor.is_none()))
229 .then_some(input.caret),
230 selection: if input.focused { &input.selection } else { &[] },
231 area: input.area,
232 underline: &input.underline,
233 })
234 }
235 NodeKind::Image(image) => match &image.source {
236 Some(source) => Content::Image(ImageView {
237 id: image.id,
238 source,
239 rect: centered(self.node.content, image.width, image.height),
240 fit: image.fit,
241 filters: image.filters,
242 playing: image.playing,
243 transition: image.transition,
244 leaving: image
245 .leaving
246 .as_ref()
247 .map(|leaving| (&leaving.source, leaving.t)),
248 }),
249 None => Content::None,
250 },
251 NodeKind::Capture(capture) => match &capture.frame {
252 Some(frame) => Content::Capture(CaptureView {
253 id: capture.id,
254 frame,
255 rect: self.node.content,
256 fit: capture.fit,
257 crop: capture.crop,
258 }),
259 None => Content::None,
260 },
261 NodeKind::Canvas(canvas) => Content::Canvas(CanvasView {
262 ops: &canvas.ops,
263 rect: self.node.rect.unwrap_or_default(),
264 }),
265 _ => Content::None,
266 }
267 }
268
269 fn inherited_color(&self) -> Color {
272 self
273 .inherited(|style| style.color)
274 .unwrap_or(DEFAULT_TEXT_COLOR)
275 }
276
277 fn inherited<T>(&self, field: impl Fn(&Style) -> Option<T>) -> Option<T> {
280 let mut id = Some(self.id);
281 while let Some(current) = id {
282 if let Some(value) = self
283 .tree
284 .node(current)
285 .and_then(|node| field(&node.shown_style()))
286 {
287 return Some(value);
288 }
289 id = self.tree.taffy.parent(current);
290 }
291 None
292 }
293}
294
295pub enum Content<'a> {
297 None,
299 Text(TextView<'a>),
301 Icon(IconView<'a>),
303 Image(ImageView<'a>),
305 Capture(CaptureView<'a>),
307 Input(InputView<'a>),
309 Canvas(CanvasView<'a>),
311}
312
313pub struct CanvasView<'a> {
316 pub ops: &'a [CanvasOp],
318 pub rect: Rect,
320}
321
322pub struct InputView<'a> {
325 pub text: TextView<'a>,
327 pub caret: Option<Rect>,
329 pub selection: &'a [Rect],
332 pub selection_color: Color,
335 pub area: Rect,
337 pub underline: &'a [Rect],
339}
340
341pub struct TextView<'a> {
345 text: &'a TextNode,
346 color: Color,
347 underline: bool,
348 strikethrough: bool,
349 selected: Option<(Place, Place, Color)>,
352}
353
354type Place = (usize, usize);
356
357impl<'a> TextView<'a> {
358 pub fn content(&self) -> &'a str {
360 &self.text.content
361 }
362
363 pub fn buffer(&self) -> &'a Buffer {
365 &self.text.buffer
366 }
367
368 pub fn runs(&self) -> impl Iterator<Item = TextRun<'a>> + 'a {
371 self.text.runs()
372 }
373
374 pub fn origin(&self) -> Point {
376 self.text.origin
377 }
378
379 pub fn color(&self) -> Color {
382 self.color
383 }
384
385 pub fn span(&self, line: usize, glyph: &LayoutGlyph) -> Option<&'a Span> {
389 let index = self.text.glyph_span(line, glyph)?;
390 self.text.spans.get(index)
391 }
392
393 pub fn glyph_lit(&self, line: usize, glyph: &LayoutGlyph) -> bool {
398 let text = self.text;
399 (text.hovered_link.is_some() || text.focused_link.is_some())
400 && text
401 .glyph_span(line, glyph)
402 .is_some_and(|index| text.lit(index))
403 }
404
405 pub fn link_hover_color(&self) -> Option<Color> {
407 self.text.link_hover_color
408 }
409
410 pub fn selected_color(&self, line: usize, glyph: &LayoutGlyph) -> Option<Color> {
414 let (start, end, color) = self.selected?;
415 let at = (line, glyph.start);
416 (start <= at && at < end).then_some(color)
417 }
418
419 pub fn underline(&self) -> bool {
421 self.underline
422 }
423
424 pub fn strikethrough(&self) -> bool {
426 self.strikethrough
427 }
428
429 pub fn selection(&self) -> Vec<Rect> {
433 text_selection(self.text).map_or_else(Vec::new, |range| highlights(self.text, range))
434 }
435}
436
437pub struct IconView<'a> {
439 pub source: &'a IconSource,
441 pub fallback: Option<&'a IconSource>,
443 pub rect: Rect,
445 pub tint: Option<Color>,
447}
448
449pub struct ImageView<'a> {
451 pub id: u64,
454 pub source: &'a IconSource,
457 pub rect: Rect,
459 pub fit: Fit,
461 pub filters: ImageFilters,
463 pub playing: bool,
465 pub transition: ImageTransition,
468 pub leaving: Option<(&'a IconSource, f32)>,
472}
473
474pub struct CaptureView<'a> {
476 pub id: u64,
478 pub frame: &'a CaptureFrame,
480 pub rect: Rect,
482 pub fit: Fit,
484 pub crop: Option<Rect>,
487}