WSMenuManager.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include <LibCore/CTimer.h>
  2. #include <LibDraw/Font.h>
  3. #include <LibDraw/Painter.h>
  4. #include <WindowServer/WSMenuManager.h>
  5. #include <WindowServer/WSWindowManager.h>
  6. #include <time.h>
  7. #include <unistd.h>
  8. WSMenuManager::WSMenuManager()
  9. {
  10. m_username = getlogin();
  11. m_needs_window_resize = false;
  12. m_timer = CTimer::construct(300, [this] {
  13. static time_t last_update_time;
  14. time_t now = time(nullptr);
  15. if (now != last_update_time || m_cpu_monitor.is_dirty()) {
  16. tick_clock();
  17. last_update_time = now;
  18. m_cpu_monitor.set_dirty(false);
  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(WSWindowManager::the().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 = wm.menubar_rect();
  42. if (m_needs_window_resize) {
  43. m_window->set_rect(menubar_rect);
  44. m_needs_window_resize = false;
  45. }
  46. Painter painter(*window().backing_store());
  47. painter.fill_rect(menubar_rect, Color::WarmGray);
  48. painter.draw_line({ 0, menubar_rect.bottom() }, { menubar_rect.right(), menubar_rect.bottom() }, Color::MidGray);
  49. int index = 0;
  50. wm.for_each_active_menubar_menu([&](WSMenu& menu) {
  51. Color text_color = Color::Black;
  52. if (is_open(menu)) {
  53. painter.fill_rect(menu.rect_in_menubar(), Color::from_rgb(0xad714f));
  54. painter.draw_rect(menu.rect_in_menubar(), Color::from_rgb(0x793016));
  55. text_color = Color::White;
  56. }
  57. painter.draw_text(
  58. menu.text_rect_in_menubar(),
  59. menu.name(),
  60. index == 1 ? wm.app_menu_font() : wm.menu_font(),
  61. TextAlignment::CenterLeft,
  62. text_color);
  63. ++index;
  64. return true;
  65. });
  66. int username_width = Font::default_bold_font().width(m_username);
  67. Rect username_rect {
  68. menubar_rect.right() - wm.menubar_menu_margin() / 2 - Font::default_bold_font().width(m_username),
  69. menubar_rect.y(),
  70. username_width,
  71. menubar_rect.height()
  72. };
  73. painter.draw_text(username_rect, m_username, Font::default_bold_font(), TextAlignment::CenterRight, Color::Black);
  74. time_t now = time(nullptr);
  75. auto* tm = localtime(&now);
  76. auto time_text = String::format("%4u-%02u-%02u %02u:%02u:%02u",
  77. tm->tm_year + 1900,
  78. tm->tm_mon + 1,
  79. tm->tm_mday,
  80. tm->tm_hour,
  81. tm->tm_min,
  82. tm->tm_sec);
  83. int time_width = wm.font().width(time_text);
  84. Rect time_rect {
  85. username_rect.left() - wm.menubar_menu_margin() / 2 - time_width,
  86. menubar_rect.y(),
  87. time_width,
  88. menubar_rect.height()
  89. };
  90. painter.draw_text(time_rect, time_text, wm.font(), TextAlignment::CenterRight, Color::Black);
  91. 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 };
  92. m_cpu_monitor.paint(painter, cpu_rect);
  93. }
  94. void WSMenuManager::tick_clock()
  95. {
  96. refresh();
  97. }
  98. void WSMenuManager::refresh()
  99. {
  100. if (!m_window)
  101. return;
  102. draw();
  103. window().invalidate();
  104. }
  105. void WSMenuManager::event(CEvent& event)
  106. {
  107. if (WSWindowManager::the().active_window_is_modal())
  108. return CObject::event(event);
  109. if (event.type() == WSEvent::MouseMove || event.type() == WSEvent::MouseUp || event.type() == WSEvent::MouseDown || event.type() == WSEvent::MouseWheel) {
  110. auto& mouse_event = static_cast<WSMouseEvent&>(event);
  111. WSWindowManager::the().for_each_active_menubar_menu([&](WSMenu& menu) {
  112. if (menu.rect_in_menubar().contains(mouse_event.position())) {
  113. handle_menu_mouse_event(menu, mouse_event);
  114. return false;
  115. }
  116. return true;
  117. });
  118. }
  119. return CObject::event(event);
  120. }
  121. void WSMenuManager::handle_menu_mouse_event(WSMenu& menu, const WSMouseEvent& event)
  122. {
  123. auto& wm = WSWindowManager::the();
  124. bool is_hover_with_any_menu_open = event.type() == WSMouseEvent::MouseMove
  125. && !m_open_menu_stack.is_empty()
  126. && (m_open_menu_stack.first()->menubar() || m_open_menu_stack.first() == wm.system_menu());
  127. bool is_mousedown_with_left_button = event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left;
  128. bool should_open_menu = &menu != wm.current_menu() && (is_hover_with_any_menu_open || is_mousedown_with_left_button);
  129. if (should_open_menu) {
  130. if (wm.current_menu() == &menu)
  131. return;
  132. wm.close_current_menu();
  133. if (!menu.is_empty()) {
  134. auto& menu_window = menu.ensure_menu_window();
  135. menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() + 2 });
  136. menu_window.set_visible(true);
  137. }
  138. wm.set_current_menu(&menu);
  139. refresh();
  140. return;
  141. }
  142. if (event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left) {
  143. wm.close_current_menu();
  144. return;
  145. }
  146. }
  147. void WSMenuManager::set_needs_window_resize()
  148. {
  149. m_needs_window_resize = true;
  150. }