aboutsummaryrefslogtreecommitdiffstats
path: root/zellij-server/src/ui/pane_contents_and_ui.rs
blob: faf72875d13c58390cee278e1d213065c9ab0367 (plain) (blame)
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
use crate::output::{CharacterChunk, Output};
use crate::panes::terminal_character::AnsiCode;
use crate::panes::PaneId;
use crate::tab::Pane;
use crate::ui::grayscale::grayscale_character_chunks;
use crate::ui::boundaries::Boundaries;
use crate::ui::pane_boundaries_frame::{FrameParams, StackListEntry};
use crate::ClientId;
use std::collections::{HashMap, HashSet};
use zellij_utils::data::{client_id_to_colors, InputMode, PaletteColor, Style};
use zellij_utils::input::theme::StackListStyle;
use zellij_utils::errors::prelude::*;
use zellij_utils::pane_size::PaneGeom;

pub fn dim_character_chunks(character_chunks: &mut Vec<CharacterChunk>) {
    for chunk in character_chunks.iter_mut() {
        for terminal_character in chunk.terminal_characters.iter_mut() {
            terminal_character.styles.update(|styles| {
                styles.dim = Some(AnsiCode::On);
            });
        }
    }
}

pub struct PaneContentsAndUi<'a> {
    pane: &'a mut Box<dyn Pane>,
    output: &'a mut Output,
    style: Style,
    focused_clients: Vec<ClientId>,
    multiple_users_exist_in_session: bool,
    z_index: Option<usize>,
    pane_is_stacked_under: bool,
    pane_is_stacked_over: bool,
    should_draw_pane_frames: bool,
    mouse_is_hovering_over_pane_for_clients: HashSet<ClientId>,
    current_pane_group: HashMap<ClientId, Vec<PaneId>>,
    show_help_text: bool,
    omit_title: bool,
    frame_geom_override: Option<PaneGeom>,
    stack_list_entry_width: Option<usize>,
    stack_list_entry_is_selected: bool,
    stack_list_entry_stack_is_focused: bool,
    stack_list_entry_index: Option<usize>,
    stack_list_entry_style: StackListStyle,
    blank_title: bool,
    mouse_scroll_resize: bool,
    dimmed_for_clients: HashSet<ClientId>,
}

