Assistant: Convert all Vector<NonnullRefPtr> to NonnullRefPtrVector

This commit is contained in:
Timothy Flynn 2021-07-01 13:08:14 -04:00 committed by Gunnar Beutner
parent d69691a26b
commit 27fe2b45e5
Notes: sideshowbarker 2024-07-18 11:03:57 +09:00
3 changed files with 31 additions and 26 deletions

View file

@ -62,12 +62,12 @@ 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(NonnullRefPtrVector<Result>)> on_complete)
{
if (query.starts_with("=") || query.starts_with('$'))
return;
Vector<NonnullRefPtr<Result>> results;
NonnullRefPtrVector<Result> results;
Desktop::AppFile::for_each([&](NonnullRefPtr<Desktop::AppFile> app_file) {
auto match_result = fuzzy_match(query, app_file->name());
@ -81,7 +81,7 @@ void AppProvider::query(String const& query, Function<void(Vector<NonnullRefPtr<
on_complete(results);
}
void CalculatorProvider::query(String const& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete)
void CalculatorProvider::query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete)
{
if (!query.starts_with("="))
return;
@ -107,20 +107,20 @@ void CalculatorProvider::query(String const& query, Function<void(Vector<Nonnull
calculation = result.to_string_without_side_effects();
}
Vector<NonnullRefPtr<Result>> results;
NonnullRefPtrVector<Result> results;
results.append(adopt_ref(*new CalculatorResult(calculation)));
on_complete(results);
}
void FileProvider::query(const String& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete)
void FileProvider::query(const String& query, Function<void(NonnullRefPtrVector<Result>)> on_complete)
{
build_filesystem_cache();
if (m_fuzzy_match_work)
m_fuzzy_match_work->cancel();
m_fuzzy_match_work = Threading::BackgroundAction<Vector<NonnullRefPtr<Result>>>::create([this, query](auto& task) {
Vector<NonnullRefPtr<Result>> results;
m_fuzzy_match_work = Threading::BackgroundAction<NonnullRefPtrVector<Result>>::create([this, query](auto& task) {
NonnullRefPtrVector<Result> results;
for (auto& path : m_full_path_cache) {
if (task.is_cancelled())
@ -172,19 +172,19 @@ void FileProvider::build_filesystem_cache()
return 0; }, [this](auto) { m_building_cache = false; });
}
void TerminalProvider::query(String const& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete)
void TerminalProvider::query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete)
{
if (!query.starts_with('$'))
return;
auto command = query.substring(1);
Vector<NonnullRefPtr<Result>> results;
NonnullRefPtrVector<Result> results;
results.append(adopt_ref(*new TerminalResult(move(command))));
on_complete(results);
}
void URLProvider::query(String const& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete)
void URLProvider::query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete)
{
URL url = URL(query);
@ -198,7 +198,7 @@ void URLProvider::query(String const& query, Function<void(Vector<NonnullRefPtr<
if (!url.is_valid())
return;
Vector<NonnullRefPtr<Result>> results;
NonnullRefPtrVector<Result> results;
results.append(adopt_ref(*new URLResult(url)));
on_complete(results);
}

View file

@ -141,7 +141,7 @@ public:
class URLProvider : public Provider {
public:
void query(String const& query, Function<void(Vector<NonnullRefPtr<Result>>)> on_complete) override;
void query(String const& query, Function<void(NonnullRefPtrVector<Result>)> on_complete) override;
};
}

View file

@ -22,7 +22,7 @@ namespace Assistant {
struct AppState {
Optional<size_t> selected_index;
Vector<NonnullRefPtr<Result>> results;
NonnullRefPtrVector<Result> results;
size_t visible_result_count { 0 };
Threading::Lock lock;
@ -123,7 +123,7 @@ public:
m_file_provider.build_filesystem_cache();
}
Function<void(Vector<NonnullRefPtr<Result>>)> on_new_results;
Function<void(NonnullRefPtrVector<Result>)> on_new_results;
void search(String const& query)
{
@ -149,7 +149,7 @@ public:
}
private:
void recv_results(String const& query, Vector<NonnullRefPtr<Result>> const& results)
void recv_results(String const& query, NonnullRefPtrVector<Result> const& results)
{
{
Threading::Locker db_locker(m_lock);
@ -160,8 +160,8 @@ private:
it = m_result_cache.find(query);
for (auto& result : results) {
auto found = it->value.find_if([result](auto& other) {
return result->equals(other);
auto found = it->value.find_if([&result](auto& other) {
return result.equals(other);
});
if (found.is_end())
@ -174,7 +174,12 @@ private:
if (new_results == m_result_cache.end())
return;
dual_pivot_quick_sort(new_results->value, 0, static_cast<int>(new_results->value.size() - 1), [](auto& a, auto& b) {
// NonnullRefPtrVector will provide dual_pivot_quick_sort references rather than pointers,
// and dual_pivot_quick_sort requires being able to construct the underlying type on the
// stack. Assistant::Result is pure virtual, thus cannot be constructed on the stack.
auto& sortable_results = static_cast<Vector<NonnullRefPtr<Result>>&>(new_results->value);
dual_pivot_quick_sort(sortable_results, 0, static_cast<int>(sortable_results.size() - 1), [](auto& a, auto& b) {
return a->score() > b->score();
});
@ -190,7 +195,7 @@ private:
URLProvider m_url_provider;
Threading::Lock m_lock;
HashMap<String, Vector<NonnullRefPtr<Result>>> m_result_cache;
HashMap<String, NonnullRefPtrVector<Result>> m_result_cache;
};
}
@ -241,7 +246,7 @@ int main(int argc, char** argv)
text_box.on_return_pressed = [&]() {
if (!app_state.selected_index.has_value())
return;
app_state.results[app_state.selected_index.value()]->activate();
app_state.results[app_state.selected_index.value()].activate();
exit(0);
};
text_box.on_up_pressed = [&]() {
@ -283,13 +288,13 @@ int main(int argc, char** argv)
results_container.remove_all_children();
for (size_t i = 0; i < app_state.visible_result_count; ++i) {
auto result = app_state.results[i];
auto& result = app_state.results[i];
auto& match = results_container.add<Assistant::ResultRow>();
match.set_image(result->bitmap());
match.set_title(result->title());
match.set_subtitle(result->subtitle());
match.on_selected = [result_copy = result]() {
result_copy->activate();
match.set_image(result.bitmap());
match.set_title(result.title());
match.set_subtitle(result.subtitle());
match.on_selected = [&result]() {
result.activate();
exit(0);
};
}