summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/src/renderer/null.rs34
-rw-r--r--core/src/text.rs90
-rw-r--r--core/src/widget/text.rs23
3 files changed, 37 insertions, 110 deletions
diff --git a/core/src/renderer/null.rs b/core/src/renderer/null.rs
index 55d58a59..0ffd3649 100644
--- a/core/src/renderer/null.rs
+++ b/core/src/renderer/null.rs
@@ -58,16 +58,6 @@ impl text::Renderer for Null {
fn load_font(&mut self, _font: Cow<'static, [u8]>) {}
- fn create_paragraph(&self, _text: Text<'_, Self::Font>) -> Self::Paragraph {
- }
-
- fn resize_paragraph(
- &self,
- _paragraph: &mut Self::Paragraph,
- _new_bounds: Size,
- ) {
- }
-
fn fill_paragraph(
&mut self,
_paragraph: &Self::Paragraph,
@@ -88,24 +78,12 @@ impl text::Renderer for Null {
impl text::Paragraph for () {
type Font = Font;
- fn content(&self) -> &str {
- ""
- }
-
- fn text_size(&self) -> Pixels {
- Pixels(16.0)
- }
-
- fn font(&self) -> Self::Font {
- Font::default()
- }
+ fn with_text(_text: Text<'_, Self::Font>) -> Self {}
- fn line_height(&self) -> text::LineHeight {
- text::LineHeight::default()
- }
+ fn resize(&mut self, _new_bounds: Size) {}
- fn shaping(&self) -> text::Shaping {
- text::Shaping::default()
+ fn compare(&self, _text: Text<'_, Self::Font>) -> text::Difference {
+ text::Difference::None
}
fn horizontal_alignment(&self) -> alignment::Horizontal {
@@ -120,10 +98,6 @@ impl text::Paragraph for () {
None
}
- fn bounds(&self) -> Size {
- Size::ZERO
- }
-
fn min_bounds(&self) -> Size {
Size::ZERO
}
diff --git a/core/src/text.rs b/core/src/text.rs
index 0e3617b1..ff85696e 100644
--- a/core/src/text.rs
+++ b/core/src/text.rs
@@ -156,33 +156,6 @@ pub trait Renderer: crate::Renderer {
/// Loads a [`Self::Font`] from its bytes.
fn load_font(&mut self, font: Cow<'static, [u8]>);
- /// Creates a new [`Paragraph`] laid out with the given [`Text`].
- fn create_paragraph(&self, text: Text<'_, Self::Font>) -> Self::Paragraph;
-
- /// Lays out the given [`Paragraph`] with some new boundaries.
- fn resize_paragraph(
- &self,
- paragraph: &mut Self::Paragraph,
- new_bounds: Size,
- );
-
- /// Updates a [`Paragraph`] to match the given [`Text`], if needed.
- fn update_paragraph(
- &self,
- paragraph: &mut Self::Paragraph,
- text: Text<'_, Self::Font>,
- ) {
- match compare(paragraph, text) {
- Difference::None => {}
- Difference::Bounds => {
- self.resize_paragraph(paragraph, text.bounds);
- }
- Difference::Shape => {
- *paragraph = self.create_paragraph(text);
- }
- }
- }
-
/// Draws the given [`Paragraph`] at the given position and with the given
/// [`Color`].
fn fill_paragraph(
@@ -201,25 +174,21 @@ pub trait Renderer: crate::Renderer {
color: Color,
);
}
+
/// A text paragraph.
-pub trait Paragraph: Default {
+pub trait Paragraph: Sized + Default {
/// The font of this [`Paragraph`].
- type Font;
-
- /// Returns the content of the [`Paragraph`].
- fn content(&self) -> &str;
-
- /// Returns the text size of the [`Paragraph`].
- fn text_size(&self) -> Pixels;
+ type Font: Copy + PartialEq;
- /// Returns the [`LineHeight`] of the [`Paragraph`].
- fn line_height(&self) -> LineHeight;
+ /// Creates a new [`Paragraph`] laid out with the given [`Text`].
+ fn with_text(text: Text<'_, Self::Font>) -> Self;
- /// Returns the [`Self::Font`] of the [`Paragraph`].
- fn font(&self) -> Self::Font;
+ /// Lays out the [`Paragraph`] with some new boundaries.
+ fn resize(&mut self, new_bounds: Size);
- /// Returns the [`Shaping`] strategy of the [`Paragraph`].
- fn shaping(&self) -> Shaping;
+ /// Compares the [`Paragraph`] with some desired [`Text`] and returns the
+ /// [`Difference`].
+ fn compare(&self, text: Text<'_, Self::Font>) -> Difference;
/// Returns the horizontal alignment of the [`Paragraph`].
fn horizontal_alignment(&self) -> alignment::Horizontal;
@@ -227,9 +196,6 @@ pub trait Paragraph: Default {
/// Returns the vertical alignment of the [`Paragraph`].
fn vertical_alignment(&self) -> alignment::Vertical;
- /// Returns the boundaries of the [`Paragraph`].
- fn bounds(&self) -> Size;
-
/// Returns the minimum boundaries that can fit the contents of the
/// [`Paragraph`].
fn min_bounds(&self) -> Size;
@@ -241,6 +207,19 @@ pub trait Paragraph: Default {
/// Returns the distance to the given grapheme index in the [`Paragraph`].
fn grapheme_position(&self, line: usize, index: usize) -> Option<Point>;
+ /// Updates the [`Paragraph`] to match the given [`Text`], if needed.
+ fn update(&mut self, text: Text<'_, Self::Font>) {
+ match self.compare(text) {
+ Difference::None => {}
+ Difference::Bounds => {
+ self.resize(text.bounds);
+ }
+ Difference::Shape => {
+ *self = Self::with_text(text);
+ }
+ }
+ }
+
/// Returns the minimum width that can fit the contents of the [`Paragraph`].
fn min_width(&self) -> f32 {
self.min_bounds().width
@@ -276,26 +255,3 @@ pub enum Difference {
/// the text is necessary.
Shape,
}
-
-/// Compares a [`Paragraph`] with some desired [`Text`] and returns the
-/// [`Difference`].
-pub fn compare<Font: PartialEq>(
- paragraph: &impl Paragraph<Font = Font>,
- text: Text<'_, Font>,
-) -> Difference {
- if paragraph.content() != text.content
- || paragraph.text_size() != text.size
- || paragraph.line_height().to_absolute(text.size)
- != text.line_height.to_absolute(text.size)
- || paragraph.font() != text.font
- || paragraph.shaping() != text.shaping
- || paragraph.horizontal_alignment() != text.horizontal_alignment
- || paragraph.vertical_alignment() != text.vertical_alignment
- {
- Difference::Shape
- } else if paragraph.bounds() != text.bounds {
- Difference::Bounds
- } else {
- Difference::None
- }
-}
diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs
index 53ed463e..c7c9f539 100644
--- a/core/src/widget/text.rs
+++ b/core/src/widget/text.rs
@@ -212,19 +212,16 @@ where
let State(ref mut paragraph) = state;
- renderer.update_paragraph(
- paragraph,
- text::Text {
- content,
- bounds,
- size,
- line_height,
- font,
- shaping,
- horizontal_alignment,
- vertical_alignment,
- },
- );
+ paragraph.update(text::Text {
+ content,
+ bounds,
+ size,
+ line_height,
+ font,
+ shaping,
+ horizontal_alignment,
+ vertical_alignment,
+ });
let size = limits.resolve(paragraph.min_bounds());