mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 06:47:35 +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:
parent
017490aa7f
commit
228fa1c51d
3 changed files with 20 additions and 3 deletions
|
@ -26,6 +26,7 @@
|
||||||
|
|
||||||
#include "SoundPlayerWidget.h"
|
#include "SoundPlayerWidget.h"
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
|
#include <LibCore/MimeData.h>
|
||||||
#include <LibGUI/BoxLayout.h>
|
#include <LibGUI/BoxLayout.h>
|
||||||
#include <LibGUI/Button.h>
|
#include <LibGUI/Button.h>
|
||||||
#include <LibGUI/Label.h>
|
#include <LibGUI/Label.h>
|
||||||
|
@ -120,9 +121,10 @@ void SoundPlayerWidget::hide_scope(bool hide)
|
||||||
void SoundPlayerWidget::open_file(String path)
|
void SoundPlayerWidget::open_file(String path)
|
||||||
{
|
{
|
||||||
NonnullRefPtr<Audio::Loader> loader = Audio::Loader::create(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(),
|
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);
|
"Filetype error", GUI::MessageBox::Type::Error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -145,6 +147,19 @@ void SoundPlayerWidget::open_file(String path)
|
||||||
update_position(0);
|
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
|
int SoundPlayerWidget::normalize_rate(int rate) const
|
||||||
{
|
{
|
||||||
return static_cast<int>(rate * m_sample_ratio);
|
return static_cast<int>(rate * m_sample_ratio);
|
||||||
|
|
|
@ -45,6 +45,8 @@ public:
|
||||||
private:
|
private:
|
||||||
explicit SoundPlayerWidget(GUI::Window&, NonnullRefPtr<Audio::ClientConnection>);
|
explicit SoundPlayerWidget(GUI::Window&, NonnullRefPtr<Audio::ClientConnection>);
|
||||||
|
|
||||||
|
virtual void drop_event(GUI::DropEvent&) override;
|
||||||
|
|
||||||
void update_position(const int position);
|
void update_position(const int position);
|
||||||
void update_ui();
|
void update_ui();
|
||||||
int normalize_rate(int) const;
|
int normalize_rate(int) const;
|
||||||
|
|
|
@ -81,7 +81,7 @@ int main(int argc, char** argv)
|
||||||
});
|
});
|
||||||
|
|
||||||
app_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
|
app_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
|
||||||
Optional<String> path = GUI::FilePicker::get_open_filepath(window, "Open wav file...");
|
Optional<String> path = GUI::FilePicker::get_open_filepath(window, "Open sound file...");
|
||||||
if (path.has_value()) {
|
if (path.has_value()) {
|
||||||
player.open_file(path.value());
|
player.open_file(path.value());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue