MenuManager.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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/ClientConnection.h>
  35. #include <WindowServer/MenuManager.h>
  36. #include <WindowServer/Screen.h>
  37. #include <WindowServer/WindowManager.h>
  38. #include <unistd.h>
  39. namespace WindowServer {
  40. static MenuManager* s_the;
  41. static constexpr int s_search_timeout = 3000;
  42. MenuManager& MenuManager::the()
  43. {
  44. VERIFY(s_the);
  45. return *s_the;
  46. }
  47. MenuManager::MenuManager()
  48. {
  49. s_the = this;
  50. m_needs_window_resize = true;
  51. // NOTE: This ensures that the system menu has the correct dimensions.
  52. set_current_menubar(nullptr);
  53. m_window = Window::construct(*this, WindowType::Menubar);
  54. m_window->set_rect(menubar_rect());
  55. m_search_timer = Core::Timer::create_single_shot(0, [this] {
  56. m_current_search.clear();
  57. });
  58. }
  59. MenuManager::~MenuManager()
  60. {
  61. }
  62. bool MenuManager::is_open(const Menu& menu) const
  63. {
  64. for (size_t i = 0; i < m_open_menu_stack.size(); ++i) {
  65. if (&menu == m_open_menu_stack[i].ptr())
  66. return true;
  67. }
  68. return false;
  69. }
  70. void MenuManager::draw()
  71. {
  72. auto& wm = WindowManager::the();
  73. auto palette = wm.palette();
  74. auto menubar_rect = this->menubar_rect();
  75. if (m_needs_window_resize) {
  76. m_window->set_rect(menubar_rect);
  77. AppletManager::the().calculate_applet_rects(window());
  78. m_needs_window_resize = false;
  79. }
  80. Gfx::Painter painter(*window().backing_store());
  81. painter.fill_rect(menubar_rect, palette.window());
  82. painter.draw_line({ 0, menubar_rect.bottom() - 1 }, { menubar_rect.right(), menubar_rect.bottom() - 1 }, palette.threed_shadow1());
  83. painter.draw_line({ 0, menubar_rect.bottom() }, { menubar_rect.right(), menubar_rect.bottom() }, palette.threed_shadow2());
  84. for_each_active_menubar_menu([&](Menu& menu) {
  85. auto text_rect = menu.text_rect_in_global_menubar();
  86. Color text_color = palette.window_text();
  87. if (is_open(menu) && &menu == m_current_menu_bar_menu) {
  88. painter.fill_rect(menu.rect_in_global_menubar(), palette.menu_selection());
  89. painter.draw_rect(menu.rect_in_global_menubar(), palette.menu_selection().darkened());
  90. text_color = palette.menu_selection_text();
  91. text_rect.move_by(1, 1);
  92. }
  93. painter.draw_text(
  94. text_rect,
  95. menu.name(),
  96. menu.title_font(),
  97. Gfx::TextAlignment::CenterLeft,
  98. text_color);
  99. return IterationDecision::Continue;
  100. });
  101. AppletManager::the().draw();
  102. }
  103. void MenuManager::refresh()
  104. {
  105. if (!m_window)
  106. return;
  107. draw();
  108. window().invalidate();
  109. }
  110. void MenuManager::event(Core::Event& event)
  111. {
  112. if (static_cast<Event&>(event).is_mouse_event()) {
  113. handle_mouse_event(static_cast<MouseEvent&>(event));
  114. return;
  115. }
  116. if (static_cast<Event&>(event).is_key_event()) {
  117. auto& key_event = static_cast<const KeyEvent&>(event);
  118. if (key_event.type() == Event::KeyUp && key_event.key() == Key_Escape) {
  119. close_everyone();
  120. return;
  121. }
  122. if (key_event.key() == Key_Backspace) {
  123. m_current_search.clear();
  124. return;
  125. }
  126. if (m_current_menu && event.type() == Event::KeyDown
  127. && ((key_event.key() >= Key_A && key_event.key() <= Key_Z)
  128. || (key_event.key() >= Key_0 && key_event.key() <= Key_9))) {
  129. m_current_search.append_code_point(key_event.code_point());
  130. m_search_timer->restart(s_search_timeout);
  131. for (int i = 0; i < m_current_menu->item_count(); ++i) {
  132. auto text = m_current_menu->item(i).text();
  133. if (text.to_lowercase().starts_with(m_current_search.to_string().to_lowercase())) {
  134. m_current_menu->set_hovered_item(i);
  135. return;
  136. }
  137. }
  138. return;
  139. }
  140. if (event.type() == Event::KeyDown) {
  141. if (key_event.key() == Key_Left) {
  142. auto it = m_open_menu_stack.find_if([&](const auto& other) { return m_current_menu == other.ptr(); });
  143. VERIFY(!it.is_end());
  144. // Going "back" a menu should be the previous menu in the stack
  145. if (it.index() > 0)
  146. set_current_menu(m_open_menu_stack.at(it.index() - 1));
  147. else {
  148. if (m_current_menu->hovered_item())
  149. m_current_menu->set_hovered_item(-1);
  150. else {
  151. auto* target_menu = previous_menu(m_current_menu);
  152. if (target_menu)
  153. open_menu(*target_menu, true);
  154. }
  155. }
  156. close_everyone_not_in_lineage(*m_current_menu);
  157. return;
  158. }
  159. if (key_event.key() == Key_Right) {
  160. auto hovered_item = m_current_menu->hovered_item();
  161. if (hovered_item && hovered_item->is_submenu())
  162. m_current_menu->descend_into_submenu_at_hovered_item();
  163. else if (m_open_menu_stack.size() <= 1) {
  164. auto* target_menu = next_menu(m_current_menu);
  165. if (target_menu) {
  166. open_menu(*target_menu, true);
  167. close_everyone_not_in_lineage(*target_menu);
  168. }
  169. }
  170. return;
  171. }
  172. if (key_event.key() == Key_Return) {
  173. auto hovered_item = m_current_menu->hovered_item();
  174. if (!hovered_item || !hovered_item->is_enabled())
  175. return;
  176. if (hovered_item->is_submenu())
  177. m_current_menu->descend_into_submenu_at_hovered_item();
  178. else
  179. m_current_menu->open_hovered_item();
  180. return;
  181. }
  182. m_current_menu->dispatch_event(event);
  183. }
  184. }
  185. return Core::Object::event(event);
  186. }
  187. void MenuManager::handle_mouse_event(MouseEvent& mouse_event)
  188. {
  189. auto* active_window = WindowManager::the().active_window();
  190. bool handled_menubar_event = false;
  191. for_each_active_menubar_menu([&](Menu& menu) {
  192. if (menu.rect_in_global_menubar().contains(mouse_event.position())) {
  193. handled_menubar_event = &menu == m_system_menu || !active_window || !active_window->is_modal();
  194. if (handled_menubar_event)
  195. handle_menu_mouse_event(menu, mouse_event);
  196. return IterationDecision::Break;
  197. }
  198. return IterationDecision::Continue;
  199. });
  200. if (handled_menubar_event)
  201. return;
  202. if (has_open_menu()) {
  203. auto* topmost_menu = m_open_menu_stack.last().ptr();
  204. VERIFY(topmost_menu);
  205. auto* window = topmost_menu->menu_window();
  206. if (!window) {
  207. dbgln("MenuManager::handle_mouse_event: No menu window");
  208. return;
  209. }
  210. VERIFY(window->is_visible());
  211. bool event_is_inside_current_menu = window->rect().contains(mouse_event.position());
  212. if (event_is_inside_current_menu) {
  213. WindowManager::the().set_hovered_window(window);
  214. auto translated_event = mouse_event.translated(-window->position());
  215. WindowManager::the().deliver_mouse_event(*window, translated_event, true);
  216. return;
  217. }
  218. if (topmost_menu->hovered_item())
  219. topmost_menu->clear_hovered_item();
  220. if (mouse_event.type() == Event::MouseDown || mouse_event.type() == Event::MouseUp) {
  221. auto* window_menu_of = topmost_menu->window_menu_of();
  222. if (window_menu_of) {
  223. bool event_is_inside_taskbar_button = window_menu_of->taskbar_rect().contains(mouse_event.position());
  224. if (event_is_inside_taskbar_button && !topmost_menu->is_window_menu_open()) {
  225. topmost_menu->set_window_menu_open(true);
  226. return;
  227. }
  228. }
  229. if (mouse_event.type() == Event::MouseDown) {
  230. for (auto& menu : m_open_menu_stack) {
  231. if (!menu)
  232. continue;
  233. if (!menu->menu_window()->rect().contains(mouse_event.position()))
  234. continue;
  235. return;
  236. }
  237. close_bar();
  238. topmost_menu->set_window_menu_open(false);
  239. }
  240. }
  241. if (mouse_event.type() == Event::MouseMove) {
  242. for (auto& menu : m_open_menu_stack) {
  243. if (!menu)
  244. continue;
  245. if (!menu->menu_window()->rect().contains(mouse_event.position()))
  246. continue;
  247. WindowManager::the().set_hovered_window(menu->menu_window());
  248. auto translated_event = mouse_event.translated(-menu->menu_window()->position());
  249. WindowManager::the().deliver_mouse_event(*menu->menu_window(), translated_event, true);
  250. break;
  251. }
  252. }
  253. return;
  254. }
  255. AppletManager::the().dispatch_event(static_cast<Event&>(mouse_event));
  256. }
  257. void MenuManager::handle_menu_mouse_event(Menu& menu, const MouseEvent& event)
  258. {
  259. bool is_hover_with_any_menu_open = event.type() == MouseEvent::MouseMove
  260. && has_open_menu() && m_current_menu_bar_menu
  261. && (m_open_menu_stack.first()->menubar() || m_open_menu_stack.first() == m_system_menu.ptr());
  262. bool is_mousedown_with_left_button = event.type() == MouseEvent::MouseDown && event.button() == MouseButton::Left;
  263. bool should_open_menu = (&menu != m_current_menu || !m_current_menu_bar_menu) && (is_hover_with_any_menu_open || is_mousedown_with_left_button);
  264. if (is_mousedown_with_left_button)
  265. m_bar_open = !m_bar_open;
  266. if (should_open_menu && m_bar_open) {
  267. close_everyone();
  268. open_menu(menu, true);
  269. return;
  270. }
  271. if (!m_bar_open && m_current_menu_bar_menu)
  272. close_everyone();
  273. }
  274. void MenuManager::set_needs_window_resize()
  275. {
  276. m_needs_window_resize = true;
  277. }
  278. void MenuManager::close_all_menus_from_client(Badge<ClientConnection>, ClientConnection& client)
  279. {
  280. if (!has_open_menu())
  281. return;
  282. if (m_open_menu_stack.first()->client() != &client)
  283. return;
  284. close_everyone();
  285. }
  286. void MenuManager::close_everyone()
  287. {
  288. for (auto& menu : m_open_menu_stack) {
  289. VERIFY(menu);
  290. if (menu->menu_window())
  291. menu->menu_window()->set_visible(false);
  292. menu->clear_hovered_item();
  293. }
  294. m_open_menu_stack.clear();
  295. m_current_search.clear();
  296. clear_current_menu();
  297. refresh();
  298. }
  299. void MenuManager::close_everyone_not_in_lineage(Menu& menu)
  300. {
  301. Vector<Menu*> menus_to_close;
  302. for (auto& open_menu : m_open_menu_stack) {
  303. if (!open_menu)
  304. continue;
  305. if (&menu == open_menu.ptr() || open_menu->is_menu_ancestor_of(menu))
  306. continue;
  307. menus_to_close.append(open_menu);
  308. }
  309. close_menus(menus_to_close);
  310. }
  311. void MenuManager::close_menus(const Vector<Menu*>& menus)
  312. {
  313. for (auto& menu : menus) {
  314. if (menu == m_current_menu)
  315. clear_current_menu();
  316. if (menu->menu_window())
  317. menu->menu_window()->set_visible(false);
  318. menu->clear_hovered_item();
  319. m_open_menu_stack.remove_first_matching([&](auto& entry) {
  320. return entry == menu;
  321. });
  322. }
  323. refresh();
  324. }
  325. static void collect_menu_subtree(Menu& menu, Vector<Menu*>& menus)
  326. {
  327. menus.append(&menu);
  328. for (int i = 0; i < menu.item_count(); ++i) {
  329. auto& item = menu.item(i);
  330. if (!item.is_submenu())
  331. continue;
  332. collect_menu_subtree(*item.submenu(), menus);
  333. }
  334. }
  335. void MenuManager::close_menu_and_descendants(Menu& menu)
  336. {
  337. Vector<Menu*> menus_to_close;
  338. collect_menu_subtree(menu, menus_to_close);
  339. close_menus(menus_to_close);
  340. }
  341. void MenuManager::set_hovered_menu(Menu* menu)
  342. {
  343. if (m_hovered_menu == menu)
  344. return;
  345. if (menu) {
  346. m_hovered_menu = menu->make_weak_ptr<Menu>();
  347. } else {
  348. // FIXME: This is quite aggressive. If we knew which window the previously hovered menu was in,
  349. // we could just invalidate that one instead of iterating all windows in the client.
  350. if (auto* client = m_hovered_menu->client()) {
  351. client->for_each_window([&](Window& window) {
  352. window.invalidate_menubar();
  353. return IterationDecision::Continue;
  354. });
  355. }
  356. m_hovered_menu = nullptr;
  357. }
  358. }
  359. void MenuManager::open_menu(Menu& menu, bool from_menu_bar, bool as_current_menu)
  360. {
  361. if (from_menu_bar)
  362. m_current_menu_bar_menu = &menu;
  363. if (is_open(menu)) {
  364. if (as_current_menu || current_menu() != &menu) {
  365. // This menu is already open. If requested, or if the current
  366. // window doesn't match this one, then set it to this
  367. set_current_menu(&menu);
  368. }
  369. return;
  370. }
  371. if (!menu.is_empty()) {
  372. menu.redraw_if_theme_changed();
  373. bool should_update_position = from_menu_bar;
  374. if (!menu.menu_window()) {
  375. should_update_position = true;
  376. menu.ensure_menu_window();
  377. }
  378. if (should_update_position)
  379. menu.menu_window()->move_to({ menu.rect_in_global_menubar().x(), menu.rect_in_global_menubar().bottom() + 2 });
  380. menu.menu_window()->set_visible(true);
  381. }
  382. if (m_open_menu_stack.find_if([&menu](auto& other) { return &menu == other.ptr(); }).is_end())
  383. m_open_menu_stack.append(menu);
  384. if (as_current_menu || !current_menu()) {
  385. // Only make this menu the current menu if requested, or if no
  386. // other menu is current
  387. set_current_menu(&menu);
  388. }
  389. refresh();
  390. }
  391. void MenuManager::clear_current_menu()
  392. {
  393. Menu* previous_current_menu = m_current_menu;
  394. m_current_menu = nullptr;
  395. m_current_menu_bar_menu = nullptr;
  396. if (previous_current_menu) {
  397. // When closing the last menu, restore the previous active input window
  398. auto& wm = WindowManager::the();
  399. wm.restore_active_input_window(m_previous_input_window);
  400. if (auto* window = wm.window_with_active_menu()) {
  401. window->invalidate_menubar();
  402. }
  403. wm.set_window_with_active_menu(nullptr);
  404. }
  405. }
  406. void MenuManager::set_current_menu(Menu* menu)
  407. {
  408. if (!menu) {
  409. clear_current_menu();
  410. return;
  411. }
  412. VERIFY(is_open(*menu));
  413. if (menu == m_current_menu) {
  414. return;
  415. }
  416. m_current_search.clear();
  417. Menu* previous_current_menu = m_current_menu;
  418. m_current_menu = menu;
  419. auto& wm = WindowManager::the();
  420. if (!previous_current_menu) {
  421. // When opening the first menu, store the current active input window
  422. if (auto* active_input = wm.active_input_window())
  423. m_previous_input_window = *active_input;
  424. else
  425. m_previous_input_window = nullptr;
  426. }
  427. wm.set_active_input_window(m_current_menu->menu_window());
  428. }
  429. void MenuManager::close_bar()
  430. {
  431. close_everyone();
  432. m_bar_open = false;
  433. }
  434. Gfx::IntRect MenuManager::menubar_rect() const
  435. {
  436. return { 0, 0, Screen::the().rect().width(), 19 };
  437. }
  438. void MenuManager::set_current_menubar(MenuBar* menubar)
  439. {
  440. if (menubar)
  441. m_current_menubar = *menubar;
  442. else
  443. m_current_menubar = nullptr;
  444. dbgln_if(MENUS_DEBUG, "[WM] Current menubar is now {}", menubar);
  445. Gfx::IntPoint next_menu_location { MenuManager::menubar_menu_margin() / 2, 0 };
  446. for_each_active_menubar_menu([&](Menu& menu) {
  447. int text_width = menu.title_font().width(menu.name());
  448. menu.set_rect_in_global_menubar({ next_menu_location.x() - MenuManager::menubar_menu_margin() / 2, 0, text_width + MenuManager::menubar_menu_margin(), menubar_rect().height() - 1 });
  449. Gfx::IntRect text_rect { next_menu_location.translated(0, 1), { text_width, menubar_rect().height() - 3 } };
  450. menu.set_text_rect_in_global_menubar(text_rect);
  451. next_menu_location.move_by(menu.rect_in_global_menubar().width(), 0);
  452. return IterationDecision::Continue;
  453. });
  454. refresh();
  455. }
  456. void MenuManager::close_menubar(MenuBar& menubar)
  457. {
  458. if (current_menubar() == &menubar)
  459. set_current_menubar(nullptr);
  460. }
  461. void MenuManager::set_system_menu(Menu& menu)
  462. {
  463. m_system_menu = menu;
  464. set_current_menubar(m_current_menubar);
  465. }
  466. Menu* MenuManager::previous_menu(Menu* current)
  467. {
  468. Menu* found = nullptr;
  469. Menu* previous = nullptr;
  470. for_each_active_menubar_menu([&](Menu& menu) {
  471. if (current == &menu) {
  472. found = previous;
  473. return IterationDecision::Break;
  474. }
  475. previous = &menu;
  476. return IterationDecision::Continue;
  477. });
  478. return found;
  479. }
  480. Menu* MenuManager::next_menu(Menu* current)
  481. {
  482. Menu* found = nullptr;
  483. bool is_next = false;
  484. for_each_active_menubar_menu([&](Menu& menu) {
  485. if (is_next) {
  486. found = &menu;
  487. return IterationDecision::Break;
  488. }
  489. if (current == &menu)
  490. is_next = true;
  491. return IterationDecision::Continue;
  492. });
  493. return found;
  494. }
  495. void MenuManager::did_change_theme()
  496. {
  497. ++m_theme_index;
  498. refresh();
  499. }
  500. }