aboutsummaryrefslogtreecommitdiffstats
path: root/default-plugins/mobile/src/frame.rs
blob: 9428de272c844eda4c69f40091416318b26bf662 (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
use zellij_tile::prelude::*;

use crate::click::{slop_key, ClickAction, ClickRegion};

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum LastEmittedCursor {
    #[default]
    Unknown,
    Sent(Option<(usize, usize)>),
}

#[derive(Default)]
pub struct Frame {
    pub click_regions: Vec<ClickRegion>,
    pub last_render_rows: usize,
    pub last_render_cols: usize,
    pub last_emitted_cursor: LastEmittedCursor,
    pub soft_keyboard_visible: bool,
}

impl Frame {
    pub fn emit_cursor(&mut self, new_pos: Option<(usize, usize)>) {
        let needs_emit = match self.last_emitted_cursor {
            LastEmittedCursor::Unknown => true,
            LastEmittedCursor::Sent(prev) => prev != new_pos,
        };
        if needs_emit {
            show_cursor(new_pos);
            self.last_emitted_cursor = LastEmittedCursor::Sent(new_pos);
        }
    }

    pub fn click_to_action(&self, row: usize, col: usize) -> Option<ClickAction> {
        for region in &self.click_regions {
            if region.priority == 0
                && region.row_start <= row
                && row < region.row_end
                && col >= region.col_start
                && col < region.col_end
            {
                return Some(region.action.clone());
            }
        }
        let mut best: Option<(&ClickRegion, u64)> = None;
        for region in &self.click_regions {
            if region.priority == 0 {
                continue;
            }
            if row < region.row_start || row >= region.row_end {
                continue;
            }
            if col < region.col_start || col >= region.col_end {
                continue;
            }
            let Some((cx, cy)) = region.center else {
                continue;
            };
            let dx = (cx as i64 - col as i64).unsigned_abs();
            let dy = (cy as i64 - row as i64).unsigned_abs();
            let dist_sq = dx * dx + dy * dy;
            best = Some(match best {
                None => (region, dist_sq),
                Some((cur, cur_d)) if dist_sq < cur_d => (region, dist_sq),
                Some((cur, cur_d)) if dist_sq == cur_d => {
                    let cur_key = slop_key(cur);
                    let new_key = slop_key(region);
                    if new_key < cur_key {
                        (region, dist_sq)
                    } else {
                        (cur, cur_d)
                    }
                },
                Some(prev) => prev,
            });
        }
        best.map(|(r, _)| r.action.clone())
    }
}

pub fn chrome_offsets(
    rows: usize,
    suppress_top_bar: bool,
    soft_keyboard_visible: bool,
) -> (usize, usize) {
    let body_top = if suppress_top_bar { 0 } else { 1 };
    let bar_height = if soft_keyboard_visible && rows.saturating_sub(body_top) >= 2 {
        1
    } else {
        0
    };
    (body_top, bar_height)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::components::modifier_bar::CellId;

    fn kb(id: u16) -> ClickAction {
        ClickAction::Keyboard(CellId(id))
    }

    #[test]
    fn tight_wins_over_overlapping_slop() {
        let mut f = Frame::default();
        f.click_regions.push(ClickRegion::tight(5, 10, 13, kb(1)));
        f.click_regions
            .push(ClickRegion::slop(5, 9, 14, kb(1), (11, 5)));
        f.click_regions.push(ClickRegion::tight(5, 13, 16, kb(2)));
        f.click_regions
            .push(ClickRegion::slop(5, 12, 17, kb(2), (14, 5)));
        assert_eq!(f.click_to_action(5, 12), Some(kb(1)));
        assert_eq!(f.click_to_action(5, 13), Some(kb(2)));
    }

    #[test]
    fn slop_resolves_by_nearest_center() {
        let mut f = Frame::default();
        f.click_regions.push(ClickRegion::tight(5, 10, 13, kb(1)));
        for r in 4..=6 {
            f.click_regions
                .push(ClickRegion::slop(r, 9, 14, kb(1), (11, 5)));
        }
        f.click_regions.push(ClickRegion::tight(7, 10, 13, kb(2)));
        for r in 6..=8 {
            f.click_regions
                .push(ClickRegion::slop(r, 9, 14, kb(2), (11, 7)));
        }
        assert_eq!(f.click_to_action(6, 11), Some(kb(1)));
        assert_eq!(f.click_to_action(8, 11), Some(kb(2)));
    }

    #[test]
    fn miss_returns_none() {
        let mut f = Frame::default();
        f.click_regions.push(ClickRegion::tight(5, 10, 13, kb(1)));
        f.click_regions
            .push(ClickRegion::slop(5, 9, 14, kb(1), (11, 5)));
        assert!(f.click_to_action(0, 0).is_none());
        assert!(f.click_to_action(5, 20).is_none());
    }
}