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