FileManager: Add basic thumbnailing of PNG images.

These use nearest neighbor and are computed synchronously on directory load
so it's neither fast nor very beautiful. These issues both need work on
other parts of the system to fix.
This commit is contained in:
Andreas Kling 2019-03-23 12:37:33 +01:00
parent 1355d09c72
commit 92627154a7
Notes: sideshowbarker 2024-07-19 14:58:03 +09:00
3 changed files with 17 additions and 2 deletions

View File

@ -6,6 +6,8 @@
#include <pwd.h>
#include <AK/FileSystemPath.h>
#include <AK/StringBuilder.h>
#include <SharedGraphics/GraphicsBitmap.h>
#include <SharedGraphics/Painter.h>
DirectoryModel::DirectoryModel()
{
@ -79,8 +81,18 @@ const GraphicsBitmap& DirectoryModel::icon_for(const Entry& entry) const
return *m_socket_icon;
if (entry.mode & S_IXUSR)
return *m_executable_icon;
if (entry.name.ends_with(".png"))
return *m_filetype_image_icon;
if (entry.name.ends_with(".png")) {
if (!entry.thumbnail) {
if (auto png_bitmap = GraphicsBitmap::load_from_file(entry.full_path(*this))) {
entry.thumbnail = GraphicsBitmap::create(png_bitmap->format(), { 32, 32 });
Painter painter(*entry.thumbnail);
painter.draw_scaled_bitmap(entry.thumbnail->rect(), *png_bitmap, png_bitmap->rect());
}
}
if (!entry.thumbnail)
return *m_filetype_image_icon;
return *entry.thumbnail;
}
return *m_file_icon;
}

View File

@ -45,8 +45,10 @@ private:
uid_t uid { 0 };
uid_t gid { 0 };
ino_t inode { 0 };
mutable RetainPtr<GraphicsBitmap> thumbnail;
bool is_directory() const { return S_ISDIR(mode); }
bool is_executable() const { return mode & S_IXUSR; }
String full_path(const DirectoryModel& model) const { return String::format("%s/%s", model.path().characters(), name.characters()); }
};
const Entry& entry(int index) const

View File

@ -30,6 +30,7 @@ public:
int shared_buffer_id() const { return m_shared_buffer ? m_shared_buffer->shared_buffer_id() : -1; }
bool has_alpha_channel() const { return m_format == Format::RGBA32; }
Format format() const { return m_format; }
private:
GraphicsBitmap(Format, const Size&);