Skip to content

Commit

Permalink
write! for RichTextBuilder (linebender#1596)
Browse files Browse the repository at this point in the history
* write! for RichTextBuilder

* panic on formatting error

* Fix doc test

* Make write_fmt doc hidden

Co-authored-by: Colin Rofls <[email protected]>
  • Loading branch information
2 people authored and richard-uk1 committed Apr 6, 2021
1 parent 9392f9e commit 55be52b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
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 @@ -617,6 +618,7 @@ Last release without a changelog :(
[#1559]: https://github.com/linebender/druid/pull/1559
[#1562]: https://github.com/linebender/druid/pull/1562
[#1592]: https://github.com/linebender/druid/pull/1592
[#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
18 changes: 17 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,18 @@ 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.
#[doc(hidden)]
pub fn write_fmt(&mut self, fmt: std::fmt::Arguments<'_>) -> AttributesAdder {
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

0 comments on commit 55be52b

Please sign in to comment.