main.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright (c) 2021, Spencer Dixon <spencercdixon@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Providers.h"
  7. #include <AK/QuickSort.h>
  8. #include <AK/String.h>
  9. #include <LibGUI/Application.h>
  10. #include <LibGUI/BoxLayout.h>
  11. #include <LibGUI/Event.h>
  12. #include <LibGUI/Icon.h>
  13. #include <LibGUI/ImageWidget.h>
  14. #include <LibGUI/Label.h>
  15. #include <LibGUI/Painter.h>
  16. #include <LibGUI/TextBox.h>
  17. #include <LibGfx/Palette.h>
  18. #include <LibThreading/Lock.h>
  19. namespace Assistant {
  20. struct AppState {
  21. Optional<size_t> selected_index;
  22. NonnullRefPtrVector<Result> results;
  23. size_t visible_result_count { 0 };
  24. Threading::Lock lock;
  25. String last_query;
  26. };
  27. class ResultRow final : public GUI::Widget {
  28. C_OBJECT(ResultRow)
  29. public:
  30. ResultRow()
  31. {
  32. auto& layout = set_layout<GUI::HorizontalBoxLayout>();
  33. layout.set_spacing(12);
  34. layout.set_margins({ 4, 4, 4, 4 });
  35. m_image = add<GUI::ImageWidget>();
  36. m_label_container = add<GUI::Widget>();
  37. m_label_container->set_layout<GUI::VerticalBoxLayout>();
  38. m_label_container->set_fixed_height(30);
  39. m_title = m_label_container->add<GUI::Label>();
  40. m_title->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  41. set_shrink_to_fit(true);
  42. set_fill_with_background_color(true);
  43. set_global_cursor_tracking(true);
  44. set_greedy_for_hits(true);
  45. }
  46. void set_image(RefPtr<Gfx::Bitmap> bitmap)
  47. {
  48. m_image->set_bitmap(bitmap);
  49. }
  50. void set_title(String text)
  51. {
  52. m_title->set_text(move(text));
  53. }
  54. void set_subtitle(String text)
  55. {
  56. if (text.is_empty()) {
  57. if (m_subtitle)
  58. m_subtitle->remove_from_parent();
  59. m_subtitle = nullptr;
  60. return;
  61. }
  62. if (!m_subtitle) {
  63. m_subtitle = m_label_container->add<GUI::Label>();
  64. m_subtitle->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  65. }
  66. m_subtitle->set_text(move(text));
  67. }
  68. void set_is_highlighted(bool value)
  69. {
  70. if (m_is_highlighted == value)
  71. return;
  72. m_is_highlighted = value;
  73. m_title->set_font_weight(value ? 700 : 400);
  74. }
  75. Function<void()> on_selected;
  76. private:
  77. void mousedown_event(GUI::MouseEvent&) override
  78. {
  79. set_background_role(ColorRole::MenuBase);
  80. }
  81. void mouseup_event(GUI::MouseEvent&) override
  82. {
  83. set_background_role(ColorRole::NoRole);
  84. on_selected();
  85. }
  86. void enter_event(Core::Event&) override
  87. {
  88. set_background_role(ColorRole::HoverHighlight);
  89. }
  90. void leave_event(Core::Event&) override
  91. {
  92. set_background_role(ColorRole::NoRole);
  93. }
  94. RefPtr<GUI::ImageWidget> m_image;
  95. RefPtr<GUI::Widget> m_label_container;
  96. RefPtr<GUI::Label> m_title;
  97. RefPtr<GUI::Label> m_subtitle;
  98. bool m_is_highlighted { false };
  99. };
  100. class Database {
  101. public:
  102. explicit Database(AppState& state)
  103. : m_state(state)
  104. {
  105. m_providers.append(make<AppProvider>());
  106. m_providers.append(make<CalculatorProvider>());
  107. m_providers.append(make<FileProvider>());
  108. m_providers.append(make<TerminalProvider>());
  109. m_providers.append(make<URLProvider>());
  110. }
  111. Function<void(NonnullRefPtrVector<Result>)> on_new_results;
  112. void search(String const& query)
  113. {
  114. for (auto& provider : m_providers) {
  115. provider.query(query, [=, this](auto results) {
  116. did_receive_results(query, results);
  117. });
  118. }
  119. }
  120. private:
  121. void did_receive_results(String const& query, NonnullRefPtrVector<Result> const& results)
  122. {
  123. {
  124. Threading::Locker db_locker(m_lock);
  125. auto it = m_result_cache.find(query);
  126. if (it == m_result_cache.end()) {
  127. m_result_cache.set(query, {});
  128. }
  129. it = m_result_cache.find(query);
  130. for (auto& result : results) {
  131. auto found = it->value.find_if([&result](auto& other) {
  132. return result.equals(other);
  133. });
  134. if (found.is_end())
  135. it->value.append(result);
  136. }
  137. }
  138. Threading::Locker state_locker(m_state.lock);
  139. auto new_results = m_result_cache.find(m_state.last_query);
  140. if (new_results == m_result_cache.end())
  141. return;
  142. // NonnullRefPtrVector will provide dual_pivot_quick_sort references rather than pointers,
  143. // and dual_pivot_quick_sort requires being able to construct the underlying type on the
  144. // stack. Assistant::Result is pure virtual, thus cannot be constructed on the stack.
  145. auto& sortable_results = static_cast<Vector<NonnullRefPtr<Result>>&>(new_results->value);
  146. dual_pivot_quick_sort(sortable_results, 0, static_cast<int>(sortable_results.size() - 1), [](auto& a, auto& b) {
  147. return a->score() > b->score();
  148. });
  149. on_new_results(new_results->value);
  150. }
  151. AppState& m_state;
  152. NonnullOwnPtrVector<Provider> m_providers;
  153. Threading::Lock m_lock;
  154. HashMap<String, NonnullRefPtrVector<Result>> m_result_cache;
  155. };
  156. }
  157. static constexpr size_t MAX_SEARCH_RESULTS = 6;
  158. int main(int argc, char** argv)
  159. {
  160. if (pledge("stdio recvfd sendfd rpath unix proc exec thread", nullptr) < 0) {
  161. perror("pledge");
  162. return 1;
  163. }
  164. auto app = GUI::Application::construct(argc, argv);
  165. auto window = GUI::Window::construct();
  166. window->set_minimizable(false);
  167. Assistant::AppState app_state;
  168. Assistant::Database db { app_state };
  169. auto& container = window->set_main_widget<GUI::Widget>();
  170. container.set_fill_with_background_color(true);
  171. auto& layout = container.set_layout<GUI::VerticalBoxLayout>();
  172. layout.set_margins({ 8, 8, 8, 0 });
  173. auto& text_box = container.add<GUI::TextBox>();
  174. auto& results_container = container.add<GUI::Widget>();
  175. auto& results_layout = results_container.set_layout<GUI::VerticalBoxLayout>();
  176. results_layout.set_margins({ 0, 10, 0, 10 });
  177. auto mark_selected_item = [&]() {
  178. for (size_t i = 0; i < app_state.visible_result_count; ++i) {
  179. auto& row = static_cast<Assistant::ResultRow&>(results_container.child_widgets()[i]);
  180. row.set_is_highlighted(i == app_state.selected_index);
  181. }
  182. };
  183. text_box.on_change = [&]() {
  184. {
  185. Threading::Locker locker(app_state.lock);
  186. if (app_state.last_query == text_box.text())
  187. return;
  188. app_state.last_query = text_box.text();
  189. }
  190. db.search(text_box.text());
  191. };
  192. text_box.on_return_pressed = [&]() {
  193. if (!app_state.selected_index.has_value())
  194. return;
  195. app_state.results[app_state.selected_index.value()].activate();
  196. exit(0);
  197. };
  198. text_box.on_up_pressed = [&]() {
  199. if (!app_state.visible_result_count)
  200. return;
  201. auto new_selected_index = app_state.selected_index.value_or(0);
  202. if (new_selected_index == 0)
  203. new_selected_index = app_state.visible_result_count - 1;
  204. else if (new_selected_index > 0)
  205. new_selected_index -= 1;
  206. app_state.selected_index = new_selected_index;
  207. mark_selected_item();
  208. };
  209. text_box.on_down_pressed = [&]() {
  210. if (!app_state.visible_result_count)
  211. return;
  212. auto new_selected_index = app_state.selected_index.value_or(0);
  213. if ((app_state.visible_result_count - 1) == new_selected_index)
  214. new_selected_index = 0;
  215. else if (app_state.visible_result_count > new_selected_index)
  216. new_selected_index += 1;
  217. app_state.selected_index = new_selected_index;
  218. mark_selected_item();
  219. };
  220. text_box.on_escape_pressed = []() {
  221. exit(0);
  222. };
  223. db.on_new_results = [&](auto results) {
  224. if (results.is_empty())
  225. app_state.selected_index = {};
  226. else
  227. app_state.selected_index = 0;
  228. app_state.results = results;
  229. app_state.visible_result_count = min(results.size(), MAX_SEARCH_RESULTS);
  230. results_container.remove_all_children();
  231. for (size_t i = 0; i < app_state.visible_result_count; ++i) {
  232. auto& result = app_state.results[i];
  233. auto& match = results_container.add<Assistant::ResultRow>();
  234. match.set_image(result.bitmap());
  235. match.set_title(result.title());
  236. match.set_subtitle(result.subtitle());
  237. match.on_selected = [&result]() {
  238. result.activate();
  239. exit(0);
  240. };
  241. }
  242. mark_selected_item();
  243. auto window_height = app_state.visible_result_count * 40 + text_box.height() + 28;
  244. window->resize(GUI::Desktop::the().rect().width() / 3, window_height);
  245. };
  246. window->set_frameless(true);
  247. window->resize(GUI::Desktop::the().rect().width() / 3, 46);
  248. window->center_on_screen();
  249. window->move_to(window->x(), window->y() - (GUI::Desktop::the().rect().height() * 0.33));
  250. window->show();
  251. return app->exec();
  252. }