Skip to main content

ochrepad_core/
view.rs

1//! `View`: a window onto a buffer with its own selections and scroll position.
2
3use serde::{Deserialize, Serialize};
4
5use crate::buffer::BufferId;
6use crate::selection::SelectionSet;
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
9pub struct ViewId(pub u64);
10
11#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
12pub struct ScrollPos {
13    pub first_line: usize,
14    /// Fraction of a line scrolled past `first_line`, in `[0, 1)`.
15    pub line_fraction: f32,
16    /// Horizontal offset in pixels.
17    pub x_offset: f32,
18}
19
20#[derive(Clone, Debug)]
21pub struct View {
22    id: ViewId,
23    buffer: BufferId,
24    pub selections: SelectionSet,
25    pub scroll: ScrollPos,
26}
27
28impl View {
29    pub(crate) fn new(id: ViewId, buffer: BufferId) -> Self {
30        Self {
31            id,
32            buffer,
33            selections: SelectionSet::caret(Default::default()),
34            scroll: ScrollPos::default(),
35        }
36    }
37
38    pub fn id(&self) -> ViewId {
39        self.id
40    }
41
42    pub fn buffer(&self) -> BufferId {
43        self.buffer
44    }
45}
46
47/// Maps document lines to display rows. Views never assume line N is row N;
48/// word wrap and diff alignment will provide other implementations.
49pub trait DisplayMap {
50    fn row_count(&self, line_count: usize) -> usize;
51    fn line_to_row(&self, line: usize) -> usize;
52    /// The document line shown on `row`, or `None` for a filler row.
53    fn row_to_line(&self, row: usize) -> Option<usize>;
54}
55
56/// v1: one row per line.
57#[derive(Clone, Copy, Debug, Default)]
58pub struct IdentityDisplayMap;
59
60impl DisplayMap for IdentityDisplayMap {
61    fn row_count(&self, line_count: usize) -> usize {
62        line_count
63    }
64
65    fn line_to_row(&self, line: usize) -> usize {
66        line
67    }
68
69    fn row_to_line(&self, row: usize) -> Option<usize> {
70        Some(row)
71    }
72}