Skip to content

Commit d80f02f

Browse files
committed
Add fade and volume step inputs to settings
1 parent d3265c8 commit d80f02f

File tree

10 files changed

+162
-11
lines changed

10 files changed

+162
-11
lines changed

assets/icons/tick.svg

Lines changed: 8 additions & 0 deletions
Loading

src/core/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
mod macros;
2-
3-
pub use self::macros::*;

src/gui/app.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ impl BumpApp {
164164
Ok(listener) => Some(listener),
165165
Err(_) => None,
166166
};
167+
let settings = Settings::new(&config);
167168

168169
let mut app = Self {
169170
player: Player::new(sender.clone(), &library, &config),
@@ -177,7 +178,7 @@ impl BumpApp {
177178
hard_pause: None,
178179
listener: Cell::new(listener),
179180
hotkeys: None,
180-
settings: Default::default(),
181+
settings,
181182
};
182183

183184
app.enable_hotkeys(app.config.get_enable_hotkeys());

src/gui/components/input.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub struct Input<Message> {
1818
}
1919

2020
/// Creates new [`Input`]
21+
#[allow(unused)]
2122
pub fn input<Message>(
2223
placeholder: Option<String>,
2324
value: Option<String>,

src/gui/components/number_input.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use iced::{
55

66
#[derive(Debug, Clone)]
77
/// [`NumberInput`] events
8+
#[allow(unused)]
89
pub enum Event {
910
InputChanged(String),
1011
}
@@ -15,6 +16,7 @@ pub struct NumberInput<Message> {
1516
}
1617

1718
/// Creates new [`NumberInput`]
19+
#[allow(unused)]
1820
pub fn number_input<Message>(
1921
value: Option<u32>,
2022
on_change: impl Fn(Option<u32>) -> Message + 'static,

src/gui/settings/msg.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ use super::SettingsPage;
66
pub enum SettingsMsg {
77
PickSearchPath,
88
Page(SettingsPage),
9+
Fade(String),
10+
FadeSave,
11+
VolJump(String),
12+
VolJumpSave,
913
}

src/gui/settings/playback.rs

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
use iced::{
2-
widget::{column, scrollable},
2+
widget::{column, container, row, scrollable, text, text_input},
33
Renderer,
44
};
5-
use iced_core::Padding;
5+
use iced_core::{Length, Padding};
66

77
use crate::{
88
config::ConfMsg,
99
gui::{
1010
app::{BumpApp, Msg},
11-
theme::Theme,
11+
svg_data::TICK,
12+
theme::{Text, Theme},
13+
widgets::{hover_grad::HoverGrad, svg_button::SvgButton},
1214
},
1315
};
1416

15-
use super::elements::toggler;
17+
use super::{elements::toggler, SettingsMsg};
1618

1719
type Element<'a> = iced::Element<'a, Msg, Renderer<Theme>>;
1820

@@ -26,15 +28,66 @@ impl BumpApp {
2628
|val| Msg::Conf(ConfMsg::ShuffleCurrent(val))
2729
),
2830
toggler(
29-
"Automatically start playing song on start".to_owned(),
31+
"Auto play on startup".to_owned(),
3032
self.config.get_autoplay(),
3133
|val| Msg::Conf(ConfMsg::Autoplay(val))
3234
),
3335
toggler(
34-
"Play songs without gap between them".to_owned(),
36+
"Gapless playback".to_owned(),
3537
self.config.get_gapless(),
3638
|val| Msg::Conf(ConfMsg::Gapless(val))
3739
),
40+
column![
41+
text("Fade play/pause:").style(Text::Normal),
42+
HoverGrad::new(
43+
row![
44+
container(
45+
SvgButton::new(TICK.into())
46+
.width(15)
47+
.height(15)
48+
.on_press(Msg::Settings(
49+
SettingsMsg::FadeSave
50+
)),
51+
)
52+
.height(28)
53+
.padding(3)
54+
.center_x()
55+
.center_y(),
56+
text_input("Fade", &self.settings.fade).on_input(
57+
|val| Msg::Settings(SettingsMsg::Fade(val))
58+
)
59+
]
60+
.into()
61+
)
62+
.height(Length::Shrink),
63+
]
64+
.spacing(3),
65+
column![
66+
text("Volume jump:").style(Text::Normal),
67+
HoverGrad::new(
68+
row![
69+
container(
70+
SvgButton::new(TICK.into())
71+
.width(15)
72+
.height(15)
73+
.on_press(Msg::Settings(
74+
SettingsMsg::VolJumpSave
75+
)),
76+
)
77+
.height(28)
78+
.padding(3)
79+
.center_x()
80+
.center_y(),
81+
text_input("Fade", &self.settings.vol_jmp)
82+
.on_input(|val| Msg::Settings(
83+
SettingsMsg::VolJump(val)
84+
))
85+
]
86+
.into()
87+
)
88+
.height(Length::Shrink),
89+
]
90+
.spacing(3)
3891
]
3992
.padding(Padding::from([5, 15])),
4093
)

src/gui/settings/settings.rs

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use std::path::PathBuf;
1+
use std::{path::PathBuf, time::Duration};
22

3+
use eyre::{Report, Result};
34
use iced::{
45
widget::{button, column, row, text},
56
Command, Renderer,
@@ -8,7 +9,7 @@ use iced_core::{Length, Padding};
89
use serde_derive::{Deserialize, Serialize};
910

1011
use crate::{
11-
config::ConfMsg,
12+
config::{ConfMsg, Config},
1213
gui::{
1314
app::{BumpApp, Msg},
1415
theme::{Button, Theme},
@@ -29,12 +30,38 @@ pub enum SettingsPage {
2930

3031
pub struct Settings {
3132
page: SettingsPage,
33+
pub fade: String,
34+
pub vol_jmp: String,
35+
}
36+
37+
impl Settings {
38+
/// Creates new [`Settings`] struct
39+
pub fn new(config: &Config) -> Self {
40+
let fade_secs = config.get_fade().as_secs();
41+
let fade_millis = config.get_fade().subsec_millis();
42+
let fade = format!(
43+
"{:02}:{:02}.{:02}",
44+
fade_secs / 60,
45+
fade_secs % 60,
46+
fade_millis,
47+
);
48+
49+
let vol_jmp = format!("{}", config.get_volume_step());
50+
51+
Self {
52+
fade,
53+
vol_jmp,
54+
..Default::default()
55+
}
56+
}
3257
}
3358

3459
impl Default for Settings {
3560
fn default() -> Self {
3661
Self {
3762
page: SettingsPage::Library,
63+
fade: "00:00.150".to_owned(),
64+
vol_jmp: "0.1".to_owned(),
3865
}
3966
}
4067
}
@@ -67,6 +94,29 @@ impl BumpApp {
6794
self.settings.page = page;
6895
Command::none()
6996
}
97+
SettingsMsg::Fade(val) => {
98+
self.settings.fade = val;
99+
Command::none()
100+
}
101+
SettingsMsg::FadeSave => {
102+
if let Ok(val) = self.convert_to_duration(&self.settings.fade)
103+
{
104+
self.config.set_fade(val);
105+
self.player.fade(self.config.get_fade());
106+
}
107+
Command::none()
108+
}
109+
SettingsMsg::VolJump(val) => {
110+
self.settings.vol_jmp = val;
111+
Command::none()
112+
}
113+
SettingsMsg::VolJumpSave => {
114+
if let Ok(val) = self.settings.vol_jmp.parse::<f32>() {
115+
self.config.set_volume_step(val);
116+
self.player.volume_step(self.config.get_volume_step());
117+
}
118+
Command::none()
119+
}
70120
}
71121
}
72122

@@ -103,6 +153,25 @@ impl BumpApp {
103153
.spacing(5)
104154
.into()
105155
}
156+
157+
fn convert_to_duration(&self, dur: &str) -> Result<Duration> {
158+
if let Some(minute_sep) = dur.find(':') {
159+
if let Some(second_sep) = dur[minute_sep + 1..].find('.') {
160+
let minutes: u64 = dur[..minute_sep].parse()?;
161+
let seconds: u64 = dur
162+
[minute_sep + 1..minute_sep + second_sep + 1]
163+
.parse()?;
164+
let milliseconds: u64 =
165+
dur[minute_sep + second_sep + 2..].parse()?;
166+
let nanoseconds: u32 =
167+
format!("{:09}", milliseconds * 1_000_000).parse()?;
168+
169+
return Ok(Duration::new(minutes * 60 + seconds, nanoseconds));
170+
}
171+
}
172+
173+
Err(Report::msg("Invalid format"))
174+
}
106175
}
107176

108177
pub async fn pick_folder() -> Option<Vec<PathBuf>> {

src/gui/svg_data.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ pub const PLUS: SvgData =
4444
SvgData::new(include_bytes!("../../assets/icons/add.svg"));
4545
*/
4646

47+
pub const TICK: SvgData =
48+
SvgData::new(include_bytes!("../../assets/icons/tick.svg"));
49+
4750
/// Gets play or pause icon based on play bool
4851
pub fn pp_icon(play: bool) -> Handle {
4952
if play {

src/player/player.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,18 @@ impl Player {
306306
self.set_playlist((0..library.count()).collect());
307307
self.find_current(id);
308308
}
309+
310+
/// Sets fade duration
311+
pub fn fade(&mut self, fade: Duration) {
312+
if let Err(e) = self.sinker.set_fade(fade) {
313+
error!("Failed to set fade: {e}");
314+
}
315+
}
316+
317+
/// Sets player volume step
318+
pub fn volume_step(&mut self, step: f32) {
319+
self.volume_step = step;
320+
}
309321
}
310322

311323
///>=======================================================================<///

0 commit comments

Comments
 (0)