summaryrefslogtreecommitdiffstats
path: root/src/ui/grid/context.rs
blob: 03a9ebe93d464709cee4c87300cb64f874e8fd7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
use gtk::prelude::*;
use gtk::DrawingArea;

use crate::ui::color::HlDefs;
use crate::ui::font::Font;
use crate::ui::grid::cursor::Cursor;
use crate::ui::grid::render;
use crate::ui::grid::row::{Cell, Row};

/// Context is manipulated by Grid.
pub struct Context {
    /// Our cairo context, that is evetually drawn to the screen.
    pub cairo_context: cairo::Context,
    /// Our cell metrics.
    pub cell_metrics: CellMetrics,
    /// Cell metrics to be updated.
    pub cell_metrics_update: Option<CellMetrics>,

    /// Internal grid.
    pub rows: Vec<Row>,

    pub cursor: Cursor,
    /// Cairo context for cursor.
    pub cursor_context: cairo::Context,

    /// If the current status is busy or not. When busy, the cursor is not
    /// drawn (like when in terminal mode in inserting text).
    pub busy: bool,

    /// If the grid that this context belongs to is active or not.
    pub active: bool,

    /// Areas to call queue_draw_area on the drawing area on flush.
    pub queue_draw_area: Vec<(f64, f64, f64, f64)>,
}

