Button.cpp 6.5 KB

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