mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 01:57:45 +00:00
Assistant: Add new URLProvider to open URL's in the browser
This commit is contained in:
parent
d12e14fa95
commit
d5dfc255ed
3 changed files with 45 additions and 0 deletions
|
@ -41,6 +41,11 @@ void FileResult::activate() const
|
||||||
Desktop::Launcher::open(URL::create_with_file_protocol(title()));
|
Desktop::Launcher::open(URL::create_with_file_protocol(title()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void URLResult::activate() const
|
||||||
|
{
|
||||||
|
Desktop::Launcher::open(URL::create_with_url_or_path(title()));
|
||||||
|
}
|
||||||
|
|
||||||
void AppProvider::query(String const& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete)
|
void AppProvider::query(String const& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete)
|
||||||
{
|
{
|
||||||
if (query.starts_with("="))
|
if (query.starts_with("="))
|
||||||
|
@ -151,4 +156,23 @@ void FileProvider::build_filesystem_cache()
|
||||||
return 0; }, [this](auto) { m_building_cache = false; });
|
return 0; }, [this](auto) { m_building_cache = false; });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void URLProvider::query(String const& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete)
|
||||||
|
{
|
||||||
|
URL url = URL(query);
|
||||||
|
|
||||||
|
if (url.scheme().is_empty())
|
||||||
|
url.set_scheme("http");
|
||||||
|
if (url.host().is_empty())
|
||||||
|
url.set_host(query);
|
||||||
|
if (url.paths().is_empty())
|
||||||
|
url.set_paths({ "" });
|
||||||
|
|
||||||
|
if (!url.is_valid())
|
||||||
|
return;
|
||||||
|
|
||||||
|
Vector<NonnullRefPtr<Result>> results;
|
||||||
|
results.append(adopt_ref(*new URLResult(url)));
|
||||||
|
on_complete(results);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
#include <AK/Queue.h>
|
#include <AK/Queue.h>
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
|
#include <AK/URL.h>
|
||||||
#include <LibDesktop/AppFile.h>
|
#include <LibDesktop/AppFile.h>
|
||||||
#include <LibGUI/Desktop.h>
|
#include <LibGUI/Desktop.h>
|
||||||
#include <LibJS/Interpreter.h>
|
#include <LibJS/Interpreter.h>
|
||||||
|
@ -84,6 +85,16 @@ public:
|
||||||
void activate() const override;
|
void activate() const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
~URLResult() override = default;
|
||||||
|
void activate() const override;
|
||||||
|
};
|
||||||
|
|
||||||
class Provider {
|
class Provider {
|
||||||
public:
|
public:
|
||||||
virtual ~Provider() = default;
|
virtual ~Provider() = default;
|
||||||
|
@ -113,4 +124,9 @@ private:
|
||||||
Queue<String> m_work_queue;
|
Queue<String> m_work_queue;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class URLProvider : public Provider {
|
||||||
|
public:
|
||||||
|
void query(String const& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete) override;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -138,6 +138,10 @@ public:
|
||||||
m_calculator_provider.query(query, [=, this](auto results) {
|
m_calculator_provider.query(query, [=, this](auto results) {
|
||||||
recv_results(query, results);
|
recv_results(query, results);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
m_url_provider.query(query, [=, this](auto results) {
|
||||||
|
recv_results(query, results);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -178,6 +182,7 @@ private:
|
||||||
AppProvider m_app_provider;
|
AppProvider m_app_provider;
|
||||||
CalculatorProvider m_calculator_provider;
|
CalculatorProvider m_calculator_provider;
|
||||||
FileProvider m_file_provider;
|
FileProvider m_file_provider;
|
||||||
|
URLProvider m_url_provider;
|
||||||
|
|
||||||
Threading::Lock m_lock;
|
Threading::Lock m_lock;
|
||||||
HashMap<String, Vector<NonnullRefPtr<Result>>> m_result_cache;
|
HashMap<String, Vector<NonnullRefPtr<Result>>> m_result_cache;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue