Feat: early mouse support
This commit is contained in:
@@ -1,4 +1,60 @@
|
||||
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
||||
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind};
|
||||
use ratatui::layout::Rect;
|
||||
|
||||
pub fn convert_egui_mouse(
|
||||
ctx: &egui::Context,
|
||||
widget_rect: egui::Rect,
|
||||
term: Rect,
|
||||
) -> Vec<MouseEvent> {
|
||||
let mut events = Vec::new();
|
||||
if widget_rect.width() < 1.0 || widget_rect.height() < 1.0 || term.width == 0 || term.height == 0 {
|
||||
return events;
|
||||
}
|
||||
|
||||
ctx.input(|i| {
|
||||
let Some(pos) = i.pointer.latest_pos() else {
|
||||
return;
|
||||
};
|
||||
if !widget_rect.contains(pos) {
|
||||
return;
|
||||
}
|
||||
|
||||
let col =
|
||||
((pos.x - widget_rect.left()) / widget_rect.width() * term.width as f32) as u16;
|
||||
let row =
|
||||
((pos.y - widget_rect.top()) / widget_rect.height() * term.height as f32) as u16;
|
||||
let col = col.min(term.width.saturating_sub(1));
|
||||
let row = row.min(term.height.saturating_sub(1));
|
||||
|
||||
if i.pointer.button_clicked(egui::PointerButton::Primary) {
|
||||
events.push(MouseEvent {
|
||||
kind: MouseEventKind::Down(MouseButton::Left),
|
||||
column: col,
|
||||
row,
|
||||
modifiers: KeyModifiers::empty(),
|
||||
});
|
||||
}
|
||||
|
||||
let scroll = i.raw_scroll_delta.y;
|
||||
if scroll > 1.0 {
|
||||
events.push(MouseEvent {
|
||||
kind: MouseEventKind::ScrollUp,
|
||||
column: col,
|
||||
row,
|
||||
modifiers: KeyModifiers::empty(),
|
||||
});
|
||||
} else if scroll < -1.0 {
|
||||
events.push(MouseEvent {
|
||||
kind: MouseEventKind::ScrollDown,
|
||||
column: col,
|
||||
row,
|
||||
modifiers: KeyModifiers::empty(),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
events
|
||||
}
|
||||
|
||||
pub fn convert_egui_events(ctx: &egui::Context) -> Vec<KeyEvent> {
|
||||
let mut events = Vec::new();
|
||||
|
||||
Reference in New Issue
Block a user