2021-06-17 04:55:44 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Spencer Dixon <spencercdixon@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/String.h>
|
|
|
|
#include <LibDesktop/AppFile.h>
|
|
|
|
#include <LibGUI/Desktop.h>
|
|
|
|
#include <LibJS/Interpreter.h>
|
|
|
|
#include <LibJS/Runtime/VM.h>
|
|
|
|
|
|
|
|
namespace Assistant {
|
|
|
|
|
|
|
|
class Result : public RefCounted<Result> {
|
|
|
|
public:
|
|
|
|
enum class Kind {
|
|
|
|
Unknown,
|
|
|
|
App,
|
|
|
|
Calculator,
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual ~Result() = default;
|
|
|
|
|
|
|
|
virtual void activate() const = 0;
|
|
|
|
|
|
|
|
RefPtr<Gfx::Bitmap> bitmap() { return m_bitmap; }
|
|
|
|
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
|
|
|
Kind kind() const { return m_kind; }
|
|
|
|
int score() const { return m_score; }
|
|
|
|
bool equals(Result const& other) const
|
|
|
|
{
|
2021-06-29 14:22:35 +03:00
|
|
|
return kind() == other.kind()
|
|
|
|
&& title() == other.title()
|
|
|
|
&& subtitle() == other.subtitle();
|
2021-06-17 04:55:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2021-06-29 14:22:35 +03:00
|
|
|
Result(RefPtr<Gfx::Bitmap> bitmap, String title, String subtitle, int score = 0, Kind kind = Kind::Unknown)
|
2021-06-17 04:55:44 +03:00
|
|
|
: m_bitmap(move(bitmap))
|
|
|
|
, 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)
|
|
|
|
, m_kind(kind)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<Gfx::Bitmap> m_bitmap;
|
|
|
|
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 };
|
|
|
|
Kind m_kind;
|
|
|
|
};
|
|
|
|
|
|
|
|
class AppResult : public Result {
|
|
|
|
public:
|
2021-06-29 14:22:35 +03:00
|
|
|
AppResult(RefPtr<Gfx::Bitmap> bitmap, String title, String subtitle, NonnullRefPtr<Desktop::AppFile> af, int score)
|
|
|
|
: Result(move(bitmap), move(title), move(subtitle), score, Kind::App)
|
2021-06-17 04:55:44 +03:00
|
|
|
, m_app_file(move(af))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
~AppResult() override = default;
|
|
|
|
void activate() const override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
NonnullRefPtr<Desktop::AppFile> m_app_file;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CalculatorResult : public Result {
|
|
|
|
public:
|
|
|
|
explicit CalculatorResult(String title)
|
2021-06-29 14:22:35 +03:00
|
|
|
: Result(GUI::Icon::default_icon("app-calculator").bitmap_for_size(16), move(title), "'Enter' will copy to clipboard"sv, 100, Kind::Calculator)
|
2021-06-17 04:55:44 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
~CalculatorResult() override = default;
|
|
|
|
void activate() const override;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Provider {
|
|
|
|
public:
|
|
|
|
virtual ~Provider() = default;
|
|
|
|
|
|
|
|
virtual void query(const String&, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class AppProvider : public Provider {
|
|
|
|
public:
|
|
|
|
void query(String const& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CalculatorProvider : public Provider {
|
|
|
|
public:
|
|
|
|
void query(String const& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|