use canvist_model::Document;
use canvist_model::Point;
use crate::Affinity;
use crate::Caret;
use crate::DocumentLayout;
use crate::LayoutError;
use crate::TextMeasurer;
use crate::document::GridStep;
use crate::document::RowKind;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum VerticalDirection {
Up,
Down,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum LineEdge {
Start,
End,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct VerticalMotion {
pub caret: Caret,
pub goal_x: f32,
}
pub struct LayoutNavigator<'a> {
layout: &'a mut DocumentLayout,
doc: &'a Document,
measurer: &'a mut dyn TextMeasurer,
}
impl std::fmt::Debug for LayoutNavigator<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("LayoutNavigator").finish_non_exhaustive()
}
}
impl DocumentLayout {
pub fn navigator<'a>(
&'a mut self,
doc: &'a Document,
measurer: &'a mut dyn TextMeasurer,
) -> LayoutNavigator<'a> {
LayoutNavigator {
layout: self,
doc,
measurer,
}
}
}
impl LayoutNavigator<'_> {
fn row(&mut self, caret: Caret) -> Result<usize, LayoutError> {
let index = self
.layout
.row_index(caret.point.block)
.ok_or(LayoutError::OutOfSync(caret.point.block))?;
self.layout.ensure_row(self.doc, self.measurer, index)?;
Ok(index)
}
fn neighbour(
&mut self,
index: usize,
direction: VerticalDirection,
goal_x: f32,
) -> Result<Option<usize>, LayoutError> {
let rows = self.layout.rows();
let step = self
.layout
.grid_step(index, direction == VerticalDirection::Down, goal_x);
let found = match (step, direction) {
(GridStep::Row(found), _) => found,
(GridStep::Flow, VerticalDirection::Up) => {
rows[..index]
.iter()
.rposition(|row| row.kind == RowKind::Text)
}
(GridStep::Flow, VerticalDirection::Down) => {
rows[index + 1..]
.iter()
.position(|row| row.kind == RowKind::Text)
.map(|offset| index + 1 + offset)
}
};
if let Some(found) = found {
self.layout.ensure_row(self.doc, self.measurer, found)?;
}
Ok(found)
}
fn adjacent_line(
&mut self,
index: usize,
line: usize,
direction: VerticalDirection,
goal_x: f32,
) -> Result<Option<(usize, usize)>, LayoutError> {
let line_count = self.layout.rows()[index]
.layout
.as_ref()
.map_or(0, |layout| layout.lines.len());
let within = match direction {
VerticalDirection::Up => line.checked_sub(1),
VerticalDirection::Down => Some(line + 1).filter(|&next| next < line_count),
};
if let Some(within) = within {
return Ok(Some((index, within)));
}
let Some(row) = self.neighbour(index, direction, goal_x)? else {
return Ok(None);
};
let Some(layout) = self.layout.rows()[row].layout.as_ref() else {
return Ok(None);
};
let entry = match direction {
VerticalDirection::Up => layout.lines.len().checked_sub(1),
VerticalDirection::Down => Some(0),
};
Ok(entry.map(|entry| (row, entry)))
}
}
impl LayoutNavigator<'_> {
pub fn vertical(
&mut self,
from: Caret,
direction: VerticalDirection,
goal_x: Option<f32>,
) -> Result<Option<VerticalMotion>, LayoutError> {
let index = self.row(from)?;
let goal_x = goal_x.or_else(|| self.layout.caret_x(from)).unwrap_or(0.0);
let Some(layout) = self.layout.rows()[index].layout.as_ref() else {
return Ok(None);
};
let line = layout.line_index(layout.laid_out(from.point.offset), from.affinity);
let Some((row, line)) = self.adjacent_line(index, line, direction, goal_x)? else {
return Ok(None);
};
let origin = self.layout.origin(row);
let Some(layout) = self.layout.rows()[row].layout.as_ref() else {
return Ok(None);
};
let (offset, affinity) = layout.hit(line, goal_x - origin.x);
let offset = layout.in_document(offset);
let block = self.layout.rows()[row].block;
Ok(Some(VerticalMotion {
caret: Caret::with_affinity(Point::new(block, offset), affinity),
goal_x,
}))
}
pub fn line_edge(&mut self, at: Caret, edge: LineEdge) -> Result<Caret, LayoutError> {
let index = self.row(at)?;
let Some(layout) = self.layout.rows()[index].layout.as_ref() else {
return Ok(at);
};
let line = &layout.lines[layout.line_index(layout.laid_out(at.point.offset), at.affinity)];
let (offset, affinity) = match edge {
LineEdge::Start => (line.range.start, Affinity::Downstream),
LineEdge::End if line.soft => (line.caret_end, Affinity::Upstream),
LineEdge::End => (line.caret_end, Affinity::Downstream),
};
Ok(Caret::with_affinity(
Point::new(at.point.block, layout.in_document(offset)),
affinity,
))
}
}