LibGUI: Update model every 100ms when generating thumbmnails

This dramatically improves the responsiveness of FileManager when
navigating to folders with many images.
This commit is contained in:
Tim Ledbetter 2023-10-03 17:51:18 +01:00 committed by Andreas Kling
parent 1abc52a689
commit debb4edcc8
2 changed files with 14 additions and 4 deletions

View file

@ -707,8 +707,8 @@ bool FileSystemModel::fetch_thumbnail_for(Node const& node)
auto const action = [path](auto&) {
return render_thumbnail(path);
};
auto const update_progress = [weak_this](bool with_success) {
using namespace AK::TimeLiterals;
if (auto strong_this = weak_this.strong_ref(); !strong_this.is_null()) {
strong_this->m_thumbnail_progress++;
if (strong_this->on_thumbnail_progress)
@ -718,17 +718,24 @@ bool FileSystemModel::fetch_thumbnail_for(Node const& node)
strong_this->m_thumbnail_progress_total = 0;
}
if (with_success)
if (with_success && (!strong_this->m_ui_update_timer.is_valid() || strong_this->m_ui_update_timer.elapsed_time() > 100_ms)) {
strong_this->did_update(UpdateFlag::DontInvalidateIndices);
strong_this->m_ui_update_timer.start();
}
}
};
auto const on_complete = [path, update_progress](auto thumbnail) -> ErrorOr<void> {
s_thumbnail_cache.with_locked([path, thumbnail](auto& cache) {
auto const on_complete = [weak_this, path, update_progress](auto thumbnail) -> ErrorOr<void> {
auto finished_generating_thumbnails = false;
s_thumbnail_cache.with_locked([path, thumbnail, &finished_generating_thumbnails](auto& cache) {
cache.thumbnail_cache.set(path, thumbnail);
cache.loading_thumbnails.remove(path);
finished_generating_thumbnails = cache.loading_thumbnails.is_empty();
});
if (auto strong_this = weak_this.strong_ref(); finished_generating_thumbnails && !strong_this.is_null())
strong_this->m_ui_update_timer.reset();
update_progress(true);
return {};

View file

@ -9,6 +9,7 @@
#include <AK/HashMap.h>
#include <LibCore/DateTime.h>
#include <LibCore/ElapsedTimer.h>
#include <LibCore/FileWatcher.h>
#include <LibGUI/Model.h>
#include <string.h>
@ -174,6 +175,8 @@ private:
unsigned m_thumbnail_progress { 0 };
unsigned m_thumbnail_progress_total { 0 };
Core::ElapsedTimer m_ui_update_timer;
Optional<Vector<DeprecatedString>> m_allowed_file_extensions;
bool m_should_show_dotfiles { false };