From a07ed3762530e8a8cc961ff84f41a37f5786e93d Mon Sep 17 00:00:00 2001 From: jenra-uwu <24357776+jenra-uwu@users.noreply.github.com> Date: Fri, 3 Sep 2021 08:57:28 -0400 Subject: [PATCH 1/5] Added strikethrough --- druid/examples/markdown_preview.rs | 7 +++++-- druid/src/text/attribute.rs | 18 ++++++++++++++++-- druid/src/text/rich_text.rs | 8 +++++++- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/druid/examples/markdown_preview.rs b/druid/examples/markdown_preview.rs index 90868a79e6..90a75e8b2b 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::*; @@ -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..f6741518d2 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)); From 9c20ae094ef844eee0a2b5bedd36633a27f03137 Mon Sep 17 00:00:00 2001 From: jenra-uwu <24357776+jenra-uwu@users.noreply.github.com> Date: Fri, 3 Sep 2021 09:03:35 -0400 Subject: [PATCH 2/5] Updated CHANGELOG.md and AUTHORS --- AUTHORS | 1 + CHANGELOG.md | 1 + 2 files changed, 2 insertions(+) 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 8d1bb0d567..1ae8315c11 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]) From ffe4d40aac607557793987ce90cf3a6334f0a5a5 Mon Sep 17 00:00:00 2001 From: jenra-uwu <24357776+jenra-uwu@users.noreply.github.com> Date: Fri, 3 Sep 2021 09:08:36 -0400 Subject: [PATCH 3/5] Ran cargo fmt --- druid/src/text/attribute.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/druid/src/text/attribute.rs b/druid/src/text/attribute.rs index f6741518d2..2de29deb54 100644 --- a/druid/src/text/attribute.rs +++ b/druid/src/text/attribute.rs @@ -182,7 +182,7 @@ impl AttributeSpans { items.extend( self.strikethrough .iter() - .map(|s| (s.range.clone(), PietAttr::Strikethrough(s.attr))) + .map(|s| (s.range.clone(), PietAttr::Strikethrough(s.attr))), ); // sort by ascending start order; this is a stable sort From 0fcab0e8d81038aa378f3bb10ef608c5f33fe2b6 Mon Sep 17 00:00:00 2001 From: jenra-uwu <24357776+jenra-uwu@users.noreply.github.com> Date: Fri, 3 Sep 2021 09:13:33 -0400 Subject: [PATCH 4/5] Added strikethrough to the sample text --- druid/examples/markdown_preview.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/druid/examples/markdown_preview.rs b/druid/examples/markdown_preview.rs index 90a75e8b2b..a0ec0d680b 100644 --- a/druid/examples/markdown_preview.rs +++ b/druid/examples/markdown_preview.rs @@ -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\ From dc3e2b21e077c8134318d41f0e20ca9da4075a8b Mon Sep 17 00:00:00 2001 From: jenra-uwu <24357776+jenra-uwu@users.noreply.github.com> Date: Fri, 3 Sep 2021 09:38:47 -0400 Subject: [PATCH 5/5] Updated CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ae8315c11..3ce6f6f8f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -514,6 +514,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 @@ -785,6 +786,7 @@ Last release without a changelog :( [#1886]: https://github.com/linebender/druid/pull/1886 [#1907]: https://github.com/linebender/druid/pull/1907 [#1929]: https://github.com/linebender/druid/pull/1929 +[#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