Skip to content

Commit

Permalink
Revert "feat: Mouse scroll (#913)"
Browse files Browse the repository at this point in the history
This reverts commit 432d6b3.
  • Loading branch information
nnyyxxxx authored Nov 12, 2024
1 parent 7147ed9 commit 9a18d08
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 130 deletions.
15 changes: 1 addition & 14 deletions tui/src/confirmation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::borrow::Cow;
use crate::{float::FloatContent, hint::Shortcut};

use ratatui::{
crossterm::event::{KeyCode, KeyEvent, MouseEvent, MouseEventKind},
crossterm::event::{KeyCode, KeyEvent},
layout::Alignment,
prelude::*,
widgets::{Block, Borders, Clear, List},
Expand Down Expand Up @@ -85,19 +85,6 @@ impl FloatContent for ConfirmPrompt {
frame.render_widget(List::new(paths_text), inner_area);
}

fn handle_mouse_event(&mut self, event: &MouseEvent) -> bool {
match event.kind {
MouseEventKind::ScrollDown => {
self.scroll_down();
}
MouseEventKind::ScrollUp => {
self.scroll_up();
}
_ => {}
}
false
}

fn handle_key_event(&mut self, key: &KeyEvent) -> bool {
use KeyCode::*;
self.status = match key.code {
Expand Down
7 changes: 1 addition & 6 deletions tui/src/float.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ratatui::{
crossterm::event::{KeyCode, KeyEvent, MouseEvent},
crossterm::event::{KeyCode, KeyEvent},
layout::{Constraint, Direction, Layout, Rect},
Frame,
};
Expand All @@ -9,7 +9,6 @@ use crate::hint::Shortcut;
pub trait FloatContent {
fn draw(&mut self, frame: &mut Frame, area: Rect);
fn handle_key_event(&mut self, key: &KeyEvent) -> bool;
fn handle_mouse_event(&mut self, key: &MouseEvent) -> bool;
fn is_finished(&self) -> bool;
fn get_shortcut_list(&self) -> (&str, Box<[Shortcut]>);
}
Expand Down Expand Up @@ -54,10 +53,6 @@ impl<Content: FloatContent + ?Sized> Float<Content> {
self.content.draw(frame, popup_area);
}

pub fn handle_mouse_event(&mut self, event: &MouseEvent) {
self.content.handle_mouse_event(event);
}

// Returns true if the floating window is finished.
pub fn handle_key_event(&mut self, key: &KeyEvent) -> bool {
match key.code {
Expand Down
13 changes: 1 addition & 12 deletions tui/src/floating_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{float::FloatContent, hint::Shortcut};
use linutil_core::Command;

use ratatui::{
crossterm::event::{KeyCode, KeyEvent, MouseEvent, MouseEventKind},
crossterm::event::{KeyCode, KeyEvent},
layout::Rect,
style::{Style, Stylize},
text::Line,
Expand Down Expand Up @@ -283,17 +283,6 @@ impl FloatContent for FloatingText {
frame.render_widget(list, inner_area);
}

fn handle_mouse_event(&mut self, event: &MouseEvent) -> bool {
match event.kind {
MouseEventKind::ScrollDown => self.scroll_down(),
MouseEventKind::ScrollUp => self.scroll_up(),
MouseEventKind::ScrollLeft => self.scroll_left(),
MouseEventKind::ScrollRight => self.scroll_right(),
_ => {}
}
false
}

fn handle_key_event(&mut self, key: &KeyEvent) -> bool {
use KeyCode::*;
match key.code {
Expand Down
25 changes: 8 additions & 17 deletions tui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use clap::Parser;
use ratatui::{
backend::CrosstermBackend,
crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyEventKind},
event::{self, DisableMouseCapture, Event, KeyEventKind},
style::ResetColor,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
Expand Down Expand Up @@ -64,8 +64,6 @@ fn main() -> io::Result<()> {
);

stdout().execute(EnterAlternateScreen)?;
stdout().execute(EnableMouseCapture)?;

enable_raw_mode()?;
let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
terminal.clear()?;
Expand Down Expand Up @@ -95,22 +93,15 @@ fn run(

// It's guaranteed that the `read()` won't block when the `poll()`
// function returns `true`
match event::read()? {
Event::Key(key) => {
if key.kind != KeyEventKind::Press && key.kind != KeyEventKind::Repeat {
continue;
}

if !state.handle_key(&key) {
return Ok(());
}
if let Event::Key(key) = event::read()? {
// We are only interested in Press and Repeat events
if key.kind != KeyEventKind::Press && key.kind != KeyEventKind::Repeat {
continue;
}
Event::Mouse(mouse_event) => {
if !state.handle_mouse(&mouse_event) {
return Ok(());
}

if !state.handle_key(&key) {
return Ok(());
}
_ => {}
}
}
}
14 changes: 1 addition & 13 deletions tui/src/running_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use portable_pty::{
ChildKiller, CommandBuilder, ExitStatus, MasterPty, NativePtySystem, PtySize, PtySystem,
};
use ratatui::{
crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseEvent, MouseEventKind},
crossterm::event::{KeyCode, KeyEvent, KeyModifiers},
layout::{Rect, Size},
style::{Color, Style, Stylize},
text::{Line, Span},
Expand Down Expand Up @@ -104,18 +104,6 @@ impl FloatContent for RunningCommand {
frame.render_widget(pseudo_term, area);
}

fn handle_mouse_event(&mut self, event: &MouseEvent) -> bool {
match event.kind {
MouseEventKind::ScrollUp => {
self.scroll_offset = self.scroll_offset.saturating_add(1);
}
MouseEventKind::ScrollDown => {
self.scroll_offset = self.scroll_offset.saturating_sub(1);
}
_ => {}
}
true
}
/// Handle key events of the running command "window". Returns true when the "window" should be
/// closed
fn handle_key_event(&mut self, key: &KeyEvent) -> bool {
Expand Down
70 changes: 2 additions & 68 deletions tui/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use linutil_core::{ego_tree::NodeId, Config, ListNode, TabList};
#[cfg(feature = "tips")]
use rand::Rng;
use ratatui::{
crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers, MouseEvent, MouseEventKind},
layout::{Alignment, Constraint, Direction, Flex, Layout, Position, Rect},
crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers},
layout::{Alignment, Constraint, Direction, Flex, Layout},
style::{Style, Stylize},
text::{Line, Span, Text},
widgets::{Block, Borders, List, ListState, Paragraph},
Expand Down Expand Up @@ -42,8 +42,6 @@ P* - privileged *
";

pub struct AppState {
/// Areas of tabs
areas: Option<Areas>,
/// Selected theme
theme: Theme,
/// Currently focused area
Expand Down Expand Up @@ -82,11 +80,6 @@ pub struct ListEntry {
pub has_children: bool,
}

pub struct Areas {
tab_list: Rect,
list: Rect,
}

enum SelectedItem {
UpDir,
Directory,
Expand All @@ -108,7 +101,6 @@ impl AppState {
let auto_execute_commands = config_path.map(|path| Config::from_file(&path).auto_execute);

let mut state = Self {
areas: None,
theme,
focus: Focus::List,
tabs,
Expand Down Expand Up @@ -329,11 +321,6 @@ impl AppState {
.split(horizontal[0]);
frame.render_widget(label, left_chunks[0]);

self.areas = Some(Areas {
tab_list: left_chunks[1],
list: horizontal[1],
});

let tabs = self
.tabs
.iter()
Expand Down Expand Up @@ -488,59 +475,6 @@ impl AppState {
frame.render_widget(keybind_para, vertical[1]);
}

pub fn handle_mouse(&mut self, event: &MouseEvent) -> bool {
if !self.drawable {
return true;
}

if matches!(self.focus, Focus::TabList | Focus::List) {
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 => {
if mouse_in_tab_list {
if self.current_tab.selected().unwrap() != self.tabs.len() - 1 {
self.current_tab.select_next();
}
self.refresh_tab();
} else if mouse_in_list {
self.selection.select_next()
}
}
MouseEventKind::ScrollUp => {
if mouse_in_tab_list {
if self.current_tab.selected().unwrap() != 0 {
self.current_tab.select_previous();
}
self.refresh_tab();
} else if mouse_in_list {
self.selection.select_previous()
}
}
_ => {}
}
}
match &mut self.focus {
Focus::FloatingWindow(float) => {
float.content.handle_mouse_event(event);
}
Focus::ConfirmationPrompt(confirm) => {
confirm.content.handle_mouse_event(event);
}
_ => {}
}
true
}

pub fn handle_key(&mut self, key: &KeyEvent) -> bool {
// This should be defined first to allow closing
// the application even when not drawable ( If terminal is small )
Expand Down

0 comments on commit 9a18d08

Please sign in to comment.