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 fallback: icon.fallback.as_ref(),
159 rect: icon_rect(self.node.content, icon.size),
160 tint: icon.tint,
161 }),
162 None => Content::None,
163 },
164 NodeKind::Input(input) => {
165 let color = self.inherited_color();
166 let color = if input.showing_placeholder() {
167 Color::rgba(color.r, color.g, color.b, color.a / 2)
168 } else {
169 color
170 };
171 Content::Input(InputView {
172 text: TextView {
173 text: &input.text,
174 color,
175 underline: self.inherited(|style| style.underline) == Some(true),
176 strikethrough: self.inherited(|style| style.strikethrough) == Some(true),
177 },
178 caret: (input.focused
180 && !matches!(&input.preedit, Some(preedit) if preedit.cursor.is_none()))
181 .then_some(input.caret),
182 selection: if input.focused { &input.selection } else { &[] },
183 area: input.area,
184 underline: &input.underline,
185 })
186 }
187 NodeKind::Image(image) => match &image.source {
188 Some(path) => Content::Image(ImageView {
189 id: image.id,
190 path,
191 rect: centered(self.node.content, image.width, image.height),
192 fit: image.fit,
193 filters: image.filters,
194 playing: image.playing,
195 transition: image.transition,
196 leaving: image
197 .leaving
198 .as_ref()
199 .map(|leaving| (leaving.source.as_path(), leaving.t)),
200 }),
201 None => Content::None,
202 },
203 NodeKind::Capture(capture) => match &capture.frame {
204 Some(frame) => Content::Capture(CaptureView {
205 id: capture.id,
206 frame,
207 rect: self.node.content,
208 fit: capture.fit,
209 }),
210 None => Content::None,
211 },
212 NodeKind::Canvas(canvas) => Content::Canvas(CanvasView {
213 ops: &canvas.ops,
214 rect: self.node.rect.unwrap_or_default(),
215 }),
216 _ => Content::None,
217 }
218 }
219
220 fn inherited_color(&self) -> Color {
223 self
224 .inherited(|style| style.color)
225 .unwrap_or(DEFAULT_TEXT_COLOR)
226 }
227
228 fn inherited<T>(&self, field: impl Fn(&Style) -> Option<T>) -> Option<T> {
231 let mut id = Some(self.id);
232 while let Some(current) = id {
233 if let Some(value) = self
234 .tree
235 .node(current)
236 .and_then(|node| field(&node.shown_style()))
237 {
238 return Some(value);
239 }
240 id = self.tree.taffy.parent(current);
241 }
242 None
243 }
244}
245
246pub enum Content<'a> {
248 None,
250 Text(TextView<'a>),
252 Icon(IconView<'a>),
254 Image(ImageView<'a>),
256 Capture(CaptureView<'a>),
258 Input(InputView<'a>),
260 Canvas(CanvasView<'a>),
262}
263
264pub struct CanvasView<'a> {
267 pub ops: &'a [CanvasOp],
269 pub rect: Rect,
271}
272
273pub struct InputView<'a> {
276 pub text: TextView<'a>,
278 pub caret: Option<Rect>,
280 pub selection: &'a [Rect],
283 pub area: Rect,
285 pub underline: &'a [Rect],
287}
288
289pub struct TextView<'a> {
293 text: &'a TextNode,
294 color: Color,
295 underline: bool,
296 strikethrough: bool,
297}
298
299impl<'a> TextView<'a> {
300 pub fn content(&self) -> &'a str {
302 &self.text.content
303 }
304
305 pub fn buffer(&self) -> &'a Buffer {
307 &self.text.buffer
308 }
309
310 pub fn runs(&self) -> impl Iterator<Item = TextRun<'a>> + 'a {
313 self.text.runs()
314 }
315
316 pub fn origin(&self) -> Point {
318 self.text.origin
319 }
320
321 pub fn color(&self) -> Color {
324 self.color
325 }
326
327 pub fn span(&self, line: usize, glyph: &LayoutGlyph) -> Option<&'a Span> {
331 let index = self.text.glyph_span(line, glyph)?;
332 self.text.spans.get(index)
333 }
334
335 pub fn glyph_lit(&self, line: usize, glyph: &LayoutGlyph) -> bool {
340 let text = self.text;
341 (text.hovered_link.is_some() || text.focused_link.is_some())
342 && text
343 .glyph_span(line, glyph)
344 .is_some_and(|index| text.lit(index))
345 }
346
347 pub fn link_hover_color(&self) -> Option<Color> {
349 self.text.link_hover_color
350 }
351
352 pub fn underline(&self) -> bool {
354 self.underline
355 }
356
357 pub fn strikethrough(&self) -> bool {
359 self.strikethrough
360 }
361
362 pub fn selection(&self) -> Vec<Rect> {
366 text_selection(self.text).map_or_else(Vec::new, |range| highlights(self.text, range))
367 }
368}
369
370pub struct IconView<'a> {
372 pub source: &'a IconSource,
374 pub fallback: Option<&'a IconSource>,
376 pub rect: Rect,
378 pub tint: Option<Color>,
380}
381
382pub struct ImageView<'a> {
384 pub id: u64,
387 pub path: &'a Path,
389 pub rect: Rect,
391 pub fit: Fit,
393 pub filters: ImageFilters,
395 pub playing: bool,
397 pub transition: ImageTransition,
400 pub leaving: Option<(&'a Path, f32)>,
404}
405
406pub struct CaptureView<'a> {
408 pub id: u64,
410 pub frame: &'a CaptureFrame,
412 pub rect: Rect,
414 pub fit: Fit,
416}