1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:27:45 +00:00

SoundPlayer: Load cover image from music files

When the visualization is set to "Album Cover", the player will now try
to load the embedded image. On failure, it defaults to a "Cover" image
file in the directory.

In Player::play_file_path, file_name_changed now needs to be executed
after that the loader have been set, to get the correct image.
This commit is contained in:
Lucas CHOLLET 2022-10-04 01:07:18 +02:00 committed by Andrew Kaster
parent 27539440df
commit 7649feb26f
8 changed files with 58 additions and 10 deletions

View file

@ -12,13 +12,19 @@
#include <LibGUI/Painter.h>
#include <LibGfx/Rect.h>
AlbumCoverVisualizationWidget::AlbumCoverVisualizationWidget(Function<RefPtr<Gfx::Bitmap>()> get_file_cover_from_player)
: m_get_file_cover_from_player(move(get_file_cover_from_player))
{
}
void AlbumCoverVisualizationWidget::paint_event(GUI::PaintEvent& event)
{
Frame::paint_event(event);
GUI::Painter painter(*this);
if (m_album_cover) {
auto album_cover_rect = m_album_cover->rect();
auto const& cover = m_file_cover ? m_file_cover : m_album_cover;
if (cover) {
auto album_cover_rect = cover->rect();
auto height_ratio = frame_inner_rect().height() / (float)album_cover_rect.height();
auto width_ratio = frame_inner_rect().width() / (float)album_cover_rect.width();
@ -27,7 +33,7 @@ void AlbumCoverVisualizationWidget::paint_event(GUI::PaintEvent& event)
Gfx::IntRect fitted_rect = { 0, 0, (int)(album_cover_rect.width() * scale), (int)(album_cover_rect.height() * scale) };
fitted_rect.center_within(frame_inner_rect());
painter.draw_scaled_bitmap(fitted_rect, *m_album_cover, m_album_cover->rect(), 1.0f);
painter.draw_scaled_bitmap(fitted_rect, *cover, cover->rect(), 1.0f);
} else {
if (!m_serenity_bg)
m_serenity_bg = Gfx::Bitmap::try_load_from_file("/res/wallpapers/sunset-retro.png"sv).release_value_but_fixme_should_propagate_errors();
@ -51,6 +57,12 @@ ErrorOr<NonnullRefPtr<Gfx::Bitmap>> AlbumCoverVisualizationWidget::get_album_cov
void AlbumCoverVisualizationWidget::start_new_file(StringView filename)
{
if (m_get_file_cover_from_player)
m_file_cover = m_get_file_cover_from_player();
if (m_file_cover)
return;
auto album_cover_or_error = get_album_cover(filename);
if (album_cover_or_error.is_error())
m_album_cover = nullptr;