summaryrefslogtreecommitdiffstats
path: root/src/process/window_size_error.rs
blob: 8188315cd4939b1487009bfb66e3555f5fd9d97e (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
use crate::constants::{MINIMUM_COMPACT_WINDOW_WIDTH, MINIMUM_WINDOW_HEIGHT, MINIMUM_WINDOW_HEIGHT_ERROR_WIDTH};
use crate::input::input_handler::InputMode;
use crate::input::Input;
use crate::process::process_module::ProcessModule;
use crate::process::process_result::ProcessResult;
use crate::process::state::State;
use crate::todo_file::TodoFile;
use crate::view::view_data::ViewData;
use crate::view::view_line::ViewLine;
use crate::view::View;

const HEIGHT_ERROR_MESSAGE: &str = "Window too small, increase height to continue";
const SHORT_ERROR_MESSAGE: &str = "Window too small";
const SIZE_ERROR_MESSAGE: &str = "Size!";
const BUG_WINDOW_SIZE_MESSAGE: &str = "Bug: window size is invalid!";

pub struct WindowSizeError {
	return_state: State,
	view_data: ViewData,
}

impl ProcessModule for WindowSizeError {
	fn activate(&mut self, _: &TodoFile, previous_state: State) -> ProcessResult {
		self.return_state = previous_state;
		ProcessResult::new()
	}

	fn build_view_data(&mut self, view: &View<'_>, _: &TodoFile) -> &ViewData {
		let view_width = view.get_view_size().width();
		let view_height = view.get_view_size().height();
		let message = if view_width <= MINIMUM_COMPACT_WINDOW_WIDTH {
			if view_width >= SHORT_ERROR_MESSAGE.len() {
				SHORT_ERROR_MESSAGE
			}
			else {
				// not much to do if the window gets too narrow
				SIZE_ERROR_MESSAGE
			}
		}
		else if view_height <= MINIMUM_WINDOW_HEIGHT {
			if view_width >= MINIMUM_WINDOW_HEIGHT_ERROR_WIDTH {
				HEIGHT_ERROR_MESSAGE
			}
			else {
				// this message will always be safe here
				SHORT_ERROR_MESSAGE
			}
		}
		else {
			BUG_WINDOW_SIZE_MESSAGE
		};

		self.view_data.clear();
		self.view_data.push_line(ViewLine::from(message));
		self.view_data.set_view_size(view_width, view_height);
		self.view_data.rebuild();
		&self.view_data
	}

	fn handle_input(&mut self, view: &mut View<'_>, _: &mut TodoFile) -> ProcessResult {
		let input = view.get_input(InputMode::Default);
		let mut result = ProcessResult::new().input(input);

		if input == Input::Resize {
			let view_width = view.get_view_size().width();
			let view_height = view.get_view_size().height();
			if !Self::is_window_too_small(view_width, view_height) {
				result = result.state(self.return_state);
			}
		}

		result
	}
}

impl WindowSizeError {
	pub const fn new() -> Self {
		Self {
			return_state: State::List,
			view_data: ViewData::new(),
		}
	}

	pub const fn is_window_too_small(window_width: usize, window_height: usize) -> bool {
		window_width <= MINIMUM_COMPACT_WINDOW_WIDTH || window_height <= MINIMUM_WINDOW_HEIGHT
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::assert_process_result;
	use crate::assert_rendered_output;
	use crate::display::size::Size;
	use crate::process::testutil::{process_module_test, TestContext, ViewState};
	use rstest::rstest;

	#[test]
	fn is_window_too_small_width_too_small() {
		assert!(WindowSizeError::is_window_too_small(
			MINIMUM_COMPACT_WINDOW_WIDTH,
			MINIMUM_WINDOW_HEIGHT + 1
		));
	}

	#[test]
	fn is_window_too_small_height_too_small() {
		assert!(WindowSizeError::is_window_too_small(
			MINIMUM_COMPACT_WINDOW_WIDTH + 1,
			MINIMUM_WINDOW_HEIGHT
		));
	}

	#[test]
	fn is_window_too_small_height_and_width_too_small() {
		assert!(WindowSizeError::is_window_too_small(
			MINIMUM_COMPACT_WINDOW_WIDTH,
			MINIMUM_WINDOW_HEIGHT
		));
	}

	#[test]
	fn is_window_too_small_width_and_height_large() {
		assert!(!WindowSizeError::is_window_too_small(
			MINIMUM_COMPACT_WINDOW_WIDTH + 1,
			MINIMUM_WINDOW_HEIGHT + 1
		));
	}

	#[rstest(
		width, height, expected,
		case::not_too_small(100, 100, "Bug: window size is invalid!"),
		case::width_too_small_long_message(SHORT_ERROR_MESSAGE.len(), MINIMUM_WINDOW_HEIGHT + 1, "Window too small"),
		case::width_too_small_short_message(SHORT_ERROR_MESSAGE.len() - 1, MINIMUM_WINDOW_HEIGHT + 1, "Size!"),
		case::height_too_small_long_message(
			MINIMUM_WINDOW_HEIGHT_ERROR_WIDTH, MINIMUM_WINDOW_HEIGHT, "Window too small, increase height to continue"
		),
		case::height_too_small_short_message(
			MINIMUM_WINDOW_HEIGHT_ERROR_WIDTH - 1, MINIMUM_WINDOW_HEIGHT, "Window too small"
		)
	)]
	#[allow(clippy::cast_possible_wrap)]
	#[serial_test::serial]
	fn build_view_data(width: usize, height: usize, expected: &str) {
		process_module_test(
			&[],
			ViewState {
				size: Size::new(width, height),
				..ViewState::default()
			},
			&[],
			|test_context: TestContext<'_>| {
				let mut module = WindowSizeError::new();
				let view_data = test_context.build_view_data(&mut module);
				assert_rendered_output!(view_data, "{BODY}", format!("{{Normal}}{}", expected));
			},
		);
	}

	#[test]
	#[serial_test::serial]
	fn input_resize_window_still_small() {
		process_module_test(
			&[],
			ViewState {
				size: Size::new(1, 1),
				..ViewState::default()
			},
			&[Input::Resize],
			|mut test_context: TestContext<'_>| {
				let mut module = WindowSizeError::new();
				test_context.activate(&mut module, State::ConfirmRebase);
				assert_process_result!(test_context.handle_input(&mut module), input = Input::Resize);
			},
		);
	}

	#[test]
	#[serial_test::serial]
	fn input_resize_window_no_longer_too_small() {
		process_module_test(
			&[],
			ViewState {
				size: Size::new(100, 100),
				..ViewState::default()
			},
			&[Input::Resize],
			|mut test_context: TestContext<'_>| {
				let mut module = WindowSizeError::new();
				test_context.activate(&mut module, State::ConfirmRebase);
				assert_process_result!(
					test_context.handle_input(&mut module),
					input = Input::Resize,
					state = State::ConfirmRebase
				);
			},
		);
	}

	#[test]
	#[serial_test::serial]
	fn input_other_character() {
		process_module_test(
			&[],
			ViewState { ..ViewState::default() },
			&[Input::Character('a')],
			|mut test_context: TestContext<'_>| {
				let mut module = WindowSizeError::new();
				test_context.activate(&mut module, State::ConfirmRebase);
				assert_process_result!(test_context.handle_input(&mut module), input = Input::Character('a'));
			},
		);
	}
}