Lines
100 %
Functions
86.36 %
Branches
//! Where the last frame drew everything a click can land on.
use ratatui::layout::{Position, Rect};
use tvui::{BarPart, Place};
use crate::event::Intent;
use crate::widgets::grid::CellId;
use crate::window::WindowId;
/// A surface the wheel and the scrollbar parts act on.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Surface {
/// The rows of the active list.
List,
/// A scrolled view that is not a list: the report table, the console scrollback, a viewer.
Pane,
/// The body of a CSV or draft overlay.
Modal,
/// The open drop-down of the focused select.
Options,
}
pub enum Target {
/// A surface that takes a click and does nothing with it.
Block,
/// The screen behind an open menu: a click closes it.
MenuBackdrop,
MenuTitle(usize),
MenuItem(usize),
/// A dialog button, by index in the modal's button table.
Button(usize),
Field(usize),
/// The mark of a select: `▐↓▌` of the plain field `field`, or `▼` of its grid cell `cell`.
Mark {
field: usize,
cell: Option<CellId>,
},
/// The text of an input, a grid cell or a text area, where a click places the caret.
Caret {
/// A cell of the grid in field `field`, by the widget's own id for it.
Cell {
cell: CellId,
/// An option of the open drop-down, by its index among the options shown.
Option(usize),
/// A scrollable surface; a click on it does nothing.
Body(Surface),
Scroll(Surface, BarPart),
/// A list row, by index among the rows the view holds.
Row(usize),
/// A status bar item: the intent it applies.
Hint(Intent),
/// The `[■]` icon of a window frame.
Close,
/// The `[↑]` or `[↕]` icon of a window frame.
Zoom,
/// The top row of a window frame; two quick clicks on it zoom the window.
Title,
/// The `─┘` corner of a window frame, which sizes the window.
Grow,
impl Target {
/// The surface the wheel moves when the pointer is on this target.
#[must_use]
pub fn surface(self) -> Option<Surface> {
match self {
Target::Row(_) => Some(Surface::List),
Target::Option(_) => Some(Surface::Options),
Target::Body(surface) | Target::Scroll(surface, _) => Some(surface),
_ => None,
/// The areas of one frame, drawn bottom to top: the last one that holds a point is the one
/// on top. An area a window drew carries the window's id.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Hits {
areas: Vec<(Rect, Target, Option<WindowId>)>,
dialog: Option<Slot>,
/// The unclipped rect of each caret area a dialog drew, in canvas cells, by its visible rect.
carets: Vec<(Rect, Rect)>,
/// The columns the dialog's canvas is shifted right of the screen.
shift: u16,
/// Where the dialog on top opened and what bounds it, as the last frame drew it.
pub struct Slot {
/// The centred rect the dialog opens at.
pub auto: Rect,
/// The least size it is sized to; its own size for a dialog that only moves.
pub min: (u16, u16),
/// The area the dialog sits in.
pub host: Rect,
impl Hits {
pub fn set_dialog(&mut self, slot: Slot) {
self.dialog = Some(slot);
pub fn dialog(&self) -> Option<Slot> {
self.dialog
/// Takes what a dialog drew in a canvas that `shift` columns hang left of the screen, as the
/// screen shows it inside `host`.
pub fn extend_shifted(&mut self, local: Hits, shift: u16, host: Rect) {
self.shift = shift;
let canvas = Place {
x: -i32::from(shift),
y: 0,
width: u16::MAX,
height: u16::MAX,
};
for &(rect, target, _) in &local.areas {
let shown = canvas.shown(rect, host);
if matches!(target, Target::Caret { .. }) && !shown.is_empty() {
self.carets.push((shown, rect));
self.extend_shown(local, None, canvas, host);
/// The caret area shown at `at`, as the dialog drew it unclipped, and the columns its canvas
/// is shifted: the point to place a caret by is `at` moved right by them.
pub fn caret_frame(&self, at: Position) -> Option<(Rect, u16)> {
let (_, full) = self
.carets
.iter()
.rev()
.find(|(shown, _)| shown.contains(at))?;
Some((*full, self.shift))
pub fn push(&mut self, rect: Rect, target: Target) {
self.push_owned(rect, target, None);
fn push_owned(&mut self, rect: Rect, target: Target, window: Option<WindowId>) {
if !rect.is_empty() {
self.areas.push((rect, target, window));
/// Takes what `owner` drew in its own cells as the screen shows it inside `area`.
pub fn extend_shown(&mut self, local: Hits, owner: Option<WindowId>, place: Place, area: Rect) {
for (rect, target, _) in local.areas {
self.push_owned(place.shown(rect, area), target, owner);
/// The area on top at `at`, what it is and the window that drew it.
pub fn hit_at(&self, at: Position) -> Option<(Rect, Target, Option<WindowId>)> {
self.areas
.find(|(rect, ..)| rect.contains(at))
.copied()
#[cfg(test)]
mod tests;