From 513e67e2ebb8b066cf3db0407e682b1f4c608a30 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 3 Jul 2021 17:46:26 +0200 Subject: [PATCH] Assistant: Make Result bitmaps virtual/lazy Result classes now return their bitmap via a virtual Gfx::Bitmap* getter. This effectively makes bitmap fetching lazier, since only results that end up on screen actually get asked for their bitmap. This drastically reduces the amount of work done by the FileProvider background worker. --- Userland/Applications/Assistant/Providers.cpp | 5 +++ Userland/Applications/Assistant/Providers.h | 43 ++++++++++++++----- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/Userland/Applications/Assistant/Providers.cpp b/Userland/Applications/Assistant/Providers.cpp index f6a4ef3e99e..aed3a834259 100644 --- a/Userland/Applications/Assistant/Providers.cpp +++ b/Userland/Applications/Assistant/Providers.cpp @@ -112,6 +112,11 @@ void CalculatorProvider::query(String const& query, Function)> on_complete) { build_filesystem_cache(); diff --git a/Userland/Applications/Assistant/Providers.h b/Userland/Applications/Assistant/Providers.h index 71c2b03e89d..31987cfc11b 100644 --- a/Userland/Applications/Assistant/Providers.h +++ b/Userland/Applications/Assistant/Providers.h @@ -24,7 +24,8 @@ public: virtual void activate() const = 0; - RefPtr bitmap() { return m_bitmap; } + virtual Gfx::Bitmap const* bitmap() const { return nullptr; } + String const& title() const { return m_title; } String const& subtitle() const { return m_subtitle; } int score() const { return m_score; } @@ -36,73 +37,93 @@ public: } protected: - Result(RefPtr bitmap, String title, String subtitle, int score = 0) - : m_bitmap(move(bitmap)) - , m_title(move(title)) + Result(String title, String subtitle, int score = 0) + : m_title(move(title)) , m_subtitle(move(subtitle)) , m_score(score) { } private: - RefPtr m_bitmap; String m_title; String m_subtitle; int m_score { 0 }; }; -class AppResult : public Result { +class AppResult final : public Result { public: AppResult(RefPtr bitmap, String title, String subtitle, NonnullRefPtr af, int score) - : Result(move(bitmap), move(title), move(subtitle), score) + : Result(move(title), move(subtitle), score) , m_app_file(move(af)) + , m_bitmap(move(bitmap)) { } ~AppResult() override = default; void activate() const override; + virtual Gfx::Bitmap const* bitmap() const override { return m_bitmap; } + private: NonnullRefPtr m_app_file; + RefPtr m_bitmap; }; class CalculatorResult : public Result { public: explicit CalculatorResult(String title) - : Result(GUI::Icon::default_icon("app-calculator").bitmap_for_size(16), move(title), "'Enter' will copy to clipboard"sv, 100) + : Result(move(title), "'Enter' will copy to clipboard"sv, 100) + , m_bitmap(GUI::Icon::default_icon("app-calculator").bitmap_for_size(16)) { } ~CalculatorResult() override = default; void activate() const override; + + virtual Gfx::Bitmap const* bitmap() const override { return m_bitmap; } + +private: + RefPtr m_bitmap; }; class FileResult : public Result { public: explicit FileResult(String title, int score) - : Result(GUI::Icon::default_icon("filetype-folder").bitmap_for_size(16), move(title), "", score) + : Result(move(title), "", score) { } ~FileResult() override = default; void activate() const override; + + virtual Gfx::Bitmap const* bitmap() const override; }; class TerminalResult : public Result { public: explicit TerminalResult(String command) - : Result(GUI::Icon::default_icon("app-terminal").bitmap_for_size(16), move(command), "Run command in Terminal"sv, 100) + : Result(move(command), "Run command in Terminal"sv, 100) + , m_bitmap(GUI::Icon::default_icon("app-terminal").bitmap_for_size(16)) { } ~TerminalResult() override = default; void activate() const override; + +private: + RefPtr m_bitmap; }; class URLResult : public Result { public: explicit URLResult(const URL& url) - : Result(GUI::Icon::default_icon("app-browser").bitmap_for_size(16), url.to_string(), "'Enter' will open this URL in the browser"sv, 50) + : Result(url.to_string(), "'Enter' will open this URL in the browser"sv, 50) + , m_bitmap(GUI::Icon::default_icon("app-browser").bitmap_for_size(16)) { } ~URLResult() override = default; void activate() const override; + + virtual Gfx::Bitmap const* bitmap() const override { return m_bitmap; } + +private: + RefPtr m_bitmap; }; class Provider {