AbstractButton.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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_deprecated, set_text_deprecated);
  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_deprecated(DeprecatedString deprecated_text)
  31. {
  32. set_text(String::from_deprecated_string(deprecated_text).release_value_but_fixme_should_propagate_errors());
  33. }
  34. void AbstractButton::set_text(String text)
  35. {
  36. if (m_text == text)
  37. return;
  38. m_text = move(text);
  39. update();
  40. }
  41. void AbstractButton::set_checked(bool checked, AllowCallback allow_callback)
  42. {
  43. if (m_checked == checked)
  44. return;
  45. m_checked = checked;
  46. if (is_exclusive() && checked && parent_widget()) {
  47. bool sibling_had_focus = false;
  48. parent_widget()->for_each_child_of_type<AbstractButton>([&](auto& sibling) {
  49. if (!sibling.is_exclusive())
  50. return IterationDecision::Continue;
  51. if (window() && window()->focused_widget() == &sibling)
  52. sibling_had_focus = true;
  53. if (!sibling.is_checked())
  54. return IterationDecision::Continue;
  55. sibling.m_checked = false;
  56. sibling.update();
  57. if (sibling.on_checked)
  58. sibling.on_checked(false);
  59. return IterationDecision::Continue;
  60. });
  61. m_checked = true;
  62. if (sibling_had_focus)
  63. set_focus(true);
  64. }
  65. if (is_exclusive() && parent_widget()) {
  66. // In a group of exclusive checkable buttons, only the currently checked button is focusable.
  67. parent_widget()->for_each_child_of_type<GUI::AbstractButton>([&](auto& button) {
  68. if (button.is_exclusive() && button.is_checkable())
  69. button.set_focus_policy(button.is_checked() ? GUI::FocusPolicy::StrongFocus : GUI::FocusPolicy::NoFocus);
  70. return IterationDecision::Continue;
  71. });
  72. }
  73. update();
  74. if (on_checked && allow_callback == AllowCallback::Yes)
  75. on_checked(checked);
  76. }
  77. void AbstractButton::set_checkable(bool checkable)
  78. {
  79. if (m_checkable == checkable)
  80. return;
  81. m_checkable = checkable;
  82. update();
  83. }
  84. void AbstractButton::mousemove_event(MouseEvent& event)
  85. {
  86. bool is_over = rect().contains(event.position());
  87. m_hovered = is_over;
  88. if (event.buttons() & m_pressed_mouse_button) {
  89. bool being_pressed = is_over;
  90. if (being_pressed != m_being_pressed) {
  91. m_being_pressed = being_pressed;
  92. if (m_auto_repeat_interval) {
  93. if (!m_being_pressed)
  94. m_auto_repeat_timer->stop();
  95. else
  96. m_auto_repeat_timer->start(m_auto_repeat_interval);
  97. }
  98. update();
  99. }
  100. }
  101. Widget::mousemove_event(event);
  102. }
  103. void AbstractButton::mousedown_event(MouseEvent& event)
  104. {
  105. if (event.button() & m_allowed_mouse_buttons_for_pressing) {
  106. m_being_pressed = true;
  107. m_pressed_mouse_button = event.button();
  108. repaint();
  109. if (m_auto_repeat_interval) {
  110. click();
  111. m_auto_repeat_timer->start(m_auto_repeat_interval);
  112. }
  113. event.accept();
  114. }
  115. Widget::mousedown_event(event);
  116. }
  117. void AbstractButton::mouseup_event(MouseEvent& event)
  118. {
  119. if (event.button() == m_pressed_mouse_button && m_being_pressed) {
  120. bool was_auto_repeating = m_auto_repeat_timer->is_active();
  121. m_auto_repeat_timer->stop();
  122. m_was_being_pressed = m_being_pressed;
  123. ScopeGuard update_was_being_pressed { [this] { m_was_being_pressed = m_being_pressed; } };
  124. m_being_pressed = false;
  125. m_pressed_mouse_button = MouseButton::None;
  126. if (!is_checkable() || is_checked())
  127. repaint();
  128. if (m_was_being_pressed && !was_auto_repeating) {
  129. switch (event.button()) {
  130. case MouseButton::Primary:
  131. click(event.modifiers());
  132. break;
  133. case MouseButton::Middle:
  134. middle_mouse_click(event.modifiers());
  135. break;
  136. default:
  137. VERIFY_NOT_REACHED();
  138. }
  139. }
  140. }
  141. Widget::mouseup_event(event);
  142. }
  143. void AbstractButton::enter_event(Core::Event&)
  144. {
  145. m_hovered = true;
  146. update();
  147. }
  148. void AbstractButton::leave_event(Core::Event& event)
  149. {
  150. m_hovered = false;
  151. if (m_being_keyboard_pressed)
  152. m_being_keyboard_pressed = m_being_pressed = false;
  153. update();
  154. event.accept();
  155. Widget::leave_event(event);
  156. }
  157. void AbstractButton::focusout_event(GUI::FocusEvent& event)
  158. {
  159. if (m_being_keyboard_pressed) {
  160. m_being_pressed = m_being_keyboard_pressed = false;
  161. event.accept();
  162. update();
  163. }
  164. Widget::focusout_event(event);
  165. }
  166. void AbstractButton::keydown_event(KeyEvent& event)
  167. {
  168. if (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Space) {
  169. m_being_pressed = m_being_keyboard_pressed = true;
  170. update();
  171. event.accept();
  172. return;
  173. } else if (m_being_pressed && event.key() == KeyCode::Key_Escape) {
  174. m_being_pressed = m_being_keyboard_pressed = false;
  175. update();
  176. event.accept();
  177. return;
  178. }
  179. // Arrow keys switch the currently checked option within an exclusive group of checkable buttons.
  180. if (event.is_arrow_key() && !event.modifiers() && is_exclusive() && is_checkable() && parent_widget()) {
  181. event.accept();
  182. Vector<GUI::AbstractButton&> exclusive_siblings;
  183. size_t this_index = 0;
  184. parent_widget()->for_each_child_of_type<GUI::AbstractButton>([&](auto& sibling) {
  185. if (&sibling == this) {
  186. VERIFY(is_enabled());
  187. this_index = exclusive_siblings.size();
  188. }
  189. if (sibling.is_exclusive() && sibling.is_checkable() && sibling.is_enabled())
  190. exclusive_siblings.append(sibling);
  191. return IterationDecision::Continue;
  192. });
  193. if (exclusive_siblings.size() <= 1)
  194. return;
  195. size_t new_checked_index;
  196. if (event.key() == KeyCode::Key_Left || event.key() == KeyCode::Key_Up)
  197. new_checked_index = this_index == 0 ? exclusive_siblings.size() - 1 : this_index - 1;
  198. else
  199. new_checked_index = this_index == exclusive_siblings.size() - 1 ? 0 : this_index + 1;
  200. exclusive_siblings[new_checked_index].click();
  201. return;
  202. }
  203. Widget::keydown_event(event);
  204. }
  205. void AbstractButton::keyup_event(KeyEvent& event)
  206. {
  207. bool was_being_pressed = m_being_pressed;
  208. if (was_being_pressed && (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Space)) {
  209. m_being_pressed = m_being_keyboard_pressed = false;
  210. click(event.modifiers());
  211. update();
  212. event.accept();
  213. return;
  214. }
  215. Widget::keyup_event(event);
  216. }
  217. void AbstractButton::paint_text(Painter& painter, Gfx::IntRect const& rect, Gfx::Font const& font, Gfx::TextAlignment text_alignment, Gfx::TextWrapping text_wrapping)
  218. {
  219. auto clipped_rect = rect.intersected(this->rect());
  220. if (!is_enabled()) {
  221. painter.draw_text(clipped_rect.translated(1, 1), text_deprecated(), font, text_alignment, palette().disabled_text_back(), Gfx::TextElision::Right, text_wrapping);
  222. painter.draw_text(clipped_rect, text_deprecated(), font, text_alignment, palette().disabled_text_front(), Gfx::TextElision::Right, text_wrapping);
  223. return;
  224. }
  225. if (text_deprecated().is_empty())
  226. return;
  227. painter.draw_text(clipped_rect, text_deprecated(), font, text_alignment, palette().color(foreground_role()), Gfx::TextElision::Right, text_wrapping);
  228. }
  229. void AbstractButton::change_event(Event& event)
  230. {
  231. if (event.type() == Event::Type::EnabledChange) {
  232. if (m_auto_repeat_timer->is_active())
  233. m_auto_repeat_timer->stop();
  234. if (!is_enabled()) {
  235. bool was_being_pressed = m_being_pressed;
  236. m_being_pressed = false;
  237. if (was_being_pressed)
  238. update();
  239. }
  240. }
  241. Widget::change_event(event);
  242. }
  243. }