AbstractButton.cpp 7.8 KB

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