summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-04-01 21:36:08 +0200
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-04-01 21:36:08 +0200
commit1d83e59e8ab51d403baee0daa878644c6a37be3f (patch)
treed081c5d1de46209ef00e4bea7ddb303cc646c5fd
parentc30b4b0a1c9b191bcb65ae1732581b5c96b8bcfe (diff)
downloadiced-1d83e59e8ab51d403baee0daa878644c6a37be3f.tar.gz
iced-1d83e59e8ab51d403baee0daa878644c6a37be3f.tar.bz2
iced-1d83e59e8ab51d403baee0daa878644c6a37be3f.zip
Specialize `widget::text` helper with custom `IntoContent` trait
-rw-r--r--core/src/widget/text.rs70
-rw-r--r--examples/lazy/src/main.rs2
-rw-r--r--examples/tour/src/main.rs6
-rw-r--r--examples/websocket/src/main.rs6
-rw-r--r--widget/src/helpers.rs4
5 files changed, 78 insertions, 10 deletions
diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs
index 12f6956a..e05eb0d9 100644
--- a/core/src/widget/text.rs
+++ b/core/src/widget/text.rs
@@ -21,7 +21,7 @@ where
Theme: Catalog,
Renderer: text::Renderer,
{
- content: Cow<'a, str>,
+ content: Content<'a>,
size: Option<Pixels>,
line_height: LineHeight,
width: Length,
@@ -39,9 +39,9 @@ where
Renderer: text::Renderer,
{
/// Create a new fragment of [`Text`] with the given contents.
- pub fn new(content: impl Into<Cow<'a, str>>) -> Self {
+ pub fn new(content: impl IntoContent<'a>) -> Self {
Text {
- content: content.into(),
+ content: content.into_content(),
size: None,
line_height: LineHeight::default(),
font: None,
@@ -366,3 +366,67 @@ impl Catalog for Theme {
class(self)
}
}
+
+/// The content of a [`Text`] widget.
+///
+/// This is just an alias to a string that may be either
+/// borrowed or owned.
+pub type Content<'a> = Cow<'a, str>;
+
+/// A trait for converting a value to some text [`Content`].
+pub trait IntoContent<'a> {
+ /// Converts the value to some text [`Content`].
+ fn into_content(self) -> Content<'a>;
+}
+
+impl<'a> IntoContent<'a> for &'a str {
+ fn into_content(self) -> Content<'a> {
+ Content::Borrowed(self)
+ }
+}
+
+impl<'a> IntoContent<'a> for &'a String {
+ fn into_content(self) -> Content<'a> {
+ Content::Borrowed(self.as_str())
+ }
+}
+
+impl<'a> IntoContent<'a> for String {
+ fn into_content(self) -> Content<'a> {
+ Content::Owned(self)
+ }
+}
+
+macro_rules! into_content {
+ ($type:ty) => {
+ impl<'a> IntoContent<'a> for $type {
+ fn into_content(self) -> Content<'a> {
+ Content::Owned(self.to_string())
+ }
+ }
+
+ impl<'a> IntoContent<'a> for &$type {
+ fn into_content(self) -> Content<'a> {
+ Content::Owned(self.to_string())
+ }
+ }
+ };
+}
+
+into_content!(char);
+into_content!(bool);
+
+into_content!(u8);
+into_content!(u16);
+into_content!(u32);
+into_content!(u64);
+into_content!(u128);
+
+into_content!(i8);
+into_content!(i16);
+into_content!(i32);
+into_content!(i64);
+into_content!(i128);
+
+into_content!(f32);
+into_content!(f64);
diff --git a/examples/lazy/src/main.rs b/examples/lazy/src/main.rs
index 2d53df93..c3f6b8de 100644
--- a/examples/lazy/src/main.rs
+++ b/examples/lazy/src/main.rs
@@ -173,7 +173,7 @@ impl App {
.style(button::danger);
row![
- text(&item.name).color(item.color),
+ text(item.name.clone()).color(item.color),
horizontal_space(),
pick_list(Color::ALL, Some(item.color), move |color| {
Message::ItemColorChanged(item.clone(), color)
diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs
index a88c0dba..e3a2ca87 100644
--- a/examples/tour/src/main.rs
+++ b/examples/tour/src/main.rs
@@ -357,7 +357,7 @@ impl<'a> Step {
.into()
}
- fn container(title: &str) -> Column<'a, StepMessage> {
+ fn container(title: &str) -> Column<'_, StepMessage> {
column![text(title).size(50)].spacing(20)
}
@@ -589,7 +589,7 @@ impl<'a> Step {
value: &str,
is_secure: bool,
is_showing_icon: bool,
- ) -> Column<'a, StepMessage> {
+ ) -> Column<'_, StepMessage> {
let mut text_input = text_input("Type something to continue...", value)
.on_input(StepMessage::InputChanged)
.padding(10)
@@ -674,7 +674,7 @@ fn ferris<'a>(
.center_x()
}
-fn padded_button<'a, Message: Clone>(label: &str) -> Button<'a, Message> {
+fn padded_button<Message: Clone>(label: &str) -> Button<'_, Message> {
button(text(label)).padding([12, 24])
}
diff --git a/examples/websocket/src/main.rs b/examples/websocket/src/main.rs
index 460d9a08..ef450524 100644
--- a/examples/websocket/src/main.rs
+++ b/examples/websocket/src/main.rs
@@ -97,7 +97,11 @@ impl WebSocket {
} else {
scrollable(
column(
- self.messages.iter().cloned().map(text).map(Element::from),
+ self.messages
+ .iter()
+ .map(ToString::to_string)
+ .map(text)
+ .map(Element::from),
)
.spacing(10),
)
diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs
index 77b30882..deff0261 100644
--- a/widget/src/helpers.rs
+++ b/widget/src/helpers.rs
@@ -145,13 +145,13 @@ where
///
/// [`Text`]: core::widget::Text
pub fn text<'a, Theme, Renderer>(
- text: impl ToString,
+ text: impl text::IntoContent<'a>,
) -> Text<'a, Theme, Renderer>
where
Theme: text::Catalog + 'a,
Renderer: core::text::Renderer,
{
- Text::new(text.to_string())
+ Text::new(text)
}
/// Creates a new [`Checkbox`].