use std::ops::Range;
use std::sync::Arc;
use canvist_model::BlockId;
use canvist_model::EmbedTypeId;
use canvist_model::Selection;
use rustc_hash::FxHashMap;
use crate::Caret;
use crate::Color;
use crate::FontId;
use crate::FontSpec;
use crate::Rect;
use crate::text::slice;
mod build;
mod fade;
mod motion;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum RectKind {
Background,
Border,
Rule,
Highlight,
Range,
Selection,
Caret,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DecorationKind {
Underline,
Strike,
Squiggle,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum SlotKind {
Block,
Embed(EmbedTypeId),
Checkbox {
checked: bool,
},
}
#[derive(Clone, Debug, PartialEq)]
pub struct TextRun {
pub block: BlockId,
pub source: Arc<str>,
pub range: Range<u32>,
pub font: FontId,
pub x: f32,
pub baseline: f32,
pub width: f32,
pub color: Color,
pub letter_spacing: f32,
pub rtl: bool,
pub bounds: Rect,
}
impl TextRun {
#[must_use]
pub fn text(&self) -> &str {
slice(&self.source, &self.range)
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum DisplayItem {
Rect {
rect: Rect,
color: Color,
radius: f32,
kind: RectKind,
block: Option<BlockId>,
},
Text(TextRun),
Decoration {
kind: DecorationKind,
x: f32,
y: f32,
width: f32,
thickness: f32,
color: Color,
block: BlockId,
},
Stroke {
points: Range<u32>,
width: f32,
color: Color,
bounds: Rect,
block: BlockId,
},
Slot {
kind: SlotKind,
block: BlockId,
offset: Option<u32>,
rect: Rect,
},
}
impl DisplayItem {
#[must_use]
pub fn block(&self) -> Option<BlockId> {
match self {
Self::Rect { block, .. } => *block,
Self::Text(run) => Some(run.block),
Self::Decoration { block, .. }
| Self::Stroke { block, .. }
| Self::Slot { block, .. } => Some(*block),
}
}
#[must_use]
pub fn bounds(&self) -> Rect {
match self {
Self::Rect { rect, .. } | Self::Slot { rect, .. } => *rect,
Self::Text(run) => run.bounds,
Self::Decoration {
x,
y,
width,
thickness,
..
} => Rect::new(*x, y - thickness * 2.0, *width, thickness * 4.0),
Self::Stroke { bounds, .. } => *bounds,
}
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct DisplayOptions {
pub top: f32,
pub bottom: f32,
pub selection: Option<Selection>,
pub caret: Option<Caret>,
pub highlights: Vec<(Selection, Color)>,
pub placeholders: Vec<(BlockId, Arc<str>)>,
pub time: Option<f32>,
pub settling: Vec<(BlockId, Range<u32>, f32)>,
}
impl DisplayOptions {
#[must_use]
pub fn viewport(top: f32, bottom: f32) -> Self {
Self {
top,
bottom,
..Self::default()
}
}
#[must_use]
pub fn selection(mut self, selection: Selection) -> Self {
self.selection = Some(selection);
self
}
#[must_use]
pub fn caret(mut self, caret: Caret) -> Self {
self.caret = Some(caret);
self
}
#[must_use]
pub fn highlight(mut self, range: Selection, color: Color) -> Self {
self.highlights.push((range, color));
self
}
#[must_use]
pub fn time(mut self, seconds: f32) -> Self {
self.time = Some(seconds);
self
}
#[must_use]
pub fn settling(mut self, block: BlockId, range: Range<u32>, elapsed: f32) -> Self {
self.settling.push((block, range, elapsed));
self
}
#[must_use]
pub fn placeholder(mut self, block: BlockId, text: &str) -> Self {
self.placeholders.push((block, Arc::from(text)));
self
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct DisplayList {
pub fonts: Vec<FontSpec>,
pub items: Vec<DisplayItem>,
pub bounds: Rect,
pub animated: bool,
pub points: Vec<f32>,
}
impl DisplayList {
#[must_use]
pub fn stroke_points(&self, range: &Range<u32>) -> &[f32] {
self.points
.get(range.start as usize..range.end as usize)
.unwrap_or_default()
}
#[must_use]
pub fn damage(&self, previous: &Self) -> Vec<Rect> {
let now = groups(self);
let before = groups(previous);
let mut rects = Vec::new();
for (key, group) in &now {
let old = before.get(key);
if old.is_some_and(|old| same_items(previous, &old.items, self, &group.items)) {
continue;
}
rects.extend(old.and_then(|old| old.bounds));
rects.extend(group.bounds);
}
for (key, old) in &before {
if !now.contains_key(key) {
rects.extend(old.bounds);
}
}
rects
}
}
fn same_items(
previous: &DisplayList,
old: &[&DisplayItem],
list: &DisplayList,
new: &[&DisplayItem],
) -> bool {
old.len() == new.len()
&& old.iter().zip(new).all(|(old, new)| {
match (old, new) {
(
DisplayItem::Stroke {
points: old_points,
width: old_width,
color: old_color,
bounds: old_bounds,
block: old_block,
},
DisplayItem::Stroke {
points,
width,
color,
bounds,
block,
},
) => {
(old_width, old_color, old_bounds, old_block) == (width, color, bounds, block)
&& previous.stroke_points(old_points) == list.stroke_points(points)
}
_ => old == new,
}
})
}
struct Group<'a> {
items: Vec<&'a DisplayItem>,
bounds: Option<Rect>,
}
fn groups(list: &DisplayList) -> FxHashMap<Option<BlockId>, Group<'_>> {
let mut groups: FxHashMap<Option<BlockId>, Group<'_>> = FxHashMap::default();
for item in &list.items {
let group = groups.entry(item.block()).or_insert(Group {
items: Vec::new(),
bounds: None,
});
let bounds = item.bounds();
group.items.push(item);
group.bounds = Some(group.bounds.map_or(bounds, |b| b.union(&bounds)));
}
groups
}
#[cfg(test)]
mod tests {
use super::*;
fn list_with_stroke(points: Vec<f32>) -> DisplayList {
let bounds = Rect::new(0.0, 0.0, 10.0, 10.0);
DisplayList {
fonts: Vec::new(),
items: vec![DisplayItem::Stroke {
points: 0..points.len() as u32,
width: 2.0,
color: Color::rgb(0, 0, 0),
bounds,
block: BlockId::new(1, 0),
}],
bounds,
animated: false,
points,
}
}
#[test]
fn strokes_compare_by_their_points_not_their_ranges() {
let before = list_with_stroke(vec![1.0, 2.0, 3.0, 4.0]);
let moved = list_with_stroke(vec![1.0, 2.0, 5.0, 6.0]);
assert!(
list_with_stroke(vec![1.0, 2.0, 3.0, 4.0])
.damage(&before)
.is_empty()
);
assert_eq!(
moved.damage(&before),
[
Rect::new(0.0, 0.0, 10.0, 10.0),
Rect::new(0.0, 0.0, 10.0, 10.0)
]
);
}
#[test]
fn a_range_outside_the_list_reads_as_no_points() {
assert_eq!(
list_with_stroke(vec![1.0, 2.0]).stroke_points(&(2..6)),
[0.0; 0]
);
}
}