AbstractButton.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. * Copyright (c) 2022, Jakob-Niklas See <git@nwex.de>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/JsonObject.h>
  9. #include <LibCore/Timer.h>
  10. #include <LibGUI/AbstractButton.h>
  11. #include <LibGUI/Painter.h>
  12. #include <LibGUI/Window.h>
  13. #include <LibGfx/Palette.h>
  14. namespace GUI {
  15. AbstractButton::AbstractButton(String text)
  16. {
  17. set_text(move(text));
  18. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  19. set_background_role(Gfx::ColorRole::Button);
  20. set_foreground_role(Gfx::ColorRole::ButtonText);
  21. m_auto_repeat_timer = add<Core::Timer>();
  22. m_auto_repeat_timer->on_timeout = [this] {
  23. click();
  24. };
  25. REGISTER_STRING_PROPERTY("text", text, set_text);
  26. REGISTER_BOOL_PROPERTY("checked", is_checked, set_checked);
  27. REGISTER_BOOL_PROPERTY("checkable", is_checkable, set_checkable);
  28. REGISTER_BOOL_PROPERTY("exclusive", is_exclusive, set_exclusive);
  29. }
  30. void AbstractButton::set_text(String text)
  31. {
  32. if (m_text == text)
  33. return;
  34. m_text = move(text);
  35. update();
  36. }
  37. void AbstractButton::set_checked(bool checked, AllowCallback allow_callback)
  38. {
  39. if (m_checked == checked)
  40. return;
  41. m_checked = checked;
  42. if (is_exclusive() && checked && parent_widget()) {
  43. bool sibling_had_focus = false;
  44. parent_widget()->for_each_child_of_type<AbstractButton>([&](auto& sibling) {
  45. if (!sibling.is_exclusive())
  46. return IterationDecision::Continue;
  47. if (window() && window()->focused_widget() == &sibling)
  48. sibling_had_focus = true;
  49. if (!sibling.is_checked())
  50. return IterationDecision::Continue;
  51. sibling.m_checked = false;
  52. sibling.update();
  53. if (sibling.on_checked)
  54. sibling.on_checked(false);
  55. return IterationDecision::Continue;
  56. });
  57. m_checked = true;
  58. if (sibling_had_focus)
  59. set_focus(true);
  60. }
  61. if (is_exclusive() && parent_widget()) {
  62. // In a group of exclusive checkable buttons, only the currently checked button is focusable.
  63. parent_widget()->for_each_child_of_type<GUI::AbstractButton>([&](auto& button) {
  64. if (button.is_exclusive() && button.is_checkable())
  65. button.set_focus_policy(button.is_checked() ? GUI::FocusPolicy::StrongFocus : GUI::FocusPolicy::NoFocus);
  66. return IterationDecision::Continue;
  67. });
  68. }
  69. update();
  70. if (on_checked && allow_callback == AllowCallback::Yes)
  71. on_checked(checked);
  72. }
  73. void AbstractButton::set_checkable(bool checkable)
  74. {
  75. if (m_checkable == checkable)
  76. return;
  77. m_checkable = checkable;
  78. update();
  79. }
  80. void AbstractButton::mousemove_event(MouseEvent& event)
  81. {
  82. bool is_over = rect().contains(event.position());
  83. m_hovered = is_over;
  84. if (event.buttons() & m_pressed_mouse_button) {
  85. bool being_pressed = is_over;
  86. if (being_pressed != m_being_pressed) {
  87. m_being_pressed = being_pressed;
  88. if (m_auto_repeat_interval) {
  89. if (!m_being_pressed)
  90. m_auto_repeat_timer->stop();
  91. else
  92. m_auto_repeat_timer->start(m_auto_repeat_interval);
  93. }
  94. update();
  95. }
  96. }
  97. Widget::mousemove_event(event);
  98. }
  99. void AbstractButton::mousedown_event(MouseEvent& event)
  100. {
  101. if (event.button() & m_allowed_mouse_buttons_for_pressing) {
  102. m_being_pressed = true;
  103. m_pressed_mouse_button = event.button();
  104. repaint();
  105. if (m_auto_repeat_interval) {
  106. click();
  107. m_auto_repeat_timer->start(m_auto_repeat_interval);
  108. }
  109. event.accept();
  110. }
  111. Widget::mousedown_event(event);
  112. }
  113. void AbstractButton::mouseup_event(MouseEvent& event)
  114. {
  115. if (event.button() == m_pressed_mouse_button && m_being_pressed) {
  116. bool was_auto_repeating = m_auto_repeat_timer->is_active();
  117. m_auto_repeat_timer->stop();
  118. bool was_being_pressed = m_being_pressed;
  119. m_being_pressed = false;
  120. m_pressed_mouse_button = MouseButton::None;
  121. repaint();
  122. if (was_being_pressed && !was_auto_repeating) {
  123. switch (event.button()) {
  124. case MouseButton::Primary:
  125. click(event.modifiers());
  126. break;
  127. case MouseButton::Middle:
  128. middle_mouse_click(event.modifiers());
  129. break;
  130. default:
  131. VERIFY_NOT_REACHED();
  132. }
  133. }
  134. }
  135. Widget::mouseup_event(event);
  136. }
  137. void AbstractButton::enter_event(Core::Event&)
  138. {
  139. m_hovered = true;
  140. update();
  141. }
  142. void AbstractButton::leave_event(Core::Event& event)
  143. {
  144. m_hovered = false;
  145. if (m_being_keyboard_pressed)
  146. m_being_keyboard_pressed = m_being_pressed = false;
  147. update();
  148. event.accept();
  149. Widget::leave_event(event);
  150. }
  151. void AbstractButton::focusout_event(GUI::FocusEvent& event)
  152. {
  153. if (m_being_keyboard_pressed) {
  154. m_being_pressed = m_being_keyboard_pressed = false;
  155. event.accept();
  156. update();
  157. }
  158. Widget::focusout_event(event);
  159. }
  160. void AbstractButton::keydown_event(KeyEvent& event)
  161. {
  162. if (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Space) {
  163. m_being_pressed = m_being_keyboard_pressed = true;
  164. update();
  165. event.accept();
  166. return;
  167. } else if (m_being_pressed && event.key() == KeyCode::Key_Escape) {
  168. m_being_pressed = m_being_keyboard_pressed = false;
  169. update();
  170. event.accept();
  171. return;
  172. }
  173. // Arrow keys switch the currently checked option within an exclusive group of checkable buttons.
  174. if (event.is_arrow_key() && !event.modifiers() && is_exclusive() && is_checkable() && parent_widget()) {
  175. event.accept();
  176. Vector<GUI::AbstractButton&> exclusive_siblings;
  177. size_t this_index = 0;
  178. parent_widget()->for_each_child_of_type<GUI::AbstractButton>([&](auto& sibling) {
  179. if (&sibling == this) {
  180. VERIFY(is_enabled());
  181. this_index = exclusive_siblings.size();
  182. }
  183. if (sibling.is_exclusive() && sibling.is_checkable() && sibling.is_enabled())
  184. exclusive_siblings.append(sibling);
  185. return IterationDecision::Continue;
  186. });
  187. if (exclusive_siblings.size() <= 1)
  188. return;
  189. size_t new_checked_index;
  190. if (event.key() == KeyCode::Key_Left || event.key() == KeyCode::Key_Up)
  191. new_checked_index = this_index == 0 ? exclusive_siblings.size() - 1 : this_index - 1;
  192. else
  193. new_checked_index = this_index == exclusive_siblings.size() - 1 ? 0 : this_index + 1;
  194. exclusive_siblings[new_checked_index].set_checked(true);
  195. return;
  196. }
  197. Widget::keydown_event(event);
  198. }
  199. void AbstractButton::keyup_event(KeyEvent& event)
  200. {
  201. bool was_being_pressed = m_being_pressed;
  202. if (was_being_pressed && (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Space)) {
  203. m_being_pressed = m_being_keyboard_pressed = false;
  204. click(event.modifiers());
  205. update();
  206. event.accept();
  207. return;
  208. }
  209. Widget::keyup_event(event);
  210. }
  211. void AbstractButton::paint_text(Painter& painter, Gfx::IntRect const& rect, Gfx::Font const& font, Gfx::TextAlignment text_alignment, Gfx::TextWrapping text_wrapping)
  212. {
  213. auto clipped_rect = rect.intersected(this->rect());
  214. if (!is_enabled()) {
  215. painter.draw_text(clipped_rect.translated(1, 1), text(), font, text_alignment, palette().disabled_text_back(), Gfx::TextElision::Right, text_wrapping);
  216. painter.draw_text(clipped_rect, text(), font, text_alignment, palette().disabled_text_front(), Gfx::TextElision::Right, text_wrapping);
  217. return;
  218. }
  219. if (text().is_empty())
  220. return;
  221. painter.draw_text(clipped_rect, text(), font, text_alignment, palette().color(foreground_role()), Gfx::TextElision::Right, text_wrapping);
  222. }
  223. void AbstractButton::change_event(Event& event)
  224. {
  225. if (event.type() == Event::Type::EnabledChange) {
  226. if (!is_enabled()) {
  227. bool was_being_pressed = m_being_pressed;
  228. m_being_pressed = false;
  229. if (was_being_pressed)
  230. update();
  231. }
  232. }
  233. Widget::change_event(event);
  234. }
  235. }