WSMenuManager.cpp 9.6 KB

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