2021-06-17 04:55:44 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Spencer Dixon <spencercdixon@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
#include <AK/ByteString.h>
|
2021-07-02 17:36:09 +03:00
|
|
|
#include <AK/Queue.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>
|
2024-01-05 07:24:11 +03:00
|
|
|
#include <LibGUI/Window.h>
|
2021-06-17 04:55:44 +03:00
|
|
|
#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 {
|
|
|
|
|
2023-04-12 19:38:08 +03:00
|
|
|
static constexpr size_t MAX_SEARCH_RESULTS = 6;
|
|
|
|
|
2021-06-17 04:55:44 +03:00
|
|
|
class Result : public RefCounted<Result> {
|
|
|
|
public:
|
|
|
|
virtual ~Result() = default;
|
|
|
|
|
2024-01-05 07:24:11 +03:00
|
|
|
virtual void activate(GUI::Window&) const = 0;
|
2021-06-17 04:55:44 +03:00
|
|
|
|
2021-07-03 22:15:47 +03:00
|
|
|
virtual Gfx::Bitmap const* bitmap() const = 0;
|
2021-07-03 18:46:26 +03:00
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString const& title() const { return m_title; }
|
2023-08-23 21:26:57 +03:00
|
|
|
String const& tooltip() const { return m_tooltip; }
|
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()
|
2022-11-25 18:34:10 +03:00
|
|
|
&& tooltip() == other.tooltip();
|
2021-06-17 04:55:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2023-12-16 17:19:34 +03:00
|
|
|
Result(ByteString title, String tooltip, int score = 0)
|
2021-07-03 18:46:26 +03:00
|
|
|
: m_title(move(title))
|
2022-11-25 18:34:10 +03:00
|
|
|
, m_tooltip(move(tooltip))
|
2021-06-17 04:55:44 +03:00
|
|
|
, m_score(score)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString m_title;
|
2023-08-23 21:26:57 +03:00
|
|
|
String m_tooltip;
|
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:
|
2023-12-16 17:19:34 +03:00
|
|
|
AppResult(RefPtr<Gfx::Bitmap const> bitmap, ByteString title, String tooltip, NonnullRefPtr<Desktop::AppFile> af, ByteString arguments, int score)
|
2022-11-25 18:34:10 +03:00
|
|
|
: Result(move(title), move(tooltip), score)
|
2021-06-17 04:55:44 +03:00
|
|
|
, m_app_file(move(af))
|
2023-01-21 00:58:37 +03:00
|
|
|
, m_arguments(move(arguments))
|
2021-07-03 18:46:26 +03:00
|
|
|
, m_bitmap(move(bitmap))
|
2021-06-17 04:55:44 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
~AppResult() override = default;
|
2024-01-05 07:24:11 +03:00
|
|
|
void activate(GUI::Window&) const override;
|
2021-06-17 04:55:44 +03:00
|
|
|
|
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;
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString m_arguments;
|
2023-02-20 02:47:48 +03:00
|
|
|
RefPtr<Gfx::Bitmap const> m_bitmap;
|
2021-06-17 04:55:44 +03:00
|
|
|
};
|
|
|
|
|
2021-07-03 22:16:19 +03:00
|
|
|
class CalculatorResult final : public Result {
|
2021-06-17 04:55:44 +03:00
|
|
|
public:
|
2023-12-16 17:19:34 +03:00
|
|
|
explicit CalculatorResult(ByteString title)
|
2023-08-23 21:26:57 +03:00
|
|
|
: Result(move(title), "Copy to Clipboard"_string, 100)
|
2022-07-11 20:32:29 +03:00
|
|
|
, m_bitmap(GUI::Icon::default_icon("app-calculator"sv).bitmap_for_size(16))
|
2021-06-17 04:55:44 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
~CalculatorResult() override = default;
|
2024-01-05 07:24:11 +03:00
|
|
|
void activate(GUI::Window&) const override;
|
2021-07-03 18:46:26 +03:00
|
|
|
|
|
|
|
virtual Gfx::Bitmap const* bitmap() const override { return m_bitmap; }
|
|
|
|
|
|
|
|
private:
|
2023-02-20 02:47:48 +03:00
|
|
|
RefPtr<Gfx::Bitmap const> m_bitmap;
|
2021-06-17 04:55:44 +03:00
|
|
|
};
|
|
|
|
|
2021-07-03 22:16:19 +03:00
|
|
|
class FileResult final : public Result {
|
2021-07-02 17:36:09 +03:00
|
|
|
public:
|
2023-12-16 17:19:34 +03:00
|
|
|
explicit FileResult(ByteString title, int score)
|
2023-08-23 21:26:57 +03:00
|
|
|
: Result(move(title), String(), score)
|
2021-07-02 17:36:09 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
~FileResult() override = default;
|
2024-01-05 07:24:11 +03:00
|
|
|
void activate(GUI::Window&) 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-03 22:16:19 +03:00
|
|
|
class TerminalResult final : public Result {
|
2021-07-01 17:15:53 +03:00
|
|
|
public:
|
2023-12-16 17:19:34 +03:00
|
|
|
explicit TerminalResult(ByteString command)
|
2023-08-23 21:26:57 +03:00
|
|
|
: Result(move(command), "Run command in Terminal"_string, 100)
|
2022-07-11 20:32:29 +03:00
|
|
|
, m_bitmap(GUI::Icon::default_icon("app-terminal"sv).bitmap_for_size(16))
|
2021-07-01 17:15:53 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
~TerminalResult() override = default;
|
2024-01-05 07:24:11 +03:00
|
|
|
void activate(GUI::Window&) 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:
|
2023-02-20 02:47:48 +03:00
|
|
|
RefPtr<Gfx::Bitmap const> m_bitmap;
|
2021-07-01 17:15:53 +03:00
|
|
|
};
|
|
|
|
|
2021-07-03 22:16:19 +03:00
|
|
|
class URLResult final : public Result {
|
2021-07-03 00:24:19 +03:00
|
|
|
public:
|
|
|
|
explicit URLResult(const URL& url)
|
2023-12-16 17:19:34 +03:00
|
|
|
: Result(url.to_byte_string(), "Open URL in Browser"_string, 50)
|
2022-07-11 20:32:29 +03:00
|
|
|
, m_bitmap(GUI::Icon::default_icon("app-browser"sv).bitmap_for_size(16))
|
2021-07-03 00:24:19 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
~URLResult() override = default;
|
2024-01-05 07:24:11 +03:00
|
|
|
void activate(GUI::Window&) const override;
|
2021-07-03 18:46:26 +03:00
|
|
|
|
|
|
|
virtual Gfx::Bitmap const* bitmap() const override { return m_bitmap; }
|
|
|
|
|
|
|
|
private:
|
2023-02-20 02:47:48 +03:00
|
|
|
RefPtr<Gfx::Bitmap const> m_bitmap;
|
2021-07-03 00:24:19 +03:00
|
|
|
};
|
|
|
|
|
2022-01-15 17:23:40 +03:00
|
|
|
class Provider : public RefCounted<Provider> {
|
2021-06-17 04:55:44 +03:00
|
|
|
public:
|
|
|
|
virtual ~Provider() = default;
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
virtual void query(ByteString const&, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete) = 0;
|
2021-06-17 04:55:44 +03:00
|
|
|
};
|
|
|
|
|
2021-07-03 22:16:19 +03:00
|
|
|
class AppProvider final : public Provider {
|
2021-06-17 04:55:44 +03:00
|
|
|
public:
|
2023-04-12 19:38:18 +03:00
|
|
|
AppProvider();
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
void query(ByteString const& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete) override;
|
2023-04-12 19:38:18 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
Vector<NonnullRefPtr<Desktop::AppFile>> m_app_file_cache;
|
2021-06-17 04:55:44 +03:00
|
|
|
};
|
|
|
|
|
2021-07-03 22:16:19 +03:00
|
|
|
class CalculatorProvider final : public Provider {
|
2021-06-17 04:55:44 +03:00
|
|
|
public:
|
2023-12-16 17:19:34 +03:00
|
|
|
void query(ByteString const& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete) override;
|
2021-06-17 04:55:44 +03:00
|
|
|
};
|
|
|
|
|
2021-07-03 22:16:19 +03:00
|
|
|
class FileProvider final : public Provider {
|
2021-07-02 17:36:09 +03:00
|
|
|
public:
|
2021-07-03 21:46:33 +03:00
|
|
|
FileProvider();
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
void query(ByteString const& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete) override;
|
2021-07-02 17:36:09 +03:00
|
|
|
void build_filesystem_cache();
|
|
|
|
|
|
|
|
private:
|
2023-03-06 16:17:01 +03:00
|
|
|
RefPtr<Threading::BackgroundAction<Optional<Vector<NonnullRefPtr<Result>>>>> m_fuzzy_match_work;
|
2021-07-02 17:36:09 +03:00
|
|
|
bool m_building_cache { false };
|
2023-12-16 17:19:34 +03:00
|
|
|
Vector<ByteString> m_full_path_cache;
|
|
|
|
Queue<ByteString> m_work_queue;
|
2021-07-02 17:36:09 +03:00
|
|
|
};
|
|
|
|
|
2021-07-03 22:16:19 +03:00
|
|
|
class TerminalProvider final : public Provider {
|
2021-07-01 17:15:53 +03:00
|
|
|
public:
|
2023-12-16 17:19:34 +03:00
|
|
|
void query(ByteString const& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete) override;
|
2021-07-01 17:15:53 +03:00
|
|
|
};
|
|
|
|
|
2021-07-03 22:16:19 +03:00
|
|
|
class URLProvider final : public Provider {
|
2021-07-03 00:24:19 +03:00
|
|
|
public:
|
2023-12-16 17:19:34 +03:00
|
|
|
void query(ByteString const& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete) override;
|
2021-07-03 00:24:19 +03:00
|
|
|
};
|
|
|
|
|
2021-06-17 04:55:44 +03:00
|
|
|
}
|