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

LibGUI: Use ErrorOr<T> in the file system thumbnail generator

This commit is contained in:
Andreas Kling 2021-11-20 16:28:46 +01:00
parent b6359b211d
commit 8e4eebe9b1

View file

@ -615,20 +615,12 @@ Icon FileSystemModel::icon_for(Node const& node) const
static HashMap<String, RefPtr<Gfx::Bitmap>> s_thumbnail_cache; static HashMap<String, RefPtr<Gfx::Bitmap>> s_thumbnail_cache;
static RefPtr<Gfx::Bitmap> render_thumbnail(StringView path) static ErrorOr<NonnullRefPtr<Gfx::Bitmap>> render_thumbnail(StringView path)
{ {
auto bitmap_or_error = Gfx::Bitmap::try_load_from_file(path); auto bitmap = TRY(Gfx::Bitmap::try_load_from_file(path));
if (bitmap_or_error.is_error()) auto thumbnail = TRY(Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { 32, 32 }));
return nullptr;
auto bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
double scale = min(32 / (double)bitmap->width(), 32 / (double)bitmap->height()); double scale = min(32 / (double)bitmap->width(), 32 / (double)bitmap->height());
auto thumbnail_or_error = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { 32, 32 });
if (thumbnail_or_error.is_error())
return nullptr;
auto thumbnail = thumbnail_or_error.release_value_but_fixme_should_propagate_errors();
auto destination = Gfx::IntRect(0, 0, (int)(bitmap->width() * scale), (int)(bitmap->height() * scale)).centered_within(thumbnail->rect()); auto destination = Gfx::IntRect(0, 0, (int)(bitmap->width() * scale), (int)(bitmap->height() * scale)).centered_within(thumbnail->rect());
Painter painter(thumbnail); Painter painter(thumbnail);
@ -657,13 +649,17 @@ bool FileSystemModel::fetch_thumbnail_for(Node const& node)
auto weak_this = make_weak_ptr(); auto weak_this = make_weak_ptr();
Threading::BackgroundAction<RefPtr<Gfx::Bitmap>>::construct( Threading::BackgroundAction<ErrorOr<NonnullRefPtr<Gfx::Bitmap>>>::construct(
[path](auto&) { [path](auto&) {
return render_thumbnail(path); return render_thumbnail(path);
}, },
[this, path, weak_this](auto thumbnail) { [this, path, weak_this](auto thumbnail_or_error) {
s_thumbnail_cache.set(path, move(thumbnail)); if (thumbnail_or_error.is_error()) {
s_thumbnail_cache.set(path, nullptr);
return;
}
s_thumbnail_cache.set(path, thumbnail_or_error.release_value());
// The model was destroyed, no need to update // The model was destroyed, no need to update
// progress or call any event handlers. // progress or call any event handlers.