MenuManager.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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)) {
  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. close_everyone_not_in_lineage(*m_current_menu);
  146. return;
  147. }
  148. if (key_event.key() == Key_Right) {
  149. auto hovered_item = m_current_menu->hovered_item();
  150. if (hovered_item && hovered_item->is_submenu())
  151. m_current_menu->descend_into_submenu_at_hovered_item();
  152. return;
  153. }
  154. if (key_event.key() == Key_Return) {
  155. auto hovered_item = m_current_menu->hovered_item();
  156. if (!hovered_item || !hovered_item->is_enabled())
  157. return;
  158. if (hovered_item->is_submenu())
  159. m_current_menu->descend_into_submenu_at_hovered_item();
  160. else
  161. m_current_menu->open_hovered_item();
  162. return;
  163. }
  164. m_current_menu->dispatch_event(event);
  165. }
  166. }
  167. return Core::Object::event(event);
  168. }
  169. void MenuManager::handle_mouse_event(MouseEvent& mouse_event)
  170. {
  171. auto* active_window = WindowManager::the().active_window();
  172. bool handled_menubar_event = false;
  173. for_each_active_menubar_menu([&](Menu& menu) {
  174. if (menu.rect_in_menubar().contains(mouse_event.position())) {
  175. handled_menubar_event = &menu == m_system_menu || !active_window || !active_window->is_modal();
  176. if (handled_menubar_event)
  177. handle_menu_mouse_event(menu, mouse_event);
  178. return IterationDecision::Break;
  179. }
  180. return IterationDecision::Continue;
  181. });
  182. if (handled_menubar_event)
  183. return;
  184. if (has_open_menu()) {
  185. auto* topmost_menu = m_open_menu_stack.last().ptr();
  186. ASSERT(topmost_menu);
  187. auto* window = topmost_menu->menu_window();
  188. if (!window) {
  189. dbgln("MenuManager::handle_mouse_event: No menu window");
  190. return;
  191. }
  192. ASSERT(window->is_visible());
  193. bool event_is_inside_current_menu = window->rect().contains(mouse_event.position());
  194. if (event_is_inside_current_menu) {
  195. WindowManager::the().set_hovered_window(window);
  196. auto translated_event = mouse_event.translated(-window->position());
  197. WindowManager::the().deliver_mouse_event(*window, translated_event);
  198. return;
  199. }
  200. if (topmost_menu->hovered_item())
  201. topmost_menu->clear_hovered_item();
  202. if (mouse_event.type() == Event::MouseDown || mouse_event.type() == Event::MouseUp) {
  203. auto* window_menu_of = topmost_menu->window_menu_of();
  204. if (window_menu_of) {
  205. bool event_is_inside_taskbar_button = window_menu_of->taskbar_rect().contains(mouse_event.position());
  206. if (event_is_inside_taskbar_button && !topmost_menu->is_window_menu_open()) {
  207. topmost_menu->set_window_menu_open(true);
  208. return;
  209. }
  210. }
  211. if (mouse_event.type() == Event::MouseDown) {
  212. for (auto& menu : m_open_menu_stack) {
  213. if (!menu)
  214. continue;
  215. if (!menu->menu_window()->rect().contains(mouse_event.position()))
  216. continue;
  217. return;
  218. }
  219. close_bar();
  220. topmost_menu->set_window_menu_open(false);
  221. }
  222. }
  223. if (mouse_event.type() == Event::MouseMove) {
  224. for (auto& menu : m_open_menu_stack) {
  225. if (!menu)
  226. continue;
  227. if (!menu->menu_window()->rect().contains(mouse_event.position()))
  228. continue;
  229. WindowManager::the().set_hovered_window(menu->menu_window());
  230. auto translated_event = mouse_event.translated(-menu->menu_window()->position());
  231. WindowManager::the().deliver_mouse_event(*menu->menu_window(), translated_event);
  232. break;
  233. }
  234. }
  235. return;
  236. }
  237. AppletManager::the().dispatch_event(static_cast<Event&>(mouse_event));
  238. }
  239. void MenuManager::handle_menu_mouse_event(Menu& menu, const MouseEvent& event)
  240. {
  241. bool is_hover_with_any_menu_open = event.type() == MouseEvent::MouseMove
  242. && has_open_menu()
  243. && (m_open_menu_stack.first()->menubar() || m_open_menu_stack.first() == m_system_menu.ptr());
  244. bool is_mousedown_with_left_button = event.type() == MouseEvent::MouseDown && event.button() == MouseButton::Left;
  245. bool should_open_menu = &menu != m_current_menu && (is_hover_with_any_menu_open || is_mousedown_with_left_button);
  246. if (is_mousedown_with_left_button)
  247. m_bar_open = !m_bar_open;
  248. if (should_open_menu && m_bar_open) {
  249. close_everyone();
  250. open_menu(menu);
  251. return;
  252. }
  253. if (!m_bar_open)
  254. close_everyone();
  255. }
  256. void MenuManager::set_needs_window_resize()
  257. {
  258. m_needs_window_resize = true;
  259. }
  260. void MenuManager::close_all_menus_from_client(Badge<ClientConnection>, ClientConnection& client)
  261. {
  262. if (!has_open_menu())
  263. return;
  264. if (m_open_menu_stack.first()->client() != &client)
  265. return;
  266. close_everyone();
  267. }
  268. void MenuManager::close_everyone()
  269. {
  270. for (auto& menu : m_open_menu_stack) {
  271. ASSERT(menu);
  272. if (menu->menu_window())
  273. menu->menu_window()->set_visible(false);
  274. menu->clear_hovered_item();
  275. }
  276. m_open_menu_stack.clear();
  277. m_current_search.clear();
  278. clear_current_menu();
  279. refresh();
  280. }
  281. void MenuManager::close_everyone_not_in_lineage(Menu& menu)
  282. {
  283. Vector<Menu*> menus_to_close;
  284. for (auto& open_menu : m_open_menu_stack) {
  285. if (!open_menu)
  286. continue;
  287. if (&menu == open_menu.ptr() || open_menu->is_menu_ancestor_of(menu))
  288. continue;
  289. menus_to_close.append(open_menu);
  290. }
  291. close_menus(menus_to_close);
  292. }
  293. void MenuManager::close_menus(const Vector<Menu*>& menus)
  294. {
  295. for (auto& menu : menus) {
  296. if (menu == m_current_menu)
  297. clear_current_menu();
  298. if (menu->menu_window())
  299. menu->menu_window()->set_visible(false);
  300. menu->clear_hovered_item();
  301. m_open_menu_stack.remove_first_matching([&](auto& entry) {
  302. return entry == menu;
  303. });
  304. }
  305. refresh();
  306. }
  307. static void collect_menu_subtree(Menu& menu, Vector<Menu*>& menus)
  308. {
  309. menus.append(&menu);
  310. for (int i = 0; i < menu.item_count(); ++i) {
  311. auto& item = menu.item(i);
  312. if (!item.is_submenu())
  313. continue;
  314. collect_menu_subtree(*item.submenu(), menus);
  315. }
  316. }
  317. void MenuManager::close_menu_and_descendants(Menu& menu)
  318. {
  319. Vector<Menu*> menus_to_close;
  320. collect_menu_subtree(menu, menus_to_close);
  321. close_menus(menus_to_close);
  322. }
  323. void MenuManager::toggle_menu(Menu& menu)
  324. {
  325. if (is_open(menu)) {
  326. close_menu_and_descendants(menu);
  327. return;
  328. }
  329. open_menu(menu);
  330. }
  331. void MenuManager::open_menu(Menu& menu, bool as_current_menu)
  332. {
  333. if (is_open(menu)) {
  334. if (as_current_menu || current_menu() != &menu) {
  335. // This menu is already open. If requested, or if the current
  336. // window doesn't match this one, then set it to this
  337. set_current_menu(&menu);
  338. }
  339. return;
  340. }
  341. if (!menu.is_empty()) {
  342. menu.redraw_if_theme_changed();
  343. if (!menu.menu_window()) {
  344. auto& menu_window = menu.ensure_menu_window();
  345. menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() + 2 });
  346. }
  347. menu.menu_window()->set_visible(true);
  348. }
  349. if (m_open_menu_stack.find_if([&menu](auto& other) { return &menu == other.ptr(); }).is_end())
  350. m_open_menu_stack.append(menu);
  351. if (as_current_menu || !current_menu()) {
  352. // Only make this menu the current menu if requested, or if no
  353. // other menu is current
  354. set_current_menu(&menu);
  355. }
  356. refresh();
  357. }
  358. void MenuManager::clear_current_menu()
  359. {
  360. Menu* previous_current_menu = m_current_menu;
  361. m_current_menu = nullptr;
  362. if (previous_current_menu) {
  363. // When closing the last menu, restore the previous active input window
  364. auto& wm = WindowManager::the();
  365. wm.restore_active_input_window(m_previous_input_window);
  366. }
  367. }
  368. void MenuManager::set_current_menu(Menu* menu)
  369. {
  370. if (!menu) {
  371. clear_current_menu();
  372. return;
  373. }
  374. ASSERT(is_open(*menu));
  375. if (menu == m_current_menu) {
  376. return;
  377. }
  378. m_current_search.clear();
  379. Menu* previous_current_menu = m_current_menu;
  380. m_current_menu = menu;
  381. auto& wm = WindowManager::the();
  382. if (!previous_current_menu) {
  383. // When opening the first menu, store the current active input window
  384. if (auto* active_input = wm.active_input_window())
  385. m_previous_input_window = *active_input;
  386. else
  387. m_previous_input_window = nullptr;
  388. }
  389. wm.set_active_input_window(m_current_menu->menu_window());
  390. }
  391. void MenuManager::close_bar()
  392. {
  393. close_everyone();
  394. m_bar_open = false;
  395. }
  396. Gfx::IntRect MenuManager::menubar_rect() const
  397. {
  398. return { 0, 0, Screen::the().rect().width(), 19 };
  399. }
  400. void MenuManager::set_current_menubar(MenuBar* menubar)
  401. {
  402. if (menubar)
  403. m_current_menubar = *menubar;
  404. else
  405. m_current_menubar = nullptr;
  406. dbgln<MENUS_DEBUG>("[WM] Current menubar is now {}", menubar);
  407. Gfx::IntPoint next_menu_location { MenuManager::menubar_menu_margin() / 2, 0 };
  408. for_each_active_menubar_menu([&](Menu& menu) {
  409. int text_width = menu.title_font().width(menu.name());
  410. 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 });
  411. Gfx::IntRect text_rect { next_menu_location.translated(0, 1), { text_width, menubar_rect().height() - 3 } };
  412. menu.set_text_rect_in_menubar(text_rect);
  413. next_menu_location.move_by(menu.rect_in_menubar().width(), 0);
  414. return IterationDecision::Continue;
  415. });
  416. refresh();
  417. }
  418. void MenuManager::close_menubar(MenuBar& menubar)
  419. {
  420. if (current_menubar() == &menubar)
  421. set_current_menubar(nullptr);
  422. }
  423. void MenuManager::set_system_menu(Menu& menu)
  424. {
  425. m_system_menu = menu;
  426. set_current_menubar(m_current_menubar);
  427. }
  428. void MenuManager::did_change_theme()
  429. {
  430. ++m_theme_index;
  431. refresh();
  432. }
  433. }