druid 0.8.3

Data-oriented Rust UI design toolkit.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright 2020 The Druid Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Text attributes and spans.

use std::ops::Range;

use crate::piet::{Color, FontFamily, FontStyle, FontWeight, TextAttribute as PietAttr};
use crate::{Command, Env, FontDescriptor, KeyOrValue};

use super::EnvUpdateCtx;

/// A clickable range of text with an associated [`Command`].
#[derive(Debug, Clone)]
pub struct Link {
    /// The range of text for the link.
    pub range: Range<usize>,
    /// A [`Command`] representing the link's payload.
    pub command: Command,
}

/// A collection of spans of attributes of various kinds.
#[derive(Debug, Clone, Default)]
pub struct AttributeSpans {
    family: SpanSet<FontFamily>,
    size: SpanSet<KeyOrValue<f64>>,
    weight: SpanSet<FontWeight>,
    fg_color: SpanSet<KeyOrValue<Color>>,
    style: SpanSet<FontStyle>,
    underline: SpanSet<bool>,
    strikethrough: SpanSet<bool>,
    font_descriptor: SpanSet<KeyOrValue<FontDescriptor>>,
}

/// A set of spans for a given attribute.
///
/// Invariant: the spans are sorted and non-overlapping.
#[derive(Debug, Clone)]
struct SpanSet<T> {
    spans: Vec<Span<T>>,
}

/// An attribute and a range.
///
/// This is used to represent text attributes of various kinds,
/// with the range representing a region of some text buffer.
#[derive(Debug, Clone, PartialEq)]
struct Span<T> {
    range: Range<usize>,
    attr: T,
}

/// Attributes that can be applied to text.
///
/// Where possible, attributes are [`KeyOrValue`] types; this means you
/// can use items defined in the [`theme`] *or* concrete types, where appropriate.
///
/// The easiest way to construct these attributes is via the various constructor
/// methods, such as [`Attribute::size`] or [`Attribute::text_color`].
///
/// # Examples
///
/// ```
/// use druid::text::Attribute;
/// use druid::{theme, Color};
///
/// let font = Attribute::font_descriptor(theme::UI_FONT);
/// let font_size = Attribute::size(32.0);
/// let explicit_color = Attribute::text_color(Color::BLACK);
/// let theme_color = Attribute::text_color(theme::SELECTION_COLOR);
/// ```
///
/// [`theme`]: crate::theme
#[derive(Debug, Clone)]
pub enum Attribute {
    /// The font family.
    FontFamily(FontFamily),
    /// The font size, in points.
    FontSize(KeyOrValue<f64>),
    /// The [`FontWeight`].
    Weight(FontWeight),
    /// The foreground color of the text.
    TextColor(KeyOrValue<Color>),
    /// The [`FontStyle`]; either regular or italic.
    Style(FontStyle),
    /// Underline.
    Underline(bool),
    /// Strikethrough
    Strikethrough(bool),
    /// A [`FontDescriptor`].
    Descriptor(KeyOrValue<FontDescriptor>),
}

impl Link {
    /// Create a new `Link`.
    pub fn new(range: Range<usize>, command: Command) -> Self {
        Self { range, command }
    }

    /// Get this `Link`'s range.
    pub fn range(&self) -> Range<usize> {
        self.range.clone()
    }
}

impl AttributeSpans {
    /// Create a new, empty `AttributeSpans`.
    pub fn new() -> Self {
        Default::default()
    }

