gravel_frontend_fltk/
scroll.rs

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
use std::cmp;

/// Handles scrolling logic.
///
/// The view is always an integer offset from the top, thus
/// never displaying a partial item.
pub struct Scroll {
	length: i32,
	max_view_size: i32,
	cursor: i32,
	scroll: i32,
}

impl Scroll {
	/// Creates a new instance.
	/// - `length`: number of items in the list
	/// - `max_view_size`: number of items that can be displayed at once
	pub fn new(length: i32, max_view_size: i32) -> Self {
		Self {
			length,
			max_view_size,
			cursor: 0,
			scroll: 0,
		}
	}

	/// Move the cursor up by one item.
	pub fn cursor_up(&mut self) {
		if self.cursor <= 0 {
			self.bottom();
		} else {
			self.cursor -= 1;
			self.scroll = cmp::min(self.scroll, self.cursor);
		}
	}

	/// Move the cursor down by one item.
	pub fn cursor_down(&mut self) {
		if self.cursor >= self.length - 1 {
			self.top();
		} else {
			self.cursor += 1;
			self.scroll = cmp::max(self.scroll, self.cursor - self.view_size() + 1);
		}
	}

	/// Move the view up one page.
	pub fn page_up(&mut self) {
		if self.scroll - self.view_size() <= 0 {
			self.top();
		} else {
			self.scroll = self.scroll() - self.view_size();
			self.cursor = self.scroll;
		}
	}

	/// Move the view down one page.
	pub fn page_down(&mut self) {
		if self.scroll + self.view_size() * 2 >= self.length {
			self.bottom();
		} else {
			self.scroll = self.scroll() + self.view_size();
			self.cursor = self.scroll + self.view_size() - 1;
		}
	}

	/// Move the view and cursor to the top.
	pub fn top(&mut self) {
		self.cursor = 0;
		self.scroll = 0;
	}

	/// Move the view and cursor to the bottom.
	pub fn bottom(&mut self) {
		self.cursor = self.length - 1;
		self.scroll = self.length - self.view_size();
	}

	/// Set the number of items in the list.
	pub fn set_length(&mut self, length: i32) {
		self.length = length;
		self.top();
	}

	/// Gets the number of items that fit inside the view.
	pub fn view_size(&self) -> i32 {
		cmp::min(self.length, self.max_view_size)
	}

	/// Gets the cursor position.
	pub fn cursor(&self) -> i32 {
		self.cursor
	}

	/// Gets the views offset from the top of the list.
	pub fn scroll(&self) -> i32 {
		self.scroll
	}

	/// Gets the length of the list.
	pub fn length(&self) -> i32 {
		self.length
	}
}