Skip to main content

Module mouse

Module mouse 

Source
Expand description

Mouse event helpers.

§Enabling mouse support

In your terminal setup / teardown, enable and disable mouse capture:

use crossterm::{execute, event::{EnableMouseCapture, DisableMouseCapture}};

// setup
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;

// teardown
execute!(terminal.backend_mut(), LeaveAlternateScreen, DisableMouseCapture)?;

§Hit-testing in the event loop

use crossterm::event::{Event, MouseButton, MouseEventKind};
use tui_kit::mouse_hit;

if let Event::Mouse(m) = event::read()? {
    if m.kind == MouseEventKind::Down(MouseButton::Left) {
        app.handle_mouse(m.column, m.row);
    }
}

§Storing panel areas

Record each focusable widget’s Rect during the render pass, then use mouse_hit to map a click coordinate back to the focused widget:

// In render:
app.areas[Panel::List as usize] = Some(list_area);

// In handle_mouse:
for (i, area) in app.areas.iter().enumerate() {
    if let Some(a) = area {
        if mouse_hit(*a, col, row) {
            app.focus = Panel::from(i);
            return;
        }
    }
}

Functions§

list_item_at
Given the outer block area (including borders) of a list widget and a click row, returns the item index that was clicked.
mouse_hit
Returns true if the terminal cell (col, row) falls inside area.
paragraph_line_at
Given the outer block area of a scrollable paragraph and a click row, returns the absolute content line number that was clicked (i.e. accounting for the scroll offset).