iced_core/text/paragraph.rs
1//! Draw paragraphs.
2use crate::alignment;
3use crate::text::{
4 Alignment, Difference, Ellipsis, Hit, LineHeight, Shaping, Span, Target, Text, Wrapping,
5};
6use crate::{Font, Pixels, Point, Rectangle, Size};
7
8/// A text paragraph.
9pub trait Paragraph: Sized + Default {
10 /// Creates a new [`Paragraph`] laid out with the given [`Text`].
11 fn with_text(text: Text<&str>) -> Self;
12
13 /// Creates a new [`Paragraph`] laid out with the given [`Text`].
14 fn with_spans<Link>(text: Text<&[Span<'_, Link>]>) -> Self;
15
16 /// Lays out the [`Paragraph`] with some new boundaries.
17 fn resize(&mut self, new_bounds: Size);
18
19 /// Compares the [`Paragraph`] with some desired [`Text`] and returns the
20 /// [`Difference`].
21 fn compare(&self, text: Text<()>) -> Difference;
22
23 /// Returns the text size of the [`Paragraph`] in [`Pixels`].
24 fn size(&self) -> Pixels;
25
26 /// Returns the hint factor of the [`Paragraph`].
27 fn hint_factor(&self) -> Option<f32>;
28
29 /// Returns the [`Font`] of the [`Paragraph`].
30 fn font(&self) -> Font;
31
32 /// Returns the [`LineHeight`] of the [`Paragraph`].
33 fn line_height(&self) -> LineHeight;
34
35 /// Returns the horizontal alignment of the [`Paragraph`].
36 fn align_x(&self) -> Alignment;
37
38 /// Returns the vertical alignment of the [`Paragraph`].
39 fn align_y(&self) -> alignment::Vertical;
40
41 /// Returns the [`Wrapping`] strategy of the [`Paragraph`]>
42 fn wrapping(&self) -> Wrapping;
43
44 /// Returns the [`Ellipsis`] strategy of the [`Paragraph`]>
45 fn ellipsis(&self) -> Ellipsis;
46
47 /// Returns the [`Shaping`] strategy of the [`Paragraph`]>
48 fn shaping(&self) -> Shaping;
49
50 /// Returns the available bounds used to layout the [`Paragraph`].
51 fn bounds(&self) -> Size;
52
53 /// Returns the minimum boundaries that can fit the contents of the
54 /// [`Paragraph`].
55 fn min_bounds(&self) -> Size;
56
57 /// Tests whether the provided point is within the boundaries of the
58 /// [`Paragraph`], returning information about the nearest character.
59 fn hit_test(&self, point: Point) -> Option<Hit>;
60
61 /// Tests whether the provided point is within the boundaries of a
62 /// [`Span`] in the [`Paragraph`], returning the index of the [`Span`]
63 /// that was hit.
64 fn hit_span(&self, point: Point) -> Option<usize>;
65
66 /// Tests whether the provided point is inside the boundaries of a
67 /// glyph in the [`Paragraph`].
68 fn hit_glyph(&self, point: Point) -> bool {
69 self.hit_test(point).is_some()
70 }
71
72 /// Returns all bounds for the provided [`Span`] index of the [`Paragraph`].
73 /// A [`Span`] can have multiple bounds for each line it's on.
74 fn span_bounds(&self, index: usize) -> Vec<Rectangle>;
75
76 /// Selects the text between the given positions.
77 ///
78 /// The positions are relative to the origin of the [`Paragraph`].
79 /// `start` is the position where the selection began, while `end`
80 /// is its final position. The [`Target`] determines how the
81 /// selection is expanded around these positions.
82 fn select(&mut self, start: Point, end: Point, target: Target);
83
84 /// Selects all the text of the [`Paragraph`].
85 fn select_all(&mut self);
86
87 /// Deselects any selected text.
88 fn deselect(&mut self);
89
90 /// Returns the regions of the current selection of the [`Paragraph`].
91 ///
92 /// Each [`Rectangle`] corresponds to a line of the selection.
93 fn selection(&self) -> &[Rectangle];
94
95 /// Returns the current selected text of the [`Paragraph`].
96 ///
97 // TODO: Make immutable
98 fn copy(&mut self) -> Option<String>;
99
100 /// Returns the minimum width that can fit the contents of the [`Paragraph`].
101 fn min_width(&self) -> f32 {
102 self.min_bounds().width
103 }
104
105 /// Returns the minimum height that can fit the contents of the [`Paragraph`].
106 fn min_height(&self) -> f32 {
107 self.min_bounds().height
108 }
109}
110
111/// A [`Paragraph`] of plain text.
112#[derive(Debug, Clone, Default)]
113pub struct Plain<P: Paragraph> {
114 raw: P,
115 content: String,
116}
117
118impl<P: Paragraph> Plain<P> {
119 /// Creates a new [`Plain`] paragraph.
120 pub fn new(text: Text<String>) -> Self {
121 Self {
122 raw: P::with_text(text.as_ref()),
123 content: text.content,
124 }
125 }
126
127 /// Updates the plain [`Paragraph`] to match the given [`Text`], if needed.
128 ///
129 /// Returns true if the [`Paragraph`] changed.
130 pub fn update(&mut self, text: Text<&str>) -> bool {
131 if self.content != text.content {
132 text.content.clone_into(&mut self.content);
133 self.raw = P::with_text(text);
134 return true;
135 }
136
137 match self.raw.compare(text.with_content(())) {
138 Difference::None => false,
139 Difference::Bounds => {
140 self.raw.resize(text.bounds);
141 true
142 }
143 Difference::Shape => {
144 self.raw = P::with_text(text);
145 true
146 }
147 }
148 }
149
150 /// Returns the horizontal alignment of the [`Paragraph`].
151 pub fn align_x(&self) -> Alignment {
152 self.raw.align_x()
153 }
154
155 /// Returns the vertical alignment of the [`Paragraph`].
156 pub fn align_y(&self) -> alignment::Vertical {
157 self.raw.align_y()
158 }
159
160 /// Returns the minimum boundaries that can fit the contents of the
161 /// [`Paragraph`].
162 pub fn min_bounds(&self) -> Size {
163 self.raw.min_bounds()
164 }
165
166 /// Returns the minimum width that can fit the contents of the
167 /// [`Paragraph`].
168 pub fn min_width(&self) -> f32 {
169 self.raw.min_width()
170 }
171
172 /// Returns the minimum height that can fit the contents of the
173 /// [`Paragraph`].
174 pub fn min_height(&self) -> f32 {
175 self.raw.min_height()
176 }
177
178 /// Returns the cached [`Paragraph`].
179 pub fn raw(&self) -> &P {
180 &self.raw
181 }
182
183 /// Returns the cached [`Paragraph`], mutably.
184 pub fn raw_mut(&mut self) -> &mut P {
185 &mut self.raw
186 }
187
188 /// Returns the current content of the plain [`Paragraph`].
189 pub fn content(&self) -> &str {
190 &self.content
191 }
192
193 /// Returns the [`Paragraph`] as a [`Text`] definition.
194 pub fn as_text(&self) -> Text<&str> {
195 Text {
196 content: &self.content,
197 bounds: self.raw.bounds(),
198 size: self.raw.size(),
199 line_height: self.raw.line_height(),
200 font: self.raw.font(),
201 align_x: self.raw.align_x(),
202 align_y: self.raw.align_y(),
203 shaping: self.raw.shaping(),
204 wrapping: self.raw.wrapping(),
205 ellipsis: self.raw.ellipsis(),
206 hint_factor: self.raw.hint_factor(),
207 }
208 }
209}