Skip to content

Commit

Permalink
make playback_buttons clickable again
Browse files Browse the repository at this point in the history
  • Loading branch information
qxb3 committed Jan 13, 2025
1 parent 01ff9f4 commit 712740a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
53 changes: 40 additions & 13 deletions src/fum.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use std::{io::Stdout, time::Duration};
use std::{io::{stdout, Stdout}, time::Duration};

use crossterm::event::{Event, KeyCode, KeyEventKind};
use crossterm::{event::{EnableMouseCapture, Event, KeyCode, KeyEventKind, MouseButton, MouseEventKind}, execute};
use mpris::Player;
use ratatui::{prelude::CrosstermBackend, Terminal};
use ratatui::{layout::Position, prelude::CrosstermBackend, Terminal};
use ratatui_image::picker::Picker;

use crate::{config::Config, meta::Meta, term_config::TermConfig, ui::Ui, utils};

pub struct Fum<'a> {
config: &'a Config,
term_config: &'a TermConfig,

terminal: Terminal<CrosstermBackend<Stdout>>,
ui: Ui<'a>,
picker: Picker,
player: Option<Player>,
meta: Meta,
Expand All @@ -32,11 +30,13 @@ impl<'a> Fum<'a> {
None => Meta::default()
};

Ok(Self {
config,
term_config,
// Enable mouse capture
execute!(stdout(), EnableMouseCapture)
.expect("Failed to enable mouse capture");

Ok(Self {
terminal: ratatui::init(),
ui: Ui::new(config, term_config),
picker,
player,
meta,
Expand All @@ -46,12 +46,10 @@ impl<'a> Fum<'a> {
}

pub fn run(&mut self) {
let mut ui = Ui::new(&self.config, &self.term_config);

while !self.exit {
if self.redraw {
self.terminal.draw(|frame| {
ui.draw(frame, &mut self.meta);
self.ui.draw(frame, &mut self.meta);
self.redraw = false;
}).expect("Failed to draw frame");
}
Expand All @@ -60,7 +58,7 @@ impl<'a> Fum<'a> {
self.update_meta();
}

ratatui::restore();
utils::restore();
}

fn term_events(&mut self) {
Expand All @@ -74,6 +72,17 @@ impl<'a> Fum<'a> {
KeyCode::Char('q') => {
self.exit = true;
},
KeyCode::Char('p') => self.prev(),
KeyCode::Char(' ') => self.play_pause(),
KeyCode::Char('n') => self.next(),
_ => {}
}
},
Event::Mouse(mouse) if mouse.kind == MouseEventKind::Down(MouseButton::Left) => {
match (mouse.column, mouse.row) {
(x, y) if self.ui.playback_buttons.prev.contains(Position::new(x, y)) => self.prev(),
(x, y) if self.ui.playback_buttons.play_pause.contains(Position::new(x, y)) => self.play_pause(),
(x, y) if self.ui.playback_buttons.next.contains(Position::new(x, y)) => self.next(),
_ => {}
}
},
Expand All @@ -98,4 +107,22 @@ impl<'a> Fum<'a> {

self.meta = Meta::default();
}

fn prev(&self) {
if let Some(player) = &self.player {
player.previous().expect("Failed to prev player");
}
}

fn play_pause(&self) {
if let Some(player) = &self.player {
player.play_pause().expect("Failed to play/pause player");
}
}

fn next(&self) {
if let Some(player) = &self.player {
player.next().expect("Failed to next player");
}
}
}
6 changes: 3 additions & 3 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use crate::{config::Config, term_config::TermConfig, utils, meta::Meta};

pub struct PlaybackButtons {
pub prev: Rect,
pub toggle: Rect,
pub play_pause: Rect,
pub next: Rect
}

impl Default for PlaybackButtons {
fn default() -> Self {
Self {
prev: Rect::default(),
toggle: Rect::default(),
play_pause: Rect::default(),
next:Rect::default()
}
}
Expand Down Expand Up @@ -149,7 +149,7 @@ impl<'a> Ui<'a> {
.areas(area);

self.playback_buttons.prev = prev;
self.playback_buttons.toggle = toggle;
self.playback_buttons.play_pause = toggle;
self.playback_buttons.next = next;

frame.render_widget(
Expand Down
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ pub fn restore() {
pub mod player {
use std::{io::Cursor, time::Duration};

use image::{DynamicImage, ImageReader};
use image::ImageReader;
use mpris::{Metadata, PlaybackStatus, Player, PlayerFinder};
use ratatui_image::{picker::Picker, protocol::StatefulProtocol};
use ratatui_image::picker::Picker;
use reqwest::header::RANGE;

use crate::{config::Config, meta::{CoverArt, Meta}};
Expand Down

0 comments on commit 712740a

Please sign in to comment.