AbstractButton.cpp 8.7 KB

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