summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-06-29 13:08:21 +0200
committerAndreas Kling <kling@serenityos.org>2021-06-29 13:08:21 +0200
commit9179a2ea7347fb4ee43848e11c6660dda7a60023 (patch)
tree7a6e4d4eb2458ac128311286d907e234c06bdd01
parentbec2b3086c195e50f233e01f7ab935ba15ac843e (diff)
downloadserenity-9179a2ea7347fb4ee43848e11c6660dda7a60023.zip
Assistant: Fix crash when activating empty search result
If there are no search results in the list, we shouldn't do anything when you try to active the selected result, since there isn't one. Fix this by using an Optional<size_t> to store the selected index.
-rw-r--r--Userland/Applications/Assistant/main.cpp36
1 files changed, 25 insertions, 11 deletions
diff --git a/Userland/Applications/Assistant/main.cpp b/Userland/Applications/Assistant/main.cpp
index 3c4c57b24d..a7cb5ebec0 100644
--- a/Userland/Applications/Assistant/main.cpp
+++ b/Userland/Applications/Assistant/main.cpp
@@ -21,7 +21,7 @@
namespace Assistant {
struct AppState {
- size_t selected_index { 0 };
+ Optional<size_t> selected_index;
Vector<NonnullRefPtr<Result>> results;
size_t visible_result_count { 0 };
@@ -218,23 +218,34 @@ int main(int argc, char** argv)
db.search(text_box.text());
};
text_box.on_return_pressed = [&]() {
- app_state.results[app_state.selected_index]->activate();
+ if (!app_state.selected_index.has_value())
+ return;
+ app_state.results[app_state.selected_index.value()]->activate();
exit(0);
};
text_box.on_up_pressed = [&]() {
- if (app_state.selected_index == 0)
- app_state.selected_index = app_state.visible_result_count - 1;
- else if (app_state.selected_index > 0)
- app_state.selected_index -= 1;
+ if (!app_state.visible_result_count)
+ return;
+ auto new_selected_index = app_state.selected_index.value_or(0);
+ if (new_selected_index == 0)
+ new_selected_index = app_state.visible_result_count - 1;
+ else if (new_selected_index > 0)
+ new_selected_index -= 1;
+ app_state.selected_index = new_selected_index;
mark_selected_item();
};
text_box.on_down_pressed = [&]() {
- if ((app_state.visible_result_count - 1) == app_state.selected_index)
- app_state.selected_index = 0;
- else if (app_state.visible_result_count > app_state.selected_index)
- app_state.selected_index += 1;
+ if (!app_state.visible_result_count)
+ return;
+
+ auto new_selected_index = app_state.selected_index.value_or(0);
+ if ((app_state.visible_result_count - 1) == new_selected_index)
+ new_selected_index = 0;
+ else if (app_state.visible_result_count > new_selected_index)
+ new_selected_index += 1;
+ app_state.selected_index = new_selected_index;
mark_selected_item();
};
text_box.on_escape_pressed = []() {
@@ -242,7 +253,10 @@ int main(int argc, char** argv)
};
db.on_new_results = [&](auto results) {
- app_state.selected_index = 0;
+ if (results.is_empty())
+ app_state.selected_index = {};
+ else
+ app_state.selected_index = 0;
app_state.results = results;
app_state.visible_result_count = min(results.size(), MAX_SEARCH_RESULTS);
results_container.remove_all_children();