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

Introduce text macro #2338

Merged
merged 7 commits into from
May 23, 2024
Merged

Introduce text macro #2338

merged 7 commits into from
May 23, 2024

Conversation

derezzedex
Copy link
Member

@derezzedex derezzedex commented Mar 18, 2024

This PR introduces a text macro in the widget::helpers module analogous to the std::format macro.

Implementation

This is a simple macro similar to the row and column helper macros, it uses format internally.
The macro_rules definition:

#[macro_export]
macro_rules! text {
    ($($arg:tt)*) => {
        $crate::Text::new(format!($($arg)*))
    };
}

Usage

Since it uses the std::format macro internally, it's usage is analogous and should be familiar to most users, but some examples are documented in the macro definition:

use iced::Element;
use iced::widget::text;

fn view(&self) -> Element<Message> {
    let simple = text!("Hello, world!");

    let keyword = text!("Hello, {}", "world!");

    let planet = "Earth";
    let local_variable = text!("Hello, {planet}!");
    // ...
}

As a result, the text(format!( pattern has been replaced with the new text macro throughout the examples.

@derezzedex derezzedex marked this pull request as ready for review March 18, 2024 23:36
@romancitodev
Copy link

It would be nice to be able to receive as a second parameter (optional) a custom style.

@hecrj hecrj added this to the 0.13 milestone Mar 20, 2024
@derezzedex
Copy link
Member Author

It would be nice to be able to receive as a second parameter (optional) a custom style.

I don't think this would help much, have you seen #2312?

I would say that .style() or .color() express the styling intent much better than a second parameter on this macro.

@romancitodev
Copy link

I would say that .style() or .color() express the styling intent much better than a second parameter on this macro.

Well, that's make more sense!

Copy link
Member

@hecrj hecrj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thanks 🙇

@hecrj hecrj enabled auto-merge May 23, 2024 11:31
@hecrj hecrj merged commit 663a081 into iced-rs:master May 23, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants