main.cpp 9.1 KB

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