Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specialize widget::text helper #2363

Merged
merged 5 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 70 additions & 4 deletions core/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ where
Theme: Catalog,
Renderer: text::Renderer,
{
content: Cow<'a, str>,
fragment: Fragment<'a>,
size: Option<Pixels>,
line_height: LineHeight,
width: Length,
Expand All @@ -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(fragment: impl IntoFragment<'a>) -> Self {
Text {
content: content.into(),
fragment: fragment.into_fragment(),
size: None,
line_height: LineHeight::default(),
font: None,
Expand Down Expand Up @@ -184,7 +184,7 @@ where
limits,
self.width,
self.height,
&self.content,
&self.fragment,
self.line_height,
self.size,
self.font,
Expand Down Expand Up @@ -366,3 +366,69 @@ impl Catalog for Theme {
class(self)
}
}

/// A fragment of [`Text`].
///
/// This is just an alias to a string that may be either
/// borrowed or owned.
pub type Fragment<'a> = Cow<'a, str>;

/// A trait for converting a value to some text [`Fragment`].
pub trait IntoFragment<'a> {
/// Converts the value to some text [`Fragment`].
fn into_fragment(self) -> Fragment<'a>;
}

impl<'a> IntoFragment<'a> for &'a str {
fn into_fragment(self) -> Fragment<'a> {
Fragment::Borrowed(self)
}
}

impl<'a> IntoFragment<'a> for &'a String {
fn into_fragment(self) -> Fragment<'a> {
Fragment::Borrowed(self.as_str())
}
}

impl<'a> IntoFragment<'a> for String {
fn into_fragment(self) -> Fragment<'a> {
Fragment::Owned(self)
}
}

macro_rules! into_fragment {
($type:ty) => {
impl<'a> IntoFragment<'a> for $type {
fn into_fragment(self) -> Fragment<'a> {
Fragment::Owned(self.to_string())
}
}

impl<'a> IntoFragment<'a> for &$type {
fn into_fragment(self) -> Fragment<'a> {
Fragment::Owned(self.to_string())
}
}
};
}

into_fragment!(char);
into_fragment!(bool);

into_fragment!(u8);
into_fragment!(u16);
into_fragment!(u32);
into_fragment!(u64);
into_fragment!(u128);
into_fragment!(usize);

into_fragment!(i8);
into_fragment!(i16);
into_fragment!(i32);
into_fragment!(i64);
into_fragment!(i128);
into_fragment!(isize);

into_fragment!(f32);
into_fragment!(f64);
2 changes: 1 addition & 1 deletion examples/lazy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions examples/tour/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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])
}

Expand Down
23 changes: 16 additions & 7 deletions examples/websocket/src/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod server;

use iced::futures;
use iced::subscription::{self, Subscription};
use iced::widget::text;

use futures::channel::mpsc;
use futures::sink::SinkExt;
Expand Down Expand Up @@ -136,16 +137,24 @@ impl Message {
pub fn disconnected() -> Self {
Message::Disconnected
}

pub fn as_str(&self) -> &str {
match self {
Message::Connected => "Connected successfully!",
Message::Disconnected => "Connection lost... Retrying...",
Message::User(message) => message.as_str(),
}
}
}

impl fmt::Display for Message {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Message::Connected => write!(f, "Connected successfully!"),
Message::Disconnected => {
write!(f, "Connection lost... Retrying...")
}
Message::User(message) => write!(f, "{message}"),
}
f.write_str(self.as_str())
}
}

impl<'a> text::IntoFragment<'a> for &'a Message {
fn into_fragment(self) -> text::Fragment<'a> {
text::Fragment::Borrowed(self.as_str())
}
}
6 changes: 2 additions & 4 deletions examples/websocket/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,8 @@ impl WebSocket {
.into()
} else {
scrollable(
column(
self.messages.iter().cloned().map(text).map(Element::from),
)
.spacing(10),
column(self.messages.iter().map(text).map(Element::from))
.spacing(10),
)
.id(MESSAGE_LOG.clone())
.height(Length::Fill)
Expand Down
17 changes: 15 additions & 2 deletions widget/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,26 @@ where
///
/// [`Text`]: core::widget::Text
pub fn text<'a, Theme, Renderer>(
text: impl ToString,
text: impl text::IntoFragment<'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 [`Text`] widget that displays the provided value.
///
/// [`Text`]: core::widget::Text
pub fn value<'a, Theme, Renderer>(
value: impl ToString,
) -> Text<'a, Theme, Renderer>
where
Theme: text::Catalog + 'a,
Renderer: core::text::Renderer,
{
Text::new(value.to_string())
}

/// Creates a new [`Checkbox`].
Expand Down
Loading