diff options
author | 2024-05-23 13:38:22 +0200 | |
---|---|---|
committer | 2024-05-23 13:38:22 +0200 | |
commit | 663a081bdd507da0b5ca7d4dc30d78a20b4618af (patch) | |
tree | 89482c8d1e3a03e00b3a8151abbb81e30ae5898c /widget | |
parent | 468794d918eb06c1dbebb33c32b10017ad335f05 (diff) | |
parent | d8ba6b0673a33724a177f3a1ba59705527280142 (diff) | |
download | iced-663a081bdd507da0b5ca7d4dc30d78a20b4618af.tar.gz iced-663a081bdd507da0b5ca7d4dc30d78a20b4618af.tar.bz2 iced-663a081bdd507da0b5ca7d4dc30d78a20b4618af.zip |
Merge pull request #2338 from derezzedex/feat/text-macro
Introduce `text` macro
Diffstat (limited to '')
-rw-r--r-- | widget/src/helpers.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index a1ecd9d1..016bafbb 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -65,6 +65,52 @@ macro_rules! stack { ); } +/// Creates a new [`Text`] widget with the provided content. +/// +/// [`Text`]: core::widget::Text +/// +/// This macro uses the same syntax as [`format!`], but creates a new [`Text`] widget instead. +/// +/// See [the formatting documentation in `std::fmt`](std::fmt) +/// for details of the macro argument syntax. +/// +/// # Examples +/// +/// ```no_run +/// # mod iced { +/// # pub struct Element<Message>(pub std::marker::PhantomData<Message>); +/// # pub mod widget { +/// # macro_rules! text { +/// # ($($arg:tt)*) => {unimplemented!()} +/// # } +/// # pub(crate) use text; +/// # } +/// # } +/// # struct Example; +/// # enum Message {} +/// use iced::Element; +/// use iced::widget::text; +/// +/// impl Example { +/// fn view(&self) -> Element<Message> { +/// let simple = text!("Hello, world!"); +/// +/// let keyword = text!("Hello, {}", "world!"); +/// +/// let planet = "Earth"; +/// let local_variable = text!("Hello, {planet}!"); +/// // ... +/// # iced::Element(std::marker::PhantomData) +/// } +/// } +/// ``` +#[macro_export] +macro_rules! text { + ($($arg:tt)*) => { + $crate::Text::new(format!($($arg)*)) + }; +} + /// Creates a new [`Container`] with the provided content. /// /// [`Container`]: crate::Container |