From 06113b4ffe3b76d85f2744751cfac646b90e9cca Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 10 Oct 2019 22:07:08 +0200 Subject: [PATCH] LibHTML+Browser: Show the number of pending resource loads For now this is simply a counter+hook exposed by ResourceLoader and shown in the Browser status bar. This is not very nuanced, and it would be nice to expose more info so we could eventually do something like a progress bar. --- Applications/Browser/main.cpp | 9 +++++++++ Libraries/LibHTML/ResourceLoader.cpp | 8 +++++++- Libraries/LibHTML/ResourceLoader.h | 6 ++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Applications/Browser/main.cpp b/Applications/Browser/main.cpp index ff6387d9ac..d2bb486145 100644 --- a/Applications/Browser/main.cpp +++ b/Applications/Browser/main.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include static const char* home_url = "file:///home/anon/www/welcome.html"; @@ -78,6 +79,14 @@ int main(int argc, char** argv) auto statusbar = GStatusBar::construct(widget); + ResourceLoader::the().on_load_counter_change = [&] { + if (ResourceLoader::the().pending_loads() == 0) { + statusbar->set_text(""); + return; + } + statusbar->set_text(String::format("Loading (%d pending resources...)", ResourceLoader::the().pending_loads())); + }; + auto menubar = make(); auto app_menu = make("Browser"); diff --git a/Libraries/LibHTML/ResourceLoader.cpp b/Libraries/LibHTML/ResourceLoader.cpp index 95c8c2106a..c0c3fe9916 100644 --- a/Libraries/LibHTML/ResourceLoader.cpp +++ b/Libraries/LibHTML/ResourceLoader.cpp @@ -35,7 +35,13 @@ void ResourceLoader::load(const URL& url, Function call request.set_url(url); request.set_method(CHttpRequest::Method::GET); auto job = request.schedule(); - job->on_finish = [job, callback = move(callback)](bool success) { + ++m_pending_loads; + if (on_load_counter_change) + on_load_counter_change(); + job->on_finish = [this, job, callback = move(callback)](bool success) { + --m_pending_loads; + if (on_load_counter_change) + on_load_counter_change(); if (!success) { dbg() << "HTTP job failed!"; ASSERT_NOT_REACHED(); diff --git a/Libraries/LibHTML/ResourceLoader.h b/Libraries/LibHTML/ResourceLoader.h index 8255e5a8cd..7d5826efc9 100644 --- a/Libraries/LibHTML/ResourceLoader.h +++ b/Libraries/LibHTML/ResourceLoader.h @@ -11,6 +11,12 @@ public: void load(const URL&, Function); + Function on_load_counter_change; + + int pending_loads() const { return m_pending_loads; } + private: ResourceLoader() {} + + int m_pending_loads { 0 }; };