ComboBox.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGUI/Button.h>
  8. #include <LibGUI/ComboBox.h>
  9. #include <LibGUI/Desktop.h>
  10. #include <LibGUI/Event.h>
  11. #include <LibGUI/ListView.h>
  12. #include <LibGUI/Model.h>
  13. #include <LibGUI/Scrollbar.h>
  14. #include <LibGUI/TextBox.h>
  15. #include <LibGUI/TextEditor.h>
  16. #include <LibGUI/Window.h>
  17. REGISTER_WIDGET(GUI, ComboBox)
  18. namespace GUI {
  19. class ComboBoxEditor final : public TextEditor {
  20. C_OBJECT(ComboBoxEditor);
  21. public:
  22. Function<void(int delta)> on_mousewheel;
  23. Function<void(KeyEvent& event)> on_keypress;
  24. private:
  25. ComboBoxEditor()
  26. : TextEditor(TextEditor::SingleLine)
  27. {
  28. }
  29. virtual void mousewheel_event(MouseEvent& event) override
  30. {
  31. if (!is_focused())
  32. set_focus(true);
  33. if (on_mousewheel)
  34. on_mousewheel(event.wheel_delta_y());
  35. }
  36. virtual void keydown_event(KeyEvent& event) override
  37. {
  38. if (event.key() == Key_Escape) {
  39. if (is_focused())
  40. set_focus(false);
  41. event.accept();
  42. } else {
  43. on_keypress(event);
  44. TextEditor::keydown_event(event);
  45. }
  46. }
  47. };
  48. ComboBox::ComboBox()
  49. {
  50. REGISTER_STRING_PROPERTY("placeholder", editor_placeholder, set_editor_placeholder);
  51. REGISTER_BOOL_PROPERTY("model_only", only_allow_values_from_model, set_only_allow_values_from_model);
  52. set_min_size({ 40, 22 });
  53. set_preferred_size({ SpecialDimension::OpportunisticGrow, 22 });
  54. m_editor = add<ComboBoxEditor>();
  55. m_editor->set_frame_thickness(0);
  56. m_editor->on_return_pressed = [this] {
  57. if (on_return_pressed)
  58. on_return_pressed();
  59. };
  60. m_editor->on_up_pressed = [this] {
  61. navigate(AbstractView::CursorMovement::Up);
  62. };
  63. m_editor->on_down_pressed = [this] {
  64. navigate(AbstractView::CursorMovement::Down);
  65. };
  66. m_editor->on_pageup_pressed = [this] {
  67. navigate(AbstractView::CursorMovement::PageUp);
  68. };
  69. m_editor->on_pagedown_pressed = [this] {
  70. navigate(AbstractView::CursorMovement::PageDown);
  71. };
  72. m_editor->on_mousewheel = [this](int delta) {
  73. // Since we can only show one item at a time we don't want to
  74. // skip any. So just move one item at a time.
  75. navigate_relative(delta > 0 ? 1 : -1);
  76. };
  77. m_editor->on_mousedown = [this] {
  78. if (only_allow_values_from_model())
  79. m_open_button->click();
  80. };
  81. m_editor->on_keypress = [this](KeyEvent& event) {
  82. if (!m_list_window->is_visible() && event.key() <= Key_Z && event.key() >= Key_A) {
  83. open();
  84. m_list_window->event(event);
  85. }
  86. };
  87. m_open_button = add<Button>();
  88. m_open_button->set_button_style(Gfx::ButtonStyle::ThickCap);
  89. m_open_button->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/downward-triangle.png"sv).release_value_but_fixme_should_propagate_errors());
  90. m_open_button->set_focus_policy(GUI::FocusPolicy::NoFocus);
  91. m_open_button->on_click = [this](auto) {
  92. if (m_list_window->is_visible())
  93. close();
  94. else
  95. open();
  96. };
  97. m_list_window = add<Window>(window());
  98. m_list_window->set_frameless(true);
  99. m_list_window->set_window_mode(WindowMode::CaptureInput);
  100. m_list_window->on_active_input_change = [this](bool is_active_input) {
  101. if (!is_active_input) {
  102. m_open_button->set_enabled(false);
  103. close();
  104. }
  105. m_open_button->set_enabled(true);
  106. };
  107. m_list_view = m_list_window->set_main_widget<ListView>();
  108. m_list_view->horizontal_scrollbar().set_visible(false);
  109. m_list_view->set_alternating_row_colors(false);
  110. m_list_view->set_hover_highlighting(true);
  111. m_list_view->set_frame_thickness(1);
  112. m_list_view->set_frame_shadow(Gfx::FrameShadow::Plain);
  113. m_list_view->on_selection_change = [this] {
  114. VERIFY(model());
  115. const auto& index = m_list_view->selection().first();
  116. m_list_view->set_activates_on_selection(true);
  117. if (m_updating_model)
  118. selection_updated(index);
  119. };
  120. m_list_view->on_activation = [this](auto& index) {
  121. deferred_invoke([this, index] {
  122. selection_updated(index);
  123. if (on_change)
  124. on_change(m_editor->text(), index);
  125. });
  126. m_list_view->set_activates_on_selection(false);
  127. close();
  128. };
  129. m_list_view->on_escape_pressed = [this] {
  130. close();
  131. };
  132. }
  133. ComboBox::~ComboBox() = default;
  134. void ComboBox::set_editor_placeholder(StringView placeholder)
  135. {
  136. m_editor->set_placeholder(placeholder);
  137. }
  138. String const& ComboBox::editor_placeholder() const
  139. {
  140. return m_editor->placeholder();
  141. }
  142. void ComboBox::navigate(AbstractView::CursorMovement cursor_movement)
  143. {
  144. auto previous_selected = m_list_view->cursor_index();
  145. m_list_view->move_cursor(cursor_movement, AbstractView::SelectionUpdate::Set);
  146. auto current_selected = m_list_view->cursor_index();
  147. selection_updated(current_selected);
  148. if (previous_selected.row() != current_selected.row() && on_change)
  149. on_change(m_editor->text(), current_selected);
  150. }
  151. void ComboBox::navigate_relative(int delta)
  152. {
  153. auto previous_selected = m_list_view->cursor_index();
  154. m_list_view->move_cursor_relative(delta, AbstractView::SelectionUpdate::Set);
  155. auto current_selected = m_list_view->cursor_index();
  156. selection_updated(current_selected);
  157. if (previous_selected.row() != current_selected.row() && on_change)
  158. on_change(m_editor->text(), current_selected);
  159. }
  160. void ComboBox::selection_updated(ModelIndex const& index)
  161. {
  162. if (index.is_valid())
  163. m_selected_index = index;
  164. else
  165. m_selected_index.clear();
  166. auto new_value = index.data().to_string();
  167. m_editor->set_text(new_value);
  168. if (!m_only_allow_values_from_model)
  169. m_editor->select_all();
  170. }
  171. void ComboBox::resize_event(ResizeEvent& event)
  172. {
  173. Widget::resize_event(event);
  174. int button_height = event.size().height() - frame_thickness() * 2;
  175. int button_width = 15;
  176. m_open_button->set_relative_rect(width() - button_width - frame_thickness(), frame_thickness(), button_width, button_height);
  177. auto editor_rect = frame_inner_rect();
  178. editor_rect.set_width(editor_rect.width() - button_width);
  179. m_editor->set_relative_rect(editor_rect);
  180. }
  181. void ComboBox::set_model(NonnullRefPtr<Model> model)
  182. {
  183. TemporaryChange change(m_updating_model, true);
  184. m_selected_index.clear();
  185. m_list_view->set_model(move(model));
  186. }
  187. void ComboBox::clear_selection()
  188. {
  189. m_selected_index.clear();
  190. m_editor->clear_selection();
  191. m_editor->clear();
  192. }
  193. void ComboBox::set_selected_index(size_t index, AllowCallback allow_callback)
  194. {
  195. if (!m_list_view->model())
  196. return;
  197. size_t previous_index = selected_index();
  198. TemporaryChange change(m_updating_model, true);
  199. auto model_index = m_list_view->model()->index(index, 0);
  200. m_list_view->set_cursor(model_index, AbstractView::SelectionUpdate::Set);
  201. selection_updated(model_index);
  202. if (previous_index != selected_index() && on_change && allow_callback == AllowCallback::Yes)
  203. on_change(m_editor->text(), m_list_view->cursor_index());
  204. }
  205. size_t ComboBox::selected_index() const
  206. {
  207. return m_selected_index.has_value() ? m_selected_index.value().row() : 0;
  208. }
  209. void ComboBox::select_all()
  210. {
  211. m_editor->select_all();
  212. }
  213. void ComboBox::open()
  214. {
  215. if (!model())
  216. return;
  217. auto my_screen_rect = screen_relative_rect();
  218. int longest_item_width = 0;
  219. for (int i = 0; i < model()->row_count(); ++i) {
  220. auto index = model()->index(i);
  221. auto item_text = index.data().to_string();
  222. longest_item_width = max(longest_item_width, m_list_view->font().width(item_text));
  223. }
  224. Gfx::IntSize size {
  225. max(width(), longest_item_width + m_list_view->width_occupied_by_vertical_scrollbar() + m_list_view->frame_thickness() * 2 + m_list_view->horizontal_padding()),
  226. model()->row_count() * m_list_view->item_height() + m_list_view->frame_thickness() * 2
  227. };
  228. auto taskbar_height = GUI::Desktop::the().taskbar_height();
  229. Gfx::IntRect list_window_rect { my_screen_rect.bottom_left(), size };
  230. list_window_rect.intersect(Desktop::the().rect().shrunken(0, taskbar_height * 2));
  231. m_editor->set_focus(true);
  232. if (m_selected_index.has_value()) {
  233. // Don't set m_updating_model to true here because we only want to
  234. // change the list view's selected item without triggering a change to it.
  235. m_list_view->set_cursor(m_selected_index.value(), AbstractView::SelectionUpdate::Set);
  236. }
  237. // Change direction and go upwards to prevent the list from becoming
  238. // infinitesimally small when pushed up against the screen edge.
  239. auto minimum_height = min(3, model()->row_count()) * m_list_view->item_height() + m_list_view->frame_thickness() * 2;
  240. bool go_upwards_instead = list_window_rect.height() < minimum_height;
  241. if (go_upwards_instead) {
  242. auto origin_point = my_screen_rect.top_left();
  243. list_window_rect = { Gfx::IntPoint { origin_point.x(), origin_point.y() - size.height() }, size };
  244. list_window_rect.intersect(Desktop::the().rect());
  245. }
  246. m_list_window->set_rect(list_window_rect);
  247. m_list_window->show();
  248. }
  249. void ComboBox::close()
  250. {
  251. m_list_window->hide();
  252. }
  253. String ComboBox::text() const
  254. {
  255. return m_editor->text();
  256. }
  257. void ComboBox::set_text(String const& text, AllowCallback allow_callback)
  258. {
  259. m_editor->set_text(text, allow_callback);
  260. }
  261. void ComboBox::set_only_allow_values_from_model(bool b)
  262. {
  263. if (m_only_allow_values_from_model == b)
  264. return;
  265. m_only_allow_values_from_model = b;
  266. m_editor->set_mode(m_only_allow_values_from_model ? TextEditor::DisplayOnly : TextEditor::Editable);
  267. }
  268. Model* ComboBox::model()
  269. {
  270. return m_list_view->model();
  271. }
  272. Model const* ComboBox::model() const
  273. {
  274. return m_list_view->model();
  275. }
  276. int ComboBox::model_column() const
  277. {
  278. return m_list_view->model_column();
  279. }
  280. void ComboBox::set_model_column(int column)
  281. {
  282. m_list_view->set_model_column(column);
  283. }
  284. }