WSMenuManager.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include <LibAudio/AClientConnection.h>
  2. #include <LibCore/CTimer.h>
  3. #include <LibDraw/Font.h>
  4. #include <LibDraw/Painter.h>
  5. #include <WindowServer/WSMenuManager.h>
  6. #include <WindowServer/WSWindowManager.h>
  7. #include <time.h>
  8. #include <unistd.h>
  9. WSMenuManager::WSMenuManager()
  10. {
  11. m_audio_client = make<AClientConnection>();
  12. m_unmuted_bitmap = GraphicsBitmap::load_from_file("/res/icons/audio-unmuted.png");
  13. m_muted_bitmap = GraphicsBitmap::load_from_file("/res/icons/audio-muted.png");
  14. m_username = getlogin();
  15. m_needs_window_resize = false;
  16. m_timer = CTimer::construct(300, [this] {
  17. static time_t last_update_time;
  18. time_t now = time(nullptr);
  19. if (now != last_update_time || m_cpu_monitor.is_dirty()) {
  20. tick_clock();
  21. last_update_time = now;
  22. m_cpu_monitor.set_dirty(false);
  23. }
  24. });
  25. }
  26. WSMenuManager::~WSMenuManager()
  27. {
  28. }
  29. void WSMenuManager::setup()
  30. {
  31. m_window = WSWindow::construct(*this, WSWindowType::Menubar);
  32. m_window->set_rect(WSWindowManager::the().menubar_rect());
  33. }
  34. bool WSMenuManager::is_open(const WSMenu& menu) const
  35. {
  36. for (int i = 0; i < m_open_menu_stack.size(); ++i) {
  37. if (&menu == m_open_menu_stack[i].ptr())
  38. return true;
  39. }
  40. return false;
  41. }
  42. void WSMenuManager::draw()
  43. {
  44. auto& wm = WSWindowManager::the();
  45. auto menubar_rect = wm.menubar_rect();
  46. if (m_needs_window_resize) {
  47. m_window->set_rect(menubar_rect);
  48. m_needs_window_resize = false;
  49. }
  50. Painter painter(*window().backing_store());
  51. painter.fill_rect(menubar_rect, Color::WarmGray);
  52. painter.draw_line({ 0, menubar_rect.bottom() }, { menubar_rect.right(), menubar_rect.bottom() }, Color::MidGray);
  53. int index = 0;
  54. wm.for_each_active_menubar_menu([&](WSMenu& menu) {
  55. Color text_color = Color::Black;
  56. if (is_open(menu)) {
  57. painter.fill_rect(menu.rect_in_menubar(), Color::from_rgb(0xad714f));
  58. painter.draw_rect(menu.rect_in_menubar(), Color::from_rgb(0x793016));
  59. text_color = Color::White;
  60. }
  61. painter.draw_text(
  62. menu.text_rect_in_menubar(),
  63. menu.name(),
  64. index == 1 ? wm.app_menu_font() : wm.menu_font(),
  65. TextAlignment::CenterLeft,
  66. text_color);
  67. ++index;
  68. return true;
  69. });
  70. int username_width = Font::default_bold_font().width(m_username);
  71. // FIXME: This rect should only be computed once.
  72. Rect username_rect {
  73. menubar_rect.right() - wm.menubar_menu_margin() / 2 - Font::default_bold_font().width(m_username),
  74. menubar_rect.y(),
  75. username_width,
  76. menubar_rect.height()
  77. };
  78. painter.draw_text(username_rect, m_username, Font::default_bold_font(), TextAlignment::CenterRight, Color::Black);
  79. time_t now = time(nullptr);
  80. auto* tm = localtime(&now);
  81. auto time_text = String::format("%4u-%02u-%02u %02u:%02u:%02u",
  82. tm->tm_year + 1900,
  83. tm->tm_mon + 1,
  84. tm->tm_mday,
  85. tm->tm_hour,
  86. tm->tm_min,
  87. tm->tm_sec);
  88. int time_width = wm.font().width(time_text);
  89. // FIXME: This rect should only be computed once.
  90. Rect time_rect {
  91. username_rect.left() - wm.menubar_menu_margin() / 2 - time_width,
  92. menubar_rect.y(),
  93. time_width,
  94. menubar_rect.height()
  95. };
  96. painter.draw_text(time_rect, time_text, wm.font(), TextAlignment::CenterRight, Color::Black);
  97. // FIXME: This rect should only be computed once.
  98. Rect cpu_rect { time_rect.right() - wm.font().width(time_text) - m_cpu_monitor.capacity() - 10, time_rect.y() + 1, m_cpu_monitor.capacity(), time_rect.height() - 2 };
  99. m_cpu_monitor.paint(painter, cpu_rect);
  100. // FIXME: This rect should only be computed once.
  101. m_audio_rect = { cpu_rect.left() - 20, cpu_rect.y(), 12, 16 };
  102. auto& audio_bitmap = m_audio_muted ? *m_muted_bitmap : *m_unmuted_bitmap;
  103. painter.blit(m_audio_rect.location(), audio_bitmap, audio_bitmap.rect());
  104. }
  105. void WSMenuManager::tick_clock()
  106. {
  107. refresh();
  108. }
  109. void WSMenuManager::refresh()
  110. {
  111. if (!m_window)
  112. return;
  113. draw();
  114. window().invalidate();
  115. }
  116. void WSMenuManager::event(CEvent& event)
  117. {
  118. if (WSWindowManager::the().active_window_is_modal())
  119. return CObject::event(event);
  120. if (event.type() == WSEvent::MouseMove || event.type() == WSEvent::MouseUp || event.type() == WSEvent::MouseDown || event.type() == WSEvent::MouseWheel) {
  121. auto& mouse_event = static_cast<WSMouseEvent&>(event);
  122. WSWindowManager::the().for_each_active_menubar_menu([&](WSMenu& menu) {
  123. if (menu.rect_in_menubar().contains(mouse_event.position())) {
  124. handle_menu_mouse_event(menu, mouse_event);
  125. return false;
  126. }
  127. return true;
  128. });
  129. if (mouse_event.type() == WSEvent::MouseDown
  130. && mouse_event.button() == MouseButton::Left
  131. && m_audio_rect.contains(mouse_event.position())) {
  132. // FIXME: This should listen for notifications from the AudioServer, once those actually exist.
  133. // Right now, we won't notice if another program changes the AudioServer muted state.
  134. m_audio_muted = !m_audio_muted;
  135. m_audio_client->set_muted(m_audio_muted);
  136. draw();
  137. m_window->invalidate();
  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. auto& menu_window = menu.ensure_menu_window();
  158. menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() + 2 });
  159. menu_window.set_visible(true);
  160. }
  161. set_current_menu(&menu);
  162. refresh();
  163. return;
  164. }
  165. if (!m_bar_open)
  166. close_everyone();
  167. }
  168. void WSMenuManager::set_needs_window_resize()
  169. {
  170. m_needs_window_resize = true;
  171. }
  172. void WSMenuManager::close_everyone()
  173. {
  174. for (auto& menu : m_open_menu_stack) {
  175. if (menu && menu->menu_window())
  176. menu->menu_window()->set_visible(false);
  177. }
  178. m_open_menu_stack.clear();
  179. m_current_menu = nullptr;
  180. refresh();
  181. }
  182. void WSMenuManager::close_everyone_not_in_lineage(WSMenu& menu)
  183. {
  184. Vector<WSMenu*> menus_to_close;
  185. for (auto& open_menu : m_open_menu_stack) {
  186. if (!open_menu)
  187. continue;
  188. if (&menu == open_menu.ptr() || open_menu->is_menu_ancestor_of(menu))
  189. continue;
  190. menus_to_close.append(open_menu);
  191. }
  192. close_menus(menus_to_close);
  193. }
  194. void WSMenuManager::close_menus(const Vector<WSMenu*>& menus)
  195. {
  196. for (auto& menu : menus) {
  197. if (menu == m_current_menu)
  198. m_current_menu = nullptr;
  199. if (menu->menu_window())
  200. menu->menu_window()->set_visible(false);
  201. m_open_menu_stack.remove_first_matching([&](auto& entry) {
  202. return entry == menu;
  203. });
  204. }
  205. refresh();
  206. }
  207. static void collect_menu_subtree(WSMenu& menu, Vector<WSMenu*>& menus)
  208. {
  209. menus.append(&menu);
  210. for (int i = 0; i < menu.item_count(); ++i) {
  211. auto& item = menu.item(i);
  212. if (!item.is_submenu())
  213. continue;
  214. collect_menu_subtree(*const_cast<WSMenuItem&>(item).submenu(), menus);
  215. }
  216. }
  217. void WSMenuManager::close_menu_and_descendants(WSMenu& menu)
  218. {
  219. Vector<WSMenu*> menus_to_close;
  220. collect_menu_subtree(menu, menus_to_close);
  221. close_menus(menus_to_close);
  222. }
  223. void WSMenuManager::set_current_menu(WSMenu* menu, bool is_submenu)
  224. {
  225. if (!is_submenu && m_current_menu)
  226. m_current_menu->close();
  227. if (menu)
  228. m_current_menu = menu->make_weak_ptr();
  229. if (!is_submenu) {
  230. close_everyone();
  231. if (menu)
  232. m_open_menu_stack.append(menu->make_weak_ptr());
  233. } else {
  234. m_open_menu_stack.append(menu->make_weak_ptr());
  235. }
  236. }
  237. void WSMenuManager::close_bar()
  238. {
  239. close_everyone();
  240. m_bar_open = false;
  241. }