aboutsummaryrefslogtreecommitdiffstats
path: root/src/frames.rs
blob: 268636ec9f182c692b7d4d90598850124f91b32c (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
use zellij_tile::prelude::*;

pub struct FrameConfig {
    pub hide_frames_for_single_pane: bool,
    pub hide_frames_except_for_search: bool,
    pub hide_frames_except_for_fullscreen: bool,
}

impl FrameConfig {
    pub fn new(
        hide_frames_for_single_pane: bool,
        hide_frames_except_for_search: bool,
        hide_frames_except_for_fullscreen: bool,
    ) -> Self {
        Self {
            hide_frames_for_single_pane,
            hide_frames_except_for_search,
            hide_frames_except_for_fullscreen,
        }
    }

    pub fn is_disabled(&self) -> bool {
        !self.hide_frames_for_single_pane
            && !self.hide_frames_except_for_search
            && !self.hide_frames_except_for_fullscreen
    }
}

#[tracing::instrument(skip_all)]
pub fn hide_frames_conditionally(
    config: &FrameConfig,
    tabs: &[TabInfo],
    pane_info: &PaneManifest,
    mode_info: &ModeInfo,
    plugin_pane_id: PluginIds,
    is_zjframes: bool,
) {
    if config.is_disabled() {
        return;
    }

    let panes = match get_current_panes(tabs, pane_info) {
        Some(panes) => panes,
        None => return,
    };

    // check if we are running for the current tab since one plugin will run for
    // each tab. If we do not prevent execution, the screen will start to flicker
    // 'cause every plugin will try to toggle the frames.
    //
    // This is only relevant for zjstatus, not zjframes; as zjframes only
    // runs once per session.
    if !is_plugin_for_current_tab(&panes, plugin_pane_id) && !is_zjframes {
        return;
    }

    let panes: Vec<&PaneInfo> = panes
        .iter()
        .filter(|p| !p.is_plugin && !p.is_floating)
        .collect();

    let frame_enabled = panes.iter().any(|p| p.pane_content_x - p.pane_x > 0);

    let frames_for_search =
        config.hide_frames_except_for_search && should_show_frames_for_search(mode_info);
    let frames_for_fullscreen =
        config.hide_frames_except_for_fullscreen && should_show_frames_for_fullscreen(&panes);
    let frames_for_single_pane = config.hide_frames_for_single_pane
        && should_show_frames_for_multiple_panes(mode_info, &panes);

    if (frames_for_search || frames_for_fullscreen || frames_for_single_pane) && !frame_enabled {
        toggle_pane_frames();
    }

    if (!frames_for_search && !frames_for_fullscreen && !frames_for_single_pane) && frame_enabled {
        toggle_pane_frames();
    }
}

pub fn should_show_frames_for_search(mode_info: &ModeInfo) -> bool {
    mode_info.mode == InputMode::EnterSearch || mode_info.mode == InputMode::Search
}

pub fn should_show_frames_for_fullscreen(panes: &[&PaneInfo]) -> bool {
    let active_pane = match panes.iter().find(|p| p.is_focused) {
        Some(p) => p,
        None => return false,
    };

    active_pane.is_fullscreen
}

#[tracing::instrument(skip_all)]
pub fn should_show_frames_for_multiple_panes(mode_info: &ModeInfo, panes: &[&PaneInfo]) -> bool {
    tracing::debug!("mode: {:?}", mode_info.mode);
    if mode_info.mode == InputMode::RenamePane
        || mode_info.mode == InputMode::Search
        || mode_info.mode == InputMode::EnterSearch
    {
        return true;
    }

    panes.len() > 1
}

fn is_plugin_for_current_tab(panes: &[PaneInfo], plugin_pane_id: PluginIds) -> bool {
    panes
        .iter()
        .any(|p| p.is_plugin && p.id == plugin_pane_id.plugin_id)
}

fn get_current_panes(tabs: &[TabInfo], pane_info: &PaneManifest) -> Option<Vec<PaneInfo>> {
    let active_tab = tabs.iter().find(|t| t.active);
    let active_tab = active_tab.as_ref()?;

    pane_info.panes.get(&active_tab.position).cloned()
}