Button.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <LibGfx/Font.h>
  13. #include <LibGfx/Palette.h>
  14. #include <LibGfx/StylePainter.h>
  15. REGISTER_WIDGET(GUI, Button)
  16. namespace GUI {
  17. Button::Button(String text)
  18. : AbstractButton(move(text))
  19. {
  20. set_min_width(32);
  21. set_fixed_height(22);
  22. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  23. REGISTER_ENUM_PROPERTY(
  24. "button_style", button_style, set_button_style, Gfx::ButtonStyle,
  25. { Gfx::ButtonStyle::Normal, "Normal" },
  26. { Gfx::ButtonStyle::Coolbar, "Coolbar" });
  27. REGISTER_STRING_PROPERTY("icon", icon, set_icon_from_path);
  28. }
  29. Button::~Button()
  30. {
  31. if (m_action)
  32. m_action->unregister_button({}, *this);
  33. }
  34. void Button::paint_event(PaintEvent& event)
  35. {
  36. Painter painter(*this);
  37. painter.add_clip_rect(event.rect());
  38. bool paint_pressed = is_being_pressed() || (m_menu && m_menu->is_visible());
  39. Gfx::StylePainter::paint_button(painter, rect(), palette(), m_button_style, paint_pressed, is_hovered(), is_checked(), is_enabled(), is_focused());
  40. if (text().is_empty() && !m_icon)
  41. return;
  42. auto content_rect = rect().shrunken(8, 2);
  43. auto icon_location = m_icon ? content_rect.center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2)) : Gfx::IntPoint();
  44. if (m_icon && !text().is_empty())
  45. icon_location.set_x(content_rect.x());
  46. if (paint_pressed || is_checked()) {
  47. painter.translate(1, 1);
  48. } else if (m_icon && is_enabled() && is_hovered() && button_style() == Gfx::ButtonStyle::Coolbar) {
  49. auto shadow_color = palette().button().darkened(0.7f);
  50. painter.blit_filtered(icon_location.translated(1, 1), *m_icon, m_icon->rect(), [&shadow_color](auto) {
  51. return shadow_color;
  52. });
  53. icon_location.translate_by(-1, -1);
  54. }
  55. if (m_icon) {
  56. if (is_enabled()) {
  57. if (is_hovered())
  58. painter.blit_brightened(icon_location, *m_icon, m_icon->rect());
  59. else
  60. painter.blit(icon_location, *m_icon, m_icon->rect());
  61. } else {
  62. painter.blit_disabled(icon_location, *m_icon, m_icon->rect(), palette());
  63. }
  64. }
  65. auto& font = is_checked() ? this->font().bold_variant() : this->font();
  66. if (m_icon && !text().is_empty()) {
  67. content_rect.translate_by(m_icon->width() + icon_spacing(), 0);
  68. content_rect.set_width(content_rect.width() - m_icon->width() - icon_spacing());
  69. }
  70. Gfx::IntRect text_rect { 0, 0, font.width(text()), font.glyph_height() };
  71. if (text_rect.width() > content_rect.width())
  72. text_rect.set_width(content_rect.width());
  73. text_rect.align_within(content_rect, text_alignment());
  74. paint_text(painter, text_rect, font, text_alignment());
  75. if (is_focused()) {
  76. Gfx::IntRect focus_rect;
  77. if (m_icon && !text().is_empty())
  78. focus_rect = text_rect.inflated(4, 4);
  79. else
  80. focus_rect = rect().shrunken(8, 8);
  81. painter.draw_focus_rect(focus_rect, palette().focus_outline());
  82. }
  83. }
  84. void Button::click(unsigned modifiers)
  85. {
  86. if (!is_enabled())
  87. return;
  88. NonnullRefPtr protector = *this;
  89. if (is_checkable()) {
  90. if (is_checked() && !is_uncheckable())
  91. return;
  92. set_checked(!is_checked());
  93. }
  94. if (on_click)
  95. on_click(modifiers);
  96. if (m_action)
  97. m_action->activate(this);
  98. }
  99. void Button::context_menu_event(ContextMenuEvent& context_menu_event)
  100. {
  101. if (!is_enabled())
  102. return;
  103. if (on_context_menu_request)
  104. on_context_menu_request(context_menu_event);
  105. }
  106. void Button::set_action(Action& action)
  107. {
  108. m_action = action;
  109. action.register_button({}, *this);
  110. set_enabled(action.is_enabled());
  111. set_checkable(action.is_checkable());
  112. if (action.is_checkable())
  113. set_checked(action.is_checked());
  114. }
  115. void Button::set_icon(RefPtr<Gfx::Bitmap> icon)
  116. {
  117. if (m_icon == icon)
  118. return;
  119. m_icon = move(icon);
  120. update();
  121. }
  122. void Button::set_icon_from_path(String const& path)
  123. {
  124. auto maybe_bitmap = Gfx::Bitmap::try_load_from_file(path);
  125. if (maybe_bitmap.is_error()) {
  126. dbgln("Unable to load bitmap `{}` for button icon", path);
  127. return;
  128. }
  129. set_icon(maybe_bitmap.release_value());
  130. }
  131. bool Button::is_uncheckable() const
  132. {
  133. if (!m_action)
  134. return true;
  135. if (!m_action->group())
  136. return true;
  137. return m_action->group()->is_unchecking_allowed();
  138. }
  139. void Button::set_menu(RefPtr<GUI::Menu> menu)
  140. {
  141. if (m_menu == menu)
  142. return;
  143. if (m_menu)
  144. m_menu->on_visibility_change = nullptr;
  145. m_menu = menu;
  146. if (m_menu) {
  147. m_menu->on_visibility_change = [&](bool) {
  148. update();
  149. };
  150. }
  151. }
  152. void Button::mousedown_event(MouseEvent& event)
  153. {
  154. if (m_menu) {
  155. m_menu->popup(screen_relative_rect().top_left());
  156. update();
  157. return;
  158. }
  159. AbstractButton::mousedown_event(event);
  160. }
  161. void Button::mousemove_event(MouseEvent& event)
  162. {
  163. if (m_menu) {
  164. return;
  165. }
  166. AbstractButton::mousemove_event(event);
  167. }
  168. }