Skip to content

Commit

Permalink
Support scrolling by mouse
Browse files Browse the repository at this point in the history
  • Loading branch information
sachaos committed Aug 29, 2024
1 parent b90ae7c commit 7070c30
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/action.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{fmt, string::ToString};

use chrono::{DateTime, Local};
use crossterm::event::KeyEvent;
use crossterm::event::{KeyEvent, MouseEvent};
use serde::{
de::{self, Deserializer, Visitor},
Deserialize, Serialize,
Expand All @@ -25,6 +25,7 @@ pub enum Action {
Resume,
Quit,
Refresh,
MouseEvent(MouseEvent),
Error(String),
Help,
StartExecution(ExecutionId, DateTime<Local>),
Expand Down
9 changes: 6 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;
use anstyle::{Color, RgbColor, Style};
use chrono::Duration;
use color_eyre::{eyre::Result, owo_colors::OwoColorize};
use crossterm::event::{Event, KeyEvent};
use crossterm::event::{Event, KeyEvent, MouseEvent};
use ratatui::{prelude::Rect, widgets::Block};
use serde::{Deserialize, Serialize};
use tokio::{
Expand Down Expand Up @@ -211,7 +211,7 @@ impl<S: Store> App<S> {
let mut tui = tui::Tui::new()?
.tick_rate(self.tick_rate)
.frame_rate(self.frame_rate);
// tui.mouse(true);
tui = tui.mouse(true);
tui.enter()?;

for component in self.components.iter_mut() {
Expand All @@ -229,6 +229,9 @@ impl<S: Store> App<S> {
loop {
if let Some(e) = tui.next().await {
match e {
tui::Event::Mouse(me) => {
action_tx.send(Action::MouseEvent(me))?;
}
tui::Event::Quit => action_tx.send(Action::Quit)?,
tui::Event::Tick => action_tx.send(Action::Tick)?,
tui::Event::Render => action_tx.send(Action::Render)?,
Expand Down Expand Up @@ -498,7 +501,7 @@ impl<S: Store> App<S> {
tui = tui::Tui::new()?
.tick_rate(self.tick_rate)
.frame_rate(self.frame_rate);
// tui.mouse(true);
tui = tui.mouse(true);
tui.enter()?;
} else if self.should_quit {
tui.stop()?;
Expand Down
23 changes: 22 additions & 1 deletion src/components/execution_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ansi_parser::{AnsiParser, AnsiSequence, Output};
use ansi_to_tui::IntoText;
use chrono::{DateTime, Local};
use color_eyre::eyre::Result;
use crossterm::event::{KeyCode, KeyEvent};
use crossterm::event::{KeyCode, KeyEvent, MouseEvent, MouseEventKind};
use ratatui::{prelude::*, widgets::*};
use serde::{Deserialize, Serialize};
use symbols::scrollbar;
Expand All @@ -17,6 +17,7 @@ use crate::{
action::Action,
config::{Config, KeyBindings, RuntimeConfig},
termtext::{Char, Text},
utils::is_in_area,
};

pub struct ExecutionResult {
Expand All @@ -33,6 +34,8 @@ pub struct ExecutionResult {
y_area_size: u16,
y_max_scroll_size: u16,
fold: bool,

rect: Rect,
}

impl ExecutionResult {
Expand All @@ -49,6 +52,7 @@ impl ExecutionResult {
y_max_scroll_size: 0,
x_position: 0,
y_position: 0,
rect: Rect::default(),
}
}

Expand Down Expand Up @@ -109,6 +113,20 @@ impl ExecutionResult {
fn set_fold(&mut self, is_fold: bool) {
self.fold = is_fold;
}

fn handle_mouse_events(&mut self, event: MouseEvent) {
if !is_in_area(event.column, event.row, self.rect) {
return;
}

match event.kind {
MouseEventKind::ScrollDown => self.scroll_down(),
MouseEventKind::ScrollUp => self.scroll_up(),
MouseEventKind::ScrollLeft => self.scroll_left(),
MouseEventKind::ScrollRight => self.scroll_right(),
_ => {}
}
}
}

fn text_width(text: &Text) -> usize {
Expand Down Expand Up @@ -148,12 +166,15 @@ impl Component for ExecutionResult {
Action::SetFold(is_fold) => self.set_fold(is_fold),
Action::BottomOfPage => self.bottom_of_page(),
Action::TopOfPage => self.top_of_page(),
Action::MouseEvent(e) => self.handle_mouse_events(e),
_ => {}
}
Ok(None)
}

fn draw(&mut self, f: &mut Frame<'_>, area: Rect) -> Result<()> {
self.rect = area;

let text = self.result.clone().unwrap_or(Text::new(""));
let mut current = text.to_string();
let mut y_max;
Expand Down
21 changes: 20 additions & 1 deletion src/components/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use color_eyre::{
eyre::{Ok, OptionExt, Result},
owo_colors::OwoColorize,
};
use crossterm::event::{KeyCode, KeyEvent};
use crossterm::event::{KeyCode, KeyEvent, MouseEvent, MouseEventKind};
use ratatui::{prelude::*, widgets::*};
use serde::{Deserialize, Serialize};
use symbols::scrollbar;
Expand All @@ -23,6 +23,7 @@ use crate::{
config::{Config, KeyBindings, RuntimeConfig},
mode::Mode,
types::ExecutionId,
utils::is_in_area,
widget::history_item::HistoryItem,
};

Expand All @@ -37,6 +38,7 @@ pub struct History {
runtime_config: RuntimeConfig,
timemachine_mode: bool,
y_state: ScrollbarState,
rect: Rect,
}

impl History {
Expand All @@ -54,6 +56,7 @@ impl History {
runtime_config,
timemachine_mode: false,
y_state: ScrollbarState::default(),
rect: Rect::default(),
}
}

Expand Down Expand Up @@ -196,6 +199,19 @@ impl History {

self.select_latest()
}

fn handle_mouse_events(&mut self, event: MouseEvent) -> Result<()> {
log::debug!("Mouse event: {:?}", event);
if !is_in_area(event.column, event.row, self.rect) {
return Ok(());
}

match event.kind {
MouseEventKind::ScrollDown => self.go_to_past(),
MouseEventKind::ScrollUp => self.go_to_future(),
_ => Ok(()),
}
}
}

impl Component for History {
Expand Down Expand Up @@ -225,13 +241,16 @@ impl Component for History {
Action::GoToMorePast => self.go_to_more_past()?,
Action::GoToOldest => self.go_to_oldest()?,
Action::GoToCurrent => self.go_to_current()?,
Action::MouseEvent(e) => self.handle_mouse_events(e)?,
_ => {}
}

Ok(None)
}

fn draw(&mut self, f: &mut Frame<'_>, area: Rect) -> Result<()> {
self.rect = area;

let block = Block::default()
.title("History")
.borders(Borders::ALL)
Expand Down
5 changes: 5 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{path::PathBuf, sync::LazyLock};
use color_eyre::eyre::Result;
use directories::{BaseDirs, ProjectDirs};
use human_panic::Metadata;
use ratatui::layout::Rect;
use tracing::error;
use tracing_error::ErrorLayer;
use tracing_subscriber::{
Expand Down Expand Up @@ -181,3 +182,7 @@ Config directory: {config_dir_path}
Data directory: {data_dir_path}"
)
}

pub fn is_in_area(x: u16, y: u16, area: Rect) -> bool {
x >= area.x && x < area.x + area.width && y >= area.y && y < area.y + area.height
}

0 comments on commit 7070c30

Please sign in to comment.