Skip to content

Commit f2e78ab

Browse files
Added strikethrough (#1953)
Adds support for strikethrough, and includes it in the markdown example.
1 parent 067b187 commit f2e78ab

File tree

5 files changed

+33
-6
lines changed

5 files changed

+33
-6
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ Maximilian Köstler
1818
Bruno Dupuis
1919
Christopher Noel Hesse
2020
Marcin Zając
21+
Laura Gallo

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ You can find its changes [documented below](#070---2021-01-01).
1212

1313
### Added
1414

15+
- Strikethrough rich text attribute ([#1953] by [@jenra-uwu])
1516
- System fonts loaded so that SVG images render text ([#1850] by [@DrGabble])
1617
- Add `scroll()` method in WidgetExt ([#1600] by [@totsteps])
1718
- `write!` for `RichTextBuilder` ([#1596] by [@Maan2003])
@@ -516,6 +517,7 @@ Last release without a changelog :(
516517
[@bjorn]: https://github.com/bjorn
517518
[@DrGabble]: https://github.com/DrGabble
518519
[@lisael]: https://github.com/lisael
520+
[@jenra-uwu]: https://github.com/jenra-uwu
519521

520522
[#599]: https://github.com/linebender/druid/pull/599
521523
[#611]: https://github.com/linebender/druid/pull/611
@@ -789,6 +791,7 @@ Last release without a changelog :(
789791
[#1929]: https://github.com/linebender/druid/pull/1929
790792
[#1947]: https://github.com/linebender/druid/pull/1947
791793
[#1967]: https://github.com/linebender/druid/pull/1967
794+
[#1953]: https://github.com/linebender/druid/pull/1953
792795

793796
[Unreleased]: https://github.com/linebender/druid/compare/v0.7.0...master
794797
[0.7.0]: https://github.com/linebender/druid/compare/v0.6.0...v0.7.0

druid/examples/markdown_preview.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
// On Windows platform, don't show a console when opening the app.
1818
#![windows_subsystem = "windows"]
1919

20-
use pulldown_cmark::{Event as ParseEvent, Parser, Tag};
20+
use pulldown_cmark::{Event as ParseEvent, Options, Parser, Tag};
2121

2222
use druid::text::{AttributesAdder, RichText, RichTextBuilder};
2323
use druid::widget::prelude::*;
@@ -32,7 +32,7 @@ const WINDOW_TITLE: LocalizedString<AppState> = LocalizedString::new("Minimal Ma
3232

3333
const TEXT: &str = "*Hello* ***world***! This is a `TextBox` where you can \
3434
use limited markdown notation, which is reflected in the \
35-
**styling** of the `Label` on the left.\n\n\
35+
**styling** of the `Label` on the left. ~~Strikethrough even works!~~\n\n\
3636
If you're curious about Druid, a good place to ask questions \
3737
and discuss development work is our [Zulip chat instance], \
3838
in the #druid-help and #druid channels, respectively.\n\n\n\
@@ -141,7 +141,7 @@ fn rebuild_rendered_text(text: &str) -> RichText {
141141
let mut builder = RichTextBuilder::new();
142142
let mut tag_stack = Vec::new();
143143

144-
let parser = Parser::new(text);
144+
let parser = Parser::new_ext(text, Options::ENABLE_STRIKETHROUGH);
145145
for event in parser {
146146
match event {
147147
ParseEvent::Start(tag) => {
@@ -218,6 +218,9 @@ fn add_attribute_for_tag(tag: &Tag, mut attrs: AttributesAdder) {
218218
Tag::Strong => {
219219
attrs.weight(FontWeight::BOLD);
220220
}
221+
Tag::Strikethrough => {
222+
attrs.strikethrough(true);
223+
}
221224
Tag::Link(_link_ty, target, _title) => {
222225
attrs
223226
.underline(true)

druid/src/text/attribute.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub struct AttributeSpans {
3939
fg_color: SpanSet<KeyOrValue<Color>>,
4040
style: SpanSet<FontStyle>,
4141
underline: SpanSet<bool>,
42+
strikethrough: SpanSet<bool>,
4243
font_descriptor: SpanSet<KeyOrValue<FontDescriptor>>,
4344
}
4445

@@ -100,6 +101,8 @@ pub enum Attribute {
100101
Style(FontStyle),
101102
/// Underline.
102103
Underline(bool),
104+
/// Strikethrough
105+
Strikethrough(bool),
103106
/// A [`FontDescriptor`](struct.FontDescriptor.html).
104107
Descriptor(KeyOrValue<FontDescriptor>),
105108
}
@@ -131,6 +134,7 @@ impl AttributeSpans {
131134
Attribute::TextColor(attr) => self.fg_color.add(Span::new(range, attr)),
132135
Attribute::Style(attr) => self.style.add(Span::new(range, attr)),
133136
Attribute::Underline(attr) => self.underline.add(Span::new(range, attr)),
137+
Attribute::Strikethrough(attr) => self.strikethrough.add(Span::new(range, attr)),
134138
Attribute::Descriptor(attr) => self.font_descriptor.add(Span::new(range, attr)),
135139
}
136140
}
@@ -175,6 +179,11 @@ impl AttributeSpans {
175179
.iter()
176180
.map(|s| (s.range.clone(), PietAttr::Underline(s.attr))),
177181
);
182+
items.extend(
183+
self.strikethrough
184+
.iter()
185+
.map(|s| (s.range.clone(), PietAttr::Strikethrough(s.attr))),
186+
);
178187

179188
// sort by ascending start order; this is a stable sort
180189
// so items that come from FontDescriptor will stay at the front
@@ -254,7 +263,7 @@ impl<T: Clone> SpanSet<T> {
254263
///
255264
/// `new_len` is the length of the inserted text.
256265
//TODO: we could be smarter here about just extending the existing spans
257-
//as requred for insertions in the interior of a span.
266+
//as required for insertions in the interior of a span.
258267
//TODO: this isn't currently used; it should be used if we use spans with
259268
//some editable type.
260269
// the branches are much more readable without sharing code
@@ -322,7 +331,7 @@ impl Attribute {
322331
Attribute::FontSize(size.into())
323332
}
324333

325-
/// Create a new forground color attribute.
334+
/// Create a new foreground color attribute.
326335
pub fn text_color(color: impl Into<KeyOrValue<Color>>) -> Self {
327336
Attribute::TextColor(color.into())
328337
}
@@ -347,6 +356,11 @@ impl Attribute {
347356
Attribute::Underline(underline)
348357
}
349358

359+
/// Create a new strikethrough attribute.
360+
pub fn strikethrough(strikethrough: bool) -> Self {
361+
Attribute::Strikethrough(strikethrough)
362+
}
363+
350364
/// Create a new `FontDescriptor` attribute.
351365
pub fn font_descriptor(font: impl Into<KeyOrValue<FontDescriptor>>) -> Self {
352366
Attribute::Descriptor(font.into())

druid/src/text/rich_text.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl AttributesAdder<'_> {
202202
self
203203
}
204204

205-
/// Add a forground color attribute.
205+
/// Add a foreground color attribute.
206206
pub fn text_color(&mut self, color: impl Into<KeyOrValue<Color>>) -> &mut Self {
207207
self.add_attr(Attribute::text_color(color));
208208
self
@@ -232,6 +232,12 @@ impl AttributesAdder<'_> {
232232
self
233233
}
234234

235+
/// Add a strikethrough attribute.
236+
pub fn strikethrough(&mut self, strikethrough: bool) -> &mut Self {
237+
self.add_attr(Attribute::Strikethrough(strikethrough));
238+
self
239+
}
240+
235241
/// Add a `FontDescriptor` attribute.
236242
pub fn font_descriptor(&mut self, font: impl Into<KeyOrValue<FontDescriptor>>) -> &mut Self {
237243
self.add_attr(Attribute::font_descriptor(font));

0 commit comments

Comments
 (0)