AbstractButton.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. * Copyright (c) 2022, networkException <networkexception@serenityos.org>
  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. if (!is_checkable() || is_checked())
  122. repaint();
  123. if (was_being_pressed && !was_auto_repeating) {
  124. switch (event.button()) {
  125. case MouseButton::Primary:
  126. click(event.modifiers());
  127. break;
  128. case MouseButton::Middle:
  129. middle_mouse_click(event.modifiers());
  130. break;
  131. default:
  132. VERIFY_NOT_REACHED();
  133. }
  134. }
  135. }
  136. Widget::mouseup_event(event);
  137. }
  138. void AbstractButton::enter_event(Core::Event&)
  139. {
  140. m_hovered = true;
  141. update();
  142. }
  143. void AbstractButton::leave_event(Core::Event& event)
  144. {
  145. m_hovered = false;
  146. if (m_being_keyboard_pressed)
  147. m_being_keyboard_pressed = m_being_pressed = false;
  148. update();
  149. event.accept();
  150. Widget::leave_event(event);
  151. }
  152. void AbstractButton::focusout_event(GUI::FocusEvent& event)
  153. {
  154. if (m_being_keyboard_pressed) {
  155. m_being_pressed = m_being_keyboard_pressed = false;
  156. event.accept();
  157. update();
  158. }
  159. Widget::focusout_event(event);
  160. }
  161. void AbstractButton::keydown_event(KeyEvent& event)
  162. {
  163. if (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Space) {
  164. m_being_pressed = m_being_keyboard_pressed = true;
  165. update();
  166. event.accept();
  167. return;
  168. } else if (m_being_pressed && event.key() == KeyCode::Key_Escape) {
  169. m_being_pressed = m_being_keyboard_pressed = false;
  170. update();
  171. event.accept();
  172. return;
  173. }
  174. // Arrow keys switch the currently checked option within an exclusive group of checkable buttons.
  175. if (event.is_arrow_key() && !event.modifiers() && is_exclusive() && is_checkable() && parent_widget()) {
  176. event.accept();
  177. Vector<GUI::AbstractButton&> exclusive_siblings;
  178. size_t this_index = 0;
  179. parent_widget()->for_each_child_of_type<GUI::AbstractButton>([&](auto& sibling) {
  180. if (&sibling == this) {
  181. VERIFY(is_enabled());
  182. this_index = exclusive_siblings.size();
  183. }
  184. if (sibling.is_exclusive() && sibling.is_checkable() && sibling.is_enabled())
  185. exclusive_siblings.append(sibling);
  186. return IterationDecision::Continue;
  187. });
  188. if (exclusive_siblings.size() <= 1)
  189. return;
  190. size_t new_checked_index;
  191. if (event.key() == KeyCode::Key_Left || event.key() == KeyCode::Key_Up)
  192. new_checked_index = this_index == 0 ? exclusive_siblings.size() - 1 : this_index - 1;
  193. else
  194. new_checked_index = this_index == exclusive_siblings.size() - 1 ? 0 : this_index + 1;
  195. exclusive_siblings[new_checked_index].click();
  196. return;
  197. }
  198. Widget::keydown_event(event);
  199. }
  200. void AbstractButton::keyup_event(KeyEvent& event)
  201. {
  202. bool was_being_pressed = m_being_pressed;
  203. if (was_being_pressed && (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Space)) {
  204. m_being_pressed = m_being_keyboard_pressed = false;
  205. click(event.modifiers());
  206. update();
  207. event.accept();
  208. return;
  209. }
  210. Widget::keyup_event(event);
  211. }
  212. void AbstractButton::paint_text(Painter& painter, Gfx::IntRect const& rect, Gfx::Font const& font, Gfx::TextAlignment text_alignment, Gfx::TextWrapping text_wrapping)
  213. {
  214. auto clipped_rect = rect.intersected(this->rect());
  215. if (!is_enabled()) {
  216. painter.draw_text(clipped_rect.translated(1, 1), text(), font, text_alignment, palette().disabled_text_back(), Gfx::TextElision::Right, text_wrapping);
  217. painter.draw_text(clipped_rect, text(), font, text_alignment, palette().disabled_text_front(), Gfx::TextElision::Right, text_wrapping);
  218. return;
  219. }
  220. if (text().is_empty())
  221. return;
  222. painter.draw_text(clipped_rect, text(), font, text_alignment, palette().color(foreground_role()), Gfx::TextElision::Right, text_wrapping);
  223. }
  224. void AbstractButton::change_event(Event& event)
  225. {
  226. if (event.type() == Event::Type::EnabledChange) {
  227. if (m_auto_repeat_timer->is_active())
  228. m_auto_repeat_timer->stop();
  229. if (!is_enabled()) {
  230. bool was_being_pressed = m_being_pressed;
  231. m_being_pressed = false;
  232. if (was_being_pressed)
  233. update();
  234. }
  235. }
  236. Widget::change_event(event);
  237. }
  238. }