diff options
author | Nícolas F. R. A. Prado <n@nfraprado.net> | 2022-02-27 20:29:38 -0500 |
---|---|---|
committer | Brian Gianforcaro <b.gianfo@gmail.com> | 2022-03-02 22:10:05 -0800 |
commit | 2b1ac91764e9caf058d4a6afa2e9ebf8dd571597 (patch) | |
tree | 1dd66c766a88bef9320a670a7b535f2d4e3a0286 /Userland/Applications/SoundPlayer | |
parent | 2e1c017bce31757b19903d26cb5fba934778ccee (diff) | |
download | serenity-2b1ac91764e9caf058d4a6afa2e9ebf8dd571597.zip |
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.
Diffstat (limited to 'Userland/Applications/SoundPlayer')
-rw-r--r-- | Userland/Applications/SoundPlayer/NoVisualizationWidget.cpp | 36 | ||||
-rw-r--r-- | Userland/Applications/SoundPlayer/NoVisualizationWidget.h | 3 |
2 files changed, 36 insertions, 3 deletions
diff --git a/Userland/Applications/SoundPlayer/NoVisualizationWidget.cpp b/Userland/Applications/SoundPlayer/NoVisualizationWidget.cpp index a83338d2b0..7a05c20e36 100644 --- a/Userland/Applications/SoundPlayer/NoVisualizationWidget.cpp +++ b/Userland/Applications/SoundPlayer/NoVisualizationWidget.cpp @@ -1,11 +1,14 @@ /* * Copyright (c) 2021, Cesar Torres <shortanemoia@protonmail.com> * Copyright (c) 2022, the SerenityOS developers. + * Copyright (c) 2022, Nícolas F. R. A. Prado <n@nfraprado.net> * * SPDX-License-Identifier: BSD-2-Clause */ #include "NoVisualizationWidget.h" +#include <AK/LexicalPath.h> +#include <LibCore/File.h> #include <LibGUI/Painter.h> void NoVisualizationWidget::paint_event(GUI::PaintEvent& event) @@ -13,9 +16,36 @@ void NoVisualizationWidget::paint_event(GUI::PaintEvent& event) Frame::paint_event(event); GUI::Painter painter(*this); - 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); + if (m_album_cover) { + painter.draw_scaled_bitmap(frame_inner_rect(), *m_album_cover, m_album_cover->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>) diff --git a/Userland/Applications/SoundPlayer/NoVisualizationWidget.h b/Userland/Applications/SoundPlayer/NoVisualizationWidget.h index 68ee01224f..fb789e0846 100644 --- a/Userland/Applications/SoundPlayer/NoVisualizationWidget.h +++ b/Userland/Applications/SoundPlayer/NoVisualizationWidget.h @@ -17,10 +17,13 @@ class NoVisualizationWidget final : public VisualizationWidget { public: ~NoVisualizationWidget() override = default; void set_buffer(RefPtr<Audio::Buffer>) override; + void start_new_file(StringView) override; private: void paint_event(GUI::PaintEvent&) override; NoVisualizationWidget() = default; + ErrorOr<NonnullRefPtr<Gfx::Bitmap>> get_album_cover(StringView const filename); RefPtr<Gfx::Bitmap> m_serenity_bg; + RefPtr<Gfx::Bitmap> m_album_cover; }; |