Refactor mouse and keyboard handlers with shared actions
All checks were successful
Build and Release / build-and-release (push) Successful in 1m24s
All checks were successful
Build and Release / build-and-release (push) Successful in 1m24s
- Extract common action functions to eliminate code duplication - Mouse left-click toggles folder open/close - Mouse right-click plays selection (identical to Enter key) - Add confirmation popup for library refresh operation - Improve popup visibility with Clear widget
This commit is contained in:
246
src/main.rs
246
src/main.rs
@@ -7,7 +7,7 @@ mod ui;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use crossterm::{
|
||||
event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers},
|
||||
event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers, MouseEvent, MouseEventKind, EnableMouseCapture, DisableMouseCapture},
|
||||
execute,
|
||||
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
||||
};
|
||||
@@ -56,7 +56,7 @@ async fn main() -> Result<()> {
|
||||
// Setup terminal
|
||||
enable_raw_mode()?;
|
||||
let mut stdout = io::stdout();
|
||||
execute!(stdout, EnterAlternateScreen)?;
|
||||
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
||||
let backend = CrosstermBackend::new(stdout);
|
||||
let mut terminal = Terminal::new(backend)?;
|
||||
|
||||
@@ -67,13 +67,83 @@ async fn main() -> Result<()> {
|
||||
disable_raw_mode()?;
|
||||
execute!(
|
||||
terminal.backend_mut(),
|
||||
LeaveAlternateScreen
|
||||
LeaveAlternateScreen,
|
||||
DisableMouseCapture
|
||||
)?;
|
||||
terminal.show_cursor()?;
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
// Common action functions that both keyboard and mouse handlers can call
|
||||
|
||||
fn action_toggle_folder(state: &mut AppState) {
|
||||
if let Some(item) = state.get_selected_item() {
|
||||
if item.node.is_dir {
|
||||
let path = item.node.path.clone();
|
||||
if state.expanded_dirs.contains(&path) {
|
||||
// Folder is open, close it
|
||||
state.collapse_selected();
|
||||
} else {
|
||||
// Folder is closed, open it
|
||||
state.expand_selected();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn action_play_selection(state: &mut AppState, player: &mut player::Player) -> Result<()> {
|
||||
state.play_selection();
|
||||
if let Some(ref path) = state.current_file {
|
||||
player.play(path)?;
|
||||
state.player_state = PlayerState::Playing;
|
||||
player.update_metadata();
|
||||
tracing::info!("Playing: {:?} (playlist: {} tracks)", path, state.playlist.len());
|
||||
}
|
||||
if state.visual_mode {
|
||||
state.visual_mode = false;
|
||||
state.marked_files.clear();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn action_toggle_play_pause(state: &mut AppState, player: &mut player::Player) -> Result<()> {
|
||||
match state.player_state {
|
||||
PlayerState::Playing => {
|
||||
player.pause()?;
|
||||
state.player_state = PlayerState::Paused;
|
||||
tracing::info!("Paused");
|
||||
}
|
||||
PlayerState::Paused => {
|
||||
player.resume()?;
|
||||
state.player_state = PlayerState::Playing;
|
||||
tracing::info!("Resumed");
|
||||
}
|
||||
PlayerState::Stopped => {
|
||||
// Restart playback from current playlist position
|
||||
if !state.playlist.is_empty() {
|
||||
state.current_file = Some(state.playlist[state.playlist_index].clone());
|
||||
state.player_state = PlayerState::Playing;
|
||||
if let Some(ref path) = state.current_file {
|
||||
player.play(path)?;
|
||||
player.update_metadata();
|
||||
tracing::info!("Restarting playback: {:?}", path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn action_stop(state: &mut AppState, player: &mut player::Player) -> Result<()> {
|
||||
player.stop()?;
|
||||
state.player_state = PlayerState::Stopped;
|
||||
state.current_position = 0.0;
|
||||
state.current_duration = 0.0;
|
||||
tracing::info!("Stopped");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn run_app<B: ratatui::backend::Backend>(
|
||||
terminal: &mut Terminal<B>,
|
||||
state: &mut AppState,
|
||||
@@ -82,6 +152,8 @@ async fn run_app<B: ratatui::backend::Backend>(
|
||||
let mut metadata_update_counter = 0u32;
|
||||
let mut last_position = 0.0f64;
|
||||
let mut needs_redraw = true;
|
||||
let mut title_bar_area = ratatui::layout::Rect::default();
|
||||
let mut file_panel_area = ratatui::layout::Rect::default();
|
||||
|
||||
loop {
|
||||
let mut state_changed = false;
|
||||
@@ -142,7 +214,11 @@ async fn run_app<B: ratatui::backend::Backend>(
|
||||
|
||||
// Only redraw if something changed or forced
|
||||
if needs_redraw || state_changed {
|
||||
terminal.draw(|f| ui::render(f, state, player))?;
|
||||
terminal.draw(|f| {
|
||||
let areas = ui::render(f, state, player);
|
||||
title_bar_area = areas.0;
|
||||
file_panel_area = areas.1;
|
||||
})?;
|
||||
needs_redraw = false;
|
||||
}
|
||||
|
||||
@@ -154,11 +230,18 @@ async fn run_app<B: ratatui::backend::Backend>(
|
||||
};
|
||||
|
||||
if event::poll(poll_duration)? {
|
||||
if let Event::Key(key) = event::read()? {
|
||||
if key.kind == KeyEventKind::Press {
|
||||
handle_key_event(terminal, state, player, key).await?;
|
||||
needs_redraw = true; // Force redraw after key event
|
||||
match event::read()? {
|
||||
Event::Key(key) => {
|
||||
if key.kind == KeyEventKind::Press {
|
||||
handle_key_event(terminal, state, player, key).await?;
|
||||
needs_redraw = true; // Force redraw after key event
|
||||
}
|
||||
}
|
||||
Event::Mouse(mouse) => {
|
||||
handle_mouse_event(state, mouse, title_bar_area, file_panel_area, player)?;
|
||||
needs_redraw = true; // Force redraw after mouse event
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,6 +254,30 @@ async fn run_app<B: ratatui::backend::Backend>(
|
||||
}
|
||||
|
||||
async fn handle_key_event<B: ratatui::backend::Backend>(terminal: &mut Terminal<B>, state: &mut AppState, player: &mut player::Player, key: KeyEvent) -> Result<()> {
|
||||
// Handle confirmation popup
|
||||
if state.show_refresh_confirm {
|
||||
match key.code {
|
||||
KeyCode::Char('y') | KeyCode::Char('Y') => {
|
||||
state.show_refresh_confirm = false;
|
||||
state.is_refreshing = true;
|
||||
terminal.draw(|f| { let _ = ui::render(f, state, player); })?; // Show "Refreshing library..." immediately
|
||||
tracing::info!("Rescanning...");
|
||||
let cache_dir = cache::get_cache_dir()?;
|
||||
let new_cache = scanner::scan_paths(&state.config.scan_paths.paths)?;
|
||||
new_cache.save(&cache_dir)?;
|
||||
state.cache = new_cache;
|
||||
state.refresh_flattened_items();
|
||||
state.is_refreshing = false;
|
||||
tracing::info!("Rescan complete");
|
||||
}
|
||||
KeyCode::Char('n') | KeyCode::Char('N') | KeyCode::Esc => {
|
||||
state.show_refresh_confirm = false;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Handle search mode separately
|
||||
if state.search_mode {
|
||||
match key.code {
|
||||
@@ -322,50 +429,13 @@ async fn handle_key_event<B: ratatui::backend::Backend>(terminal: &mut Terminal<
|
||||
state.clear_playlist();
|
||||
}
|
||||
(KeyCode::Enter, _) => {
|
||||
state.play_selection();
|
||||
if let Some(ref path) = state.current_file {
|
||||
player.play(path)?;
|
||||
player.update_metadata(); // Update metadata immediately when playing new file
|
||||
tracing::info!("Playing: {:?} (playlist: {} tracks)", path, state.playlist.len());
|
||||
}
|
||||
if state.visual_mode {
|
||||
state.visual_mode = false;
|
||||
state.marked_files.clear();
|
||||
}
|
||||
action_play_selection(state, player)?;
|
||||
}
|
||||
(KeyCode::Char('s'), _) => {
|
||||
// s: Stop playback
|
||||
player.stop()?;
|
||||
state.player_state = PlayerState::Stopped;
|
||||
state.current_position = 0.0;
|
||||
state.current_duration = 0.0;
|
||||
tracing::info!("Stopped");
|
||||
action_stop(state, player)?;
|
||||
}
|
||||
(KeyCode::Char(' '), _) => {
|
||||
match state.player_state {
|
||||
PlayerState::Playing => {
|
||||
player.pause()?;
|
||||
state.player_state = PlayerState::Paused;
|
||||
tracing::info!("Paused");
|
||||
}
|
||||
PlayerState::Paused => {
|
||||
player.resume()?;
|
||||
state.player_state = PlayerState::Playing;
|
||||
tracing::info!("Resumed");
|
||||
}
|
||||
PlayerState::Stopped => {
|
||||
// Restart playback from current playlist position
|
||||
if !state.playlist.is_empty() {
|
||||
state.current_file = Some(state.playlist[state.playlist_index].clone());
|
||||
state.player_state = PlayerState::Playing;
|
||||
if let Some(ref path) = state.current_file {
|
||||
player.play(path)?;
|
||||
player.update_metadata(); // Update metadata immediately
|
||||
tracing::info!("Restarting playback: {:?}", path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
action_toggle_play_pause(state, player)?;
|
||||
}
|
||||
(KeyCode::Char('H'), KeyModifiers::SHIFT) => {
|
||||
if state.player_state != PlayerState::Stopped {
|
||||
@@ -392,19 +462,79 @@ async fn handle_key_event<B: ratatui::backend::Backend>(terminal: &mut Terminal<
|
||||
tracing::info!("Volume: {}%", new_volume);
|
||||
}
|
||||
(KeyCode::Char('r'), _) => {
|
||||
state.is_refreshing = true;
|
||||
terminal.draw(|f| ui::render(f, state, player))?; // Show "Refreshing library..." immediately
|
||||
tracing::info!("Rescanning...");
|
||||
let cache_dir = cache::get_cache_dir()?;
|
||||
let new_cache = scanner::scan_paths(&state.config.scan_paths.paths)?;
|
||||
new_cache.save(&cache_dir)?;
|
||||
state.cache = new_cache;
|
||||
state.refresh_flattened_items();
|
||||
state.is_refreshing = false;
|
||||
tracing::info!("Rescan complete");
|
||||
state.show_refresh_confirm = true;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_mouse_event(state: &mut AppState, mouse: MouseEvent, title_bar_area: ratatui::layout::Rect, file_panel_area: ratatui::layout::Rect, player: &mut player::Player) -> Result<()> {
|
||||
use crossterm::event::MouseButton;
|
||||
|
||||
match mouse.kind {
|
||||
MouseEventKind::ScrollDown => {
|
||||
// Scroll down - move selection down
|
||||
state.move_selection_down();
|
||||
}
|
||||
MouseEventKind::ScrollUp => {
|
||||
// Scroll up - move selection up
|
||||
state.move_selection_up();
|
||||
}
|
||||
MouseEventKind::Down(button) => {
|
||||
let x = mouse.column;
|
||||
let y = mouse.row;
|
||||
|
||||
// Check if click is on title bar
|
||||
if x >= title_bar_area.x
|
||||
&& x < title_bar_area.x + title_bar_area.width
|
||||
&& y >= title_bar_area.y
|
||||
&& y < title_bar_area.y + title_bar_area.height
|
||||
{
|
||||
match button {
|
||||
MouseButton::Left => {
|
||||
// Left click on title bar = play/pause toggle
|
||||
action_toggle_play_pause(state, player)?;
|
||||
}
|
||||
MouseButton::Right => {
|
||||
// Right click on title bar = stop
|
||||
action_stop(state, player)?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
// Check if click is within file panel area
|
||||
else if x >= file_panel_area.x
|
||||
&& x < file_panel_area.x + file_panel_area.width
|
||||
&& y >= file_panel_area.y
|
||||
&& y < file_panel_area.y + file_panel_area.height
|
||||
{
|
||||
// Calculate which item was clicked (accounting for borders and scroll offset)
|
||||
// Border takes 1 line at top, so subtract 1 from y position
|
||||
let relative_y = (y - file_panel_area.y).saturating_sub(1);
|
||||
let clicked_index = state.scroll_offset + relative_y as usize;
|
||||
|
||||
// Set selection to clicked item if valid
|
||||
if clicked_index < state.flattened_items.len() {
|
||||
state.selected_index = clicked_index;
|
||||
|
||||
// Handle different mouse buttons
|
||||
match button {
|
||||
MouseButton::Left => {
|
||||
// Left click = toggle folder open/close
|
||||
action_toggle_folder(state);
|
||||
}
|
||||
MouseButton::Right => {
|
||||
// Right click = play (like Enter key)
|
||||
action_play_selection(state, player)?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user