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
const MINIMUM_WINDOW_HEIGHT: usize = 5;
const MINIMUM_COMPACT_WINDOW_WIDTH: usize = 20;
const MINIMUM_FULL_WINDOW_WIDTH: usize = 34;
#[derive(Debug, Copy, Clone)]
pub struct RenderContext {
height: usize,
width: usize,
}
impl RenderContext {
#[must_use]
#[inline]
pub const fn new(width: u16, height: u16) -> Self {
Self {
height: height as usize,
width: width as usize,
}
}
#[inline]
pub fn update(&mut self, width: u16, height: u16) {
self.width = width as usize;
self.height = height as usize;
}
#[must_use]
#[inline]
pub const fn width(&self) -> usize {
self.width
}
#[must_use]
#[inline]
pub const fn height(&self) -> usize {
self.height
}
#[must_use]
#[inline]
pub const fn is_minimum_view_width(&self) -> bool {
self.width > MINIMUM_COMPACT_WINDOW_WIDTH
}
#[must_use]
#[inline]
pub const fn is_minimum_view_height(&self) -> bool {
self.height > MINIMUM_WINDOW_HEIGHT
}
#[must_use]
#[inline]
pub const fn is_full_width(&self) -> bool {
self.width >= MINIMUM_FULL_WINDOW_WIDTH
}
#[must_use]
#[inline]
pub const fn is_window_too_small(&self) -> bool {
!self.is_minimum_view_width() || !self.is_minimum_view_height()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn update() {
let mut context = RenderContext { width: 10, height: 20 };
context.update(100, 200);
assert_eq!(context.width(), 100);
assert_eq!(context.height(), 200);
}
#[test]
fn is_window_too_small_width_too_small() {
let context = RenderContext {
width: MINIMUM_COMPACT_WINDOW_WIDTH,
height: MINIMUM_WINDOW_HEIGHT + 1,
};
assert!(context.is_window_too_small());
}
#[test]
fn is_window_too_small_height_too_small() {
let context = RenderContext {
width: MINIMUM_COMPACT_WINDOW_WIDTH + 1,
height: MINIMUM_WINDOW_HEIGHT,
};
assert!(context.is_window_too_small());
}
#[test]
fn is_window_too_small_height_and_width_too_small() {
let context = RenderContext {
width: MINIMUM_COMPACT_WINDOW_WIDTH,
height: MINIMUM_WINDOW_HEIGHT,
};
assert!(context.is_window_too_small());
}
#[test]
fn is_window_too_small_width_and_height_large() {
let context = RenderContext {
width: MINIMUM_COMPACT_WINDOW_WIDTH + 1,
height: MINIMUM_WINDOW_HEIGHT + 1,
};
assert!(!context.is_window_too_small());
}
}