1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 09:37:34 +00:00

SoundPlayer: Display album cover in the NoVisualizationWidget

Display the album cover for the current playing song in the
visualization area for the "None" Visualization.

For now only "cover.png" and "cover.jpg" are looked for in the same
directory for the album cover image.

When no cover image is found the serenity background is shown instead as
a fallback.
This commit is contained in:
Nícolas F. R. A. Prado 2022-02-27 20:29:38 -05:00 committed by Brian Gianforcaro
parent 2e1c017bce
commit 2b1ac91764
2 changed files with 36 additions and 3 deletions

View file

@ -1,11 +1,14 @@
/* /*
* Copyright (c) 2021, Cesar Torres <shortanemoia@protonmail.com> * Copyright (c) 2021, Cesar Torres <shortanemoia@protonmail.com>
* Copyright (c) 2022, the SerenityOS developers. * Copyright (c) 2022, the SerenityOS developers.
* Copyright (c) 2022, Nícolas F. R. A. Prado <n@nfraprado.net>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include "NoVisualizationWidget.h" #include "NoVisualizationWidget.h"
#include <AK/LexicalPath.h>
#include <LibCore/File.h>
#include <LibGUI/Painter.h> #include <LibGUI/Painter.h>
void NoVisualizationWidget::paint_event(GUI::PaintEvent& event) void NoVisualizationWidget::paint_event(GUI::PaintEvent& event)
@ -13,9 +16,36 @@ void NoVisualizationWidget::paint_event(GUI::PaintEvent& event)
Frame::paint_event(event); Frame::paint_event(event);
GUI::Painter painter(*this); GUI::Painter painter(*this);
if (!m_serenity_bg) if (m_album_cover) {
m_serenity_bg = Gfx::Bitmap::try_load_from_file("/res/wallpapers/sunset-retro.png").release_value_but_fixme_should_propagate_errors(); painter.draw_scaled_bitmap(frame_inner_rect(), *m_album_cover, m_album_cover->rect(), 1.0f);
painter.draw_scaled_bitmap(frame_inner_rect(), *m_serenity_bg, m_serenity_bg->rect(), 1.0f); } else {
if (!m_serenity_bg)
m_serenity_bg = Gfx::Bitmap::try_load_from_file("/res/wallpapers/sunset-retro.png").release_value_but_fixme_should_propagate_errors();
painter.draw_scaled_bitmap(frame_inner_rect(), *m_serenity_bg, m_serenity_bg->rect(), 1.0f);
}
}
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> NoVisualizationWidget::get_album_cover(StringView const filename)
{
auto directory = LexicalPath::dirname(filename);
static constexpr auto possible_cover_filenames = Array { "cover.png"sv, "cover.jpg"sv };
for (auto& it : possible_cover_filenames) {
LexicalPath cover_path = LexicalPath::join(directory, it);
if (Core::File::exists(cover_path.string()))
return Gfx::Bitmap::try_load_from_file(cover_path.string());
}
return Error::from_string_literal("No cover file found");
}
void NoVisualizationWidget::start_new_file(StringView filename)
{
auto album_cover_or_error = get_album_cover(filename);
if (album_cover_or_error.is_error())
m_album_cover = nullptr;
else
m_album_cover = album_cover_or_error.value();
} }
void NoVisualizationWidget::set_buffer(RefPtr<Audio::Buffer>) void NoVisualizationWidget::set_buffer(RefPtr<Audio::Buffer>)

View file

@ -17,10 +17,13 @@ class NoVisualizationWidget final : public VisualizationWidget {
public: public:
~NoVisualizationWidget() override = default; ~NoVisualizationWidget() override = default;
void set_buffer(RefPtr<Audio::Buffer>) override; void set_buffer(RefPtr<Audio::Buffer>) override;
void start_new_file(StringView) override;
private: private:
void paint_event(GUI::PaintEvent&) override; void paint_event(GUI::PaintEvent&) override;
NoVisualizationWidget() = default; NoVisualizationWidget() = default;
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> get_album_cover(StringView const filename);
RefPtr<Gfx::Bitmap> m_serenity_bg; RefPtr<Gfx::Bitmap> m_serenity_bg;
RefPtr<Gfx::Bitmap> m_album_cover;
}; };