Button.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringBuilder.h>
  7. #include <LibGUI/Action.h>
  8. #include <LibGUI/ActionGroup.h>
  9. #include <LibGUI/Button.h>
  10. #include <LibGUI/Menu.h>
  11. #include <LibGUI/Painter.h>
  12. #include <LibGUI/Window.h>
  13. #include <LibGfx/Font/Font.h>
  14. #include <LibGfx/Palette.h>
  15. #include <LibGfx/StylePainter.h>
  16. REGISTER_WIDGET(GUI, Button)
  17. REGISTER_WIDGET(GUI, DialogButton)
  18. namespace GUI {
  19. Button::Button(String text)
  20. : AbstractButton(move(text))
  21. {
  22. set_min_width(32);
  23. set_fixed_height(22);
  24. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  25. on_focus_change = [this](bool has_focus, auto) {
  26. if (!is_default())
  27. return;
  28. if (!has_focus && is<Button>(window()->focused_widget()))
  29. m_another_button_has_focus = true;
  30. else
  31. m_another_button_has_focus = false;
  32. update();
  33. };
  34. REGISTER_ENUM_PROPERTY(
  35. "button_style", button_style, set_button_style, Gfx::ButtonStyle,
  36. { Gfx::ButtonStyle::Normal, "Normal" },
  37. { Gfx::ButtonStyle::Coolbar, "Coolbar" });
  38. REGISTER_STRING_PROPERTY("icon", icon, set_icon_from_path);
  39. }
  40. Button::~Button()
  41. {
  42. if (m_action)
  43. m_action->unregister_button({}, *this);
  44. }
  45. void Button::paint_event(PaintEvent& event)
  46. {
  47. Painter painter(*this);
  48. painter.add_clip_rect(event.rect());
  49. bool paint_pressed = is_being_pressed() || is_mimic_pressed() || (m_menu && m_menu->is_visible());
  50. Gfx::StylePainter::paint_button(painter, rect(), palette(), m_button_style, paint_pressed, is_hovered(), is_checked(), is_enabled(), is_focused(), is_default() && !another_button_has_focus());
  51. if (text().is_empty() && !m_icon)
  52. return;
  53. auto content_rect = rect().shrunken(8, 2);
  54. auto icon_location = m_icon ? content_rect.center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2)) : Gfx::IntPoint();
  55. if (m_icon && !text().is_empty())
  56. icon_location.set_x(content_rect.x());
  57. if (paint_pressed || is_checked()) {
  58. painter.translate(1, 1);
  59. } else if (m_icon && is_enabled() && is_hovered() && button_style() == Gfx::ButtonStyle::Coolbar) {
  60. auto shadow_color = palette().button().darkened(0.7f);
  61. painter.blit_filtered(icon_location.translated(1, 1), *m_icon, m_icon->rect(), [&shadow_color](auto) {
  62. return shadow_color;
  63. });
  64. icon_location.translate_by(-1, -1);
  65. }
  66. if (m_icon) {
  67. auto solid_color = m_icon->solid_color(60);
  68. // Note: 4.5 is the minimum recommended contrast ratio for text on the web:
  69. // (https://developer.mozilla.org/en-US/docs/Web/Accessibility/Understanding_WCAG/Perceivable/Color_contrast)
  70. // Reusing that threshold here as it seems to work reasonably well.
  71. bool should_invert_icon = solid_color.has_value() && palette().button().contrast_ratio(*solid_color) < 4.5f;
  72. if (should_invert_icon)
  73. m_icon->invert();
  74. if (is_enabled()) {
  75. if (is_hovered())
  76. painter.blit_brightened(icon_location, *m_icon, m_icon->rect());
  77. else
  78. painter.blit(icon_location, *m_icon, m_icon->rect());
  79. } else {
  80. painter.blit_disabled(icon_location, *m_icon, m_icon->rect(), palette());
  81. }
  82. if (should_invert_icon)
  83. m_icon->invert();
  84. }
  85. auto& font = is_checked() ? this->font().bold_variant() : this->font();
  86. if (m_icon && !text().is_empty()) {
  87. content_rect.translate_by(m_icon->width() + icon_spacing(), 0);
  88. content_rect.set_width(content_rect.width() - m_icon->width() - icon_spacing());
  89. }
  90. Gfx::IntRect text_rect { 0, 0, font.width(text()), font.glyph_height() };
  91. if (text_rect.width() > content_rect.width())
  92. text_rect.set_width(content_rect.width());
  93. text_rect.align_within(content_rect, text_alignment());
  94. paint_text(painter, text_rect, font, text_alignment());
  95. if (is_focused()) {
  96. Gfx::IntRect focus_rect;
  97. if (m_icon && !text().is_empty())
  98. focus_rect = text_rect.inflated(4, 4);
  99. else
  100. focus_rect = rect().shrunken(8, 8);
  101. painter.draw_focus_rect(focus_rect, palette().focus_outline());
  102. }
  103. }
  104. void Button::click(unsigned modifiers)
  105. {
  106. if (!is_enabled())
  107. return;
  108. NonnullRefPtr protector = *this;
  109. if (is_checkable()) {
  110. if (is_checked() && !is_uncheckable())
  111. return;
  112. set_checked(!is_checked());
  113. }
  114. if (on_click)
  115. on_click(modifiers);
  116. if (m_action)
  117. m_action->activate(this);
  118. }
  119. void Button::context_menu_event(ContextMenuEvent& context_menu_event)
  120. {
  121. if (!is_enabled())
  122. return;
  123. if (on_context_menu_request)
  124. on_context_menu_request(context_menu_event);
  125. }
  126. void Button::set_action(Action& action)
  127. {
  128. m_action = action;
  129. action.register_button({}, *this);
  130. set_enabled(action.is_enabled());
  131. set_checkable(action.is_checkable());
  132. if (action.is_checkable())
  133. set_checked(action.is_checked());
  134. }
  135. void Button::set_icon(RefPtr<Gfx::Bitmap> icon)
  136. {
  137. if (m_icon == icon)
  138. return;
  139. m_icon = move(icon);
  140. update();
  141. }
  142. void Button::set_icon_from_path(String const& path)
  143. {
  144. auto maybe_bitmap = Gfx::Bitmap::try_load_from_file(path);
  145. if (maybe_bitmap.is_error()) {
  146. dbgln("Unable to load bitmap `{}` for button icon", path);
  147. return;
  148. }
  149. set_icon(maybe_bitmap.release_value());
  150. }
  151. bool Button::is_uncheckable() const
  152. {
  153. if (!m_action)
  154. return true;
  155. if (!m_action->group())
  156. return true;
  157. return m_action->group()->is_unchecking_allowed();
  158. }
  159. void Button::set_menu(RefPtr<GUI::Menu> menu)
  160. {
  161. if (m_menu == menu)
  162. return;
  163. if (m_menu)
  164. m_menu->on_visibility_change = nullptr;
  165. m_menu = menu;
  166. if (m_menu) {
  167. m_menu->on_visibility_change = [&](bool) {
  168. update();
  169. };
  170. }
  171. }
  172. void Button::mousedown_event(MouseEvent& event)
  173. {
  174. if (m_menu) {
  175. if (button_style() == Gfx::ButtonStyle::Tray)
  176. m_menu->popup(screen_relative_rect().top_right());
  177. else
  178. m_menu->popup(screen_relative_rect().top_left());
  179. update();
  180. return;
  181. }
  182. AbstractButton::mousedown_event(event);
  183. }
  184. void Button::mousemove_event(MouseEvent& event)
  185. {
  186. if (m_menu) {
  187. return;
  188. }
  189. AbstractButton::mousemove_event(event);
  190. }
  191. bool Button::is_default() const
  192. {
  193. if (!window())
  194. return false;
  195. return this == window()->default_return_key_widget();
  196. }
  197. void Button::set_default(bool default_button)
  198. {
  199. deferred_invoke([this, default_button] {
  200. VERIFY(window());
  201. window()->set_default_return_key_widget(default_button ? this : nullptr);
  202. });
  203. }
  204. void Button::set_mimic_pressed(bool mimic_pressed)
  205. {
  206. if (!is_being_pressed()) {
  207. m_mimic_pressed = mimic_pressed;
  208. stop_timer();
  209. start_timer(80, Core::TimerShouldFireWhenNotVisible::Yes);
  210. update();
  211. }
  212. }
  213. void Button::timer_event(Core::TimerEvent&)
  214. {
  215. if (is_mimic_pressed()) {
  216. m_mimic_pressed = false;
  217. update();
  218. }
  219. }
  220. }