From 2b1ac91764e9caf058d4a6afa2e9ebf8dd571597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=ADcolas=20F=2E=20R=2E=20A=2E=20Prado?= Date: Sun, 27 Feb 2022 20:29:38 -0500 Subject: [PATCH] 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. --- .../SoundPlayer/NoVisualizationWidget.cpp | 36 +++++++++++++++++-- .../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 * Copyright (c) 2022, the SerenityOS developers. + * Copyright (c) 2022, NĂ­colas F. R. A. Prado * * SPDX-License-Identifier: BSD-2-Clause */ #include "NoVisualizationWidget.h" +#include +#include #include 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> 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) 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) override; + void start_new_file(StringView) override; private: void paint_event(GUI::PaintEvent&) override; NoVisualizationWidget() = default; + ErrorOr> get_album_cover(StringView const filename); RefPtr m_serenity_bg; + RefPtr m_album_cover; };