    /// Add a new [`Attribute`] over the provided [`Range`].
    pub fn add(&mut self, range: Range<usize>, attr: Attribute) {
        match attr {
            Attribute::FontFamily(attr) => self.family.add(Span::new(range, attr)),
            Attribute::FontSize(attr) => self.size.add(Span::new(range, attr)),
            Attribute::Weight(attr) => self.weight.add(Span::new(range, attr)),
            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)),
        }
    }

    pub(crate) fn to_piet_attrs(&self, env: &Env) -> Vec<(Range<usize>, PietAttr)> {
        let mut items = Vec::new();
        for Span { range, attr } in self.font_descriptor.iter() {
            let font = attr.resolve(env);
            items.push((range.clone(), PietAttr::FontFamily(font.family)));
            items.push((range.clone(), PietAttr::FontSize(font.size)));
            items.push((range.clone(), PietAttr::Weight(font.weight)));
            items.push((range.clone(), PietAttr::Style(font.style)));
        }

        items.extend(
            self.family
                .iter()
                .map(|s| (s.range.clone(), PietAttr::FontFamily(s.attr.clone()))),
        );
        items.extend(
            self.size
                .iter()
                .map(|s| (s.range.clone(), PietAttr::FontSize(s.attr.resolve(env)))),
        );
        items.extend(
            self.weight
                .iter()
                .map(|s| (s.range.clone(), PietAttr::Weight(s.attr))),
        );
        items.extend(
            self.fg_color
                .iter()
                .map(|s| (s.range.clone(), PietAttr::TextColor(s.attr.resolve(env)))),
        );
        items.extend(
            self.style
                .iter()
                .map(|s| (s.range.clone(), PietAttr::Style(s.attr))),
        );
        items.extend(
            self.underline
                .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
        items.sort_by(|a, b| a.0.start.cmp(&b.0.start));
        items
    }

    pub(crate) fn env_update(&self, ctx: &EnvUpdateCtx) -> bool {
        self.size
            .iter()
            .any(|span_attr| ctx.env_key_changed(&span_attr.attr))
            || self
                .fg_color
                .iter()
                .any(|span_attr| ctx.env_key_changed(&span_attr.attr))
            || self
                .font_descriptor
                .iter()
                .any(|span_attr| ctx.env_key_changed(&span_attr.attr))
    }
}

impl<T: Clone> SpanSet<T> {
    fn iter(&self) -> impl Iterator<Item = &Span<T>> {
        self.spans.iter()
    }

    /// Add a `Span` to this `SpanSet`.
    ///
    /// Spans can be added in any order. existing spans will be updated
    /// as required.
    fn add(&mut self, span: Span<T>) {
        let span_start = span.range.start;
        let span_end = span.range.end;
        let insert_idx = self
            .spans
            .iter()
            .position(|x| x.range.start >= span.range.start)
            .unwrap_or(self.spans.len());

        // if we are inserting into the middle of an existing span we need
        // to add the trailing portion back afterwards.
        let mut prev_remainder = None;

        if insert_idx > 0 {
            // truncate the preceding item, if necessary
            let before = self.spans.get_mut(insert_idx - 1).unwrap();
            if before.range.end > span_end {
                let mut remainder = before.clone();
                remainder.range.start = span_end;
                prev_remainder = Some(remainder);
            }
            before.range.end = before.range.end.min(span_start);
        }

        self.spans.insert(insert_idx, span);
        if let Some(remainder) = prev_remainder.take() {
            self.spans.insert(insert_idx + 1, remainder);
        }

        // clip any existing spans as needed
        for after in self.spans.iter_mut().skip(insert_idx + 1) {
            after.range.start = after.range.start.max(span_end);
            after.range.end = after.range.end.max(span_end);
        }

        // remove any spans that have been overwritten
        self.spans.retain(|span| !span.is_empty());
    }

