1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 19:38:12 +00:00

SoundPlayer: Accept drop events

Files can now be dragged into the window and loading errors will be
handled more gracefully.
This commit is contained in:
Julian Offenhäuser 2020-12-02 23:58:00 +01:00 committed by Andreas Kling
parent 017490aa7f
commit 228fa1c51d
3 changed files with 20 additions and 3 deletions

View file

@ -26,6 +26,7 @@
#include "SoundPlayerWidget.h"
#include <AK/StringBuilder.h>
#include <LibCore/MimeData.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
#include <LibGUI/Label.h>
@ -120,9 +121,10 @@ void SoundPlayerWidget::hide_scope(bool hide)
void SoundPlayerWidget::open_file(String path)
{
NonnullRefPtr<Audio::Loader> loader = Audio::Loader::create(path);
if (loader->has_error()) {
if (loader->has_error() || !loader->sample_rate()) {
const String error_string = loader->error_string();
GUI::MessageBox::show(window(),
String::formatted("Failed to load audio file: {} ({})", path, loader->error_string()),
String::formatted("Failed to load audio file: {} ({})", path, error_string.is_null() ? "Unknown error" : error_string),
"Filetype error", GUI::MessageBox::Type::Error);
return;
}
@ -145,6 +147,19 @@ void SoundPlayerWidget::open_file(String path)
update_position(0);
}
void SoundPlayerWidget::drop_event(GUI::DropEvent& event)
{
event.accept();
window()->move_to_front();
if (event.mime_data().has_urls()) {
auto urls = event.mime_data().urls();
if (urls.is_empty())
return;
open_file(urls.first().path());
}
}
int SoundPlayerWidget::normalize_rate(int rate) const
{
return static_cast<int>(rate * m_sample_ratio);