Skip to content

Commit

Permalink
Merge pull request #1107 from RamType0/TextContentByCow
Browse files Browse the repository at this point in the history
Text content by cow
  • Loading branch information
hecrj authored Sep 21, 2022
2 parents f15bc3c + ce3b896 commit 7420ea7
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 22 deletions.
6 changes: 3 additions & 3 deletions examples/todos/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,19 +469,19 @@ const ICONS: Font = Font::External {
bytes: include_bytes!("../../todos/fonts/icons.ttf"),
};

fn icon(unicode: char) -> Text {
fn icon(unicode: char) -> Text<'static> {
text(unicode.to_string())
.font(ICONS)
.width(Length::Units(20))
.horizontal_alignment(alignment::Horizontal::Center)
.size(20)
}

fn edit_icon() -> Text {
fn edit_icon() -> Text<'static> {
icon('\u{F303}')
}

fn delete_icon() -> Text {
fn delete_icon() -> Text<'static> {
icon('\u{F1F8}')
}

Expand Down
2 changes: 1 addition & 1 deletion native/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl<'a, Message, Renderer> Element<'a, Message, Renderer> {
///
/// ```
/// # mod counter {
/// # type Text = iced_native::widget::Text<iced_native::renderer::Null>;
/// # type Text<'a> = iced_native::widget::Text<'a, iced_native::renderer::Null>;
/// #
/// # #[derive(Debug, Clone, Copy)]
/// # pub enum Message {}
Expand Down
6 changes: 3 additions & 3 deletions native/src/widget/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,18 @@ where
Renderer: crate::text::Renderer,
Renderer::Theme: widget::container::StyleSheet + widget::text::StyleSheet,
{
widget::Tooltip::new(content, tooltip, position)
widget::Tooltip::new(content, tooltip.to_string(), position)
}

/// Creates a new [`Text`] widget with the provided content.
///
/// [`Text`]: widget::Text
pub fn text<Renderer>(text: impl ToString) -> widget::Text<Renderer>
pub fn text<'a, Renderer>(text: impl ToString) -> widget::Text<'a, Renderer>
where
Renderer: crate::text::Renderer,
Renderer::Theme: widget::text::StyleSheet,
{
widget::Text::new(text)
widget::Text::new(text.to_string())
}

/// Creates a new [`Checkbox`].
Expand Down
22 changes: 12 additions & 10 deletions native/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::text;
use crate::widget::Tree;
use crate::{Element, Layout, Length, Point, Rectangle, Size, Widget};

use std::borrow::Cow;

pub use iced_style::text::{Appearance, StyleSheet};

/// A paragraph of text.
Expand All @@ -15,7 +17,7 @@ pub use iced_style::text::{Appearance, StyleSheet};
/// ```
/// # use iced_native::Color;
/// #
/// # type Text = iced_native::widget::Text<iced_native::renderer::Null>;
/// # type Text<'a> = iced_native::widget::Text<'a, iced_native::renderer::Null>;
/// #
/// Text::new("I <3 iced!")
/// .size(40)
Expand All @@ -24,12 +26,12 @@ pub use iced_style::text::{Appearance, StyleSheet};
///
/// ![Text drawn by `iced_wgpu`](https://github.com/iced-rs/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/text.png?raw=true)
#[allow(missing_debug_implementations)]
pub struct Text<Renderer>
pub struct Text<'a, Renderer>
where
Renderer: text::Renderer,
Renderer::Theme: StyleSheet,
{
content: String,
content: Cow<'a, str>,
size: Option<u16>,
width: Length,
height: Length,
Expand All @@ -39,15 +41,15 @@ where
style: <Renderer::Theme as StyleSheet>::Style,
}

impl<Renderer> Text<Renderer>
impl<'a, Renderer> Text<'a, Renderer>
where
Renderer: text::Renderer,
Renderer::Theme: StyleSheet,
{
/// Create a new fragment of [`Text`] with the given contents.
pub fn new<T: ToString>(label: T) -> Self {
pub fn new(content: impl Into<Cow<'a, str>>) -> Self {
Text {
content: label.to_string(),
content: content.into(),
size: None,
font: Default::default(),
width: Length::Shrink,
Expand Down Expand Up @@ -112,7 +114,7 @@ where
}
}

impl<Message, Renderer> Widget<Message, Renderer> for Text<Renderer>
impl<'a, Message, Renderer> Widget<Message, Renderer> for Text<'a, Renderer>
where
Renderer: text::Renderer,
Renderer::Theme: StyleSheet,
Expand Down Expand Up @@ -216,18 +218,18 @@ pub fn draw<Renderer>(
});
}

impl<'a, Message, Renderer> From<Text<Renderer>>
impl<'a, Message, Renderer> From<Text<'a, Renderer>>
for Element<'a, Message, Renderer>
where
Renderer: text::Renderer + 'a,
Renderer::Theme: StyleSheet,
{
fn from(text: Text<Renderer>) -> Element<'a, Message, Renderer> {
fn from(text: Text<'a, Renderer>) -> Element<'a, Message, Renderer> {
Element::new(text)
}
}

impl<Renderer> Clone for Text<Renderer>
impl<'a, Renderer> Clone for Text<'a, Renderer>
where
Renderer: text::Renderer,
Renderer::Theme: StyleSheet,
Expand Down
8 changes: 5 additions & 3 deletions native/src/widget/tooltip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use crate::{
Shell, Size, Vector, Widget,
};

use std::borrow::Cow;

/// An element to display a widget over another.
#[allow(missing_debug_implementations)]
pub struct Tooltip<'a, Message, Renderer: text::Renderer>
Expand All @@ -21,7 +23,7 @@ where
Renderer::Theme: container::StyleSheet + widget::text::StyleSheet,
{
content: Element<'a, Message, Renderer>,
tooltip: Text<Renderer>,
tooltip: Text<'a, Renderer>,
position: Position,
gap: u16,
padding: u16,
Expand All @@ -42,12 +44,12 @@ where
/// [`Tooltip`]: struct.Tooltip.html
pub fn new(
content: impl Into<Element<'a, Message, Renderer>>,
tooltip: impl ToString,
tooltip: impl Into<Cow<'a, str>>,
position: Position,
) -> Self {
Tooltip {
content: content.into(),
tooltip: Text::new(tooltip.to_string()),
tooltip: Text::new(tooltip),
position,
gap: 0,
padding: Self::DEFAULT_PADDING,
Expand Down
4 changes: 2 additions & 2 deletions src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ pub mod text {
pub use iced_native::widget::text::{Appearance, StyleSheet};

/// A paragraph of text.
pub type Text<Renderer = crate::Renderer> =
iced_native::widget::Text<Renderer>;
pub type Text<'a, Renderer = crate::Renderer> =
iced_native::widget::Text<'a, Renderer>;
}

pub mod button {
Expand Down

0 comments on commit 7420ea7

Please sign in to comment.