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
//! A module to describe regions of the screen that can be rendered to.
/// The part of the text to render if the full text cannot be rendered
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[allow(missing_docs)]
pub enum RenderRegion {
Top,
Middle,
Bottom,
}
impl Default for RenderRegion {
fn default() -> Self {
RenderRegion::Middle
}
}
/// `Layout` represents a portion of the screen that is available to be rendered to.
///
/// Assume the highlighted part of the block below is the place available for rendering
/// in the given box
/// ```text
/// ____________
/// | |
/// | ███████|
/// | ██████████|
/// | ██████████|
/// '------------'
/// ```
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Default)]
pub struct Layout {
/// ```text
/// ____________
/// | vvv-- line_offset
/// | ███████|
/// | ██████████|
/// | ██████████|
/// '------------'
/// ```
pub line_offset: u16,
/// ```text
/// ____________
/// |vv-- offset_x
/// | ███████|
/// | ██████████|
/// | ██████████|
/// '------------'
/// ```
pub offset_x: u16,
/// ```text
/// .-- offset_y
/// |'> |
/// | ███████|
/// | ██████████|
/// | ██████████|
/// '------------'
/// ```
pub offset_y: u16,
/// ```text
/// ____________
/// | |
/// | ███████|
/// | ██████████|
/// | ██████████|
/// '------------'
/// ^^^^^^^^^^^^-- width
/// ```
pub width: u16,
/// ```text
/// _____ height --.
/// | | <'
/// | ███████| <'
/// | ██████████| <'
/// | ██████████| <'
/// '------------'
/// ```
pub height: u16,
/// ```text
/// ____________
/// |.-- max_height
/// |'> ███████|
/// |'>██████████|
/// |'>██████████|
/// '------------'
/// ```
pub max_height: u16,
/// The region to render if full text cannot be rendered
pub render_region: RenderRegion,
}
impl Layout {
/// Creates a new `Layout`.
pub fn new(line_offset: u16, size: crate::backend::Size) -> Self {
Self {
line_offset,
offset_x: 0,
offset_y: 0,
width: size.width,
height: size.height,
max_height: size.height,
render_region: RenderRegion::Top,
}
}
/// Creates a new `Layout` with given `line_offset`.
pub fn with_line_offset(mut self, line_offset: u16) -> Self {
self.line_offset = line_offset;
self
}
/// Creates a new `Layout` with given `width` and `height`.
pub fn with_size(mut self, size: crate::backend::Size) -> Self {
self.set_size(size);
self
}
/// Creates a new `Layout` with new `offset_x` and `offset_y`.
pub fn with_offset(mut self, offset_x: u16, offset_y: u16) -> Self {
self.offset_x = offset_x;
self.offset_y = offset_y;
self
}
/// Creates a new `Layout` with new `render_region`.
pub fn with_render_region(mut self, region: RenderRegion) -> Self {
self.render_region = region;
self
}
/// Creates a new `Layout` with new `max_height`.
pub fn with_max_height(mut self, max_height: u16) -> Self {
self.max_height = max_height;
self
}
/// Creates a new `Layout` that represents a region past the `cursor_pos`. `cursor_pos` is
/// relative to `offset_x` and `offset_y`.
pub fn with_cursor_pos(mut self, cursor_pos: (u16, u16)) -> Self {
self.line_offset = cursor_pos.0;
self.offset_y = cursor_pos.1;
self
}
/// Sets the `width` and `height` of the layout.
pub fn set_size(&mut self, terminal_size: crate::backend::Size) {
self.width = terminal_size.width;
self.height = terminal_size.height;
}
/// Converts a `cursor_pos` relative to (`offset_x`, `offset_y`) to be relative to (0, 0)
pub fn offset_cursor(&self, cursor_pos: (u16, u16)) -> (u16, u16) {
(self.offset_x + cursor_pos.0, self.offset_y + cursor_pos.1)
}
/// Gets the width of renderable space on the first line.
///
/// ```text
/// ____________
/// | vvvvvvv-- line_width
/// | ███████|
/// | ██████████|
/// | ██████████|
/// '------------'
/// ```
pub fn line_width(&self) -> u16 {
self.available_width() - self.line_offset
}
/// Gets the width of renderable space on subsequent lines.
///
/// ```text
/// ____________
/// | vvvvvvvvvv-- available_width
/// | ███████|
/// | ██████████|
/// | ██████████|
/// '------------'
/// ```
pub fn available_width(&self) -> u16 {
self.width - self.offset_x
}
/// Gets the starting line number for the given `height` taking into account the `max_height`
/// and the `render_region`.
///
/// If the height of the widget to render is 5 and the max_height is 2, then the start would be:
/// - `RenderRegion::Top`: 0
/// - `RenderRegion::Middle`: 1
/// - `RenderRegion::Top`: 3
pub fn get_start(&self, height: u16) -> u16 {
if height > self.max_height {
match self.render_region {
RenderRegion::Top => 0,
RenderRegion::Middle => (height - self.max_height) / 2,
RenderRegion::Bottom => height - self.max_height,
}
} else {
0
}
}
}
#[test]
fn test_layout() {
let layout = Layout::new(0, (100, 5).into());
assert_eq!(
layout.with_render_region(RenderRegion::Top).get_start(10),
0
);
assert_eq!(
layout
.with_render_region(RenderRegion::Middle)
.get_start(10),
2
);
assert_eq!(
layout
.with_render_region(RenderRegion::Bottom)
.get_start(10),
5
);
}