feat: implemetation of mouse position

Suggested by Liam (<33645555+lj3954@users.noreply.github.com>)
This commit is contained in:
Jeevitha Kannan K S 2024-11-05 18:35:32 +05:30
parent 5e4e336eff
commit 3a0717d267
No known key found for this signature in database
GPG Key ID: 5904C34A2F7CE333

View File

@ -13,7 +13,7 @@ use linutil_core::{ListNode, Tab};
#[cfg(feature = "tips")] #[cfg(feature = "tips")]
use rand::Rng; use rand::Rng;
use ratatui::{ use ratatui::{
layout::{Alignment, Constraint, Direction, Flex, Layout}, layout::{Alignment, Constraint, Direction, Flex, Layout, Position, Rect},
style::{Style, Stylize}, style::{Style, Stylize},
text::{Line, Span, Text}, text::{Line, Span, Text},
widgets::{Block, Borders, List, ListState, Paragraph}, widgets::{Block, Borders, List, ListState, Paragraph},
@ -62,6 +62,7 @@ pub struct AppState {
drawable: bool, drawable: bool,
#[cfg(feature = "tips")] #[cfg(feature = "tips")]
tip: String, tip: String,
areas: Option<Areas>,
} }
pub enum Focus { pub enum Focus {
@ -78,6 +79,11 @@ pub struct ListEntry {
pub has_children: bool, pub has_children: bool,
} }
pub struct Areas {
tab_list: Rect,
list: Rect,
}
enum SelectedItem { enum SelectedItem {
UpDir, UpDir,
Directory, Directory,
@ -104,6 +110,7 @@ impl AppState {
drawable: false, drawable: false,
#[cfg(feature = "tips")] #[cfg(feature = "tips")]
tip: get_random_tip(), tip: get_random_tip(),
areas: None,
}; };
state.update_items(); state.update_items();
@ -282,6 +289,11 @@ impl AppState {
.split(horizontal[0]); .split(horizontal[0]);
frame.render_widget(label, left_chunks[0]); frame.render_widget(label, left_chunks[0]);
self.areas = Some(Areas {
tab_list: left_chunks[1],
list: horizontal[1],
});
let tabs = self let tabs = self
.tabs .tabs
.iter() .iter()
@ -421,29 +433,43 @@ impl AppState {
return true; return true;
} }
match &mut self.focus { if matches!(self.focus, Focus::TabList | Focus::List) {
Focus::TabList => match event.kind { let position = Position::new(event.column, event.row);
let mouse_in_tab_list = self.areas.as_ref().unwrap().tab_list.contains(position);
let mouse_in_list = self.areas.as_ref().unwrap().list.contains(position);
match event.kind {
MouseEventKind::Moved => {
if mouse_in_list {
self.focus = Focus::List
} else if mouse_in_tab_list {
self.focus = Focus::TabList
}
}
MouseEventKind::ScrollDown => { MouseEventKind::ScrollDown => {
if mouse_in_tab_list {
if self.current_tab.selected().unwrap() != self.tabs.len() - 1 { if self.current_tab.selected().unwrap() != self.tabs.len() - 1 {
self.current_tab.select_next(); self.current_tab.select_next();
} }
self.refresh_tab(); self.refresh_tab();
} else if mouse_in_list {
self.selection.select_next()
}
} }
MouseEventKind::ScrollUp => { MouseEventKind::ScrollUp => {
if mouse_in_tab_list {
if self.current_tab.selected().unwrap() != 0 { if self.current_tab.selected().unwrap() != 0 {
self.current_tab.select_previous(); self.current_tab.select_previous();
} }
self.refresh_tab(); self.refresh_tab();
} else if mouse_in_list {
self.selection.select_previous()
}
} }
MouseEventKind::ScrollRight => self.focus = Focus::List,
_ => {} _ => {}
}, }
Focus::List => match event.kind { }
MouseEventKind::ScrollDown => self.selection.select_next(), match &mut self.focus {
MouseEventKind::ScrollUp => self.selection.select_previous(),
MouseEventKind::ScrollLeft => self.focus = Focus::TabList,
_ => {}
},
Focus::FloatingWindow(float) => { Focus::FloatingWindow(float) => {
float.content.handle_mouse_event(event); float.content.handle_mouse_event(event);
} }