1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 17:57:35 +00:00

SoundPlayer: Fix playback slider page stepping

Fixes a bug that was preventing the playback slider from changing
value when clicking ahead/behind the current position.
This commit is contained in:
Nick Miller 2021-06-01 18:45:09 -07:00 committed by Andreas Kling
parent 15b69eef66
commit f02d976ed7
2 changed files with 22 additions and 3 deletions

View file

@ -19,17 +19,29 @@ public:
GUI::Slider::set_value(value); GUI::Slider::set_value(value);
} }
bool mouse_is_down() const { return m_mouse_is_down; }
protected: protected:
AutoSlider(Orientation orientation) AutoSlider(Orientation orientation)
: GUI::Slider(orientation) : GUI::Slider(orientation)
{ {
} }
virtual void mousedown_event(GUI::MouseEvent& event) override
{
m_mouse_is_down = true;
GUI::Slider::mousedown_event(event);
}
virtual void mouseup_event(GUI::MouseEvent& event) override virtual void mouseup_event(GUI::MouseEvent& event) override
{ {
m_mouse_is_down = false;
if (on_knob_released && is_enabled()) if (on_knob_released && is_enabled())
on_knob_released(value()); on_knob_released(value());
GUI::Slider::mouseup_event(event); GUI::Slider::mouseup_event(event);
} }
private:
bool m_mouse_is_down { false };
}; };

View file

@ -47,10 +47,15 @@ SoundPlayerWidgetAdvancedView::SoundPlayerWidgetAdvancedView(GUI::Window& window
m_visualization = m_player_view->add<BarsVisualizationWidget>(); m_visualization = m_player_view->add<BarsVisualizationWidget>();
// Set a temporary value for total samples.
// This value will be set properly when we load a new file.
const int total_samples = this->manager().total_length() * 44100;
m_playback_progress_slider = m_player_view->add<AutoSlider>(Orientation::Horizontal); m_playback_progress_slider = m_player_view->add<AutoSlider>(Orientation::Horizontal);
m_playback_progress_slider->set_fixed_height(20); m_playback_progress_slider->set_fixed_height(20);
m_playback_progress_slider->set_min(0); m_playback_progress_slider->set_min(0);
m_playback_progress_slider->set_max(this->manager().total_length() * 44100); //this value should be set when we load a new file m_playback_progress_slider->set_max(total_samples);
m_playback_progress_slider->set_page_step(total_samples / 10);
m_playback_progress_slider->on_knob_released = [&](int value) { m_playback_progress_slider->on_knob_released = [&](int value) {
this->manager().seek(value); this->manager().seek(value);
}; };
@ -139,7 +144,9 @@ SoundPlayerWidgetAdvancedView::SoundPlayerWidgetAdvancedView(GUI::Window& window
int samples_played = client_connection().get_played_samples() + this->manager().last_seek(); int samples_played = client_connection().get_played_samples() + this->manager().last_seek();
int current_second = samples_played / 44100; int current_second = samples_played / 44100;
timestamp_label.set_text(String::formatted("Elapsed: {:02}:{:02}:{:02}", current_second / 3600, current_second / 60, current_second % 60)); timestamp_label.set_text(String::formatted("Elapsed: {:02}:{:02}:{:02}", current_second / 3600, current_second / 60, current_second % 60));
m_playback_progress_slider->set_value(samples_played); if (!m_playback_progress_slider->mouse_is_down()) {
m_playback_progress_slider->set_value(samples_played);
}
dynamic_cast<Visualization*>(m_visualization.ptr())->set_buffer(this->manager().current_buffer()); dynamic_cast<Visualization*>(m_visualization.ptr())->set_buffer(this->manager().current_buffer());
dynamic_cast<Visualization*>(m_visualization.ptr())->set_samplerate(loaded_file_samplerate()); dynamic_cast<Visualization*>(m_visualization.ptr())->set_samplerate(loaded_file_samplerate());
@ -191,11 +198,11 @@ void SoundPlayerWidgetAdvancedView::open_file(StringView path)
} }
m_window.set_title(String::formatted("{} - Sound Player", loader->file()->filename())); m_window.set_title(String::formatted("{} - Sound Player", loader->file()->filename()));
m_playback_progress_slider->set_max(loader->total_samples()); m_playback_progress_slider->set_max(loader->total_samples());
m_playback_progress_slider->set_page_step(loader->total_samples() / 10);
m_playback_progress_slider->set_enabled(true); m_playback_progress_slider->set_enabled(true);
m_play_button->set_enabled(true); m_play_button->set_enabled(true);
m_play_button->set_icon(*m_pause_icon); m_play_button->set_icon(*m_pause_icon);
m_stop_button->set_enabled(true); m_stop_button->set_enabled(true);
m_playback_progress_slider->set_max(loader->total_samples());
manager().set_loader(move(loader)); manager().set_loader(move(loader));
set_has_loaded_file(true); set_has_loaded_file(true);
set_loaded_file_samplerate(loader->sample_rate()); set_loaded_file_samplerate(loader->sample_rate());