Skip to main content

ochrepad_core/
view.rs

1//! `View`: a window onto a buffer with its own selections and scroll position.
2
3use std::cell::RefCell;
4use std::sync::Arc;
5
6use serde::{Deserialize, Serialize};
7
8use crate::buffer::BufferId;
9use crate::display::{Padding, WrapCache};
10use crate::fold::FoldSet;
11use crate::position::ByteOffset;
12use crate::selection::SelectionSet;
13
14#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
15pub struct ViewId(pub u64);
16
17#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
18pub struct ScrollPos {
19    /// The top row: `first_sub` is the row within `first_line` (see
20    /// [`crate::display`]).
21    pub first_line: usize,
22    #[serde(default)]
23    pub first_sub: usize,
24    /// Fraction of the top row scrolled past, in `[0, 1)`.
25    pub line_fraction: f32,
26    /// Horizontal offset in pixels.
27    pub x_offset: f32,
28}
29
30#[derive(Clone, Debug)]
31pub struct View {
32    id: ViewId,
33    buffer: BufferId,
34    pub selections: SelectionSet,
35    pub scroll: ScrollPos,
36    /// Folded regions; changed only through `Editor`, which keeps them in
37    /// step with the text.
38    pub(crate) folds: FoldSet,
39    /// Wrap width in cells, or `None` when lines do not wrap. The editor
40    /// widget sets it from the pane's width.
41    pub wrap_cols: Option<usize>,
42    pub(crate) wrap_cache: RefCell<WrapCache>,
43    /// Blank rows between lines, aligning this view with another (one side
44    /// of a compare view). With padding, folds and wrap do not apply.
45    pub padding: Option<Arc<Padding>>,
46    /// Not a tab of its own (a compare view's side): left out of
47    /// [`crate::Editor::views_of`].
48    aux: bool,
49    /// Where Edit > Begin/End Select began, until it ends; `Editor` keeps
50    /// it in step with edits, as it does selections.
51    pub begin_select: Option<ByteOffset>,
52}
53
54impl View {
55    pub(crate) fn new(id: ViewId, buffer: BufferId) -> Self {
56        Self {
57            id,
58            buffer,
59            selections: SelectionSet::caret(Default::default()),
60            scroll: ScrollPos::default(),
61            folds: FoldSet::default(),
62            wrap_cols: None,
63            wrap_cache: RefCell::default(),
64            padding: None,
65            aux: false,
66            begin_select: None,
67        }
68    }
69
70    pub fn is_aux(&self) -> bool {
71        self.aux
72    }
73
74    pub(crate) fn set_aux(&mut self) {
75        self.aux = true;
76    }
77
78    pub fn id(&self) -> ViewId {
79        self.id
80    }
81
82    pub fn buffer(&self) -> BufferId {
83        self.buffer
84    }
85
86    pub fn folds(&self) -> &FoldSet {
87        &self.folds
88    }
89}