Button.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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(DeprecatedString deprecated_text)
  20. : Button(String::from_deprecated_string(deprecated_text).release_value_but_fixme_should_propagate_errors())
  21. {
  22. }
  23. Button::Button(String text)
  24. : AbstractButton(move(text))
  25. {
  26. set_min_size({ 40, 22 });
  27. set_preferred_size({ SpecialDimension::OpportunisticGrow, 22 });
  28. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  29. on_focus_change = [this](bool has_focus, auto) {
  30. if (!is_default())
  31. return;
  32. if (!has_focus && is<Button>(window()->focused_widget()))
  33. m_another_button_has_focus = true;
  34. else
  35. m_another_button_has_focus = false;
  36. update();
  37. };
  38. REGISTER_ENUM_PROPERTY(
  39. "button_style", button_style, set_button_style, Gfx::ButtonStyle,
  40. { Gfx::ButtonStyle::Normal, "Normal" },
  41. { Gfx::ButtonStyle::Coolbar, "Coolbar" });
  42. REGISTER_WRITE_ONLY_STRING_PROPERTY("icon", set_icon_from_path);
  43. REGISTER_BOOL_PROPERTY("default", is_default, set_default);
  44. }
  45. Button::~Button()
  46. {
  47. if (m_action)
  48. m_action->unregister_button({}, *this);
  49. }
  50. void Button::paint_event(PaintEvent& event)
  51. {
  52. Painter painter(*this);
  53. painter.add_clip_rect(event.rect());
  54. bool paint_pressed = is_being_pressed() || m_mimic_pressed || (m_menu && m_menu->is_visible());
  55. 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());
  56. if (text_deprecated().is_empty() && !m_icon)
  57. return;
  58. auto content_rect = rect().shrunken(8, 2);
  59. auto icon_location = m_icon ? content_rect.center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2)) : Gfx::IntPoint();
  60. if (m_icon && !text_deprecated().is_empty())
  61. icon_location.set_x(content_rect.x());
  62. if (paint_pressed || is_checked()) {
  63. painter.translate(1, 1);
  64. } else if (m_icon && is_enabled() && is_hovered() && button_style() == Gfx::ButtonStyle::Coolbar) {
  65. auto shadow_color = palette().button().darkened(0.7f);
  66. painter.blit_filtered(icon_location.translated(1, 1), *m_icon, m_icon->rect(), [&shadow_color](auto) {
  67. return shadow_color;
  68. });
  69. icon_location.translate_by(-1, -1);
  70. }
  71. if (m_icon) {
  72. auto solid_color = m_icon->solid_color(60);
  73. bool should_invert_icon = false;
  74. if (solid_color.has_value()) {
  75. auto contrast_ratio = palette().button().contrast_ratio(*solid_color);
  76. // Note: 4.5 is the minimum recommended contrast ratio for text on the web:
  77. // (https://developer.mozilla.org/en-US/docs/Web/Accessibility/Understanding_WCAG/Perceivable/Color_contrast)
  78. // Reusing that threshold here as it seems to work reasonably well.
  79. should_invert_icon = contrast_ratio < 4.5f && contrast_ratio < palette().button().contrast_ratio(solid_color->inverted());
  80. }
  81. if (should_invert_icon)
  82. m_icon->invert();
  83. if (is_enabled()) {
  84. if (is_hovered())
  85. painter.blit_brightened(icon_location, *m_icon, m_icon->rect());
  86. else
  87. painter.blit(icon_location, *m_icon, m_icon->rect());
  88. } else {
  89. painter.blit_disabled(icon_location, *m_icon, m_icon->rect(), palette());
  90. }
  91. if (should_invert_icon)
  92. m_icon->invert();
  93. }
  94. auto& font = is_checked() ? this->font().bold_variant() : this->font();
  95. if (m_icon && !text_deprecated().is_empty()) {
  96. content_rect.translate_by(m_icon->width() + icon_spacing(), 0);
  97. content_rect.set_width(content_rect.width() - m_icon->width() - icon_spacing());
  98. }
  99. Gfx::IntRect text_rect { 0, 0, static_cast<int>(ceilf(font.width(text_deprecated()))), font.glyph_height() };
  100. if (text_rect.width() > content_rect.width())
  101. text_rect.set_width(content_rect.width());
  102. text_rect.align_within(content_rect, text_alignment());
  103. paint_text(painter, text_rect, font, text_alignment());
  104. if (is_focused()) {
  105. Gfx::IntRect focus_rect;
  106. if (m_icon && !text_deprecated().is_empty())
  107. focus_rect = text_rect.inflated(4, 4);
  108. else
  109. focus_rect = rect().shrunken(8, 8);
  110. painter.draw_focus_rect(focus_rect, palette().focus_outline());
  111. }
  112. }
  113. void Button::click(unsigned modifiers)
  114. {
  115. if (!is_enabled())
  116. return;
  117. NonnullRefPtr protector = *this;
  118. if (is_checkable()) {
  119. if (is_checked() && !is_uncheckable())
  120. return;
  121. set_checked(!is_checked());
  122. }
  123. mimic_pressed();
  124. if (on_click)
  125. on_click(modifiers);
  126. if (m_action)
  127. m_action->activate(this);
  128. }
  129. void Button::middle_mouse_click(unsigned int modifiers)
  130. {
  131. if (!is_enabled())
  132. return;
  133. NonnullRefPtr protector = *this;
  134. if (on_middle_mouse_click)
  135. on_middle_mouse_click(modifiers);
  136. }
  137. void Button::context_menu_event(ContextMenuEvent& context_menu_event)
  138. {
  139. if (!is_enabled())
  140. return;
  141. if (on_context_menu_request)
  142. on_context_menu_request(context_menu_event);
  143. }
  144. void Button::set_action(Action& action)
  145. {
  146. m_action = action;
  147. action.register_button({}, *this);
  148. set_visible(action.is_visible());
  149. set_enabled(action.is_enabled());
  150. set_checkable(action.is_checkable());
  151. if (action.is_checkable())
  152. set_checked(action.is_checked());
  153. }
  154. void Button::set_icon(RefPtr<Gfx::Bitmap> icon)
  155. {
  156. if (m_icon == icon)
  157. return;
  158. m_icon = move(icon);
  159. update();
  160. }
  161. void Button::set_icon_from_path(DeprecatedString const& path)
  162. {
  163. auto maybe_bitmap = Gfx::Bitmap::load_from_file(path);
  164. if (maybe_bitmap.is_error()) {
  165. dbgln("Unable to load bitmap `{}` for button icon", path);
  166. return;
  167. }
  168. set_icon(maybe_bitmap.release_value());
  169. }
  170. bool Button::is_uncheckable() const
  171. {
  172. if (!m_action)
  173. return true;
  174. if (!m_action->group())
  175. return true;
  176. return m_action->group()->is_unchecking_allowed();
  177. }
  178. void Button::set_menu(RefPtr<GUI::Menu> menu)
  179. {
  180. if (m_menu == menu)
  181. return;
  182. if (m_menu)
  183. m_menu->on_visibility_change = nullptr;
  184. m_menu = menu;
  185. if (m_menu) {
  186. m_menu->on_visibility_change = [&](bool) {
  187. update();
  188. };
  189. }
  190. }
  191. void Button::mousedown_event(MouseEvent& event)
  192. {
  193. if (m_menu) {
  194. m_menu->popup(screen_relative_rect().bottom_left(), {}, rect());
  195. update();
  196. return;
  197. }
  198. AbstractButton::mousedown_event(event);
  199. }
  200. void Button::mousemove_event(MouseEvent& event)
  201. {
  202. if (m_menu) {
  203. return;
  204. }
  205. AbstractButton::mousemove_event(event);
  206. }
  207. bool Button::is_default() const
  208. {
  209. if (!window())
  210. return false;
  211. return this == window()->default_return_key_widget();
  212. }
  213. void Button::set_default(bool default_button)
  214. {
  215. deferred_invoke([this, default_button] {
  216. VERIFY(window());
  217. window()->set_default_return_key_widget(default_button ? this : nullptr);
  218. });
  219. }
  220. void Button::mimic_pressed()
  221. {
  222. if (!is_being_pressed() && !was_being_pressed()) {
  223. m_mimic_pressed = true;
  224. stop_timer();
  225. start_timer(80, Core::TimerShouldFireWhenNotVisible::Yes);
  226. update();
  227. }
  228. }
  229. void Button::timer_event(Core::TimerEvent&)
  230. {
  231. if (m_mimic_pressed) {
  232. m_mimic_pressed = false;
  233. update();
  234. }
  235. }
  236. Optional<UISize> Button::calculated_min_size() const
  237. {
  238. int horizontal = 0, vertical = 0;
  239. if (!text_deprecated().is_empty()) {
  240. auto& font = this->font();
  241. horizontal = font.width(text_deprecated()) + 2;
  242. vertical = font.glyph_height() + 4; // FIXME: Use actual maximum total height
  243. }
  244. if (m_icon) {
  245. vertical = max(vertical, m_icon->height());
  246. horizontal += m_icon->width() + icon_spacing();
  247. }
  248. horizontal += 8;
  249. vertical += 4;
  250. return UISize(horizontal, vertical);
  251. }
  252. }