impl<'a> PaneContentsAndUi<'a> {
    pub fn new(
        pane: &'a mut Box<dyn Pane>,
        output: &'a mut Output,
        style: Style,
        active_panes: &HashMap<ClientId, PaneId>,
        multiple_users_exist_in_session: bool,
        z_index: Option<usize>,
        pane_is_stacked_under: bool,
        pane_is_stacked_over: bool,
        should_draw_pane_frames: bool,
        mouse_hover_pane_id: &HashMap<ClientId, PaneId>,
        current_pane_group: HashMap<ClientId, Vec<PaneId>>,
        show_help_text: bool,
        omit_title: bool,
        mouse_scroll_resize: bool,
        dimmed_for_clients: HashSet<ClientId>,
    ) -> Self {
        let mut focused_clients: Vec<ClientId> = active_panes
            .iter()
            .filter(|(_c_id, p_id)| **p_id == pane.pid())
            .map(|(c_id, _p_id)| *c_id)
            .collect();
        focused_clients.sort_unstable();
        let mouse_is_hovering_over_pane_for_clients = mouse_hover_pane_id
            .iter()
            .filter_map(|(client_id, pane_id)| {
                if pane_id == &pane.pid() {
                    Some(*client_id)
                } else {
                    None
                }
            })
            .collect();
        PaneContentsAndUi {
            pane,
            output,
            style,
            focused_clients,
            multiple_users_exist_in_session,
            z_index,
            pane_is_stacked_under,
            pane_is_stacked_over,
            should_draw_pane_frames,
            mouse_is_hovering_over_pane_for_clients,
            current_pane_group,
            show_help_text,
            omit_title,
            frame_geom_override: None,
            stack_list_entry_width: None,
            stack_list_entry_is_selected: false,
            stack_list_entry_stack_is_focused: false,
            stack_list_entry_index: None,
            stack_list_entry_style: StackListStyle::default(),
            blank_title: false,
            mouse_scroll_resize,
            dimmed_for_clients,
        }
    }
    fn frame_is_dimmed_for_client(&self, client_id: ClientId) -> bool {
        self.dimmed_for_clients.contains(&client_id) && !self.focused_clients.contains(&client_id)
    }
    pub fn set_frame_geom_override(&mut self, frame_geom_override: Option<PaneGeom>) {
        self.frame_geom_override = frame_geom_override;
    }
    pub fn set_blank_title(&mut self, blank_title: bool) {
        self.blank_title = blank_title;
    }
    pub fn set_stack_list_entry(
        &mut self,
        stack_list_entry_width: Option<usize>,
        stack_list_entry_is_selected: bool,
        stack_list_entry_stack_is_focused: bool,
        stack_list_entry_index: Option<usize>,
        stack_list_entry_style: StackListStyle,
    ) {
        self.stack_list_entry_width = stack_list_entry_width;
        self.stack_list_entry_is_selected = stack_list_entry_is_selected;
        self.stack_list_entry_stack_is_focused = stack_list_entry_stack_is_focused;
        self.stack_list_entry_index = stack_list_entry_index;
        self.stack_list_entry_style = stack_list_entry_style;
    }
    pub fn render_pane_contents_to_multiple_clients(
        &mut self,
        clients: impl Iterator<Item = ClientId>,
    ) -> Result<()> {
        let err_context = "failed to render pane contents to multiple clients";

        // here we drop the fake cursors so that their lines will be updated
        // and we can clear them from the UI below
        drop(self.pane.drain_fake_cursors());

        if let Some((character_chunks, raw_vte_output, sixel_image_chunks, kitty_image_chunks)) =
            self.pane.render(None).context(err_context)?
        {
            let clients: Vec<ClientId> = clients.collect();
            self.output
                .add_character_chunks_to_multiple_clients(
                    character_chunks,
                    clients.iter().copied(),
                    self.z_index,
                )
                .context(err_context)?;
            self.output.add_sixel_image_chunks_to_multiple_clients(
                sixel_image_chunks,
                clients.iter().copied(),
                self.z_index,
            );
            self.output.add_kitty_image_chunks_to_multiple_clients(
                self.pane.pid(),
                kitty_image_chunks,
                clients.iter().copied(),
                self.z_index,
            );
            if let Some(raw_vte_output) = raw_vte_output {
                if !raw_vte_output.is_empty() {
                    self.output.add_post_vte_instruction_to_multiple_clients(
                        clients.iter().copied(),
                        &format!(
                            "\u{1b}[{};{}H\u{1b}[m{}",
                            self.pane.y() + 1,
                            self.pane.x() + 1,
                            raw_vte_output
                        ),
                    );
                }
            }
        }
        Ok(())
    }
    pub fn render_pane_contents_for_client(&mut self, client_id: ClientId) -> Result<()> {
        let err_context = || format!("failed to render pane contents for client {client_id}");

        if let Some((mut character_chunks, raw_vte_output, sixel_image_chunks, kitty_image_chunks)) =
            self.pane
                .render(Some(client_id))
                .with_context(err_context)?
        {
            // Drain the colour out of panes this client is not focused on.
            // NOTE this cannot be done on the shared multi-client path: focus
            // is per client, so two clients looking at the same pane may need
            // different output.
            let grayscale = self.style.inactive_pane_grayscale;
            if grayscale > 0 && !self.focused_clients.contains(&client_id) {
                grayscale_character_chunks(&mut character_chunks, grayscale);
            }
            self.output
                .add_character_chunks_to_client(client_id, character_chunks, self.z_index)
                .with_context(err_context)?;
            self.output.add_sixel_image_chunks_to_client(
                client_id,
                sixel_image_chunks,
                self.z_index,
            );
            self.output.add_kitty_image_chunks_to_client(
                client_id,
                self.pane.pid(),
                kitty_image_chunks,
                self.z_index,
            );
            if let Some(raw_vte_output) = raw_vte_output {
                self.output.add_post_vte_instruction_to_client(
                    client_id,
                    &format!(
                        "\u{1b}[{};{}H\u{1b}[m{}",
                        self.pane.y() + 1,
                        self.pane.x() + 1,
                        raw_vte_output
                    ),
                );
            }
        }
        Ok(())
    }
    pub fn client_has_guest_modal(&self, client_id: ClientId) -> bool {
        self.pane.guest_modal_selection(client_id).is_some()
    }
    pub fn drain_pane_render_state(&mut self) {
        drop(self.pane.drain_fake_cursors());
        let _ = self.pane.render(None);
    }
    pub fn render_guest_modal_for_client(&mut self, client_id: ClientId) -> Result<()> {
        let err_context = || format!("failed to render guest modal for client {client_id}");
        let selection = self.pane.guest_modal_selection(client_id).unwrap_or(0);
        let session_name = self
            .pane
            .guest_session_name()
            .unwrap_or_else(|| String::from("unknown"));
        let columns = self.pane.get_content_columns();
        let rows = self.pane.get_content_rows();
        let content_x = self.pane.get_content_x();
        let content_y = self.pane.get_content_y();
        let shortcuts = self.pane.guest_modal_shortcuts();
        let chunks = crate::panes::nested_session_modal::guest_modal_chunks(
            columns,
            rows,
            content_x,
            content_y,
            &self.style,
            &session_name,
            selection,
            &shortcuts,
        );
        self.output
            .add_character_chunks_to_client(client_id, chunks, self.z_index)
            .with_context(err_context)?;
        Ok(())
    }
    pub fn render_fake_cursor_if_needed(&mut self, client_id: ClientId) -> Result<()> {
        let pane_focused_for_client_id = self.focused_clients.contains(&client_id);
        let pane_focused_for_different_client = self
            .focused_clients
            .iter()
            .filter(|&&c_id| c_id != client_id)
            .count()
            > 0;
        if pane_focused_for_different_client && !pane_focused_for_client_id {
            let fake_cursor_client_id = self
                .focused_clients
                .iter()
                .find(|&&c_id| c_id != client_id)
                .with_context(|| {
                    format!("failed to render fake cursor if needed for client {client_id}")
                })?;
            if let Some(colors) = client_id_to_colors(
                *fake_cursor_client_id,
                self.style.colors.multiplayer_user_colors,
            ) {
                let cursor_is_visible = self
                    .pane
                    .cursor_coordinates(Some(*fake_cursor_client_id))
                    .and_then(|(x, y, is_visible)| {
                        if is_visible {
                            Some(self.output.cursor_is_visible(
                                self.pane.x() + x,
                                self.pane.y() + y,
                                self.z_index,
                            ))
                        } else {
                            None
                        }
                    })
                    .unwrap_or(false);
                if cursor_is_visible {
                    if let Some(vte_output) = self.pane.render_fake_cursor(colors.0, colors.1) {
                        self.output.add_post_vte_instruction_to_client(
                            client_id,
                            &format!(
                                "\u{1b}[{};{}H\u{1b}[m{}",
                                self.pane.y() + 1,
                                self.pane.x() + 1,
                                vte_output
                            ),
                        );
                    }
                }
            }
        }
        Ok(())
    }
    pub fn render_terminal_title_if_needed(
        &mut self,
        client_id: ClientId,
        client_mode: InputMode,
        previous_title: &mut Option<String>,
    ) {
        if !self.focused_clients.contains(&client_id) {
            return;
        }
        let vte_output = self.pane.render_terminal_title(client_mode);
        if let Some(previous_title) = previous_title {
            if *previous_title == vte_output {
                return;
            }
        }
        *previous_title = Some(vte_output.clone());
        self.output
            .add_post_vte_instruction_to_client(client_id, &vte_output);
    }
    pub fn render_pane_frame(
        &mut self,
        client_id: ClientId,
        client_mode: InputMode,
        session_is_mirrored: bool,
        pane_is_floating: bool,
        pane_is_selectable: bool,
    ) -> Result<()> {
        let err_context = || format!("failed to render pane frame for client {client_id}");

        let pane_focused_for_client_id = self.focused_clients.contains(&client_id);
        let other_focused_clients: Vec<ClientId> = self
            .focused_clients
            .iter()
            .filter(|&&c_id| c_id != client_id)
            .copied()
            .collect();
        let pane_focused_for_differet_client = !other_focused_clients.is_empty();

        let frame_color = self.frame_color(client_id, client_mode, session_is_mirrored);
        let highlight_tooltip = self.pane.cached_hover_tooltip();
        let focused_client = if pane_focused_for_client_id {
            Some(client_id)
        } else if pane_focused_for_differet_client {
            Some(*other_focused_clients.first().with_context(err_context)?)
        } else {
            None
        };
        let pane_is_stacked = self.pane.current_geom().is_stacked();
        let pane_is_in_group = self
            .current_pane_group
            .get(&client_id)
            .map(|p| p.contains(&self.pane.pid()))
            .unwrap_or(false);
        let stack_list_entry = self.stack_list_entry_width.map(|width| StackListEntry {
            width,
            label: self.pane.stack_list_entry_label(),
            is_selected: self.stack_list_entry_is_selected,
            stack_is_focused: self.stack_list_entry_stack_is_focused,
            index: self.stack_list_entry_index,
            style: self.stack_list_entry_style,
            is_emphasized: pane_is_in_group
                || (self
                    .mouse_is_hovering_over_pane_for_clients
                    .contains(&client_id)
                    && !pane_focused_for_client_id),
        });
        let frame_is_dimmed = self.frame_is_dimmed_for_client(client_id);
        let guest_choice_indicator = self.pane.guest_choice_indicator(client_id);
        let frame_params = if session_is_mirrored {
            FrameParams {
                focused_client,
                is_main_client: pane_focused_for_client_id,
                other_focused_clients: vec![],
                style: self.style,
                color: frame_color.map(|c| c.0),
                other_cursors_exist_in_session: false,
                pane_is_stacked_over: self.pane_is_stacked_over,
                pane_is_stacked_under: self.pane_is_stacked_under,
                pane_is_stacked,
                should_draw_pane_frames: self.should_draw_pane_frames,
                pane_is_floating,
                content_offset: self.pane.get_content_offset(),
                mouse_is_hovering_over_pane: self
                    .mouse_is_hovering_over_pane_for_clients
                    .contains(&client_id),
                pane_is_selectable,
                show_help_text: self.show_help_text,
                status_bar: self.pane.status_bar_text(),
                status_bar_buttons: self.pane.status_bar_buttons(),
                hovered_status_bar_button: self.pane.hovered_status_bar_button(client_id),
                highlight_tooltip: highlight_tooltip.clone(),
                omit_title: self.omit_title,
                frame_geom_override: self.frame_geom_override,
                stack_list_entry: stack_list_entry.clone(),
                blank_title: self.blank_title,
                mouse_scroll_resize: self.mouse_scroll_resize,
                dimmed: frame_is_dimmed,
                guest_choice_indicator,
            }
        } else {
            FrameParams {
                focused_client,
                is_main_client: pane_focused_for_client_id,
                other_focused_clients,
                style: self.style,
                color: frame_color.map(|c| c.0),
                other_cursors_exist_in_session: self.multiple_users_exist_in_session,
                pane_is_stacked_over: self.pane_is_stacked_over,
                pane_is_stacked_under: self.pane_is_stacked_under,
                pane_is_stacked,
                should_draw_pane_frames: self.should_draw_pane_frames,
                pane_is_floating,
                content_offset: self.pane.get_content_offset(),
                mouse_is_hovering_over_pane: self
                    .mouse_is_hovering_over_pane_for_clients
                    .contains(&client_id),
                pane_is_selectable,
                show_help_text: self.show_help_text,
                status_bar: self.pane.status_bar_text(),
                status_bar_buttons: self.pane.status_bar_buttons(),
                hovered_status_bar_button: self.pane.hovered_status_bar_button(client_id),
                highlight_tooltip,
                omit_title: self.omit_title,
                frame_geom_override: self.frame_geom_override,
                stack_list_entry,
                blank_title: self.blank_title,
                mouse_scroll_resize: self.mouse_scroll_resize,
                dimmed: frame_is_dimmed,
                guest_choice_indicator,
            }
        };

        if let Some((mut frame_terminal_characters, vte_output)) = self
            .pane
            .render_frame(client_id, frame_params, client_mode)
            .with_context(err_context)?
        {
            if self.frame_is_dimmed_for_client(client_id) {
                dim_character_chunks(&mut frame_terminal_characters);
            }
            self.output
                .add_character_chunks_to_client(client_id, frame_terminal_characters, self.z_index)
                .with_context(err_context)?;
            if let Some(vte_output) = vte_output {
                self.output
                    .add_post_vte_instruction_to_client(client_id, &vte_output);
            }
        }

        Ok(())
    }
    pub fn render_pane_boundaries(
        &self,
        client_id: ClientId,
        client_mode: InputMode,
        boundaries: &mut Boundaries,
        session_is_mirrored: bool,
        pane_is_on_top_of_stack: bool,
        pane_is_on_bottom_of_stack: bool,
    ) {
        let color = self.frame_color(client_id, client_mode, session_is_mirrored);
        boundaries.add_rect(
            self.pane.as_ref(),
            color,
            pane_is_on_top_of_stack,
            pane_is_on_bottom_of_stack,
            self.pane_is_stacked_under,
        );
    }
    fn frame_color(
        &self,
        client_id: ClientId,
        mode: InputMode,
        session_is_mirrored: bool,
    ) -> Option<(PaletteColor, usize)> {
        // (color, color_precedence) (the color_precedence is used
        // for the no-pane-frames mode)
        let pane_focused_for_client_id = self.focused_clients.contains(&client_id);
        let pane_is_in_group = self
            .current_pane_group
            .get(&client_id)
            .map(|p| p.contains(&self.pane.pid()))
            .unwrap_or(false);
        if self.pane.frame_color_override().is_some() && !pane_is_in_group {
            self.pane
                .frame_color_override()
                .map(|override_color| (override_color, 4))
        } else if pane_is_in_group && !pane_focused_for_client_id {
            Some((self.style.colors.frame_highlight.emphasis_0, 2))
        } else if pane_is_in_group && pane_focused_for_client_id {
            Some((self.style.colors.frame_highlight.emphasis_1, 3))
        } else if pane_focused_for_client_id {
            match mode {
                InputMode::Normal | InputMode::Locked => {
                    if session_is_mirrored || !self.multiple_users_exist_in_session {
                        Some((self.style.colors.frame_selected.base, 3))
                    } else {
                        let colors = client_id_to_colors(
                            client_id,
                            self.style.colors.multiplayer_user_colors,
                        );
                        colors.map(|colors| (colors.0, 3))
                    }
                },
                _ => Some((self.style.colors.frame_highlight.base, 3)),
            }
        } else if self
            .mouse_is_hovering_over_pane_for_clients
            .contains(&client_id)
        {
            Some((self.style.colors.frame_highlight.base, 1))
        } else {
            self.style
                .colors
                .frame_unselected
                .map(|frame| (frame.base, 0))
        }
    }
}