2021-06-17 04:55:44 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Spencer Dixon <spencercdixon@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-07-02 17:36:09 +03:00
|
|
|
#include <AK/Queue.h>
|
2021-06-17 04:55:44 +03:00
|
|
|
#include <AK/String.h>
|
2021-07-03 00:24:19 +03:00
|
|
|
#include <AK/URL.h>
|
2021-06-17 04:55:44 +03:00
|
|
|
#include <LibDesktop/AppFile.h>
|
|
|
|
#include <LibGUI/Desktop.h>
|
|
|
|
#include <LibJS/Interpreter.h>
|
|
|
|
#include <LibJS/Runtime/VM.h>
|
2021-07-02 17:36:09 +03:00
|
|
|
#include <LibThreading/BackgroundAction.h>
|
2021-07-02 16:20:30 +03:00
|
|
|
#include <typeinfo>
|
2021-06-17 04:55:44 +03:00
|
|
|
|
|
|
|
namespace Assistant {
|
|
|
|
|
|
|
|
class Result : public RefCounted<Result> {
|
|
|
|
public:
|
|
|
|
virtual ~Result() = default;
|
|
|
|
|
|
|
|
virtual void activate() const = 0;
|
|
|
|
|
2021-07-03 22:15:47 +03:00
|
|
|
virtual Gfx::Bitmap const* bitmap() const = 0;
|
2021-07-03 18:46:26 +03:00
|
|
|
|
2021-06-17 04:55:44 +03:00
|
|
|
String const& title() const { return m_title; }
|
2021-06-29 14:22:35 +03:00
|
|
|
String const& subtitle() const { return m_subtitle; }
|
2021-06-17 04:55:44 +03:00
|
|
|
int score() const { return m_score; }
|
|
|
|
bool equals(Result const& other) const
|
|
|
|
{
|
2021-07-02 16:20:30 +03:00
|
|
|
return typeid(this) == typeid(&other)
|
2021-06-29 14:22:35 +03:00
|
|
|
&& title() == other.title()
|
|
|
|
&& subtitle() == other.subtitle();
|
2021-06-17 04:55:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2021-07-03 18:46:26 +03:00
|
|
|
Result(String title, String subtitle, int score = 0)
|
|
|
|
: m_title(move(title))
|
2021-06-29 14:22:35 +03:00
|
|
|
, m_subtitle(move(subtitle))
|
2021-06-17 04:55:44 +03:00
|
|
|
, m_score(score)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
String m_title;
|
2021-06-29 14:22:35 +03:00
|
|
|
String m_subtitle;
|
2021-06-17 04:55:44 +03:00
|
|
|
int m_score { 0 };
|
|
|
|
};
|
|
|
|
|
2021-07-03 18:46:26 +03:00
|
|
|
class AppResult final : public Result {
|
2021-06-17 04:55:44 +03:00
|
|
|
public:
|
2021-06-29 14:22:35 +03:00
|
|
|
AppResult(RefPtr<Gfx::Bitmap> bitmap, String title, String subtitle, NonnullRefPtr<Desktop::AppFile> af, int score)
|
2021-07-03 18:46:26 +03:00
|
|
|
: Result(move(title), move(subtitle), score)
|
2021-06-17 04:55:44 +03:00
|
|
|
, m_app_file(move(af))
|
2021-07-03 18:46:26 +03:00
|
|
|
, m_bitmap(move(bitmap))
|
2021-06-17 04:55:44 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
~AppResult() override = default;
|
|
|
|
void activate() const override;
|
|
|
|
|
2021-07-03 18:46:26 +03:00
|
|
|
virtual Gfx::Bitmap const* bitmap() const override { return m_bitmap; }
|
|
|
|
|
2021-06-17 04:55:44 +03:00
|
|
|
private:
|
|
|
|
NonnullRefPtr<Desktop::AppFile> m_app_file;
|
2021-07-03 18:46:26 +03:00
|
|
|
RefPtr<Gfx::Bitmap> m_bitmap;
|
2021-06-17 04:55:44 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class CalculatorResult : public Result {
|
|
|
|
public:
|
|
|
|
explicit CalculatorResult(String title)
|
2021-07-03 18:46:26 +03:00
|
|
|
: Result(move(title), "'Enter' will copy to clipboard"sv, 100)
|
|
|
|
, m_bitmap(GUI::Icon::default_icon("app-calculator").bitmap_for_size(16))
|
2021-06-17 04:55:44 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
~CalculatorResult() override = default;
|
|
|
|
void activate() const override;
|
2021-07-03 18:46:26 +03:00
|
|
|
|
|
|
|
virtual Gfx::Bitmap const* bitmap() const override { return m_bitmap; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<Gfx::Bitmap> m_bitmap;
|
2021-06-17 04:55:44 +03:00
|
|
|
};
|
|
|
|
|
2021-07-02 17:36:09 +03:00
|
|
|
class FileResult : public Result {
|
|
|
|
public:
|
|
|
|
explicit FileResult(String title, int score)
|
2021-07-03 18:46:26 +03:00
|
|
|
: Result(move(title), "", score)
|
2021-07-02 17:36:09 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
~FileResult() override = default;
|
|
|
|
void activate() const override;
|
2021-07-03 18:46:26 +03:00
|
|
|
|
|
|
|
virtual Gfx::Bitmap const* bitmap() const override;
|
2021-07-02 17:36:09 +03:00
|
|
|
};
|
|
|
|
|
2021-07-01 17:15:53 +03:00
|
|
|
class TerminalResult : public Result {
|
|
|
|
public:
|
|
|
|
explicit TerminalResult(String command)
|
2021-07-03 18:46:26 +03:00
|
|
|
: Result(move(command), "Run command in Terminal"sv, 100)
|
|
|
|
, m_bitmap(GUI::Icon::default_icon("app-terminal").bitmap_for_size(16))
|
2021-07-01 17:15:53 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
~TerminalResult() override = default;
|
|
|
|
void activate() const override;
|
2021-07-03 18:46:26 +03:00
|
|
|
|
2021-07-03 22:13:30 +03:00
|
|
|
virtual Gfx::Bitmap const* bitmap() const override { return m_bitmap; }
|
|
|
|
|
2021-07-03 18:46:26 +03:00
|
|
|
private:
|
|
|
|
RefPtr<Gfx::Bitmap> m_bitmap;
|
2021-07-01 17:15:53 +03:00
|
|
|
};
|
|
|
|
|
2021-07-03 00:24:19 +03:00
|
|
|
class URLResult : public Result {
|
|
|
|
public:
|
|
|
|
explicit URLResult(const URL& url)
|
2021-07-03 18:46:26 +03:00
|
|
|
: 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))
|
2021-07-03 00:24:19 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
~URLResult() override = default;
|
|
|
|
void activate() const override;
|
2021-07-03 18:46:26 +03:00
|
|
|
|
|
|
|
virtual Gfx::Bitmap const* bitmap() const override { return m_bitmap; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<Gfx::Bitmap> m_bitmap;
|
2021-07-03 00:24:19 +03:00
|
|
|
};
|
|
|
|
|
2021-06-17 04:55:44 +03:00
|
|
|
class Provider {
|
|
|
|
public:
|
|
|
|
virtual ~Provider() = default;
|
|
|
|
|
2021-07-01 17:15:53 +03:00
|
|
|
virtual void query(const String&, Function<void(NonnullRefPtrVector<Result>)> on_complete) = 0;
|
2021-06-17 04:55:44 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class AppProvider : public Provider {
|
|
|
|
public:
|
2021-07-01 17:15:53 +03:00
|
|
|
void query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete) override;
|
2021-06-17 04:55:44 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class CalculatorProvider : public Provider {
|
|
|
|
public:
|
2021-07-01 17:15:53 +03:00
|
|
|
void query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete) override;
|
2021-06-17 04:55:44 +03:00
|
|
|
};
|
|
|
|
|
2021-07-02 17:36:09 +03:00
|
|
|
class FileProvider : public Provider {
|
|
|
|
public:
|
2021-07-03 21:46:33 +03:00
|
|
|
FileProvider();
|
|
|
|
|
2021-07-01 17:15:53 +03:00
|
|
|
void query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete) override;
|
2021-07-02 17:36:09 +03:00
|
|
|
void build_filesystem_cache();
|
|
|
|
|
|
|
|
private:
|
2021-07-01 17:15:53 +03:00
|
|
|
RefPtr<Threading::BackgroundAction<NonnullRefPtrVector<Result>>> m_fuzzy_match_work;
|
2021-07-02 17:36:09 +03:00
|
|
|
bool m_building_cache { false };
|
|
|
|
Vector<String> m_full_path_cache;
|
|
|
|
Queue<String> m_work_queue;
|
|
|
|
};
|
|
|
|
|
2021-07-01 17:15:53 +03:00
|
|
|
class TerminalProvider : public Provider {
|
|
|
|
public:
|
|
|
|
void query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete) override;
|
|
|
|
};
|
|
|
|
|
2021-07-03 00:24:19 +03:00
|
|
|
class URLProvider : public Provider {
|
|
|
|
public:
|
2021-07-01 20:08:14 +03:00
|
|
|
void query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete) override;
|
2021-07-03 00:24:19 +03:00
|
|
|
};
|
|
|
|
|
2021-06-17 04:55:44 +03:00
|
|
|
}
|