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

write! for RichTextBuilder #1596

Merged
merged 6 commits into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ You can find its changes [documented below](#070---2021-01-01).
### Highlights

### Added
- `write!` for `RichTextBuilder` ([#1596] by [@Maan2003])
- Sub windows: Allow opening windows that share state with arbitrary parts of the widget hierarchy ([#1254] by [@rjwittams])
- WindowCloseRequested/WindowDisconnected event when a window is closing ([#1254] by [@rjwittams])
- RichTextBuilder ([#1520] by [@Maan2003])
Expand Down Expand Up @@ -614,6 +615,7 @@ Last release without a changelog :(
[#1254]: https://github.com/linebender/druid/pull/1254
[#1559]: https://github.com/linebender/druid/pull/1559
[#1562]: https://github.com/linebender/druid/pull/1562
[#1596]: https://github.com/linebender/druid/pull/1596

[Unreleased]: https://github.com/linebender/druid/compare/v0.6.0...master
[0.6.0]: https://github.com/linebender/druid/compare/v0.5.0...v0.6.0
Expand Down
17 changes: 16 additions & 1 deletion druid/src/text/rich_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,14 @@ impl TextStorage for RichText {
/// # Example
/// ```
/// # use druid::text::{Attribute, RichTextBuilder};
/// # use druid::FontWeight;
/// # use druid::{FontWeight, Color};
/// let mut builder = RichTextBuilder::new();
/// builder.push("Hello ");
/// builder.push("World!").weight(FontWeight::BOLD);
///
/// // Can also use write!
/// write!(builder, "Here is your number: {}", 1).underline(true).text_color(Color::RED);
///
/// let rich_text = builder.build();
/// ```
///
Expand All @@ -131,6 +135,17 @@ impl RichTextBuilder {
self.add_attributes_for_range(range)
}

/// Glue for usage of the write! macro.
///
/// This method should generally not be invoked manually, but rather through the write! macro itself.
pub fn write_fmt(&mut self, fmt: std::fmt::Arguments<'_>) -> AttributesAdder {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add #[doc(hidden)] on this, since it's really an implementation detail and doesn't need to show up in public docs?

Hmm, should we be implementing the Write trait directly? Is this depending on an implementation detail of the macro, where it calls write_fmt with no qualifiers, instead of doing like Write::write_fmt(writer, args)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I didn't implement Write because it returns a Result, I wanted it to feel more like format!.

Is this depending on an implementation detail of the macro?

the docs say it just needs something with write_fmt method.

The writer may be any value with a write_fmt method; generally this comes from an implementation of either the fmt::Write or the io::Write trait.

use std::fmt::Write;
let start = self.buffer.len();
self.buffer
.write_fmt(fmt)
.expect("a formatting trait implementation returned an error");
self.add_attributes_for_range(start..self.buffer.len())
}
/// Get an [`AttributesAdder`] for the given range.
///
/// This can be used to modify styles for a given range after it has been added.
Expand Down