diff --git a/AUTHORS b/AUTHORS index b7c9b3d08c..b78e737b01 100644 --- a/AUTHORS +++ b/AUTHORS @@ -18,3 +18,4 @@ Maximilian Köstler Bruno Dupuis Christopher Noel Hesse Marcin Zając +Laura Gallo diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f5e91d01a..ff850b1043 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ You can find its changes [documented below](#070---2021-01-01). ### Added +- Strikethrough rich text attribute ([#1953] by [@jenra-uwu]) - System fonts loaded so that SVG images render text ([#1850] by [@DrGabble]) - Add `scroll()` method in WidgetExt ([#1600] by [@totsteps]) - `write!` for `RichTextBuilder` ([#1596] by [@Maan2003]) @@ -516,6 +517,7 @@ Last release without a changelog :( [@bjorn]: https://github.com/bjorn [@DrGabble]: https://github.com/DrGabble [@lisael]: https://github.com/lisael +[@jenra-uwu]: https://github.com/jenra-uwu [#599]: https://github.com/linebender/druid/pull/599 [#611]: https://github.com/linebender/druid/pull/611 @@ -789,6 +791,7 @@ Last release without a changelog :( [#1929]: https://github.com/linebender/druid/pull/1929 [#1947]: https://github.com/linebender/druid/pull/1947 [#1967]: https://github.com/linebender/druid/pull/1967 +[#1953]: https://github.com/linebender/druid/pull/1953 [Unreleased]: https://github.com/linebender/druid/compare/v0.7.0...master [0.7.0]: https://github.com/linebender/druid/compare/v0.6.0...v0.7.0 diff --git a/druid/examples/markdown_preview.rs b/druid/examples/markdown_preview.rs index 90868a79e6..a0ec0d680b 100644 --- a/druid/examples/markdown_preview.rs +++ b/druid/examples/markdown_preview.rs @@ -17,7 +17,7 @@ // On Windows platform, don't show a console when opening the app. #![windows_subsystem = "windows"] -use pulldown_cmark::{Event as ParseEvent, Parser, Tag}; +use pulldown_cmark::{Event as ParseEvent, Options, Parser, Tag}; use druid::text::{AttributesAdder, RichText, RichTextBuilder}; use druid::widget::prelude::*; @@ -32,7 +32,7 @@ const WINDOW_TITLE: LocalizedString = LocalizedString::new("Minimal Ma const TEXT: &str = "*Hello* ***world***! This is a `TextBox` where you can \ use limited markdown notation, which is reflected in the \ - **styling** of the `Label` on the left.\n\n\ + **styling** of the `Label` on the left. ~~Strikethrough even works!~~\n\n\ If you're curious about Druid, a good place to ask questions \ and discuss development work is our [Zulip chat instance], \ in the #druid-help and #druid channels, respectively.\n\n\n\ @@ -141,7 +141,7 @@ fn rebuild_rendered_text(text: &str) -> RichText { let mut builder = RichTextBuilder::new(); let mut tag_stack = Vec::new(); - let parser = Parser::new(text); + let parser = Parser::new_ext(text, Options::ENABLE_STRIKETHROUGH); for event in parser { match event { ParseEvent::Start(tag) => { @@ -218,6 +218,9 @@ fn add_attribute_for_tag(tag: &Tag, mut attrs: AttributesAdder) { Tag::Strong => { attrs.weight(FontWeight::BOLD); } + Tag::Strikethrough => { + attrs.strikethrough(true); + } Tag::Link(_link_ty, target, _title) => { attrs .underline(true) diff --git a/druid/src/text/attribute.rs b/druid/src/text/attribute.rs index 36b80fb8aa..2de29deb54 100644 --- a/druid/src/text/attribute.rs +++ b/druid/src/text/attribute.rs @@ -39,6 +39,7 @@ pub struct AttributeSpans { fg_color: SpanSet>, style: SpanSet, underline: SpanSet, + strikethrough: SpanSet, font_descriptor: SpanSet>, } @@ -100,6 +101,8 @@ pub enum Attribute { Style(FontStyle), /// Underline. Underline(bool), + /// Strikethrough + Strikethrough(bool), /// A [`FontDescriptor`](struct.FontDescriptor.html). Descriptor(KeyOrValue), } @@ -131,6 +134,7 @@ impl AttributeSpans { Attribute::TextColor(attr) => self.fg_color.add(Span::new(range, attr)), Attribute::Style(attr) => self.style.add(Span::new(range, attr)), Attribute::Underline(attr) => self.underline.add(Span::new(range, attr)), + Attribute::Strikethrough(attr) => self.strikethrough.add(Span::new(range, attr)), Attribute::Descriptor(attr) => self.font_descriptor.add(Span::new(range, attr)), } } @@ -175,6 +179,11 @@ impl AttributeSpans { .iter() .map(|s| (s.range.clone(), PietAttr::Underline(s.attr))), ); + items.extend( + self.strikethrough + .iter() + .map(|s| (s.range.clone(), PietAttr::Strikethrough(s.attr))), + ); // sort by ascending start order; this is a stable sort // so items that come from FontDescriptor will stay at the front @@ -254,7 +263,7 @@ impl SpanSet { /// /// `new_len` is the length of the inserted text. //TODO: we could be smarter here about just extending the existing spans - //as requred for insertions in the interior of a span. + //as required for insertions in the interior of a span. //TODO: this isn't currently used; it should be used if we use spans with //some editable type. // the branches are much more readable without sharing code @@ -322,7 +331,7 @@ impl Attribute { Attribute::FontSize(size.into()) } - /// Create a new forground color attribute. + /// Create a new foreground color attribute. pub fn text_color(color: impl Into>) -> Self { Attribute::TextColor(color.into()) } @@ -347,6 +356,11 @@ impl Attribute { Attribute::Underline(underline) } + /// Create a new strikethrough attribute. + pub fn strikethrough(strikethrough: bool) -> Self { + Attribute::Strikethrough(strikethrough) + } + /// Create a new `FontDescriptor` attribute. pub fn font_descriptor(font: impl Into>) -> Self { Attribute::Descriptor(font.into()) diff --git a/druid/src/text/rich_text.rs b/druid/src/text/rich_text.rs index feea98d7d4..96e23c7f04 100644 --- a/druid/src/text/rich_text.rs +++ b/druid/src/text/rich_text.rs @@ -202,7 +202,7 @@ impl AttributesAdder<'_> { self } - /// Add a forground color attribute. + /// Add a foreground color attribute. pub fn text_color(&mut self, color: impl Into>) -> &mut Self { self.add_attr(Attribute::text_color(color)); self @@ -232,6 +232,12 @@ impl AttributesAdder<'_> { self } + /// Add a strikethrough attribute. + pub fn strikethrough(&mut self, strikethrough: bool) -> &mut Self { + self.add_attr(Attribute::Strikethrough(strikethrough)); + self + } + /// Add a `FontDescriptor` attribute. pub fn font_descriptor(&mut self, font: impl Into>) -> &mut Self { self.add_attr(Attribute::font_descriptor(font));