mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:17:44 +00:00
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.
This commit is contained in:
parent
bec2b3086c
commit
9179a2ea73
1 changed files with 25 additions and 11 deletions
|
@ -21,7 +21,7 @@
|
||||||
namespace Assistant {
|
namespace Assistant {
|
||||||
|
|
||||||
struct AppState {
|
struct AppState {
|
||||||
size_t selected_index { 0 };
|
Optional<size_t> selected_index;
|
||||||
Vector<NonnullRefPtr<Result>> results;
|
Vector<NonnullRefPtr<Result>> results;
|
||||||
size_t visible_result_count { 0 };
|
size_t visible_result_count { 0 };
|
||||||
|
|
||||||
|
@ -218,23 +218,34 @@ int main(int argc, char** argv)
|
||||||
db.search(text_box.text());
|
db.search(text_box.text());
|
||||||
};
|
};
|
||||||
text_box.on_return_pressed = [&]() {
|
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);
|
exit(0);
|
||||||
};
|
};
|
||||||
text_box.on_up_pressed = [&]() {
|
text_box.on_up_pressed = [&]() {
|
||||||
if (app_state.selected_index == 0)
|
if (!app_state.visible_result_count)
|
||||||
app_state.selected_index = app_state.visible_result_count - 1;
|
return;
|
||||||
else if (app_state.selected_index > 0)
|
auto new_selected_index = app_state.selected_index.value_or(0);
|
||||||
app_state.selected_index -= 1;
|
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();
|
mark_selected_item();
|
||||||
};
|
};
|
||||||
text_box.on_down_pressed = [&]() {
|
text_box.on_down_pressed = [&]() {
|
||||||
if ((app_state.visible_result_count - 1) == app_state.selected_index)
|
if (!app_state.visible_result_count)
|
||||||
app_state.selected_index = 0;
|
return;
|
||||||
else if (app_state.visible_result_count > app_state.selected_index)
|
|
||||||
app_state.selected_index += 1;
|
|
||||||
|
|
||||||
|
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();
|
mark_selected_item();
|
||||||
};
|
};
|
||||||
text_box.on_escape_pressed = []() {
|
text_box.on_escape_pressed = []() {
|
||||||
|
@ -242,7 +253,10 @@ int main(int argc, char** argv)
|
||||||
};
|
};
|
||||||
|
|
||||||
db.on_new_results = [&](auto results) {
|
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.results = results;
|
||||||
app_state.visible_result_count = min(results.size(), MAX_SEARCH_RESULTS);
|
app_state.visible_result_count = min(results.size(), MAX_SEARCH_RESULTS);
|
||||||
results_container.remove_all_children();
|
results_container.remove_all_children();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue