WSMenu.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include "WSMenu.h"
  2. #include "WSMenuItem.h"
  3. #include "WSWindow.h"
  4. #include "WSMessage.h"
  5. #include "WSMessageLoop.h"
  6. #include "WSWindowManager.h"
  7. #include <WindowServer/WSAPITypes.h>
  8. #include <WindowServer/WSClientConnection.h>
  9. #include <SharedGraphics/Painter.h>
  10. #include <SharedGraphics/Font.h>
  11. WSMenu::WSMenu(WSClientConnection* client, int menu_id, String&& name)
  12. : m_client(client)
  13. , m_menu_id(menu_id)
  14. , m_name(move(name))
  15. {
  16. }
  17. WSMenu::~WSMenu()
  18. {
  19. }
  20. const Font& WSMenu::font() const
  21. {
  22. return Font::default_font();
  23. }
  24. int WSMenu::width() const
  25. {
  26. int longest = 0;
  27. for (auto& item : m_items) {
  28. if (item->type() == WSMenuItem::Text) {
  29. int item_width = font().width(item->text());
  30. if (!item->shortcut_text().is_empty())
  31. item_width += padding_between_text_and_shortcut() + font().width(item->shortcut_text());
  32. longest = max(longest, item_width);
  33. }
  34. }
  35. return max(longest, rect_in_menubar().width()) + horizontal_padding();
  36. }
  37. int WSMenu::height() const
  38. {
  39. if (m_items.is_empty())
  40. return 0;
  41. return (m_items.last()->rect().bottom() - 1) + vertical_padding();
  42. }
  43. void WSMenu::redraw()
  44. {
  45. ASSERT(menu_window());
  46. draw();
  47. menu_window()->invalidate();
  48. }
  49. WSWindow& WSMenu::ensure_menu_window()
  50. {
  51. if (!m_menu_window) {
  52. Point next_item_location(1, vertical_padding() / 2);
  53. for (auto& item : m_items) {
  54. int height = 0;
  55. if (item->type() == WSMenuItem::Text)
  56. height = item_height();
  57. else if (item->type() == WSMenuItem::Separator)
  58. height = 7;
  59. item->set_rect({ next_item_location, { width() - 2, height } });
  60. next_item_location.move_by(0, height);
  61. }
  62. auto window = make<WSWindow>(*this, WSWindowType::Menu);
  63. window->set_opacity(0.95f);
  64. window->set_rect(0, 0, width(), height());
  65. m_menu_window = move(window);
  66. draw();
  67. }
  68. return *m_menu_window;
  69. }
  70. void WSMenu::draw()
  71. {
  72. ASSERT(menu_window());
  73. ASSERT(menu_window()->backing_store());
  74. Painter painter(*menu_window()->backing_store());
  75. Rect rect { { }, menu_window()->size() };
  76. painter.draw_rect(rect, Color::White);
  77. painter.fill_rect(rect.shrunken(2, 2), Color::LightGray);
  78. for (auto& item : m_items) {
  79. if (item->type() == WSMenuItem::Text) {
  80. Color text_color = Color::Black;
  81. if (item.ptr() == m_hovered_item) {
  82. painter.fill_rect(item->rect(), WSWindowManager::the().menu_selection_color());
  83. text_color = Color::White;
  84. }
  85. painter.draw_text(item->rect().translated(left_padding(), 0), item->text(), TextAlignment::CenterLeft, text_color);
  86. if (!item->shortcut_text().is_empty()) {
  87. painter.draw_text(item->rect().translated(-right_padding(), 0), item->shortcut_text(), TextAlignment::CenterRight, text_color);
  88. }
  89. } else if (item->type() == WSMenuItem::Separator) {
  90. Point p1(1, item->rect().center().y());
  91. Point p2(width() - 2, item->rect().center().y());
  92. painter.draw_line(p1, p2, Color::MidGray);
  93. }
  94. }
  95. }
  96. void WSMenu::on_message(WSMessage& message)
  97. {
  98. ASSERT(menu_window());
  99. if (message.type() == WSMessage::MouseMove) {
  100. auto* item = item_at(static_cast<WSMouseEvent&>(message).position());
  101. if (!item || m_hovered_item == item)
  102. return;
  103. m_hovered_item = item;
  104. redraw();
  105. return;
  106. }
  107. if (message.type() == WSMessage::MouseUp) {
  108. if (!m_hovered_item)
  109. return;
  110. did_activate(*m_hovered_item);
  111. clear_hovered_item();
  112. return;
  113. }
  114. }
  115. void WSMenu::clear_hovered_item()
  116. {
  117. if (!m_hovered_item)
  118. return;
  119. m_hovered_item = nullptr;
  120. redraw();
  121. }
  122. void WSMenu::did_activate(WSMenuItem& item)
  123. {
  124. if (on_item_activation)
  125. on_item_activation(item);
  126. close();
  127. WSAPI_ServerMessage message;
  128. message.type = WSAPI_ServerMessage::Type::MenuItemActivated;
  129. message.menu.menu_id = m_menu_id;
  130. message.menu.identifier = item.identifier();
  131. if (m_client)
  132. m_client->post_message(message);
  133. }
  134. WSMenuItem* WSMenu::item_at(const Point& position)
  135. {
  136. for (auto& item : m_items) {
  137. if (!item->rect().contains(position))
  138. continue;
  139. return item.ptr();
  140. }
  141. return nullptr;
  142. }
  143. void WSMenu::close()
  144. {
  145. WSWindowManager::the().close_menu(*this);
  146. };