CharacterSearchWidget.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "CharacterSearchWidget.h"
  8. #include "SearchCharacters.h"
  9. #include <Applications/CharacterMap/CharacterSearchWindowGML.h>
  10. struct SearchResult {
  11. u32 code_point;
  12. String code_point_string;
  13. String display_text;
  14. };
  15. class CharacterSearchModel final : public GUI::Model {
  16. public:
  17. CharacterSearchModel() = default;
  18. int row_count(GUI::ModelIndex const&) const override { return m_data.size(); }
  19. int column_count(GUI::ModelIndex const&) const override { return 2; }
  20. GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role) const override
  21. {
  22. auto& result = m_data.at(index.row());
  23. if (role == GUI::ModelRole::Display) {
  24. if (index.column() == 0)
  25. return result.code_point_string;
  26. return result.display_text;
  27. }
  28. if (role == GUI::ModelRole::Custom)
  29. return result.code_point;
  30. return {};
  31. }
  32. void clear()
  33. {
  34. m_data.clear();
  35. invalidate();
  36. }
  37. void add_result(SearchResult result)
  38. {
  39. m_data.append(move(result));
  40. invalidate();
  41. }
  42. private:
  43. Vector<SearchResult> m_data;
  44. };
  45. CharacterSearchWidget::CharacterSearchWidget()
  46. {
  47. load_from_gml(character_search_window_gml);
  48. m_search_input = find_descendant_of_type_named<GUI::TextBox>("search_input");
  49. m_search_button = find_descendant_of_type_named<GUI::Button>("search_button");
  50. m_results_table = find_descendant_of_type_named<GUI::TableView>("results_table");
  51. m_search_input->on_up_pressed = [this] { m_results_table->move_cursor(GUI::AbstractView::CursorMovement::Up, GUI::AbstractView::SelectionUpdate::Set); };
  52. m_search_input->on_down_pressed = [this] { m_results_table->move_cursor(GUI::AbstractView::CursorMovement::Down, GUI::AbstractView::SelectionUpdate::Set); };
  53. m_search_input->on_return_pressed = [this] { search(); };
  54. m_search_button->on_click = [this](auto) { search(); };
  55. m_results_table->horizontal_scrollbar().set_visible(false);
  56. m_results_table->set_column_headers_visible(false);
  57. m_results_table->set_model(adopt_ref(*new CharacterSearchModel()));
  58. m_results_table->on_activation = [&](GUI::ModelIndex const& index) {
  59. auto& model = static_cast<CharacterSearchModel&>(*m_results_table->model());
  60. auto code_point = model.data(index, GUI::ModelRole::Custom).as_u32();
  61. if (on_character_selected)
  62. on_character_selected(code_point);
  63. };
  64. }
  65. void CharacterSearchWidget::search()
  66. {
  67. // TODO: Sort the results nicely. They're sorted by code-point for now, which is easy, but not the most useful.
  68. // Sorting intelligently in a style similar to Assistant would be nicer.
  69. auto& model = static_cast<CharacterSearchModel&>(*m_results_table->model());
  70. model.clear();
  71. auto query = m_search_input->text();
  72. if (query.is_empty())
  73. return;
  74. for_each_character_containing(query, [&](auto code_point, auto display_name) {
  75. StringBuilder builder;
  76. builder.append_code_point(code_point);
  77. model.add_result({ code_point, builder.build(), move(display_name) });
  78. });
  79. }