MenuManager.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Shannon Booth <shannon.ml.booth@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/Badge.h>
  28. #include <AK/Debug.h>
  29. #include <AK/QuickSort.h>
  30. #include <LibCore/DirIterator.h>
  31. #include <LibGfx/Font.h>
  32. #include <LibGfx/Painter.h>
  33. #include <WindowServer/AppletManager.h>
  34. #include <WindowServer/MenuManager.h>
  35. #include <WindowServer/Screen.h>
  36. #include <WindowServer/WindowManager.h>
  37. #include <unistd.h>
  38. namespace WindowServer {
  39. static MenuManager* s_the;
  40. static constexpr int s_search_timeout = 3000;
  41. MenuManager& MenuManager::the()
  42. {
  43. ASSERT(s_the);
  44. return *s_the;
  45. }
  46. MenuManager::MenuManager()
  47. {
  48. s_the = this;
  49. m_needs_window_resize = true;
  50. // NOTE: This ensures that the system menu has the correct dimensions.
  51. set_current_menubar(nullptr);
  52. m_window = Window::construct(*this, WindowType::Menubar);
  53. m_window->set_rect(menubar_rect());
  54. m_search_timer = Core::Timer::create_single_shot(0, [this] {
  55. m_current_search.clear();
  56. });
  57. }
  58. MenuManager::~MenuManager()
  59. {
  60. }
  61. bool MenuManager::is_open(const Menu& menu) const
  62. {
  63. for (size_t i = 0; i < m_open_menu_stack.size(); ++i) {
  64. if (&menu == m_open_menu_stack[i].ptr())
  65. return true;
  66. }
  67. return false;
  68. }
  69. void MenuManager::draw()
  70. {
  71. auto& wm = WindowManager::the();
  72. auto palette = wm.palette();
  73. auto menubar_rect = this->menubar_rect();
  74. if (m_needs_window_resize) {
  75. m_window->set_rect(menubar_rect);
  76. AppletManager::the().calculate_applet_rects(window());
  77. m_needs_window_resize = false;
  78. }
  79. Gfx::Painter painter(*window().backing_store());
  80. painter.fill_rect(menubar_rect, palette.window());
  81. painter.draw_line({ 0, menubar_rect.bottom() - 1 }, { menubar_rect.right(), menubar_rect.bottom() - 1 }, palette.threed_shadow1());
  82. painter.draw_line({ 0, menubar_rect.bottom() }, { menubar_rect.right(), menubar_rect.bottom() }, palette.threed_shadow2());
  83. for_each_active_menubar_menu([&](Menu& menu) {
  84. Color text_color = palette.window_text();
  85. if (is_open(menu) && &menu == m_current_menu_bar_menu) {
  86. painter.fill_rect(menu.rect_in_menubar(), palette.menu_selection());
  87. painter.draw_rect(menu.rect_in_menubar(), palette.menu_selection().darkened());
  88. text_color = palette.menu_selection_text();
  89. }
  90. painter.draw_text(
  91. menu.text_rect_in_menubar(),
  92. menu.name(),
  93. menu.title_font(),
  94. Gfx::TextAlignment::CenterLeft,
  95. text_color);
  96. //painter.draw_rect(menu.text_rect_in_menubar(), Color::Magenta);
  97. return IterationDecision::Continue;
  98. });
  99. AppletManager::the().draw();
  100. }
  101. void MenuManager::refresh()
  102. {
  103. if (!m_window)
  104. return;
  105. draw();
  106. window().invalidate();
  107. }
  108. void MenuManager::event(Core::Event& event)
  109. {
  110. if (static_cast<Event&>(event).is_mouse_event()) {
  111. handle_mouse_event(static_cast<MouseEvent&>(event));
  112. return;
  113. }
  114. if (static_cast<Event&>(event).is_key_event()) {
  115. auto& key_event = static_cast<const KeyEvent&>(event);
  116. if (key_event.type() == Event::KeyUp && key_event.key() == Key_Escape) {
  117. close_everyone();
  118. return;
  119. }
  120. if (key_event.key() == Key_Backspace) {
  121. m_current_search.clear();
  122. return;
  123. }
  124. if (m_current_menu && event.type() == Event::KeyDown
  125. && ((key_event.key() >= Key_A && key_event.key() <= Key_Z)
  126. || (key_event.key() >= Key_0 && key_event.key() <= Key_9))) {
  127. m_current_search.append_code_point(key_event.code_point());
  128. m_search_timer->restart(s_search_timeout);
  129. for (int i = 0; i < m_current_menu->item_count(); ++i) {
  130. auto text = m_current_menu->item(i).text();
  131. if (text.to_lowercase().starts_with(m_current_search.to_string().to_lowercase())) {
  132. m_current_menu->set_hovered_item(i);
  133. return;
  134. }
  135. }
  136. return;
  137. }
  138. if (event.type() == Event::KeyDown) {
  139. if (key_event.key() == Key_Left) {
  140. auto it = m_open_menu_stack.find_if([&](const auto& other) { return m_current_menu == other.ptr(); });
  141. ASSERT(!it.is_end());
  142. // Going "back" a menu should be the previous menu in the stack
  143. if (it.index() > 0)
  144. set_current_menu(m_open_menu_stack.at(it.index() - 1));
  145. else {
  146. if (m_current_menu->hovered_item())
  147. m_current_menu->set_hovered_item(-1);
  148. else {
  149. auto* target_menu = previous_menu(m_current_menu);
  150. if (target_menu)
  151. open_menu(*target_menu, true);
  152. }
  153. }
  154. close_everyone_not_in_lineage(*m_current_menu);
  155. return;
  156. }
  157. if (key_event.key() == Key_Right) {
  158. auto hovered_item = m_current_menu->hovered_item();
  159. if (hovered_item && hovered_item->is_submenu())
  160. m_current_menu->descend_into_submenu_at_hovered_item();
  161. else if (m_open_menu_stack.size() <= 1) {
  162. auto* target_menu = next_menu(m_current_menu);
  163. if (target_menu) {
  164. open_menu(*target_menu, true);
  165. close_everyone_not_in_lineage(*target_menu);
  166. }
  167. }
  168. return;
  169. }
  170. if (key_event.key() == Key_Return) {
  171. auto hovered_item = m_current_menu->hovered_item();
  172. if (!hovered_item || !hovered_item->is_enabled())
  173. return;
  174. if (hovered_item->is_submenu())
  175. m_current_menu->descend_into_submenu_at_hovered_item();
  176. else
  177. m_current_menu->open_hovered_item();
  178. return;
  179. }
  180. m_current_menu->dispatch_event(event);
  181. }
  182. }
  183. return Core::Object::event(event);
  184. }
  185. void MenuManager::handle_mouse_event(MouseEvent& mouse_event)
  186. {
  187. auto* active_window = WindowManager::the().active_window();
  188. bool handled_menubar_event = false;
  189. for_each_active_menubar_menu([&](Menu& menu) {
  190. if (menu.rect_in_menubar().contains(mouse_event.position())) {
  191. handled_menubar_event = &menu == m_system_menu || !active_window || !active_window->is_modal();
  192. if (handled_menubar_event)
  193. handle_menu_mouse_event(menu, mouse_event);
  194. return IterationDecision::Break;
  195. }
  196. return IterationDecision::Continue;
  197. });
  198. if (handled_menubar_event)
  199. return;
  200. if (has_open_menu()) {
  201. auto* topmost_menu = m_open_menu_stack.last().ptr();
  202. ASSERT(topmost_menu);
  203. auto* window = topmost_menu->menu_window();
  204. if (!window) {
  205. dbgln("MenuManager::handle_mouse_event: No menu window");
  206. return;
  207. }
  208. ASSERT(window->is_visible());
  209. bool event_is_inside_current_menu = window->rect().contains(mouse_event.position());
  210. if (event_is_inside_current_menu) {
  211. WindowManager::the().set_hovered_window(window);
  212. auto translated_event = mouse_event.translated(-window->position());
  213. WindowManager::the().deliver_mouse_event(*window, translated_event, true);
  214. return;
  215. }
  216. if (topmost_menu->hovered_item())
  217. topmost_menu->clear_hovered_item();
  218. if (mouse_event.type() == Event::MouseDown || mouse_event.type() == Event::MouseUp) {
  219. auto* window_menu_of = topmost_menu->window_menu_of();
  220. if (window_menu_of) {
  221. bool event_is_inside_taskbar_button = window_menu_of->taskbar_rect().contains(mouse_event.position());
  222. if (event_is_inside_taskbar_button && !topmost_menu->is_window_menu_open()) {
  223. topmost_menu->set_window_menu_open(true);
  224. return;
  225. }
  226. }
  227. if (mouse_event.type() == Event::MouseDown) {
  228. for (auto& menu : m_open_menu_stack) {
  229. if (!menu)
  230. continue;
  231. if (!menu->menu_window()->rect().contains(mouse_event.position()))
  232. continue;
  233. return;
  234. }
  235. close_bar();
  236. topmost_menu->set_window_menu_open(false);
  237. }
  238. }
  239. if (mouse_event.type() == Event::MouseMove) {
  240. for (auto& menu : m_open_menu_stack) {
  241. if (!menu)
  242. continue;
  243. if (!menu->menu_window()->rect().contains(mouse_event.position()))
  244. continue;
  245. WindowManager::the().set_hovered_window(menu->menu_window());
  246. auto translated_event = mouse_event.translated(-menu->menu_window()->position());
  247. WindowManager::the().deliver_mouse_event(*menu->menu_window(), translated_event, true);
  248. break;
  249. }
  250. }
  251. return;
  252. }
  253. AppletManager::the().dispatch_event(static_cast<Event&>(mouse_event));
  254. }
  255. void MenuManager::handle_menu_mouse_event(Menu& menu, const MouseEvent& event)
  256. {
  257. bool is_hover_with_any_menu_open = event.type() == MouseEvent::MouseMove
  258. && has_open_menu() && m_current_menu_bar_menu
  259. && (m_open_menu_stack.first()->menubar() || m_open_menu_stack.first() == m_system_menu.ptr());
  260. bool is_mousedown_with_left_button = event.type() == MouseEvent::MouseDown && event.button() == MouseButton::Left;
  261. bool should_open_menu = (&menu != m_current_menu || !m_current_menu_bar_menu) && (is_hover_with_any_menu_open || is_mousedown_with_left_button);
  262. if (is_mousedown_with_left_button)
  263. m_bar_open = !m_bar_open;
  264. if (should_open_menu && m_bar_open) {
  265. close_everyone();
  266. open_menu(menu, true);
  267. return;
  268. }
  269. if (!m_bar_open && m_current_menu_bar_menu)
  270. close_everyone();
  271. }
  272. void MenuManager::set_needs_window_resize()
  273. {
  274. m_needs_window_resize = true;
  275. }
  276. void MenuManager::close_all_menus_from_client(Badge<ClientConnection>, ClientConnection& client)
  277. {
  278. if (!has_open_menu())
  279. return;
  280. if (m_open_menu_stack.first()->client() != &client)
  281. return;
  282. close_everyone();
  283. }
  284. void MenuManager::close_everyone()
  285. {
  286. for (auto& menu : m_open_menu_stack) {
  287. ASSERT(menu);
  288. if (menu->menu_window())
  289. menu->menu_window()->set_visible(false);
  290. menu->clear_hovered_item();
  291. }
  292. m_open_menu_stack.clear();
  293. m_current_search.clear();
  294. clear_current_menu();
  295. refresh();
  296. }
  297. void MenuManager::close_everyone_not_in_lineage(Menu& menu)
  298. {
  299. Vector<Menu*> menus_to_close;
  300. for (auto& open_menu : m_open_menu_stack) {
  301. if (!open_menu)
  302. continue;
  303. if (&menu == open_menu.ptr() || open_menu->is_menu_ancestor_of(menu))
  304. continue;
  305. menus_to_close.append(open_menu);
  306. }
  307. close_menus(menus_to_close);
  308. }
  309. void MenuManager::close_menus(const Vector<Menu*>& menus)
  310. {
  311. for (auto& menu : menus) {
  312. if (menu == m_current_menu)
  313. clear_current_menu();
  314. if (menu->menu_window())
  315. menu->menu_window()->set_visible(false);
  316. menu->clear_hovered_item();
  317. m_open_menu_stack.remove_first_matching([&](auto& entry) {
  318. return entry == menu;
  319. });
  320. }
  321. refresh();
  322. }
  323. static void collect_menu_subtree(Menu& menu, Vector<Menu*>& menus)
  324. {
  325. menus.append(&menu);
  326. for (int i = 0; i < menu.item_count(); ++i) {
  327. auto& item = menu.item(i);
  328. if (!item.is_submenu())
  329. continue;
  330. collect_menu_subtree(*item.submenu(), menus);
  331. }
  332. }
  333. void MenuManager::close_menu_and_descendants(Menu& menu)
  334. {
  335. Vector<Menu*> menus_to_close;
  336. collect_menu_subtree(menu, menus_to_close);
  337. close_menus(menus_to_close);
  338. }
  339. void MenuManager::open_menu(Menu& menu, bool from_menu_bar, bool as_current_menu)
  340. {
  341. if (from_menu_bar)
  342. m_current_menu_bar_menu = &menu;
  343. if (is_open(menu)) {
  344. if (as_current_menu || current_menu() != &menu) {
  345. // This menu is already open. If requested, or if the current
  346. // window doesn't match this one, then set it to this
  347. set_current_menu(&menu);
  348. }
  349. return;
  350. }
  351. if (!menu.is_empty()) {
  352. menu.redraw_if_theme_changed();
  353. bool should_update_position = from_menu_bar;
  354. if (!menu.menu_window()) {
  355. should_update_position = true;
  356. menu.ensure_menu_window();
  357. }
  358. if (should_update_position)
  359. menu.menu_window()->move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() + 2 });
  360. menu.menu_window()->set_visible(true);
  361. }
  362. if (m_open_menu_stack.find_if([&menu](auto& other) { return &menu == other.ptr(); }).is_end())
  363. m_open_menu_stack.append(menu);
  364. if (as_current_menu || !current_menu()) {
  365. // Only make this menu the current menu if requested, or if no
  366. // other menu is current
  367. set_current_menu(&menu);
  368. }
  369. refresh();
  370. }
  371. void MenuManager::clear_current_menu()
  372. {
  373. Menu* previous_current_menu = m_current_menu;
  374. m_current_menu = nullptr;
  375. m_current_menu_bar_menu = nullptr;
  376. if (previous_current_menu) {
  377. // When closing the last menu, restore the previous active input window
  378. auto& wm = WindowManager::the();
  379. wm.restore_active_input_window(m_previous_input_window);
  380. }
  381. }
  382. void MenuManager::set_current_menu(Menu* menu)
  383. {
  384. if (!menu) {
  385. clear_current_menu();
  386. return;
  387. }
  388. ASSERT(is_open(*menu));
  389. if (menu == m_current_menu) {
  390. return;
  391. }
  392. m_current_search.clear();
  393. Menu* previous_current_menu = m_current_menu;
  394. m_current_menu = menu;
  395. auto& wm = WindowManager::the();
  396. if (!previous_current_menu) {
  397. // When opening the first menu, store the current active input window
  398. if (auto* active_input = wm.active_input_window())
  399. m_previous_input_window = *active_input;
  400. else
  401. m_previous_input_window = nullptr;
  402. }
  403. wm.set_active_input_window(m_current_menu->menu_window());
  404. }
  405. void MenuManager::close_bar()
  406. {
  407. close_everyone();
  408. m_bar_open = false;
  409. }
  410. Gfx::IntRect MenuManager::menubar_rect() const
  411. {
  412. return { 0, 0, Screen::the().rect().width(), 19 };
  413. }
  414. void MenuManager::set_current_menubar(MenuBar* menubar)
  415. {
  416. if (menubar)
  417. m_current_menubar = *menubar;
  418. else
  419. m_current_menubar = nullptr;
  420. dbgln_if(MENUS_DEBUG, "[WM] Current menubar is now {}", menubar);
  421. Gfx::IntPoint next_menu_location { MenuManager::menubar_menu_margin() / 2, 0 };
  422. for_each_active_menubar_menu([&](Menu& menu) {
  423. int text_width = menu.title_font().width(menu.name());
  424. menu.set_rect_in_menubar({ next_menu_location.x() - MenuManager::menubar_menu_margin() / 2, 0, text_width + MenuManager::menubar_menu_margin(), menubar_rect().height() - 1 });
  425. Gfx::IntRect text_rect { next_menu_location.translated(0, 1), { text_width, menubar_rect().height() - 3 } };
  426. menu.set_text_rect_in_menubar(text_rect);
  427. next_menu_location.move_by(menu.rect_in_menubar().width(), 0);
  428. return IterationDecision::Continue;
  429. });
  430. refresh();
  431. }
  432. void MenuManager::close_menubar(MenuBar& menubar)
  433. {
  434. if (current_menubar() == &menubar)
  435. set_current_menubar(nullptr);
  436. }
  437. void MenuManager::set_system_menu(Menu& menu)
  438. {
  439. m_system_menu = menu;
  440. set_current_menubar(m_current_menubar);
  441. }
  442. Menu* MenuManager::previous_menu(Menu* current)
  443. {
  444. Menu* found = nullptr;
  445. Menu* previous = nullptr;
  446. for_each_active_menubar_menu([&](Menu& menu) {
  447. if (current == &menu) {
  448. found = previous;
  449. return IterationDecision::Break;
  450. }
  451. previous = &menu;
  452. return IterationDecision::Continue;
  453. });
  454. return found;
  455. }
  456. Menu* MenuManager::next_menu(Menu* current)
  457. {
  458. Menu* found = nullptr;
  459. bool is_next = false;
  460. for_each_active_menubar_menu([&](Menu& menu) {
  461. if (is_next) {
  462. found = &menu;
  463. return IterationDecision::Break;
  464. }
  465. if (current == &menu)
  466. is_next = true;
  467. return IterationDecision::Continue;
  468. });
  469. return found;
  470. }
  471. void MenuManager::did_change_theme()
  472. {
  473. ++m_theme_index;
  474. refresh();
  475. }
  476. }