Button.cpp 8.4 KB

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