Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f1412b4f8c | |||
| ffe7cd0090 | |||
| 907a734be3 | |||
| 135700ce02 |
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "cm-player"
|
name = "cm-player"
|
||||||
version = "0.1.15"
|
version = "0.1.19"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
284
src/main.rs
284
src/main.rs
@@ -144,6 +144,102 @@ fn action_stop(state: &mut AppState, player: &mut player::Player) -> Result<()>
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn action_remove_from_playlist(state: &mut AppState, player: &mut player::Player) -> Result<()> {
|
||||||
|
let was_playing_removed = state.playlist_index == state.selected_playlist_index;
|
||||||
|
state.remove_selected_playlist_item();
|
||||||
|
|
||||||
|
if state.playlist.is_empty() {
|
||||||
|
state.player_state = PlayerState::Stopped;
|
||||||
|
state.current_file = None;
|
||||||
|
player.stop()?;
|
||||||
|
} else if was_playing_removed && state.player_state == PlayerState::Playing {
|
||||||
|
state.current_file = Some(state.playlist[state.playlist_index].clone());
|
||||||
|
if let Some(ref path) = state.current_file {
|
||||||
|
player.play(path)?;
|
||||||
|
player.update_metadata();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn action_play_from_playlist(state: &mut AppState, player: &mut player::Player, preserve_pause: bool) -> Result<()> {
|
||||||
|
state.playlist_index = state.selected_playlist_index;
|
||||||
|
state.current_file = Some(state.playlist[state.playlist_index].clone());
|
||||||
|
|
||||||
|
if preserve_pause {
|
||||||
|
match state.player_state {
|
||||||
|
PlayerState::Playing => {
|
||||||
|
if let Some(ref path) = state.current_file {
|
||||||
|
player.play(path)?;
|
||||||
|
player.update_metadata();
|
||||||
|
tracing::info!("Jumped to track: {:?}", path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PlayerState::Paused => {
|
||||||
|
if let Some(ref path) = state.current_file {
|
||||||
|
player.play(path)?;
|
||||||
|
player.update_metadata();
|
||||||
|
player.pause()?;
|
||||||
|
tracing::info!("Jumped to track (paused): {:?}", path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PlayerState::Stopped => {
|
||||||
|
state.player_state = PlayerState::Playing;
|
||||||
|
if let Some(ref path) = state.current_file {
|
||||||
|
player.play(path)?;
|
||||||
|
player.update_metadata();
|
||||||
|
tracing::info!("Started playing track: {:?}", path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
state.player_state = PlayerState::Playing;
|
||||||
|
if let Some(ref path) = state.current_file {
|
||||||
|
player.play(path)?;
|
||||||
|
player.update_metadata();
|
||||||
|
tracing::info!("Playing from playlist: {:?}", path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_context_menu_action(menu_type: state::ContextMenuType, selected: usize, state: &mut AppState, player: &mut player::Player) -> Result<()> {
|
||||||
|
match menu_type {
|
||||||
|
state::ContextMenuType::FilePanel => {
|
||||||
|
match selected {
|
||||||
|
0 => action_play_selection(state, player)?,
|
||||||
|
1 => state.add_to_playlist(),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
state::ContextMenuType::Playlist => {
|
||||||
|
match selected {
|
||||||
|
0 => action_remove_from_playlist(state, player)?,
|
||||||
|
1 => {
|
||||||
|
state.shuffle_playlist();
|
||||||
|
tracing::info!("Playlist randomised from context menu");
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
state::ContextMenuType::TitleBar => {
|
||||||
|
match selected {
|
||||||
|
0 => action_stop(state, player)?,
|
||||||
|
1 => {
|
||||||
|
state.cycle_play_mode();
|
||||||
|
tracing::info!("Play mode: {:?}", state.play_mode);
|
||||||
|
}
|
||||||
|
2 => {
|
||||||
|
state.show_refresh_confirm = true;
|
||||||
|
tracing::info!("Refresh requested from context menu");
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
async fn run_app<B: ratatui::backend::Backend>(
|
async fn run_app<B: ratatui::backend::Backend>(
|
||||||
terminal: &mut Terminal<B>,
|
terminal: &mut Terminal<B>,
|
||||||
state: &mut AppState,
|
state: &mut AppState,
|
||||||
@@ -183,8 +279,10 @@ async fn run_app<B: ratatui::backend::Backend>(
|
|||||||
let new_position = player.get_position().unwrap_or(0.0);
|
let new_position = player.get_position().unwrap_or(0.0);
|
||||||
let new_duration = player.get_duration().unwrap_or(0.0);
|
let new_duration = player.get_duration().unwrap_or(0.0);
|
||||||
|
|
||||||
// Only mark as changed if position moved by at least 0.5 seconds
|
// Only update if displayed value (rounded to seconds) changed
|
||||||
if (new_position - last_position).abs() >= 0.5 {
|
let old_display_secs = last_position as u32;
|
||||||
|
let new_display_secs = new_position as u32;
|
||||||
|
if new_display_secs != old_display_secs {
|
||||||
state.current_position = new_position;
|
state.current_position = new_position;
|
||||||
last_position = new_position;
|
last_position = new_position;
|
||||||
state_changed = true;
|
state_changed = true;
|
||||||
@@ -359,64 +457,7 @@ async fn handle_key_event<B: ratatui::backend::Backend>(terminal: &mut Terminal<
|
|||||||
let menu_type = menu.menu_type;
|
let menu_type = menu.menu_type;
|
||||||
let selected = menu.selected_index;
|
let selected = menu.selected_index;
|
||||||
state.context_menu = None;
|
state.context_menu = None;
|
||||||
|
handle_context_menu_action(menu_type, selected, state, player)?;
|
||||||
match menu_type {
|
|
||||||
ContextMenuType::FilePanel => {
|
|
||||||
match selected {
|
|
||||||
0 => action_play_selection(state, player)?,
|
|
||||||
1 => state.add_to_playlist(),
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ContextMenuType::Playlist => {
|
|
||||||
match selected {
|
|
||||||
0 => {
|
|
||||||
// Remove
|
|
||||||
let was_playing_removed = state.playlist_index == state.selected_playlist_index;
|
|
||||||
state.remove_selected_playlist_item();
|
|
||||||
|
|
||||||
// Handle edge cases after removal
|
|
||||||
if state.playlist.is_empty() {
|
|
||||||
state.player_state = PlayerState::Stopped;
|
|
||||||
state.current_file = None;
|
|
||||||
player.stop()?;
|
|
||||||
} else if was_playing_removed && state.player_state == PlayerState::Playing {
|
|
||||||
// Removed currently playing track, start new one at same index
|
|
||||||
state.current_file = Some(state.playlist[state.playlist_index].clone());
|
|
||||||
if let Some(ref path) = state.current_file {
|
|
||||||
player.play(path)?;
|
|
||||||
player.update_metadata();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
1 => {
|
|
||||||
// Randomise
|
|
||||||
state.shuffle_playlist();
|
|
||||||
tracing::info!("Playlist randomised from context menu");
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ContextMenuType::TitleBar => {
|
|
||||||
match selected {
|
|
||||||
0 => {
|
|
||||||
// Stop
|
|
||||||
action_stop(state, player)?;
|
|
||||||
}
|
|
||||||
1 => {
|
|
||||||
// Toggle Loop
|
|
||||||
state.cycle_play_mode();
|
|
||||||
tracing::info!("Play mode: {:?}", state.play_mode);
|
|
||||||
}
|
|
||||||
2 => {
|
|
||||||
// Refresh
|
|
||||||
state.show_refresh_confirm = true;
|
|
||||||
tracing::info!("Refresh requested from context menu");
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
KeyCode::Esc => {
|
KeyCode::Esc => {
|
||||||
state.context_menu = None;
|
state.context_menu = None;
|
||||||
@@ -602,39 +643,13 @@ async fn handle_key_event<B: ratatui::backend::Backend>(terminal: &mut Terminal<
|
|||||||
}
|
}
|
||||||
(KeyCode::Char('d'), _) => {
|
(KeyCode::Char('d'), _) => {
|
||||||
if state.focus_playlist {
|
if state.focus_playlist {
|
||||||
// Remove selected track from playlist
|
action_remove_from_playlist(state, player)?;
|
||||||
state.remove_selected_playlist_item();
|
|
||||||
// If removed currently playing track, handle it
|
|
||||||
if state.playlist.is_empty() {
|
|
||||||
state.player_state = PlayerState::Stopped;
|
|
||||||
state.current_file = None;
|
|
||||||
player.stop()?;
|
|
||||||
} else if state.playlist_index == state.selected_playlist_index {
|
|
||||||
// Removed currently playing track, play next one
|
|
||||||
if state.playlist_index < state.playlist.len() {
|
|
||||||
state.current_file = Some(state.playlist[state.playlist_index].clone());
|
|
||||||
if state.player_state == PlayerState::Playing {
|
|
||||||
if let Some(ref path) = state.current_file {
|
|
||||||
player.play(path)?;
|
|
||||||
player.update_metadata();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(KeyCode::Enter, _) => {
|
(KeyCode::Enter, _) => {
|
||||||
if state.focus_playlist {
|
if state.focus_playlist {
|
||||||
// Play selected track from playlist
|
|
||||||
if state.selected_playlist_index < state.playlist.len() {
|
if state.selected_playlist_index < state.playlist.len() {
|
||||||
state.playlist_index = state.selected_playlist_index;
|
action_play_from_playlist(state, player, false)?;
|
||||||
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!("Playing from playlist: {:?}", path);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
action_play_selection(state, player)?;
|
action_play_selection(state, player)?;
|
||||||
@@ -756,63 +771,7 @@ fn handle_mouse_event(state: &mut AppState, mouse: MouseEvent, title_bar_area: r
|
|||||||
let menu_type = menu.menu_type;
|
let menu_type = menu.menu_type;
|
||||||
let selected = relative_y;
|
let selected = relative_y;
|
||||||
state.context_menu = None;
|
state.context_menu = None;
|
||||||
|
handle_context_menu_action(menu_type, selected, state, player)?;
|
||||||
match menu_type {
|
|
||||||
ContextMenuType::FilePanel => {
|
|
||||||
match selected {
|
|
||||||
0 => action_play_selection(state, player)?,
|
|
||||||
1 => state.add_to_playlist(),
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ContextMenuType::Playlist => {
|
|
||||||
match selected {
|
|
||||||
0 => {
|
|
||||||
// Remove
|
|
||||||
let was_playing_removed = state.playlist_index == state.selected_playlist_index;
|
|
||||||
state.remove_selected_playlist_item();
|
|
||||||
|
|
||||||
// Handle edge cases after removal
|
|
||||||
if state.playlist.is_empty() {
|
|
||||||
state.player_state = PlayerState::Stopped;
|
|
||||||
state.current_file = None;
|
|
||||||
player.stop()?;
|
|
||||||
} else if was_playing_removed && state.player_state == PlayerState::Playing {
|
|
||||||
state.current_file = Some(state.playlist[state.playlist_index].clone());
|
|
||||||
if let Some(ref path) = state.current_file {
|
|
||||||
player.play(path)?;
|
|
||||||
player.update_metadata();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
1 => {
|
|
||||||
// Randomise
|
|
||||||
state.shuffle_playlist();
|
|
||||||
tracing::info!("Playlist randomised from context menu (mouse)");
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ContextMenuType::TitleBar => {
|
|
||||||
match selected {
|
|
||||||
0 => {
|
|
||||||
// Stop
|
|
||||||
action_stop(state, player)?;
|
|
||||||
}
|
|
||||||
1 => {
|
|
||||||
// Toggle Loop
|
|
||||||
state.cycle_play_mode();
|
|
||||||
tracing::info!("Play mode: {:?} (mouse)", state.play_mode);
|
|
||||||
}
|
|
||||||
2 => {
|
|
||||||
// Refresh
|
|
||||||
state.show_refresh_confirm = true;
|
|
||||||
tracing::info!("Refresh requested from context menu (mouse)");
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return Ok(());
|
return Ok(());
|
||||||
} else {
|
} else {
|
||||||
@@ -992,38 +951,9 @@ fn handle_mouse_event(state: &mut AppState, mouse: MouseEvent, title_bar_area: r
|
|||||||
};
|
};
|
||||||
|
|
||||||
if is_double_click {
|
if is_double_click {
|
||||||
// Double click = play the track
|
// Double click = play the track (preserve pause state)
|
||||||
state.playlist_index = actual_track;
|
state.selected_playlist_index = actual_track;
|
||||||
state.current_file = Some(state.playlist[state.playlist_index].clone());
|
action_play_from_playlist(state, player, true)?;
|
||||||
|
|
||||||
match state.player_state {
|
|
||||||
PlayerState::Playing => {
|
|
||||||
// Keep playing
|
|
||||||
if let Some(ref path) = state.current_file {
|
|
||||||
player.play(path)?;
|
|
||||||
player.update_metadata();
|
|
||||||
tracing::info!("Jumped to track: {:?}", path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
PlayerState::Paused => {
|
|
||||||
// Load but stay paused
|
|
||||||
if let Some(ref path) = state.current_file {
|
|
||||||
player.play(path)?;
|
|
||||||
player.update_metadata();
|
|
||||||
player.pause()?;
|
|
||||||
tracing::info!("Jumped to track (paused): {:?}", path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
PlayerState::Stopped => {
|
|
||||||
// Start playing from clicked track
|
|
||||||
state.player_state = PlayerState::Playing;
|
|
||||||
if let Some(ref path) = state.current_file {
|
|
||||||
player.play(path)?;
|
|
||||||
player.update_metadata();
|
|
||||||
tracing::info!("Started playing track: {:?}", path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Reset click tracking after action
|
// Reset click tracking after action
|
||||||
state.last_click_time = None;
|
state.last_click_time = None;
|
||||||
state.last_click_index = None;
|
state.last_click_index = None;
|
||||||
|
|||||||
@@ -249,13 +249,6 @@ impl Player {
|
|||||||
self.is_idle = idle;
|
self.is_idle = idle;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update cache duration (how many seconds are buffered ahead)
|
|
||||||
if let Some(val) = self.get_property("demuxer-cache-duration") {
|
|
||||||
self.cache_duration = val.as_f64();
|
|
||||||
} else {
|
|
||||||
self.cache_duration = None;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_metadata(&mut self) {
|
pub fn update_metadata(&mut self) {
|
||||||
@@ -313,6 +306,13 @@ impl Player {
|
|||||||
if let Some(val) = self.get_property("audio-params/samplerate") {
|
if let Some(val) = self.get_property("audio-params/samplerate") {
|
||||||
self.sample_rate = val.as_i64();
|
self.sample_rate = val.as_i64();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update cache duration (how many seconds are buffered ahead)
|
||||||
|
if let Some(val) = self.get_property("demuxer-cache-duration") {
|
||||||
|
self.cache_duration = val.as_f64();
|
||||||
|
} else {
|
||||||
|
self.cache_duration = None;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_position(&self) -> Option<f64> {
|
pub fn get_position(&self) -> Option<f64> {
|
||||||
|
|||||||
@@ -536,7 +536,7 @@ fn render_status_bar(frame: &mut Frame, state: &AppState, player: &Player, area:
|
|||||||
|
|
||||||
if let Some(cache_dur) = player.cache_duration {
|
if let Some(cache_dur) = player.cache_duration {
|
||||||
if cache_dur > 0.0 {
|
if cache_dur > 0.0 {
|
||||||
right_parts.push(format!("Cache:{:.1}s", cache_dur));
|
right_parts.push(format!("{:.1}s", cache_dur));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user