Button.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (c) 2018-2023, 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({ SpecialDimension::Shrink });
  23. set_preferred_size({ SpecialDimension::OpportunisticGrow, SpecialDimension::Shrink });
  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_WRITE_ONLY_STRING_PROPERTY("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() || m_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. auto icon = should_invert_icon ? m_icon->inverted().release_value_but_fixme_should_propagate_errors() : NonnullRefPtr { *m_icon };
  78. if (is_enabled()) {
  79. if (is_hovered())
  80. painter.blit_brightened(icon_location, *icon, icon->rect());
  81. else
  82. painter.blit(icon_location, *icon, icon->rect());
  83. } else {
  84. painter.blit_disabled(icon_location, *icon, icon->rect(), palette());
  85. }
  86. }
  87. auto& font = is_checked() ? this->font().bold_variant() : this->font();
  88. if (m_icon && !text().is_empty()) {
  89. content_rect.translate_by(m_icon->width() + icon_spacing(), 0);
  90. content_rect.set_width(content_rect.width() - m_icon->width() - icon_spacing());
  91. }
  92. Gfx::IntRect text_rect { 0, 0, font.width_rounded_up(text()), font.pixel_size_rounded_up() };
  93. if (text_rect.width() > content_rect.width())
  94. text_rect.set_width(content_rect.width());
  95. text_rect.align_within(content_rect, text_alignment());
  96. paint_text(painter, text_rect, font, text_alignment());
  97. if (is_focused()) {
  98. Gfx::IntRect focus_rect;
  99. if (m_icon && !text().is_empty())
  100. focus_rect = text_rect.inflated(4, 4);
  101. else
  102. focus_rect = rect().shrunken(8, 8);
  103. painter.draw_focus_rect(focus_rect, palette().focus_outline());
  104. }
  105. }
  106. void Button::click(unsigned modifiers)
  107. {
  108. if (!is_enabled())
  109. return;
  110. NonnullRefPtr protector = *this;
  111. if (is_checkable()) {
  112. if (is_checked() && !is_uncheckable())
  113. return;
  114. set_checked(!is_checked());
  115. }
  116. mimic_pressed();
  117. if (on_click)
  118. on_click(modifiers);
  119. if (m_action)
  120. m_action->activate(this);
  121. }
  122. void Button::double_click(unsigned int modifiers)
  123. {
  124. if (on_double_click)
  125. on_double_click(modifiers);
  126. }
  127. void Button::middle_mouse_click(unsigned int modifiers)
  128. {
  129. if (!is_enabled())
  130. return;
  131. NonnullRefPtr protector = *this;
  132. if (on_middle_mouse_click)
  133. on_middle_mouse_click(modifiers);
  134. }
  135. void Button::context_menu_event(ContextMenuEvent& context_menu_event)
  136. {
  137. if (!is_enabled())
  138. return;
  139. if (on_context_menu_request)
  140. on_context_menu_request(context_menu_event);
  141. }
  142. void Button::set_action(Action& action)
  143. {
  144. m_action = action;
  145. action.register_button({}, *this);
  146. set_visible(action.is_visible());
  147. set_enabled(action.is_enabled());
  148. set_checkable(action.is_checkable());
  149. if (action.is_checkable())
  150. set_checked(action.is_checked());
  151. }
  152. void Button::set_icon(RefPtr<Gfx::Bitmap const> icon)
  153. {
  154. if (m_icon == icon)
  155. return;
  156. m_icon = move(icon);
  157. update();
  158. }
  159. void Button::set_icon_from_path(DeprecatedString const& path)
  160. {
  161. auto maybe_bitmap = Gfx::Bitmap::load_from_file(path);
  162. if (maybe_bitmap.is_error()) {
  163. dbgln("Unable to load bitmap `{}` for button icon", path);
  164. return;
  165. }
  166. set_icon(maybe_bitmap.release_value());
  167. }
  168. bool Button::is_uncheckable() const
  169. {
  170. if (!m_action)
  171. return true;
  172. if (!m_action->group())
  173. return true;
  174. return m_action->group()->is_unchecking_allowed();
  175. }
  176. void Button::set_menu(RefPtr<GUI::Menu> menu)
  177. {
  178. if (m_menu == menu)
  179. return;
  180. if (m_menu)
  181. m_menu->on_visibility_change = nullptr;
  182. m_menu = menu;
  183. if (m_menu) {
  184. m_menu->on_visibility_change = [&](bool) {
  185. update();
  186. };
  187. }
  188. }
  189. void Button::mousedown_event(MouseEvent& event)
  190. {
  191. if (m_menu) {
  192. m_menu->popup(screen_relative_rect().bottom_left().moved_up(1), {}, rect());
  193. update();
  194. return;
  195. }
  196. AbstractButton::mousedown_event(event);
  197. }
  198. void Button::mousemove_event(MouseEvent& event)
  199. {
  200. if (m_menu) {
  201. return;
  202. }
  203. AbstractButton::mousemove_event(event);
  204. }
  205. bool Button::is_default() const
  206. {
  207. if (!window())
  208. return false;
  209. return this == window()->default_return_key_widget();
  210. }
  211. void Button::set_default(bool default_button)
  212. {
  213. deferred_invoke([this, default_button] {
  214. VERIFY(window());
  215. window()->set_default_return_key_widget(default_button ? this : nullptr);
  216. });
  217. }
  218. void Button::mimic_pressed()
  219. {
  220. if (!is_being_pressed() && !was_being_pressed()) {
  221. m_mimic_pressed = true;
  222. stop_timer();
  223. start_timer(80, Core::TimerShouldFireWhenNotVisible::Yes);
  224. update();
  225. }
  226. }
  227. void Button::timer_event(Core::TimerEvent&)
  228. {
  229. if (m_mimic_pressed) {
  230. m_mimic_pressed = false;
  231. update();
  232. }
  233. }
  234. Optional<UISize> Button::calculated_min_size() const
  235. {
  236. int width = 22;
  237. int height = 22;
  238. int constexpr padding = 6;
  239. if (!text().is_empty()) {
  240. width = max(width, font().width_rounded_up("..."sv) + padding);
  241. height = max(height, font().pixel_size_rounded_up() + padding);
  242. }
  243. if (icon()) {
  244. int icon_width = icon()->width() + icon_spacing();
  245. width = text().is_empty() ? max(width, icon_width) : width + icon_width;
  246. height = max(height, icon()->height() + padding);
  247. }
  248. return UISize(width, height);
  249. }
  250. Optional<UISize> DialogButton::calculated_min_size() const
  251. {
  252. int constexpr scale = 8;
  253. int constexpr padding = 6;
  254. int width = max(80, font().presentation_size() * scale);
  255. int height = max(22, font().pixel_size_rounded_up() + padding);
  256. return UISize(width, height);
  257. }
  258. }