MenuManager.cpp 16 KB

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