From 4c49e0f4a327f1b3ae4b228dbc8d78ebeaa999f6 Mon Sep 17 00:00:00 2001 From: Caoimhe Date: Sun, 19 Mar 2023 16:36:51 +0000 Subject: SoundPlayer: Move `get_image_from_music_file` to the advanced view --- .../SoundPlayer/SoundPlayerWidgetAdvancedView.cpp | 21 +++++++++++++++++- .../SoundPlayer/SoundPlayerWidgetAdvancedView.h | 5 ++++- Userland/Applications/SoundPlayer/main.cpp | 25 ++++++---------------- 3 files changed, 30 insertions(+), 21 deletions(-) (limited to 'Userland/Applications') diff --git a/Userland/Applications/SoundPlayer/SoundPlayerWidgetAdvancedView.cpp b/Userland/Applications/SoundPlayer/SoundPlayerWidgetAdvancedView.cpp index 9fe0fc8aae..35216a5b7c 100644 --- a/Userland/Applications/SoundPlayer/SoundPlayerWidgetAdvancedView.cpp +++ b/Userland/Applications/SoundPlayer/SoundPlayerWidgetAdvancedView.cpp @@ -6,9 +6,11 @@ */ #include "SoundPlayerWidgetAdvancedView.h" +#include "AlbumCoverVisualizationWidget.h" #include "BarsVisualizationWidget.h" #include "M3UParser.h" #include "PlaybackManager.h" +#include "SampleWidget.h" #include #include #include @@ -24,9 +26,10 @@ #include #include -SoundPlayerWidgetAdvancedView::SoundPlayerWidgetAdvancedView(GUI::Window& window, Audio::ConnectionToServer& connection) +SoundPlayerWidgetAdvancedView::SoundPlayerWidgetAdvancedView(GUI::Window& window, Audio::ConnectionToServer& connection, ImageDecoderClient::Client& image_decoder_client) : Player(connection) , m_window(window) + , m_image_decoder_client(image_decoder_client) { window.resize(455, 350); window.set_resizable(true); @@ -173,6 +176,22 @@ void SoundPlayerWidgetAdvancedView::set_playlist_visible(bool visible) } } +RefPtr SoundPlayerWidgetAdvancedView::get_image_from_music_file() +{ + auto const& pictures = this->pictures(); + if (pictures.is_empty()) + return {}; + + // FIXME: We randomly select the first picture available for the track, + // We might want to hardcode or let the user set a preference. + auto decoded_image_or_error = m_image_decoder_client.decode_image(pictures[0].data); + if (!decoded_image_or_error.has_value()) + return {}; + + auto const decoded_image = decoded_image_or_error.release_value(); + return decoded_image.frames[0].bitmap; +} + void SoundPlayerWidgetAdvancedView::play_state_changed(Player::PlayState state) { sync_previous_next_actions(); diff --git a/Userland/Applications/SoundPlayer/SoundPlayerWidgetAdvancedView.h b/Userland/Applications/SoundPlayer/SoundPlayerWidgetAdvancedView.h index eb00456f5b..51e335f1a3 100644 --- a/Userland/Applications/SoundPlayer/SoundPlayerWidgetAdvancedView.h +++ b/Userland/Applications/SoundPlayer/SoundPlayerWidgetAdvancedView.h @@ -16,6 +16,7 @@ #include #include #include +#include class SoundPlayerWidgetAdvancedView final : public GUI::Widget , public Player { @@ -24,6 +25,7 @@ class SoundPlayerWidgetAdvancedView final : public GUI::Widget public: void set_nonlinear_volume_slider(bool nonlinear); void set_playlist_visible(bool visible); + RefPtr get_image_from_music_file(); template void set_visualization(Args... args) @@ -53,13 +55,14 @@ protected: void keydown_event(GUI::KeyEvent&) override; private: - SoundPlayerWidgetAdvancedView(GUI::Window&, Audio::ConnectionToServer&); + SoundPlayerWidgetAdvancedView(GUI::Window&, Audio::ConnectionToServer&, ImageDecoderClient::Client&); void sync_previous_next_actions(); void drag_enter_event(GUI::DragEvent& event) override; void drop_event(GUI::DropEvent& event) override; GUI::Window& m_window; + ImageDecoderClient::Client& m_image_decoder_client; RefPtr m_splitter; RefPtr m_player_view; diff --git a/Userland/Applications/SoundPlayer/main.cpp b/Userland/Applications/SoundPlayer/main.cpp index 47200616a8..f717d1b5df 100644 --- a/Userland/Applications/SoundPlayer/main.cpp +++ b/Userland/Applications/SoundPlayer/main.cpp @@ -1,6 +1,6 @@ /* * Copyright (c) 2018-2020, Andreas Kling - * Copyright (c) 2021, the SerenityOS developers. + * Copyright (c) 2021-2023, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ @@ -48,7 +48,7 @@ ErrorOr serenity_main(Main::Arguments arguments) window->set_icon(app_icon.bitmap_for_size(16)); // start in advanced view by default - Player* player = TRY(window->set_main_widget(window, audio_client)); + Player* player = TRY(window->set_main_widget(window, audio_client, decoder_client)); if (!file_path.is_empty()) { player->play_file_path(file_path); @@ -134,23 +134,10 @@ ErrorOr serenity_main(Main::Arguments arguments) visualization_actions.add_action(samples); auto album_cover_visualization = GUI::Action::create_checkable("&Album Cover", [&](auto&) { - auto get_image_from_music_file = [&player, &decoder_client]() -> RefPtr { - auto const& pictures = player->pictures(); - - if (pictures.is_empty()) - return {}; - - // FIXME: We randomly select the first picture available for the track, - // We might want to hardcode or let the user set a preference. - auto decoded_image_or_error = decoder_client->decode_image(pictures[0].data); - if (!decoded_image_or_error.has_value()) - return {}; - - auto const decoded_image = decoded_image_or_error.release_value(); - return decoded_image.frames[0].bitmap; - }; - - static_cast(player)->set_visualization(get_image_from_music_file); + auto* view = static_cast(player); + view->set_visualization([&view]() { + return view->get_image_from_music_file(); + }); }); TRY(visualization_menu->try_add_action(album_cover_visualization)); visualization_actions.add_action(album_cover_visualization); -- cgit v1.2.3