Skip to content

Commit fb25542

Browse files
committed
Merge branch 'dev'
2 parents 7f9e7a5 + 2f1746a commit fb25542

File tree

5 files changed

+84
-84
lines changed

5 files changed

+84
-84
lines changed

build.rs

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/cli/args.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use {anyhow::bail, clap::Parser};
44

55
use crate::TapError;
66

7-
// TODO - update README
8-
97
// A struct that represents the command line arguments.
108
#[derive(Debug, Parser)]
119
#[command(

src/finder/finder_view.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
use std::{cmp::min, time::Instant};
22

3-
use cursive::event::Key;
4-
use once_cell::sync::Lazy;
5-
63
use {
74
anyhow::anyhow,
85
cursive::{
9-
event::{Event, EventResult, MouseButton, MouseEvent},
6+
event::{Event, EventResult, Key, MouseButton, MouseEvent},
107
theme::Effect,
118
view::{Nameable, Resizable},
129
CbSink, Cursive, Printer, View, XY,
1310
},
11+
once_cell::sync::Lazy,
1412
rand::{seq::SliceRandom, thread_rng},
1513
unicode_segmentation::UnicodeSegmentation,
1614
unicode_width::UnicodeWidthStr,
@@ -63,10 +61,8 @@ impl FinderView {
6361

6462
pub fn load(filter: LibraryFilter) -> Option<EventResult> {
6563
Some(EventResult::with_cb(move |siv: &mut Cursive| {
66-
siv.set_fps(0);
67-
6864
let library = {
69-
let base_library = siv
65+
let base_library = &siv
7066
.user_data::<Library>()
7167
.expect("Library should be set in user_data");
7268

@@ -379,6 +375,9 @@ impl FinderView {
379375
}
380376

381377
pub fn remove_finder_view(siv: &mut cursive::Cursive) {
378+
siv.call_on_name(player::ID, |player_view: &mut PlayerView| {
379+
player_view.show();
380+
});
382381
if siv.find_name::<FinderView>(super::ID).is_some() {
383382
siv.set_fps(crate::FPS);
384383
siv.pop_layer();

src/player/mod.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,6 @@ impl Player {
120120
self.last_elapsed = Duration::ZERO;
121121
}
122122

123-
// Starts playback if not playing, pauses otherwise.
124-
pub fn play_or_pause(&mut self) {
125-
match self.status {
126-
PlaybackStatus::Paused => self.resume(),
127-
PlaybackStatus::Playing => self.pause(),
128-
PlaybackStatus::Stopped => self.play(),
129-
};
130-
}
131-
132123
// Play the last track in the current playlist.
133124
pub fn play_last_track(&mut self) {
134125
let last = self.current.audio_files.len().saturating_sub(1);
@@ -162,6 +153,10 @@ impl Player {
162153
}
163154
}
164155

156+
pub fn set_volume(&mut self, value: f32) {
157+
self.sink.set_volume(value);
158+
}
159+
165160
// Increases volume by 10%, to maximum of 120%.
166161
pub fn increment_volume(&mut self) {
167162
if self.volume < 120 {
@@ -231,8 +226,10 @@ impl Player {
231226

232227
// Performs the seek operation in the forward direction.
233228
pub fn seek_forward(&mut self, seek: Duration) {
234-
if !self.is_playing() {
235-
self.play_or_pause();
229+
if self.is_paused() {
230+
self.resume();
231+
} else if self.is_stopped() {
232+
self.play();
236233
}
237234

238235
let elapsed = self.elapsed();
@@ -254,8 +251,10 @@ impl Player {
254251

255252
// Performs the seek operation in the backward direction.
256253
pub fn seek_backward(&mut self, seek: Duration) {
257-
if !self.is_playing() {
258-
self.play_or_pause();
254+
if self.is_paused() {
255+
self.resume();
256+
} else if self.is_stopped() {
257+
self.play();
259258
}
260259

261260
let elapsed = self.elapsed();

src/player/player_view.rs

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
use std::time::Duration;
22

3-
use cursive::{
4-
theme::Style,
5-
utils::{markup::StyledString, span::SpannedString},
6-
};
7-
83
use {
94
cursive::{
105
event::{Event, EventResult, MouseButton, MouseEvent},
11-
theme::{ColorStyle, Effect},
6+
theme::{ColorStyle, Effect, Style},
127
traits::View,
8+
utils::{markup::StyledString, span::SpannedString},
139
view::Nameable,
1410
CbSink, Cursive, Printer, XY,
1511
},
@@ -29,6 +25,7 @@ const SEEK_TIME: Duration = Duration::from_secs(10);
2925

3026
// Drawing constants
3127
const SPACER: &'static str = " ";
28+
const TICK: u32 = 5;
3229

3330
// A struct representing the view and state of the audio player.
3431
pub struct PlayerView {
@@ -44,6 +41,12 @@ pub struct PlayerView {
4441
timed_bool: ExpiringBool,
4542
// Whether or not the player is being shown.
4643
is_visible: bool,
44+
// The elapsed playback time in whole seconds.
45+
elapsed: usize,
46+
// The duration of the current track.
47+
duration: usize,
48+
// The index of the current track.
49+
index: usize,
4750
// The styled strings required to render the header and playlist.
4851
lines: Vec<SpannedString<Style>>,
4952
// The vertical offset required to ensure the current track is visible in the playlist.
@@ -62,6 +65,9 @@ impl PlayerView {
6265
mouse_seek_time: None,
6366
offset_y: 0,
6467
is_visible: true,
68+
elapsed: 0,
69+
duration: 0,
70+
index: 0,
6571
lines: Vec::new(),
6672
showing_volume: ExpiringBool::new(false, Duration::from_millis(1500)),
6773
number_input: vec![],
@@ -74,36 +80,58 @@ impl PlayerView {
7480
let cb_sink = siv.cb_sink().clone();
7581
let player_view = PlayerView::new(player, cb_sink).with_name(super::ID);
7682

77-
siv.set_fps(crate::FPS);
83+
siv.set_fps(TICK);
7884
siv.pop_layer();
7985
siv.add_layer(player_view);
8086
}
8187

8288
pub fn update_playlist(&mut self, next: Playlist, set_playing: bool) {
83-
_ = self.cb_sink.send(Box::new(|siv| {
84-
siv.set_fps(crate::FPS);
85-
}));
86-
8789
let is_stopped = self.player.is_stopped();
90+
91+
let volume = if self.player.is_muted {
92+
0.0
93+
} else {
94+
(self.player.volume as f32) / 100.0
95+
};
96+
8897
self.player.previous = Some(self.player.current.clone());
8998
self.player.current = next;
9099
self.player.stop();
91100
self.player.play();
92101

102+
self.player.set_volume(volume);
103+
93104
if !set_playing && is_stopped {
94105
self.player.stop();
95106
}
96107

97108
self.lines.clear();
98-
self.is_visible = true;
109+
110+
self.show();
99111
}
100112

101113
pub fn hide(&mut self) {
102114
self.is_visible = false;
115+
116+
_ = self.cb_sink.send(Box::new(|siv| {
117+
siv.set_fps(0);
118+
}));
103119
}
104120

105121
pub fn show(&mut self) {
106122
self.is_visible = true;
123+
124+
_ = self.cb_sink.send(Box::new(|siv| {
125+
siv.set_fps(TICK);
126+
}));
127+
}
128+
129+
fn play_or_pause(&mut self) {
130+
match self.player.status {
131+
PlaybackStatus::Paused => self.player.resume(),
132+
PlaybackStatus::Playing => self.player.pause(),
133+
PlaybackStatus::Stopped => self.player.play(),
134+
};
107135
}
108136

109137
fn increase_volume(&mut self) {
@@ -265,12 +293,12 @@ impl PlayerView {
265293
let index = position.y - offset.y + self.offset_y - 1;
266294

267295
if index == self.player.current.index {
268-
self.player.play_or_pause();
296+
self.play_or_pause();
269297
} else {
270298
self.player.play_index(index);
271299
}
272300
}
273-
Area::Background => _ = self.player.play_or_pause(),
301+
Area::Background => _ = self.play_or_pause(),
274302
}
275303
}
276304

@@ -364,16 +392,6 @@ impl PlayerView {
364392
format!("{}vol: {:>3} %{}", SPACER, self.player.volume, SPACER)
365393
}
366394

367-
// The elapsed playback time to display. When seeking with the mouse we use the
368-
// elapsed time had the seeking process completed.
369-
#[inline]
370-
fn elapsed(&self) -> usize {
371-
match self.mouse_seek_time {
372-
Some(t) if self.player.is_paused() => t,
373-
_ => self.player.elapsed().as_secs() as usize,
374-
}
375-
}
376-
377395
// Computes the y offset needed to show the results of the fuzzy match.
378396
#[inline]
379397
fn update_offset(&self) -> usize {
@@ -440,6 +458,14 @@ impl View for PlayerView {
440458
}
441459
}
442460

461+
self.elapsed = match self.mouse_seek_time {
462+
Some(t) if self.player.is_paused() => t,
463+
_ => self.player.elapsed().as_secs() as usize,
464+
};
465+
466+
self.duration = self.player.current_file().duration;
467+
self.index = self.player.current.index;
468+
443469
self.size = self.required_size(size);
444470
self.offset_y = self.update_offset();
445471

@@ -465,24 +491,23 @@ impl View for PlayerView {
465491
}
466492

467493
let (w, h) = (self.size.x, self.size.y);
468-
let f = self.player.current_file();
469-
let duration_x = w.saturating_sub(9);
470-
let elapsed = self.elapsed();
471-
let (length, extra) = ratio(elapsed, f.duration, w.saturating_sub(16));
494+
495+
let dur_col = w.saturating_sub(9);
472496

473497
// HEADER
474498
if h > 1 {
475499
// Artist + album + year
476-
if let Some(entry) = self.lines.chunks(5).nth(self.player.current.index) {
477-
if let Some(header) = entry.get(4) {
500+
501+
if let Some(line) = self.lines.chunks(5).nth(self.index) {
502+
if let Some(header) = line.get(4) {
478503
p.print_styled((0, 0), header);
479504
}
480505
}
481506

482507
// Volume
483508
if self.showing_volume.is_true() {
484509
p.with_color(ColorStyles::prompt(), |p| {
485-
p.print((duration_x.saturating_sub(5), 0), &self.volume())
510+
p.print((dur_col.saturating_sub(5), 0), &self.volume())
486511
});
487512
};
488513
}
@@ -525,38 +550,39 @@ impl View for PlayerView {
525550
if let Some(option) = self.playback_opts() {
526551
p.with_color(ColorStyles::info(), |p| {
527552
p.with_effect(Effect::Italic, |p| {
528-
p.print((duration_x.saturating_sub(3), row), option)
553+
p.print((dur_col.saturating_sub(3), row), option)
529554
})
530555
})
531556
}
532557
}
533558

534559
// Duration
535-
p.print_styled((duration_x, row), duration_line);
560+
p.print_styled((dur_col, row), duration_line);
536561
}
537562

538563
// FOOTER
539564
if h > 0 {
540-
let bottom_row = h - 1;
541-
let remaining = f.duration.saturating_sub(elapsed);
565+
let last_row = h - 1;
566+
let (length, extra) = ratio(self.elapsed, self.duration, w.saturating_sub(16));
567+
let remaining = self.duration.saturating_sub(self.elapsed);
542568

543569
// Elapsed time
544570
p.with_color(ColorStyles::hl(), |p| {
545-
p.print((0, bottom_row), &mins_and_secs(elapsed));
571+
p.print((0, last_row), &mins_and_secs(self.elapsed));
546572
});
547573

548574
// Progress bar
549575
p.with_color(ColorStyles::progress(), |p| {
550-
p.print((length + 8, bottom_row), sub_block(extra));
576+
p.print((length + 8, last_row), sub_block(extra));
551577
});
552578
p.cropped((length + 8, h))
553579
.with_color(ColorStyles::progress(), |p| {
554-
p.print_hline((8, bottom_row), length, "█");
580+
p.print_hline((8, last_row), length, "█");
555581
});
556582

557583
// Remaining time
558584
p.with_color(ColorStyles::hl(), |p| {
559-
p.print((duration_x, bottom_row), &mins_and_secs(remaining))
585+
p.print((dur_col, last_row), &mins_and_secs(remaining))
560586
});
561587
}
562588
}
@@ -567,7 +593,7 @@ impl View for PlayerView {
567593

568594
if let Some(action) = PLAYER_EVENT_TO_ACTION.get(&event) {
569595
match action {
570-
PlayOrPause => self.player.play_or_pause(),
596+
PlayOrPause => self.play_or_pause(),
571597
Stop => self.player.stop(),
572598
Next => self.next(),
573599
Previous => self.previous(),

0 commit comments

Comments
 (0)