    /// Edit the spans, inserting empty space into the changed region if needed.
    ///
    /// This is used to keep the spans up to date as edits occur in the buffer.
    ///
    /// `changed` is the range of the string that has been replaced; this can
    /// be an empty range (eg, 10..10) for the insertion case.
    ///
    /// `new_len` is the length of the inserted text.
    //TODO: we could be smarter here about just extending the existing spans
    //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
    #[allow(dead_code, clippy::branches_sharing_code)]
    fn edit(&mut self, changed: Range<usize>, new_len: usize) {
        let old_len = changed.len();
        let mut to_insert = None;

        for (idx, Span { range, attr }) in self.spans.iter_mut().enumerate() {
            if range.end <= changed.start {
                continue;
            } else if range.start < changed.start {
                // we start before but end inside; truncate end
                if range.end <= changed.end {
                    range.end = changed.start;
                // we start before and end after; this is a special case,
                // we'll need to add a new span
                } else {
                    let new_start = changed.start + new_len;
                    let new_end = range.end - old_len + new_len;
                    let new_span = Span::new(new_start..new_end, attr.clone());
                    to_insert = Some((idx + 1, new_span));
                    range.end = changed.start;
                }
            // start inside
            } else if range.start < changed.end {
                range.start = changed.start + new_len;
                // end inside; collapse
                if range.end <= changed.end {
                    range.end = changed.start + new_len;
                // end outside: adjust by length delta
                } else {
                    range.end -= old_len;
                    range.end += new_len;
                }
            // whole range is after:
            } else {
                range.start -= old_len;
                range.start += new_len;
                range.end -= old_len;
                range.end += new_len;
            }
        }
        if let Some((idx, span)) = to_insert.take() {
            self.spans.insert(idx, span);
        }

        self.spans.retain(|span| !span.is_empty());
    }
}

impl<T> Span<T> {
    fn new(range: Range<usize>, attr: T) -> Self {
        Span { range, attr }
    }

    fn is_empty(&self) -> bool {
        self.range.end <= self.range.start
    }
}

impl Attribute {
    /// Create a new font size attribute.
    pub fn size(size: impl Into<KeyOrValue<f64>>) -> Self {
        Attribute::FontSize(size.into())
    }

    /// Create a new foreground color attribute.
    pub fn text_color(color: impl Into<KeyOrValue<Color>>) -> Self {
        Attribute::TextColor(color.into())
    }

    /// Create a new font family attribute.
    pub fn font_family(family: FontFamily) -> Self {
        Attribute::FontFamily(family)
    }

    /// Create a new `FontWeight` attribute.
    pub fn weight(weight: FontWeight) -> Self {
        Attribute::Weight(weight)
    }

    /// Create a new `FontStyle` attribute.
    pub fn style(style: FontStyle) -> Self {
        Attribute::Style(style)
    }

    /// Create a new underline attribute.
    pub fn underline(underline: bool) -> Self {
        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<KeyOrValue<FontDescriptor>>) -> Self {
        Attribute::Descriptor(font.into())
    }
}

impl<T> Default for SpanSet<T> {
    fn default() -> Self {
        SpanSet { spans: Vec::new() }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use test_log::test;

    #[test]
    fn smoke_test_spans() {
        let mut spans = SpanSet::<u32>::default();
        spans.add(Span::new(2..10, 1));
        spans.add(Span::new(3..6, 2));
        assert_eq!(
            &spans.spans,
            &vec![Span::new(2..3, 1), Span::new(3..6, 2), Span::new(6..10, 1)]
        );

        spans.add(Span::new(0..12, 3));
        assert_eq!(&spans.spans, &vec![Span::new(0..12, 3)]);
        spans.add(Span::new(5..20, 4));
        assert_eq!(&spans.spans, &vec![Span::new(0..5, 3), Span::new(5..20, 4)]);
    }

    #[test]
    fn edit_spans() {
        let mut spans = SpanSet::<u32>::default();
        spans.add(Span::new(0..2, 1));
        spans.add(Span::new(8..12, 2));
        spans.add(Span::new(13..16, 3));
        spans.add(Span::new(20..22, 4));

        let mut deletion = spans.clone();
        deletion.edit(6..14, 0);
        assert_eq!(
            &deletion.spans,
            &vec![Span::new(0..2, 1), Span::new(6..8, 3), Span::new(12..14, 4)]
        );

        spans.edit(10..10, 2);
        assert_eq!(
            &spans.spans,
            &vec![
                Span::new(0..2, 1),
                Span::new(8..10, 2),
                Span::new(12..14, 2),
                Span::new(15..18, 3),
                Span::new(22..24, 4),
            ]
        );
    }
}