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
mod scroll_bar;
mod scroll_thumb;
mod scroll_view;
mod virtual_scroll_view;

use freya_elements::events::{keyboard::Key, KeyboardEvent};
pub use scroll_bar::*;
pub use scroll_thumb::*;
pub use scroll_view::*;
pub use virtual_scroll_view::*;

#[doc(hidden)]
pub const SCROLLBAR_SIZE: u8 = 15;

// Holding alt while scrolling makes it 5x faster (VSCode behavior).
#[doc(hidden)]
pub const SCROLL_SPEED_MULTIPLIER: f32 = 5.0;

#[doc(hidden)]
#[derive(Debug, PartialEq, Eq)]
pub enum Axis {
    X,
    Y,
}

#[doc(hidden)]
pub fn get_container_size(is_scrollbar_visible: bool) -> String {
    if is_scrollbar_visible {
        format!("calc(100% - {SCROLLBAR_SIZE})")
    } else {
        "100%".to_string()
    }
}

#[doc(hidden)]
pub fn is_scrollbar_visible(
    is_scrollbar_enabled: bool,
    inner_size: f32,
    viewport_size: f32,
) -> bool {
    if is_scrollbar_enabled {
        viewport_size < inner_size
    } else {
        false
    }
}

#[doc(hidden)]
pub fn get_scrollbar_pos_and_size(
    inner_size: f32,
    viewport_size: f32,
    scroll_position: f32,
) -> (f32, f32) {
    let scrollbar_height = if viewport_size >= inner_size {
        inner_size
    } else {
        let viewable_ratio_height = viewport_size / inner_size;
        viewport_size * viewable_ratio_height
    };
    let scroll_position = (100.0 / inner_size) * -scroll_position;
    let scrollbar_position = (scroll_position / 100.0) * viewport_size;
    (scrollbar_position, scrollbar_height)
}

#[doc(hidden)]
pub fn get_scroll_position_from_cursor(
    cursor_position: f32,
    inner_size: f32,
    viewport_size: f32,
) -> i32 {
    let per = 100.0 / viewport_size * cursor_position;

    if viewport_size >= inner_size {
        return 0;
    }

    let new_position = -(inner_size / 100.0 * per);

    if new_position >= 0.0 {
        return 0;
    }

    if new_position <= -(inner_size - viewport_size) {
        return -(inner_size - viewport_size) as i32;
    }
    new_position as i32
}

#[doc(hidden)]
pub fn get_scroll_position_from_wheel(
    wheel_movement: f32,
    inner_size: f32,
    viewport_size: f32,
    scroll_position: f32,
) -> i32 {
    if viewport_size >= inner_size {
        return 0;
    }

    let new_position = scroll_position + wheel_movement;

    if new_position >= 0.0 && wheel_movement > 0.0 {
        return 0;
    }

    if new_position <= -(inner_size - viewport_size) && wheel_movement < 0.0 {
        return -(inner_size - viewport_size) as i32;
    }

    new_position as i32
}

/// Limit the scroll position to the scroll view bounds to avoid overflows
#[doc(hidden)]
pub fn get_corrected_scroll_position(
    inner_size: f32,
    viewport_size: f32,
    scroll_position: f32,
) -> f32 {
    // Considering it was a vertical scroll view, the start would be on top and the end on bottom.
    let overscrolled_start = scroll_position > 0.0;
    let overscrolled_end = (-scroll_position + viewport_size) > inner_size;

    if overscrolled_start {
        0f32
    } else if overscrolled_end {
        if viewport_size < inner_size {
            -(inner_size - viewport_size)
        } else {
            0f32
        }
    } else {
        scroll_position
    }
}

pub fn manage_key_event(
    e: KeyboardEvent,
    (mut x, mut y): (f32, f32),
    inner_height: f32,
    inner_width: f32,
    viewport_height: f32,
    viewport_width: f32,
) -> (f32, f32) {
    let y_page_delta = viewport_height;
    let y_line_delta = y_page_delta / 5.0;
    let x_page_delta = viewport_width;
    let x_line_delta = x_page_delta / 5.0;

    // TODO(tropix126): Handle spacebar and spacebar + shift as Home and End

    match e.key {
        Key::ArrowUp => {
            y = get_corrected_scroll_position(inner_height, viewport_height, y + y_line_delta)
        }
        Key::ArrowDown => {
            y = get_corrected_scroll_position(inner_height, viewport_height, y - y_line_delta)
        }
        Key::PageUp => {
            y = get_corrected_scroll_position(inner_height, viewport_height, y + y_line_delta)
        }
        Key::PageDown => {
            y = get_corrected_scroll_position(inner_height, viewport_height, y - y_line_delta)
        }
        Key::ArrowLeft => {
            x = get_corrected_scroll_position(inner_width, viewport_width, x + x_line_delta)
        }
        Key::ArrowRight => {
            x = get_corrected_scroll_position(inner_width, viewport_width, x - x_line_delta)
        }
        Key::Home => {
            y = 0.0;
        }
        Key::End => {
            y = -inner_height;
        }
        _ => {}
    };

    (x, y)
}