WSMenuManager.cpp 9.6 KB

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