From 228fa1c51dbe8432b500816299d1fc0cc797aaa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20Offenh=C3=A4user?= Date: Wed, 2 Dec 2020 23:58:00 +0100 Subject: [PATCH] SoundPlayer: Accept drop events Files can now be dragged into the window and loading errors will be handled more gracefully. --- .../SoundPlayer/SoundPlayerWidget.cpp | 19 +++++++++++++++++-- Applications/SoundPlayer/SoundPlayerWidget.h | 2 ++ Applications/SoundPlayer/main.cpp | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Applications/SoundPlayer/SoundPlayerWidget.cpp b/Applications/SoundPlayer/SoundPlayerWidget.cpp index 1f44f97d1cd..4c221a064d5 100644 --- a/Applications/SoundPlayer/SoundPlayerWidget.cpp +++ b/Applications/SoundPlayer/SoundPlayerWidget.cpp @@ -26,6 +26,7 @@ #include "SoundPlayerWidget.h" #include +#include #include #include #include @@ -120,9 +121,10 @@ void SoundPlayerWidget::hide_scope(bool hide) void SoundPlayerWidget::open_file(String path) { NonnullRefPtr 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(rate * m_sample_ratio); diff --git a/Applications/SoundPlayer/SoundPlayerWidget.h b/Applications/SoundPlayer/SoundPlayerWidget.h index 58500c0d093..ab7bc60aeaa 100644 --- a/Applications/SoundPlayer/SoundPlayerWidget.h +++ b/Applications/SoundPlayer/SoundPlayerWidget.h @@ -45,6 +45,8 @@ public: private: explicit SoundPlayerWidget(GUI::Window&, NonnullRefPtr); + virtual void drop_event(GUI::DropEvent&) override; + void update_position(const int position); void update_ui(); int normalize_rate(int) const; diff --git a/Applications/SoundPlayer/main.cpp b/Applications/SoundPlayer/main.cpp index 1bb3b98e51a..cb0ae4a6072 100644 --- a/Applications/SoundPlayer/main.cpp +++ b/Applications/SoundPlayer/main.cpp @@ -81,7 +81,7 @@ int main(int argc, char** argv) }); app_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) { - Optional path = GUI::FilePicker::get_open_filepath(window, "Open wav file..."); + Optional path = GUI::FilePicker::get_open_filepath(window, "Open sound file..."); if (path.has_value()) { player.open_file(path.value()); }