summaryrefslogtreecommitdiffstats
path: root/src/util.rs
blob: 81eac5d31b846186894b5b39632571fa1f3255ff (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
use crate::input::{Event, StandardEvent};

#[macro_export]
macro_rules! select {
	(default $default: expr, $first: expr) => {
		if let Some(value) = $first() {
			value
		}
		else {
			$default()
		}
	};
	(default $default: expr, $first: expr, $($arg:expr),*) => {
		if let Some(value) = $first() {
			value
		}
		$(else if let Some(value) = $arg() {
			value
		})*
		else {
			$default()
		}
	};
}

#[macro_export]
macro_rules! first {
	($first: expr, $($arg:expr),*) => {
		if $first().is_some() {
		}
		$(else if $arg().is_some() {
		})*
	};
}

/// Utility function to handle scroll events.
#[must_use]
pub(crate) fn handle_view_data_scroll(event: Event, view_state: &crate::view::State) -> Option<Event> {
	match event {
		Event::Standard(StandardEvent::ScrollLeft) => view_state.scroll_left(),
		Event::Standard(StandardEvent::ScrollRight) => view_state.scroll_right(),
		Event::Standard(StandardEvent::ScrollDown) => view_state.scroll_down(),
		Event::Standard(StandardEvent::ScrollUp) => view_state.scroll_up(),
		Event::Standard(StandardEvent::ScrollTop) => view_state.scroll_top(),
		Event::Standard(StandardEvent::ScrollBottom) => view_state.scroll_bottom(),
		Event::Standard(StandardEvent::ScrollJumpDown) => view_state.scroll_page_down(),
		Event::Standard(StandardEvent::ScrollJumpUp) => view_state.scroll_page_up(),
		_ => return None,
	};
	Some(event)
}

#[cfg(test)]
mod tests {
	use captur::capture;
	use rstest::rstest;

	use super::*;
	use crate::test_helpers::with_view_state;

	#[rstest]
	#[case::scroll_left(StandardEvent::ScrollLeft, "ScrollLeft")]
	#[case::scroll_right(StandardEvent::ScrollRight, "ScrollRight")]
	#[case::scroll_down(StandardEvent::ScrollDown, "ScrollDown")]
	#[case::scroll_up(StandardEvent::ScrollUp, "ScrollUp")]
	#[case::scroll_top(StandardEvent::ScrollTop, "ScrollTop")]
	#[case::scroll_bottom(StandardEvent::ScrollBottom, "ScrollBottom")]
	#[case::jump_down(StandardEvent::ScrollJumpDown, "PageDown")]
	#[case::jump_up(StandardEvent::ScrollJumpUp, "PageUp")]
	fn handle_view_data_scroll_event(#[case] meta_event: StandardEvent, #[case] action: &str) {
		with_view_state(|context| {
			capture!(action);
			let event = Event::from(meta_event);
			assert_eq!(handle_view_data_scroll(event, &context.state), Some(event));
			context.assert_render_action(&[action]);
		});
	}

	#[test]
	fn handle_view_data_scroll_event_other() {
		with_view_state(|context| {
			let event = Event::from('a');
			assert!(handle_view_data_scroll(event, &context.state).is_none());
			context.assert_render_action(&[]);
		});
	}
}