rat_widget/pager/mod.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 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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
//!
//! If you are tired of scrolling, try paging :)
//!
//! If you have a lot of widgets to display, splitting
//! them into pages is an alternative to scrolling.
//!
//! [PageLayout] helps with the dynamic page-breaks.
//! [SinglePager] and [DualPager] are the widgets that display
//! everything as one or two columns.
//!
//! Same as the other containers in this crate they leave the
//! actual rendering of the widgets to the caller.
//! [relocate](SinglePagerState::relocate) tells you
//! if a widget is visible and where it should be rendered.
//!
use ratatui::layout::Rect;
use ratatui::style::Style;
use ratatui::widgets::Block;
use std::cell::RefCell;
use std::rc::Rc;
mod dual_pager;
mod single_pager;
pub use dual_pager::*;
pub use single_pager::*;
/// PageLayout is fed with the areas that should be displayed.
///
/// The areas must use widget relative coordinates not screen
/// coordinates.
///
/// It then splits the list in pages in a way that there are
/// no broken areas.
#[derive(Debug, Default, Clone)]
pub struct PageLayout {
core: Rc<RefCell<PageLayoutCore>>,
}
/// Handle for an added area. Can be used to get the displayed area.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct AreaHandle(usize);
#[derive(Debug, Default, Clone)]
struct PageLayoutCore {
// just for checks on re-layout
page: Rect,
// collected areas
areas: Vec<Rect>,
// manual breaks
man_breaks: Vec<u16>,
// calculated breaks
breaks: Vec<u16>,
}
impl PageLayout {
pub fn new() -> Self {
Self::default()
}
/// Add a rect.
pub fn add(&mut self, area: Rect) -> AreaHandle {
let mut core = self.core.borrow_mut();
// reset page to re-layout
core.page = Default::default();
core.areas.push(area);
AreaHandle(core.areas.len() - 1)
}
/// Add rects.
pub fn add_all(&mut self, areas: impl IntoIterator<Item = Rect>) {
let mut core = self.core.borrow_mut();
// reset page to re-layout
core.page = Default::default();
core.areas.extend(areas)
}
/// Add rects. Appends the resulting handles.
pub fn extend(&mut self, areas: impl IntoIterator<Item = Rect>, handles: &mut Vec<AreaHandle>) {
let mut core = self.core.borrow_mut();
// reset page to re-layout
core.page = Default::default();
let start = core.areas.len();
core.areas.extend(areas);
let end = core.areas.len();
for i in start..end {
handles.push(AreaHandle(i));
}
}
/// Add a manual break after the given position.
pub fn break_after(&mut self, y: u16) {
let mut core = self.core.borrow_mut();
// reset page to re-layout
core.page = Default::default();
core.man_breaks.push(y + 1);
}
/// Add a manual break before the given position.
pub fn break_before(&mut self, y: u16) {
let mut core = self.core.borrow_mut();
// reset page to re-layout
core.page = Default::default();
core.man_breaks.push(y);
}
/// Run the layout algorithm.
pub fn layout(&mut self, page: Rect) {
let mut core = self.core.borrow_mut();
core._layout(page);
}
/// Number of pages.
///
pub fn len(&self) -> usize {
self.core.borrow().breaks.len()
}
/// Any pages
pub fn is_empty(&self) -> bool {
self.core.borrow().breaks.is_empty()
}
/// Locate an area by handle.
///
/// This will return a Rect with a y-value relative to the
/// page it is in. But still in layout-coords.
///
/// And it returns the page the Rect is on.
pub fn locate_handle(&self, handle: AreaHandle) -> (usize, Rect) {
let area = self.core.borrow().areas[handle.0];
self.locate(area)
}
/// Locate an area.
///
/// This will return a Rect with a y-value relative to the
/// page it is in. But still in layout-coords.
///
/// And it returns the page the Rect is on.
pub fn locate(&self, area: Rect) -> (usize, Rect) {
let core = self.core.borrow();
// find page
let (page, brk) = core
.breaks
.iter()
.enumerate()
.rev()
.find(|(_i, v)| **v <= area.y)
.expect("valid breaks");
(
page,
Rect::new(area.x, area.y - *brk, area.width, area.height),
)
}
/// First area on the given page.
pub fn first_area(&self, page: usize) -> Option<Rect> {
let core = self.core.borrow();
let brk = core.breaks[page];
core.areas.iter().skip(1).find(|v| v.y >= brk).cloned()
}
/// First area-handle on the given page.
pub fn first_handle(&self, page: usize) -> Option<AreaHandle> {
let core = self.core.borrow();
let brk = core.breaks[page];
core.areas.iter().enumerate().skip(1).find_map(|(i, v)| {
if v.y >= brk {
Some(AreaHandle(i))
} else {
None
}
})
}
}
impl PageLayoutCore {
/// Run the layout algorithm.
fn _layout(&mut self, page: Rect) {
if self.page == page {
return;
}
self.areas.sort_by(|a, b| a.y.cmp(&b.y));
self.man_breaks.sort_by(|a, b| b.cmp(a));
self.breaks.clear();
self.breaks.push(0);
let mut last_break = 0;
let mut man_breaks = self.man_breaks.clone();
for v in self.areas.iter() {
if Some(&v.y) == man_breaks.last() {
self.breaks.push(v.y);
last_break = v.y;
man_breaks.pop();
} else if v.y >= last_break {
let ry = v.y - last_break;
if ry + v.height > page.height {
self.breaks.push(v.y);
last_break = v.y;
}
}
}
}
}
/// All styles for a pager.
#[derive(Debug, Default, Clone)]
pub struct PagerStyle {
pub style: Style,
pub nav: Option<Style>,
pub title: Option<Style>,
pub block: Option<Block<'static>>,
}
pub(crate) mod event {
use rat_event::{ConsumedEvent, Outcome};
/// Result of event handling.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum PagerOutcome {
/// The given event has not been used at all.
Continue,
/// The event has been recognized, but the result was nil.
/// Further processing for this event may stop.
Unchanged,
/// The event has been recognized and there is some change
/// due to it.
/// Further processing for this event may stop.
/// Rendering the ui is advised.
Changed,
/// Displayed page changed.
Page(usize),
}
impl ConsumedEvent for PagerOutcome {
fn is_consumed(&self) -> bool {
*self != PagerOutcome::Continue
}
}
// Useful for converting most navigation/edit results.
impl From<bool> for PagerOutcome {
fn from(value: bool) -> Self {
if value {
PagerOutcome::Changed
} else {
PagerOutcome::Unchanged
}
}
}
impl From<Outcome> for PagerOutcome {
fn from(value: Outcome) -> Self {
match value {
Outcome::Continue => PagerOutcome::Continue,
Outcome::Unchanged => PagerOutcome::Unchanged,
Outcome::Changed => PagerOutcome::Changed,
}
}
}
impl From<PagerOutcome> for Outcome {
fn from(value: PagerOutcome) -> Self {
match value {
PagerOutcome::Continue => Outcome::Continue,
PagerOutcome::Unchanged => Outcome::Unchanged,
PagerOutcome::Changed => Outcome::Changed,
PagerOutcome::Page(_) => Outcome::Changed,
}
}
}
}
#[cfg(test)]
mod test {
use crate::pager::PageLayout;
use ratatui::layout::Rect;
use std::ops::Deref;
fn hr(y: u16, height: u16) -> Rect {
Rect::new(0, y, 0, height)
}
#[test]
fn test_layout() {
let mut p0 = PageLayout::new();
p0.add(hr(5, 1));
p0.add(hr(5, 2));
p0.add(hr(9, 1));
p0.add(hr(9, 2));
p0.add(hr(9, 1));
p0.add(hr(9, 0));
p0.add(hr(12, 1));
p0.add(hr(14, 1));
p0.add(hr(16, 1));
p0.add(hr(18, 1));
p0.add(hr(19, 1));
p0.add(hr(20, 1));
p0.layout(hr(0, 10));
assert_eq!(p0.core.borrow().breaks.deref(), &vec![0, 9, 19]);
}
}