WSMenuManager.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_timer = CTimer::construct(300, [this] {
  12. static time_t last_update_time;
  13. time_t now = time(nullptr);
  14. if (now != last_update_time || m_cpu_monitor.is_dirty()) {
  15. tick_clock();
  16. last_update_time = now;
  17. m_cpu_monitor.set_dirty(false);
  18. }
  19. });
  20. }
  21. WSMenuManager::~WSMenuManager()
  22. {
  23. }
  24. void WSMenuManager::setup()
  25. {
  26. m_window = make<WSWindow>(*this, WSWindowType::Menubar);
  27. m_window->set_rect(WSWindowManager::the().menubar_rect());
  28. }
  29. bool WSMenuManager::is_open(const WSMenu& menu) const
  30. {
  31. for (int i = 0; i < m_open_menu_stack.size(); ++i) {
  32. if (&menu == m_open_menu_stack[i].ptr())
  33. return true;
  34. }
  35. return false;
  36. }
  37. void WSMenuManager::draw()
  38. {
  39. auto& wm = WSWindowManager::the();
  40. auto menubar_rect = wm.menubar_rect();
  41. Painter painter(*window().backing_store());
  42. painter.fill_rect(menubar_rect, Color::WarmGray);
  43. painter.draw_line({ 0, menubar_rect.bottom() }, { menubar_rect.right(), menubar_rect.bottom() }, Color::MidGray);
  44. int index = 0;
  45. wm.for_each_active_menubar_menu([&](WSMenu& menu) {
  46. Color text_color = Color::Black;
  47. if (is_open(menu)) {
  48. painter.fill_rect(menu.rect_in_menubar(), Color::from_rgb(0xad714f));
  49. painter.draw_rect(menu.rect_in_menubar(), Color::from_rgb(0x793016));
  50. text_color = Color::White;
  51. }
  52. painter.draw_text(
  53. menu.text_rect_in_menubar(),
  54. menu.name(),
  55. index == 1 ? wm.app_menu_font() : wm.menu_font(),
  56. TextAlignment::CenterLeft,
  57. text_color);
  58. ++index;
  59. return true;
  60. });
  61. int username_width = Font::default_bold_font().width(m_username);
  62. Rect username_rect {
  63. menubar_rect.right() - wm.menubar_menu_margin() / 2 - Font::default_bold_font().width(m_username),
  64. menubar_rect.y(),
  65. username_width,
  66. menubar_rect.height()
  67. };
  68. painter.draw_text(username_rect, m_username, Font::default_bold_font(), TextAlignment::CenterRight, Color::Black);
  69. time_t now = time(nullptr);
  70. auto* tm = localtime(&now);
  71. auto time_text = String::format("%4u-%02u-%02u %02u:%02u:%02u",
  72. tm->tm_year + 1900,
  73. tm->tm_mon + 1,
  74. tm->tm_mday,
  75. tm->tm_hour,
  76. tm->tm_min,
  77. tm->tm_sec);
  78. int time_width = wm.font().width(time_text);
  79. Rect time_rect {
  80. username_rect.left() - wm.menubar_menu_margin() / 2 - time_width,
  81. menubar_rect.y(),
  82. time_width,
  83. menubar_rect.height()
  84. };
  85. painter.draw_text(time_rect, time_text, wm.font(), TextAlignment::CenterRight, Color::Black);
  86. 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 };
  87. m_cpu_monitor.paint(painter, cpu_rect);
  88. }
  89. void WSMenuManager::tick_clock()
  90. {
  91. refresh();
  92. }
  93. void WSMenuManager::refresh()
  94. {
  95. if (!m_window)
  96. return;
  97. draw();
  98. window().invalidate();
  99. }
  100. void WSMenuManager::event(CEvent& event)
  101. {
  102. if (WSWindowManager::the().active_window_is_modal())
  103. return CObject::event(event);
  104. if (event.type() == WSEvent::MouseMove || event.type() == WSEvent::MouseUp || event.type() == WSEvent::MouseDown || event.type() == WSEvent::MouseWheel) {
  105. auto& mouse_event = static_cast<WSMouseEvent&>(event);
  106. WSWindowManager::the().for_each_active_menubar_menu([&](WSMenu& menu) {
  107. if (menu.rect_in_menubar().contains(mouse_event.position())) {
  108. handle_menu_mouse_event(menu, mouse_event);
  109. return false;
  110. }
  111. return true;
  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 != wm.current_menu() && (is_hover_with_any_menu_open || is_mousedown_with_left_button);
  124. if (should_open_menu) {
  125. if (wm.current_menu() == &menu)
  126. return;
  127. wm.close_current_menu();
  128. if (!menu.is_empty()) {
  129. auto& menu_window = menu.ensure_menu_window();
  130. menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() + 2 });
  131. menu_window.set_visible(true);
  132. }
  133. wm.set_current_menu(&menu);
  134. refresh();
  135. return;
  136. }
  137. if (event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left) {
  138. wm.close_current_menu();
  139. return;
  140. }
  141. }