WSMenuManager.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #include <LibDraw/Font.h>
  2. #include <LibDraw/Painter.h>
  3. #include <WindowServer/WSMenuManager.h>
  4. #include <WindowServer/WSScreen.h>
  5. #include <WindowServer/WSWindowManager.h>
  6. #include <unistd.h>
  7. WSMenuManager::WSMenuManager()
  8. {
  9. m_username = getlogin();
  10. m_needs_window_resize = true;
  11. }
  12. WSMenuManager::~WSMenuManager()
  13. {
  14. }
  15. void WSMenuManager::setup()
  16. {
  17. m_window = WSWindow::construct(*this, WSWindowType::Menubar);
  18. m_window->set_rect(menubar_rect());
  19. }
  20. bool WSMenuManager::is_open(const WSMenu& menu) const
  21. {
  22. for (int i = 0; i < m_open_menu_stack.size(); ++i) {
  23. if (&menu == m_open_menu_stack[i].ptr())
  24. return true;
  25. }
  26. return false;
  27. }
  28. void WSMenuManager::draw()
  29. {
  30. auto& wm = WSWindowManager::the();
  31. auto& palette = wm.palette();
  32. auto menubar_rect = this->menubar_rect();
  33. if (m_needs_window_resize) {
  34. int username_width = Font::default_bold_font().width(m_username);
  35. m_username_rect = {
  36. menubar_rect.right() - menubar_menu_margin() / 2 - Font::default_bold_font().width(m_username),
  37. menubar_rect.y(),
  38. username_width,
  39. menubar_rect.height()
  40. };
  41. int right_edge_x = m_username_rect.left() - 4;
  42. for (auto& existing_applet : m_applets) {
  43. if (!existing_applet)
  44. continue;
  45. Rect new_applet_rect(right_edge_x - existing_applet->size().width(), 0, existing_applet->size().width(), existing_applet->size().height());
  46. Rect dummy_menubar_rect(0, 0, 0, 18);
  47. new_applet_rect.center_vertically_within(dummy_menubar_rect);
  48. existing_applet->set_rect_in_menubar(new_applet_rect);
  49. right_edge_x = existing_applet->rect_in_menubar().x() - 4;
  50. }
  51. m_window->set_rect(menubar_rect);
  52. m_needs_window_resize = false;
  53. }
  54. Painter painter(*window().backing_store());
  55. painter.fill_rect(menubar_rect, palette.window());
  56. painter.draw_line({ 0, menubar_rect.bottom() }, { menubar_rect.right(), menubar_rect.bottom() }, palette.threed_shadow1());
  57. int index = 0;
  58. wm.for_each_active_menubar_menu([&](WSMenu& menu) {
  59. Color text_color = palette.window_text();
  60. if (is_open(menu)) {
  61. painter.fill_rect(menu.rect_in_menubar(), palette.menu_selection());
  62. painter.draw_rect(menu.rect_in_menubar(), palette.menu_selection().darkened());
  63. text_color = Color::White;
  64. }
  65. painter.draw_text(
  66. menu.text_rect_in_menubar(),
  67. menu.name(),
  68. index == 1 ? wm.app_menu_font() : wm.menu_font(),
  69. TextAlignment::CenterLeft,
  70. text_color);
  71. ++index;
  72. return IterationDecision::Continue;
  73. });
  74. painter.draw_text(m_username_rect, m_username, Font::default_bold_font(), TextAlignment::CenterRight, palette.window_text());
  75. for (auto& applet : m_applets) {
  76. if (!applet)
  77. continue;
  78. draw_applet(*applet);
  79. }
  80. }
  81. void WSMenuManager::tick_clock()
  82. {
  83. refresh();
  84. }
  85. void WSMenuManager::refresh()
  86. {
  87. if (!m_window)
  88. return;
  89. draw();
  90. window().invalidate();
  91. }
  92. void WSMenuManager::event(CEvent& event)
  93. {
  94. if (WSWindowManager::the().active_window_is_modal())
  95. return CObject::event(event);
  96. if (event.type() == WSEvent::MouseMove || event.type() == WSEvent::MouseUp || event.type() == WSEvent::MouseDown || event.type() == WSEvent::MouseWheel) {
  97. auto& mouse_event = static_cast<WSMouseEvent&>(event);
  98. WSWindowManager::the().for_each_active_menubar_menu([&](WSMenu& menu) {
  99. if (menu.rect_in_menubar().contains(mouse_event.position())) {
  100. handle_menu_mouse_event(menu, mouse_event);
  101. return IterationDecision::Break;
  102. }
  103. return IterationDecision::Continue;
  104. });
  105. for (auto& applet : m_applets) {
  106. if (!applet)
  107. continue;
  108. if (!applet->rect_in_menubar().contains(mouse_event.position()))
  109. continue;
  110. auto local_event = mouse_event.translated(-applet->rect_in_menubar().location());
  111. applet->event(local_event);
  112. }
  113. }
  114. return CObject::event(event);
  115. }
  116. void WSMenuManager::handle_menu_mouse_event(WSMenu& menu, const WSMouseEvent& event)
  117. {
  118. auto& wm = WSWindowManager::the();
  119. bool is_hover_with_any_menu_open = event.type() == WSMouseEvent::MouseMove
  120. && !m_open_menu_stack.is_empty()
  121. && (m_open_menu_stack.first()->menubar() || m_open_menu_stack.first() == wm.system_menu());
  122. bool is_mousedown_with_left_button = event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left;
  123. bool should_open_menu = &menu != m_current_menu && (is_hover_with_any_menu_open || is_mousedown_with_left_button);
  124. if (is_mousedown_with_left_button)
  125. m_bar_open = !m_bar_open;
  126. if (should_open_menu && m_bar_open) {
  127. if (m_current_menu == &menu)
  128. return;
  129. close_everyone();
  130. if (!menu.is_empty()) {
  131. menu.redraw_if_theme_changed();
  132. auto& menu_window = menu.ensure_menu_window();
  133. menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() + 2 });
  134. menu_window.set_visible(true);
  135. }
  136. set_current_menu(&menu);
  137. refresh();
  138. return;
  139. }
  140. if (!m_bar_open)
  141. close_everyone();
  142. }
  143. void WSMenuManager::set_needs_window_resize()
  144. {
  145. m_needs_window_resize = true;
  146. }
  147. void WSMenuManager::close_everyone()
  148. {
  149. for (auto& menu : m_open_menu_stack) {
  150. if (menu && menu->menu_window())
  151. menu->menu_window()->set_visible(false);
  152. }
  153. m_open_menu_stack.clear();
  154. m_current_menu = nullptr;
  155. refresh();
  156. }
  157. void WSMenuManager::close_everyone_not_in_lineage(WSMenu& menu)
  158. {
  159. Vector<WSMenu*> menus_to_close;
  160. for (auto& open_menu : m_open_menu_stack) {
  161. if (!open_menu)
  162. continue;
  163. if (&menu == open_menu.ptr() || open_menu->is_menu_ancestor_of(menu))
  164. continue;
  165. menus_to_close.append(open_menu);
  166. }
  167. close_menus(menus_to_close);
  168. }
  169. void WSMenuManager::close_menus(const Vector<WSMenu*>& menus)
  170. {
  171. for (auto& menu : menus) {
  172. if (menu == m_current_menu)
  173. m_current_menu = nullptr;
  174. if (menu->menu_window())
  175. menu->menu_window()->set_visible(false);
  176. m_open_menu_stack.remove_first_matching([&](auto& entry) {
  177. return entry == menu;
  178. });
  179. }
  180. refresh();
  181. }
  182. static void collect_menu_subtree(WSMenu& menu, Vector<WSMenu*>& menus)
  183. {
  184. menus.append(&menu);
  185. for (int i = 0; i < menu.item_count(); ++i) {
  186. auto& item = menu.item(i);
  187. if (!item.is_submenu())
  188. continue;
  189. collect_menu_subtree(*const_cast<WSMenuItem&>(item).submenu(), menus);
  190. }
  191. }
  192. void WSMenuManager::close_menu_and_descendants(WSMenu& menu)
  193. {
  194. Vector<WSMenu*> menus_to_close;
  195. collect_menu_subtree(menu, menus_to_close);
  196. close_menus(menus_to_close);
  197. }
  198. void WSMenuManager::set_current_menu(WSMenu* menu, bool is_submenu)
  199. {
  200. if (!is_submenu && m_current_menu)
  201. m_current_menu->close();
  202. if (menu)
  203. m_current_menu = menu->make_weak_ptr();
  204. if (!is_submenu) {
  205. close_everyone();
  206. if (menu)
  207. m_open_menu_stack.append(menu->make_weak_ptr());
  208. } else {
  209. m_open_menu_stack.append(menu->make_weak_ptr());
  210. }
  211. }
  212. void WSMenuManager::close_bar()
  213. {
  214. close_everyone();
  215. m_bar_open = false;
  216. }
  217. void WSMenuManager::add_applet(WSWindow& applet)
  218. {
  219. int right_edge_x = m_username_rect.left() - 4;
  220. for (auto& existing_applet : m_applets) {
  221. if (existing_applet)
  222. right_edge_x = existing_applet->rect_in_menubar().x() - 4;
  223. }
  224. Rect new_applet_rect(right_edge_x - applet.size().width(), 0, applet.size().width(), applet.size().height());
  225. Rect dummy_menubar_rect(0, 0, 0, 18);
  226. new_applet_rect.center_vertically_within(dummy_menubar_rect);
  227. applet.set_rect_in_menubar(new_applet_rect);
  228. m_applets.append(applet.make_weak_ptr());
  229. }
  230. void WSMenuManager::remove_applet(WSWindow& applet)
  231. {
  232. m_applets.remove_first_matching([&](auto& entry) {
  233. return &applet == entry.ptr();
  234. });
  235. }
  236. void WSMenuManager::draw_applet(const WSWindow& applet)
  237. {
  238. if (!applet.backing_store())
  239. return;
  240. Painter painter(*window().backing_store());
  241. painter.blit(applet.rect_in_menubar().location(), *applet.backing_store(), applet.backing_store()->rect());
  242. }
  243. void WSMenuManager::invalidate_applet(const WSWindow& applet, const Rect& rect)
  244. {
  245. // FIXME: This should only invalidate the applet's own rect, not the whole menubar.
  246. (void)rect;
  247. draw_applet(applet);
  248. window().invalidate();
  249. }
  250. Rect WSMenuManager::menubar_rect() const
  251. {
  252. return { 0, 0, WSScreen::the().rect().width(), 18 };
  253. }