impl Context {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        da: &DrawingArea,
        win: &gdk::Window,
        font: Font,
        line_space: i64,
        cols: usize,
        rows: usize,
        hl_defs: &HlDefs,
        enable_cursor_animations: bool,
    ) -> Self {
        let pango_context = da.get_pango_context();

        let font_desc = font.as_pango_font();
        pango_context.set_font_description(&font_desc);

        let mut cell_metrics = CellMetrics::default();
        cell_metrics.font = font;
        cell_metrics.line_space = line_space;
        cell_metrics.update(&pango_context);

        let w = cell_metrics.width * cols as f64;
        let h = cell_metrics.height * rows as f64;
        let surface = win
            .create_similar_surface(
                cairo::Content::Color,
                w.ceil() as i32,
                h.ceil() as i32,
            )
            .unwrap();

        let cairo_context = cairo::Context::new(&surface);

        // Fill the context with default bg color.
        cairo_context.save();
        cairo_context.set_source_rgb(
            hl_defs.default_bg.r,
            hl_defs.default_bg.g,
            hl_defs.default_bg.b,
        );
        cairo_context.paint();
        cairo_context.restore();

        let cursor_context = {
            let surface = win
                .create_similar_surface(
                    cairo::Content::ColorAlpha,
                    (cell_metrics.width * 2.0) as i32, // times two for double width chars.
                    (cell_metrics.height + cell_metrics.ascent).ceil() as i32,
                )
                .unwrap();
            cairo::Context::new(&surface)
        };

        let cursor = Cursor {
            disable_animation: !enable_cursor_animations,
            ..Cursor::default()
        };

        Context {
            cairo_context,
            cell_metrics,
            cell_metrics_update: None,
            rows: vec![],

            cursor,
            cursor_context,

            busy: false,
            active: false,

            queue_draw_area: vec![],
        }
    }

    /// Updates internals that are dependant on the drawing area.
    pub fn resize(
        &mut self,
        da: &DrawingArea,
        win: &gdk::Window,
        cols: usize,
        rows: usize,
        hl_defs: &HlDefs,
    ) {
        let prev_rows = self.rows.len();
        let prev_cols = self.rows.get(0).map(|r| r.len()).unwrap_or(0);

        if self.rows.len() != rows {
            self.rows.resize_with(rows, || Row::new(cols));
        }

        if self.rows.get(0).unwrap().len() != cols {
            for row in self.rows.iter_mut() {
                row.resize(cols);
            }
        }

        let pctx = da.get_pango_context();
        pctx.set_font_description(&self.cell_metrics.font.as_pango_font());

        self.cell_metrics.update(&pctx);

        let w = self.cell_metrics.width * cols as f64;
        let h = self.cell_metrics.height * rows as f64;
        let surface = win
            .create_similar_surface(
                cairo::Content::Color,
                w.ceil() as i32,
                h.ceil() as i32,
            )
            .unwrap();
        let ctx = cairo::Context::new(&surface);

        // Fill the context with default bg color.
        ctx.save();
        ctx.set_source_rgb(
            hl_defs.default_bg.r,
            hl_defs.default_bg.g,
            hl_defs.default_bg.b,
        );
        ctx.paint();
        ctx.restore();

        let s = self.cairo_context.get_target();
        self.cairo_context.save();
        ctx.set_source_surface(&s, 0.0, 0.0);
        ctx.set_operator(cairo::Operator::Source);
        // Make sure we only paint the area that _was_ visible before this update
        // so we don't undo the bg color paint we did earlier. Note that we're
        // calculating the used area based on the current cell metrics. This is
        // becuase if font changes that might reduce the area we "have available".
        // Otherwise, when changing to smaller font, we might draw our "old" surface
        // on a area that wont be cleared by nvim (e.g. over "fresh" whitespace).
        ctx.rectangle(
            0.0,
            0.0,
            self.cell_metrics.width * prev_cols as f64,
            self.cell_metrics.height * prev_rows as f64,
        );
        ctx.fill();
        self.cairo_context.restore();

        self.cairo_context = ctx;
    }

    /// Sets the cell metrics to be updated. If font or line_space is None,
    /// the earlier value for each is used. Call `finish_metrics_update` to
    /// make the update take place.
    pub fn update_metrics(
        &mut self,
        font: Font,
        line_space: i64,
        da: &gtk::DrawingArea,
        win: &gdk::Window,
    ) {
        let pango_context = da.get_pango_context();
        pango_context.set_font_description(&font.as_pango_font());

        self.cell_metrics.font = font;
        self.cell_metrics.line_space = line_space;
        self.cell_metrics.update(&pango_context);

        self.cursor_context = {
            let surface = win
                .create_similar_surface(
                    cairo::Content::ColorAlpha,
                    (self.cell_metrics.width * 2.0).ceil() as i32, // times two for double width chars.
                    (self.cell_metrics.height + self.cell_metrics.ascent).ceil()
                        as i32,
                )
                .unwrap();
            cairo::Context::new(&surface)
        };
    }

    /// Returns x, y, width and height for cursor position on the screen (e.g. might be in middle
    /// of an animation).
    pub fn get_cursor_rect(&self) -> (i32, i32, i32, i32) {
        let double_width = self
            .cell_at_cursor()
            .map(|cell| cell.double_width)
            .unwrap_or(false);

        // Dont use cursor.get_position here, because we want to use the position on the screen.
        let pos = self.cursor.pos.unwrap_or((0.0, 0.0));

        let cm = &self.cell_metrics;
        let (x, y) = render::get_coords(cm.height, cm.width, pos.0, pos.1);
        (
            x.floor() as i32,
            y.floor() as i32,
            if double_width {
                (cm.width * 2.0).ceil() as i32
            } else {
                cm.width.ceil() as i32
            },
            cm.height.ceil() as i32,
        )
    }

    pub fn cursor_goto(&mut self, row: u64, col: u64, clock: &gdk::FrameClock) {
        // Clear old cursor position.
        let (x, y, w, h) = self.get_cursor_rect();
        self.queue_draw_area.push((
            f64::from(x),
            f64::from(y),
            f64::from(w),
            f64::from(h),
        ));
        self.cursor
            .goto(row as f64, col as f64, clock.get_frame_time());

        // Mark the new cursor position to be drawn.
        let (x, y, w, h) = self.get_cursor_rect();
        self.queue_draw_area.push((
            f64::from(x),
            f64::from(y),
            f64::from(w),
            f64::from(h),
        ));
    }

    pub fn tick(&mut self, da: &DrawingArea, clock: &gdk::FrameClock) {
        let