ComboBox.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. event.accept();
  36. }
  37. virtual void keydown_event(KeyEvent& event) override
  38. {
  39. if (event.key() == Key_Escape) {
  40. if (is_focused())
  41. set_focus(false);
  42. event.accept();
  43. } else {
  44. on_keypress(event);
  45. TextEditor::keydown_event(event);
  46. }
  47. }
  48. };
  49. ComboBox::ComboBox()
  50. {
  51. REGISTER_DEPRECATED_STRING_PROPERTY("placeholder", editor_placeholder, set_editor_placeholder);
  52. REGISTER_BOOL_PROPERTY("model_only", only_allow_values_from_model, set_only_allow_values_from_model);
  53. REGISTER_INT_PROPERTY("max_visible_items", max_visible_items, set_max_visible_items);
  54. set_min_size({ 40, 22 });
  55. set_preferred_size({ SpecialDimension::OpportunisticGrow, 22 });
  56. m_editor = add<ComboBoxEditor>();
  57. m_editor->set_frame_style(Gfx::FrameStyle::NoFrame);
  58. m_editor->on_return_pressed = [this] {
  59. if (on_return_pressed)
  60. on_return_pressed();
  61. };
  62. m_editor->on_up_pressed = [this] {
  63. navigate(AbstractView::CursorMovement::Up);
  64. };
  65. m_editor->on_down_pressed = [this] {
  66. navigate(AbstractView::CursorMovement::Down);
  67. };
  68. m_editor->on_pageup_pressed = [this] {
  69. navigate(AbstractView::CursorMovement::PageUp);
  70. };
  71. m_editor->on_pagedown_pressed = [this] {
  72. navigate(AbstractView::CursorMovement::PageDown);
  73. };
  74. m_editor->on_mousewheel = [this](int delta) {
  75. // Since we can only show one item at a time we don't want to
  76. // skip any. So just move one item at a time.
  77. navigate_relative(delta > 0 ? 1 : -1);
  78. };
  79. m_editor->on_mousedown = [this] {
  80. if (only_allow_values_from_model())
  81. m_open_button->click();
  82. };
  83. m_editor->on_keypress = [this](KeyEvent& event) {
  84. if (!m_only_allow_values_from_model)
  85. return;
  86. if (!m_list_window->is_visible() && event.key() <= Key_Z && event.key() >= Key_A && event.modifiers() == Mod_None) {
  87. open();
  88. m_list_window->event(event);
  89. }
  90. };
  91. m_open_button = add<Button>();
  92. m_open_button->set_button_style(Gfx::ButtonStyle::ThickCap);
  93. m_open_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/downward-triangle.png"sv).release_value_but_fixme_should_propagate_errors());
  94. m_open_button->set_focus_policy(GUI::FocusPolicy::NoFocus);
  95. m_open_button->on_click = [this](auto) {
  96. if (!m_list_view->item_count())
  97. return;
  98. if (m_list_window->is_visible())
  99. close();
  100. else
  101. open();
  102. };
  103. m_list_window = add<Window>(window());
  104. m_list_window->set_window_type(GUI::WindowType::Popup);
  105. m_list_view = m_list_window->set_main_widget<ListView>().release_value_but_fixme_should_propagate_errors();
  106. m_list_view->set_should_hide_unnecessary_scrollbars(true);
  107. m_list_view->set_alternating_row_colors(false);
  108. m_list_view->set_hover_highlighting(true);
  109. m_list_view->set_frame_style(Gfx::FrameStyle::Plain);
  110. m_list_view->set_activates_on_selection(true);
  111. m_list_view->on_selection_change = [this] {
  112. VERIFY(model());
  113. const auto& index = m_list_view->selection().first();
  114. if (m_updating_model)
  115. selection_updated(index);
  116. };
  117. m_list_view->on_activation = [this](auto& index) {
  118. deferred_invoke([this, index] {
  119. selection_updated(index);
  120. if (on_change)
  121. on_change(m_editor->text(), index);
  122. });
  123. close();
  124. };
  125. m_list_view->on_escape_pressed = [this] {
  126. close();
  127. };
  128. }
  129. ComboBox::~ComboBox() = default;
  130. void ComboBox::set_editor_placeholder(StringView placeholder)
  131. {
  132. m_editor->set_placeholder(placeholder);
  133. }
  134. DeprecatedString const& ComboBox::editor_placeholder() const
  135. {
  136. return m_editor->placeholder();
  137. }
  138. void ComboBox::navigate(AbstractView::CursorMovement cursor_movement)
  139. {
  140. auto previous_selected = m_list_view->cursor_index();
  141. m_list_view->move_cursor(cursor_movement, AbstractView::SelectionUpdate::Set);
  142. auto current_selected = m_list_view->cursor_index();
  143. selection_updated(current_selected);
  144. if (previous_selected.row() != current_selected.row() && on_change)
  145. on_change(m_editor->text(), current_selected);
  146. }
  147. void ComboBox::navigate_relative(int delta)
  148. {
  149. auto previous_selected = m_list_view->cursor_index();
  150. m_list_view->move_cursor_relative(delta, AbstractView::SelectionUpdate::Set);
  151. auto current_selected = m_list_view->cursor_index();
  152. selection_updated(current_selected);
  153. if (previous_selected.row() != current_selected.row() && on_change)
  154. on_change(m_editor->text(), current_selected);
  155. }
  156. void ComboBox::selection_updated(ModelIndex const& index)
  157. {
  158. if (index.is_valid())
  159. m_selected_index = index;
  160. else
  161. m_selected_index.clear();
  162. auto new_value = index.data().to_deprecated_string();
  163. m_editor->set_text(new_value);
  164. if (!m_only_allow_values_from_model)
  165. m_editor->select_all();
  166. }
  167. void ComboBox::resize_event(ResizeEvent& event)
  168. {
  169. Widget::resize_event(event);
  170. int button_height = event.size().height() - frame_thickness() * 2;
  171. int button_width = 15;
  172. m_open_button->set_relative_rect(width() - button_width - frame_thickness(), frame_thickness(), button_width, button_height);
  173. auto editor_rect = frame_inner_rect();
  174. editor_rect.set_width(editor_rect.width() - button_width);
  175. m_editor->set_relative_rect(editor_rect);
  176. }
  177. void ComboBox::set_model(NonnullRefPtr<Model> model)
  178. {
  179. TemporaryChange change(m_updating_model, true);
  180. m_selected_index.clear();
  181. m_list_view->set_model(move(model));
  182. }
  183. void ComboBox::clear_selection()
  184. {
  185. m_selected_index.clear();
  186. m_editor->clear_selection();
  187. m_editor->clear();
  188. }
  189. void ComboBox::set_selected_index(size_t index, AllowCallback allow_callback)
  190. {
  191. if (!m_list_view->model())
  192. return;
  193. size_t previous_index = selected_index();
  194. TemporaryChange change(m_updating_model, true);
  195. auto model_index = m_list_view->model()->index(index, 0);
  196. m_list_view->set_cursor(model_index, AbstractView::SelectionUpdate::Set);
  197. selection_updated(model_index);
  198. if (previous_index != selected_index() && on_change && allow_callback == AllowCallback::Yes)
  199. on_change(m_editor->text(), m_list_view->cursor_index());
  200. }
  201. size_t ComboBox::selected_index() const
  202. {
  203. return m_selected_index.has_value() ? m_selected_index.value().row() : 0;
  204. }
  205. void ComboBox::select_all()
  206. {
  207. m_editor->select_all();
  208. }
  209. void ComboBox::open()
  210. {
  211. m_editor->set_focus(true);
  212. // Force content size update while invisible
  213. m_list_view->resize({});
  214. auto frame = m_list_view->frame_thickness() * 2;
  215. auto max_height = min(m_list_view->item_height() * m_max_visible_items, m_list_view->content_height());
  216. auto min_width = m_list_view->content_width() + frame;
  217. Gfx::IntSize size { max(width(), min_width), max_height + frame };
  218. Gfx::IntRect rect { screen_relative_rect().bottom_left().moved_up(1), size };
  219. auto desktop = Desktop::the().rect();
  220. auto min_height = 5 * m_list_view->item_height() + frame;
  221. auto go_upwards_instead = rect.bottom() - 1 >= desktop.height() && rect.intersected(desktop).height() < min_height;
  222. if (go_upwards_instead) {
  223. auto origin = screen_relative_rect().top_left();
  224. rect = { Gfx::IntPoint { origin.x(), origin.y() - size.height() }, size };
  225. }
  226. auto intersection = rect.intersected(desktop);
  227. rect.set_top(intersection.top());
  228. rect.set_bottom(intersection.bottom());
  229. auto remainder = (rect.height() - frame) % m_list_view->item_height();
  230. go_upwards_instead ? rect.take_from_top(remainder) : rect.take_from_bottom(remainder);
  231. auto scrollbar_width = m_list_view->vertical_scrollbar().width();
  232. if (max_height > rect.height() && width() < min_width + scrollbar_width)
  233. rect.set_width(rect.width() + scrollbar_width);
  234. m_list_window->set_rect(rect);
  235. m_list_view->set_min_size(rect.size());
  236. if (m_selected_index.has_value()) {
  237. m_list_view->set_cursor(m_selected_index.value(), AbstractView::SelectionUpdate::Set, false);
  238. auto index_top = m_list_view->vertical_scrollbar().step() * m_selected_index.value().row();
  239. m_list_view->vertical_scrollbar().set_value(index_top);
  240. }
  241. m_list_window->show();
  242. }
  243. void ComboBox::close()
  244. {
  245. m_list_window->hide();
  246. }
  247. DeprecatedString ComboBox::text() const
  248. {
  249. return m_editor->text();
  250. }
  251. void ComboBox::set_text(DeprecatedString const& text, AllowCallback allow_callback)
  252. {
  253. m_editor->set_text(text, allow_callback);
  254. }
  255. void ComboBox::set_only_allow_values_from_model(bool b)
  256. {
  257. if (m_only_allow_values_from_model == b)
  258. return;
  259. m_only_allow_values_from_model = b;
  260. m_editor->set_mode(m_only_allow_values_from_model ? TextEditor::DisplayOnly : TextEditor::Editable);
  261. }
  262. Model* ComboBox::model()
  263. {
  264. return m_list_view->model();
  265. }
  266. Model const* ComboBox::model() const
  267. {
  268. return m_list_view->model();
  269. }
  270. int ComboBox::model_column() const
  271. {
  272. return m_list_view->model_column();
  273. }
  274. void ComboBox::set_model_column(int column)
  275. {
  276. m_list_view->set_model_column(column);
  277. }
  278. }