summaryrefslogtreecommitdiffstats
path: root/src/ui/common.rs
blob: cb22ef509b7419b6d3ecb420d9824007885660db (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
use futures::future::Future;

pub fn spawn_local<F: Future<Output = ()> + 'static>(f: F) {
    let c = glib::MainContext::default();
    c.spawn_local(f);
}

pub fn calc_line_space(space: i64) -> (i32, i32) {
    let half = space as f64 / 2.0;
    if half as f64 % 2.0 != 0.0 {
        (half.ceil() as i32, half.floor() as i32)
    } else {
        (half as i32, half as i32)
    }
}

/// Calculate the preferred width and x-position.
pub fn get_preferred_horizontal_position(
    area: &gdk::Rectangle,
    pos: &gdk::Rectangle,
    mut width: i32,
) -> (i32, i32) {
    let mut x = pos.x;

    let rigth = x + width;
    // If we're overflowing to the right...
    if rigth > area.width {
        let overflow = rigth - area.width;
        // Move our x position to the left, but not father that 0.
        x = (x - overflow).max(0);

        // And set our width to be either the original width, or truncate
        // it to area.width it happens to be smaller (otherwise we'd still
        // overflow).
        width = width.min(area.width);
    }

    (x, width)
}

/// Calculate the preferred height and y-position.
pub fn get_preferred_vertical_position(
    area: &gdk::Rectangle,
    pos: &gdk::Rectangle,
    mut height: i32,
) -> (i32, i32) {
    let mut y = pos.y - height;

    if y < area.y {
        let max_above = area.y + pos.y;
        let max_below = area.height - (pos.y + pos.height);

        if max_above > max_below {
            y = area.y;
            height = max_above;
        } else {
            y = pos.y + pos.height;
            height = height.min(max_below);
        }
    }

    (y, height)
}

#[cfg(test)]
mod test {

    use super::*;

    #[test]
    fn test_calc_line_space() {
        assert_eq!((1, 0), calc_line_space(1));
        assert_eq!((1, 1), calc_line_space(2));
        assert_eq!((3, 2), calc_line_space(5));
    }

    #[test]
    fn test_get_preferred_vertical_position1() {
        // Case 1: there is room just fine in the obvious position.
        let area = gdk::Rectangle {
            x: 0,
            y: 0,
            width: 300,
            height: 300,
        };
        let pos = gdk::Rectangle {
            x: 0,
            y: 30,
            width: 300,
            height: 15,
        };
        let height = 30;
        let (y, h) = get_preferred_vertical_position(&area, &pos, height);
        assert_eq!(y, 0);
        assert_eq!(h, 30);
    }

    #[test]
    fn test_get_preferred_vertical_position2() {
        // Case 2: there is no room above the `pos`, so we should position our
        // selves below the pos.
        let area = gdk::Rectangle {
            x: 0,
            y: 0,
            width: 300,
            height: 300,
        };
        let pos = gdk::Rectangle {
            x: 0,
            y: 0,
            width: 300,
            height: 15,
        };
        let height = 30;
        let (y, h) = get_preferred_vertical_position(&area, &pos, height);
        assert_eq!(y, 15);
        assert_eq!(h, 30);
    }

    #[test]
    fn test_get_preferred_vertical_position3() {
        // Case 3: there is no room above the `pos`, so we should position our
        // selves below the pos but in this case, we need to truncate our height too.
        let area = gdk::Rectangle {
            x: 0,
            y: 0,
            width: 300,
            height: 35,
        };
        let pos = gdk::Rectangle {
            x: 0,
            y: 0,
            width: 300,
            height: 15,
        };
        let height = 30;
        let (y, h) = get_preferred_vertical_position(&area, &pos, height);
        assert_eq!(y, 15);
        assert_eq!(h, 20);
    }

    #[test]
    fn test_get_preferred_vertical_position4() {
        // Case 4: there is no room above the `pos`, but below it there is even less
        // space. We should go above, but truncate our height.
        let area = gdk::Rectangle {
            x: 0,
            y: 0,
            width: 300,
            height: 40,
        };
        let pos = gdk::Rectangle {
            x: 0,
            y: 30,
            width: 300,
            height: 50,
        };
        let height = 80;
        let (y, h) = get_preferred_vertical_position(&area, &pos, height);
        assert_eq!(y, 0);
        assert_eq!(h, 30);
    }

    #[test]
    fn test_get_preferred_horizontal_position1() {
        // Case 1: Everything fits.
        let area = gdk::Rectangle {
            x: 0,
            y: 0,
            height: 0,
            width: 10,
        };

        let pos = gdk::Rectangle {
            x: 0,
            y: 0,
            width: 0,
            height: 0,
        };

        let width = 10;
        let (x, w) = get_preferred_horizontal_position(&area, &pos, width);
        assert_eq!(x, 0);
        assert_eq!(w, 10);
    }

    #[test]
    fn test_get_preferred_horizontal_position2() {
        // Case 2: Width is trucated.
        let area = gdk::Rectangle {
            x: 0,
            y: 0,
            height: 0,
            width: 5,
        };

        let pos = gdk::Rectangle {
            x: 0,
            y: 0,
            width: 0,
            height: 0,
        };

        let width = 10;
        let (x, w) = get_preferred_horizontal_position(&area, &pos, width);
        assert_eq!(x, 0);
        assert_eq!(w, 5);
    }

    #[test]
    fn test_get_preferred_horizontal_position3() {
        // Case 3: X is moved to left.
        let area = gdk::Rectangle {
            x: 0,
            y: 0,
            height: 0,
            width: 20,
        };

        let pos = gdk::Rectangle {
            x: 15,
            y: 0,
            width: 0,
            height: 0,
        };

        let width = 15;
        let (x, w) = get_preferred_horizontal_position(&area, &pos, width);
        assert_eq!(x, 5);
        assert_eq!(w, 15);
    }

    #[test]
    fn test_get_preferred_horizontal_position4() {
        // Case 4: X is moved to left and width is truncated
        let area = gdk::Rectangle {
            x: 0,
            y: 0,
            height: 0,
            width: 20,
        };

        let pos = gdk::Rectangle {
            x: 15,
            y: 0,
            width: 0,
            height: 0,
        };

        let width = 150;
        let (x, w) = get_preferred_horizontal_position(&area, &pos, width);
        assert_eq!(x, 0);
        assert_eq!(w, 20);
    }
}