Tray.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGUI/Painter.h>
  8. #include <LibGUI/Tray.h>
  9. #include <LibGfx/Font/Font.h>
  10. #include <LibGfx/Palette.h>
  11. #include <LibGfx/StylePainter.h>
  12. REGISTER_WIDGET(GUI, Tray);
  13. namespace GUI {
  14. Tray::Tray()
  15. {
  16. set_fill_with_background_color(true);
  17. set_background_role(Gfx::ColorRole::Tray);
  18. set_focus_policy(GUI::FocusPolicy::TabFocus);
  19. }
  20. Gfx::IntRect Tray::Item::rect(Tray const& tray) const
  21. {
  22. static constexpr int item_height = 22;
  23. return Gfx::IntRect {
  24. tray.frame_thickness(),
  25. tray.frame_thickness() + static_cast<int>(index) * item_height,
  26. tray.frame_inner_rect().width(),
  27. item_height,
  28. };
  29. }
  30. size_t Tray::add_item(String text, RefPtr<Gfx::Bitmap> bitmap, String custom_data)
  31. {
  32. auto new_index = m_items.size();
  33. m_items.append(Item {
  34. .text = move(text),
  35. .bitmap = move(bitmap),
  36. .custom_data = move(custom_data),
  37. .index = new_index,
  38. });
  39. update();
  40. return new_index;
  41. }
  42. void Tray::set_item_checked(size_t index, bool checked)
  43. {
  44. if (checked) {
  45. m_checked_item_index = index;
  46. } else {
  47. if (m_checked_item_index == index)
  48. m_checked_item_index = {};
  49. }
  50. update();
  51. }
  52. void Tray::paint_event(GUI::PaintEvent& event)
  53. {
  54. GUI::Frame::paint_event(event);
  55. GUI::Painter painter(*this);
  56. painter.add_clip_rect(event.rect());
  57. for (auto& item : m_items) {
  58. auto rect = item.rect(*this);
  59. bool is_pressed = item.index == m_pressed_item_index;
  60. bool is_hovered = item.index == m_hovered_item_index;
  61. bool is_checked = item.index == m_checked_item_index;
  62. Gfx::StylePainter::paint_button(painter, rect, palette(), Gfx::ButtonStyle::Tray, is_pressed && is_hovered, is_hovered, is_checked, is_enabled());
  63. Gfx::IntRect icon_rect {
  64. rect.x() + 4,
  65. 0,
  66. 16,
  67. 16,
  68. };
  69. icon_rect.center_vertically_within(rect);
  70. Gfx::IntRect text_rect {
  71. icon_rect.right() + 5,
  72. rect.y(),
  73. rect.width(),
  74. rect.height(),
  75. };
  76. text_rect.intersect(rect);
  77. if (is_pressed && is_hovered) {
  78. icon_rect.translate_by(1, 1);
  79. text_rect.translate_by(1, 1);
  80. }
  81. if (item.bitmap)
  82. painter.blit(icon_rect.location(), *item.bitmap, item.bitmap->rect());
  83. auto const& font = is_checked ? this->font().bold_variant() : this->font();
  84. painter.draw_text(text_rect, item.text, font, Gfx::TextAlignment::CenterLeft, palette().color(Gfx::ColorRole::TrayText));
  85. }
  86. }
  87. void Tray::mousemove_event(GUI::MouseEvent& event)
  88. {
  89. auto* hovered_item = item_at(event.position());
  90. if (!hovered_item) {
  91. if (m_hovered_item_index.has_value())
  92. update();
  93. m_hovered_item_index = {};
  94. return;
  95. }
  96. if (m_hovered_item_index != hovered_item->index) {
  97. m_hovered_item_index = hovered_item->index;
  98. update();
  99. }
  100. }
  101. void Tray::mousedown_event(GUI::MouseEvent& event)
  102. {
  103. if (event.button() != GUI::MouseButton::Primary)
  104. return;
  105. auto* pressed_item = item_at(event.position());
  106. if (!pressed_item)
  107. return;
  108. if (m_pressed_item_index != pressed_item->index) {
  109. m_pressed_item_index = pressed_item->index;
  110. update();
  111. }
  112. }
  113. void Tray::mouseup_event(GUI::MouseEvent& event)
  114. {
  115. if (event.button() != GUI::MouseButton::Primary)
  116. return;
  117. if (auto* pressed_item = item_at(event.position()); pressed_item && m_pressed_item_index == pressed_item->index) {
  118. on_item_activation(pressed_item->custom_data);
  119. }
  120. m_pressed_item_index = {};
  121. update();
  122. }
  123. void Tray::leave_event(Core::Event&)
  124. {
  125. m_hovered_item_index = {};
  126. update();
  127. }
  128. Tray::Item* Tray::item_at(Gfx::IntPoint const& position)
  129. {
  130. for (auto& item : m_items) {
  131. if (item.rect(*this).contains(position))
  132. return &item;
  133. }
  134. return nullptr;
  135. }
  136. void Tray::focusin_event(GUI::FocusEvent&)
  137. {
  138. if (m_items.is_empty())
  139. return;
  140. m_hovered_item_index = 0;
  141. update();
  142. }
  143. void Tray::focusout_event(GUI::FocusEvent&)
  144. {
  145. if (m_items.is_empty())
  146. return;
  147. m_hovered_item_index = {};
  148. update();
  149. }
  150. void Tray::keydown_event(GUI::KeyEvent& event)
  151. {
  152. if (m_items.is_empty() || event.modifiers())
  153. return Frame::keydown_event(event);
  154. if (event.key() == KeyCode::Key_Down) {
  155. if (!m_hovered_item_index.has_value())
  156. m_hovered_item_index = 0;
  157. else
  158. m_hovered_item_index = (*m_hovered_item_index + 1) % m_items.size();
  159. update();
  160. return;
  161. }
  162. if (event.key() == KeyCode::Key_Up) {
  163. if (!m_hovered_item_index.has_value() || m_hovered_item_index == 0u)
  164. m_hovered_item_index = m_items.size() - 1;
  165. else
  166. m_hovered_item_index = *m_hovered_item_index - 1;
  167. update();
  168. return;
  169. }
  170. if (event.key() == KeyCode::Key_Return) {
  171. if (m_hovered_item_index.has_value())
  172. on_item_activation(m_items[*m_hovered_item_index].custom_data);
  173. return;
  174. }
  175. Frame::keydown_event(event);
  176. }
  177. }