summaryrefslogtreecommitdiffstats
path: root/core/src/text
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector@hecrj.dev>2024-07-18 22:39:49 +0200
committerLibravatar GitHub <noreply@github.com>2024-07-18 22:39:49 +0200
commit23ad15391c88f562c90f4344d3949f76b6f9caf9 (patch)
tree883f5752e3cfe516ee22048015e9255b502bb04e /core/src/text
parent616689ca54942a13aac3615e571ae995ad4571b6 (diff)
parent06acb740fba1889c6a9fb48dfa3ae3aaac1df3ab (diff)
downloadiced-23ad15391c88f562c90f4344d3949f76b6f9caf9.tar.gz
iced-23ad15391c88f562c90f4344d3949f76b6f9caf9.tar.bz2
iced-23ad15391c88f562c90f4344d3949f76b6f9caf9.zip
Merge pull request #2508 from iced-rs/feature/rich-text
`rich_text` and `markdown` widgets
Diffstat (limited to 'core/src/text')
-rw-r--r--core/src/text/paragraph.rs91
1 files changed, 78 insertions, 13 deletions
diff --git a/core/src/text/paragraph.rs b/core/src/text/paragraph.rs
index 8ff04015..4ee83798 100644
--- a/core/src/text/paragraph.rs
+++ b/core/src/text/paragraph.rs
@@ -1,5 +1,6 @@
+//! Draw paragraphs.
use crate::alignment;
-use crate::text::{Difference, Hit, Text};
+use crate::text::{Difference, Hit, Span, Text};
use crate::{Point, Size};
/// A text paragraph.
@@ -10,12 +11,15 @@ pub trait Paragraph: Sized + Default {
/// Creates a new [`Paragraph`] laid out with the given [`Text`].
fn with_text(text: Text<&str, Self::Font>) -> Self;
+ /// Creates a new [`Paragraph`] laid out with the given [`Text`].
+ fn with_spans(text: Text<&[Span<'_, Self::Font>], Self::Font>) -> Self;
+
/// Lays out the [`Paragraph`] with some new boundaries.
fn resize(&mut self, new_bounds: Size);
/// Compares the [`Paragraph`] with some desired [`Text`] and returns the
/// [`Difference`].
- fn compare(&self, text: Text<&str, Self::Font>) -> Difference;
+ fn compare(&self, text: Text<(), Self::Font>) -> Difference;
/// Returns the horizontal alignment of the [`Paragraph`].
fn horizontal_alignment(&self) -> alignment::Horizontal;
@@ -34,26 +38,87 @@ pub trait Paragraph: Sized + 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<&str, Self::Font>) {
- match self.compare(text) {
+ /// Returns the minimum width that can fit the contents of the [`Paragraph`].
+ fn min_width(&self) -> f32 {
+ self.min_bounds().width
+ }
+
+ /// Returns the minimum height that can fit the contents of the [`Paragraph`].
+ fn min_height(&self) -> f32 {
+ self.min_bounds().height
+ }
+}
+
+/// A [`Paragraph`] of plain text.
+#[derive(Debug, Clone, Default)]
+pub struct Plain<P: Paragraph> {
+ raw: P,
+ content: String,
+}
+
+impl<P: Paragraph> Plain<P> {
+ /// Creates a new [`Plain`] paragraph.
+ pub fn new(text: Text<&str, P::Font>) -> Self {
+ let content = text.content.to_owned();
+
+ Self {
+ raw: P::with_text(text),
+ content,
+ }
+ }
+
+ /// Updates the plain [`Paragraph`] to match the given [`Text`], if needed.
+ pub fn update(&mut self, text: Text<&str, P::Font>) {
+ if self.content != text.content {
+ text.content.clone_into(&mut self.content);
+ self.raw = P::with_text(text);
+ return;
+ }
+
+ match self.raw.compare(Text {
+ content: (),
+ bounds: text.bounds,
+ size: text.size,
+ line_height: text.line_height,
+ font: text.font,
+ horizontal_alignment: text.horizontal_alignment,
+ vertical_alignment: text.vertical_alignment,
+ shaping: text.shaping,
+ }) {
Difference::None => {}
Difference::Bounds => {
- self.resize(text.bounds);
+ self.raw.resize(text.bounds);
}
Difference::Shape => {
- *self = Self::with_text(text);
+ self.raw = P::with_text(text);
}
}
}
- /// Returns the minimum width that can fit the contents of the [`Paragraph`].
- fn min_width(&self) -> f32 {
- self.min_bounds().width
+ /// Returns the horizontal alignment of the [`Paragraph`].
+ pub fn horizontal_alignment(&self) -> alignment::Horizontal {
+ self.raw.horizontal_alignment()
}
- /// Returns the minimum height that can fit the contents of the [`Paragraph`].
- fn min_height(&self) -> f32 {
- self.min_bounds().height
+ /// Returns the vertical alignment of the [`Paragraph`].
+ pub fn vertical_alignment(&self) -> alignment::Vertical {
+ self.raw.vertical_alignment()
+ }
+
+ /// Returns the minimum boundaries that can fit the contents of the
+ /// [`Paragraph`].
+ pub fn min_bounds(&self) -> Size {
+ self.raw.min_bounds()
+ }
+
+ /// Returns the minimum width that can fit the contents of the
+ /// [`Paragraph`].
+ pub fn min_width(&self) -> f32 {
+ self.raw.min_width()
+ }
+
+ /// Returns the cached [`Paragraph`].
+ pub fn raw(&self) -> &P {
+ &self.raw
}
}