From 910eb72a0620b34e5b3d7793bbd5ab7290e08dd6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 17 Jul 2024 22:04:11 +0200 Subject: Implement `rich_text` widget and `markdown` example --- widget/src/helpers.rs | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) (limited to 'widget/src/helpers.rs') diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 1f282f54..66b37ccb 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -112,6 +112,19 @@ macro_rules! text { }; } +/// Creates some [`Rich`] text with the given spans. +/// +/// [`Rich`]: text::Rich +#[macro_export] +macro_rules! rich_text { + () => ( + $crate::Column::new() + ); + ($($x:expr),+ $(,)?) => ( + $crate::text::Rich::with_spans([$($crate::text::Span::from($x)),+]) + ); +} + /// Creates a new [`Container`] with the provided content. /// /// [`Container`]: crate::Container @@ -646,8 +659,6 @@ where } /// Creates a new [`Text`] widget with the provided content. -/// -/// [`Text`]: core::widget::Text pub fn text<'a, Theme, Renderer>( text: impl text::IntoFragment<'a>, ) -> Text<'a, Theme, Renderer> @@ -659,8 +670,6 @@ where } /// Creates a new [`Text`] widget that displays the provided value. -/// -/// [`Text`]: core::widget::Text pub fn value<'a, Theme, Renderer>( value: impl ToString, ) -> Text<'a, Theme, Renderer> @@ -671,6 +680,28 @@ where Text::new(value.to_string()) } +/// Creates a new [`Rich`] text widget with the provided spans. +/// +/// [`Rich`]: text::Rich +pub fn rich_text<'a, Theme, Renderer>( + spans: impl IntoIterator>, +) -> text::Rich<'a, Theme, Renderer> +where + Theme: text::Catalog + 'a, + Renderer: core::text::Renderer, +{ + text::Rich::with_spans(spans) +} + +/// Creates a new [`Span`] of text with the provided content. +/// +/// [`Span`]: text::Span +pub fn span<'a, Font>( + text: impl text::IntoFragment<'a>, +) -> text::Span<'a, Font> { + text::Span::new(text) +} + /// Creates a new [`Checkbox`]. /// /// [`Checkbox`]: crate::Checkbox -- cgit From 904704d7c1b006c850654dcf3bf9e856e23cb317 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 18 Jul 2024 13:14:56 +0200 Subject: Flesh out the `markdown` example a bit more --- widget/src/helpers.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'widget/src/helpers.rs') diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 66b37ccb..0390079f 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -24,7 +24,7 @@ use crate::tooltip::{self, Tooltip}; use crate::vertical_slider::{self, VerticalSlider}; use crate::{Column, MouseArea, Row, Space, Stack, Themer}; -use std::borrow::Borrow; +use std::borrow::{Borrow, Cow}; use std::ops::RangeInclusive; /// Creates a [`Column`] with the given children. @@ -684,7 +684,7 @@ where /// /// [`Rich`]: text::Rich pub fn rich_text<'a, Theme, Renderer>( - spans: impl IntoIterator>, + spans: impl Into]>>, ) -> text::Rich<'a, Theme, Renderer> where Theme: text::Catalog + 'a, -- cgit From 47b7a36f36b99e346909390621b04f6691ff46d4 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 18 Jul 2024 14:34:00 +0200 Subject: Create `markdown` widget helpers in `iced_widget` --- widget/src/helpers.rs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'widget/src/helpers.rs') diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 0390079f..43fee845 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -7,6 +7,7 @@ use crate::core; use crate::core::widget::operation; use crate::core::{Element, Length, Pixels, Widget}; use crate::keyed; +use crate::markdown::{self}; use crate::overlay; use crate::pick_list::{self, PickList}; use crate::progress_bar::{self, ProgressBar}; @@ -702,6 +703,10 @@ pub fn span<'a, Font>( text::Span::new(text) } +#[cfg(feature = "markdown")] +#[doc(inline)] +pub use markdown::view as markdown; + /// Creates a new [`Checkbox`]. /// /// [`Checkbox`]: crate::Checkbox -- cgit From 06dc507beba311f0862a0619285dc3d97348fd65 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 18 Jul 2024 14:54:26 +0200 Subject: Fix `markdown` import in `iced_widget` --- widget/src/helpers.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'widget/src/helpers.rs') diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 43fee845..aa9394cb 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -7,7 +7,6 @@ use crate::core; use crate::core::widget::operation; use crate::core::{Element, Length, Pixels, Widget}; use crate::keyed; -use crate::markdown::{self}; use crate::overlay; use crate::pick_list::{self, PickList}; use crate::progress_bar::{self, ProgressBar}; @@ -705,7 +704,7 @@ pub fn span<'a, Font>( #[cfg(feature = "markdown")] #[doc(inline)] -pub use markdown::view as markdown; +pub use crate::markdown::view as markdown; /// Creates a new [`Checkbox`]. /// -- cgit