MenuManager.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Shannon Booth <shannon.ml.booth@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Badge.h>
  8. #include <WindowServer/ClientConnection.h>
  9. #include <WindowServer/MenuManager.h>
  10. #include <WindowServer/Screen.h>
  11. #include <WindowServer/WindowManager.h>
  12. namespace WindowServer {
  13. static MenuManager* s_the;
  14. MenuManager& MenuManager::the()
  15. {
  16. VERIFY(s_the);
  17. return *s_the;
  18. }
  19. MenuManager::MenuManager()
  20. {
  21. s_the = this;
  22. }
  23. MenuManager::~MenuManager()
  24. {
  25. }
  26. bool MenuManager::is_open(const Menu& menu) const
  27. {
  28. for (size_t i = 0; i < m_open_menu_stack.size(); ++i) {
  29. if (&menu == m_open_menu_stack[i].ptr())
  30. return true;
  31. }
  32. return false;
  33. }
  34. void MenuManager::refresh()
  35. {
  36. ClientConnection::for_each_client([&](ClientConnection& client) {
  37. client.for_each_menu([&](Menu& menu) {
  38. menu.redraw();
  39. return IterationDecision::Continue;
  40. });
  41. });
  42. }
  43. void MenuManager::event(Core::Event& event)
  44. {
  45. auto& wm = WindowManager::the();
  46. if (static_cast<Event&>(event).is_mouse_event()) {
  47. handle_mouse_event(static_cast<MouseEvent&>(event));
  48. return;
  49. }
  50. if (static_cast<Event&>(event).is_key_event()) {
  51. auto& key_event = static_cast<const KeyEvent&>(event);
  52. if (key_event.type() == Event::KeyUp && key_event.key() == Key_Escape) {
  53. close_everyone();
  54. return;
  55. }
  56. if (m_current_menu && event.type() == Event::KeyDown
  57. && ((key_event.key() >= Key_A && key_event.key() <= Key_Z)
  58. || (key_event.key() >= Key_0 && key_event.key() <= Key_9))) {
  59. if (auto* shortcut_item_indices = m_current_menu->items_with_alt_shortcut(key_event.code_point())) {
  60. VERIFY(!shortcut_item_indices->is_empty());
  61. // FIXME: If there are multiple items with the same Alt shortcut, we should cycle through them
  62. // with each keypress instead of activating immediately.
  63. auto index = shortcut_item_indices->at(0);
  64. auto& item = m_current_menu->item(index);
  65. m_current_menu->set_hovered_index(index);
  66. if (item.is_submenu())
  67. m_current_menu->descend_into_submenu_at_hovered_item();
  68. else
  69. m_current_menu->open_hovered_item(false);
  70. }
  71. return;
  72. }
  73. if (event.type() == Event::KeyDown) {
  74. if (key_event.key() == Key_Left) {
  75. auto it = m_open_menu_stack.find_if([&](const auto& other) { return m_current_menu == other.ptr(); });
  76. VERIFY(!it.is_end());
  77. // Going "back" a menu should be the previous menu in the stack
  78. if (it.index() > 0)
  79. set_current_menu(m_open_menu_stack.at(it.index() - 1));
  80. else {
  81. if (m_current_menu->hovered_item())
  82. m_current_menu->set_hovered_index(-1);
  83. else {
  84. auto* target_menu = previous_menu(m_current_menu);
  85. if (target_menu) {
  86. target_menu->ensure_menu_window(target_menu->rect_in_window_menubar().bottom_left().translated(wm.window_with_active_menu()->frame().rect().location()).translated(wm.window_with_active_menu()->frame().menubar_rect().location()));
  87. open_menu(*target_menu);
  88. wm.window_with_active_menu()->invalidate_menubar();
  89. }
  90. }
  91. }
  92. close_everyone_not_in_lineage(*m_current_menu);
  93. return;
  94. }
  95. if (key_event.key() == Key_Right) {
  96. auto hovered_item = m_current_menu->hovered_item();
  97. if (hovered_item && hovered_item->is_submenu())
  98. m_current_menu->descend_into_submenu_at_hovered_item();
  99. else if (m_open_menu_stack.size() <= 1 && wm.window_with_active_menu()) {
  100. auto* target_menu = next_menu(m_current_menu);
  101. if (target_menu) {
  102. target_menu->ensure_menu_window(target_menu->rect_in_window_menubar().bottom_left().translated(wm.window_with_active_menu()->frame().rect().location()).translated(wm.window_with_active_menu()->frame().menubar_rect().location()));
  103. open_menu(*target_menu);
  104. wm.window_with_active_menu()->invalidate_menubar();
  105. close_everyone_not_in_lineage(*target_menu);
  106. }
  107. }
  108. return;
  109. }
  110. if (key_event.key() == Key_Return) {
  111. auto hovered_item = m_current_menu->hovered_item();
  112. if (!hovered_item || !hovered_item->is_enabled())
  113. return;
  114. if (hovered_item->is_submenu())
  115. m_current_menu->descend_into_submenu_at_hovered_item();
  116. else
  117. m_current_menu->open_hovered_item(key_event.modifiers() & KeyModifier::Mod_Ctrl);
  118. return;
  119. }
  120. m_current_menu->dispatch_event(event);
  121. }
  122. }
  123. return Core::Object::event(event);
  124. }
  125. void MenuManager::handle_mouse_event(MouseEvent& mouse_event)
  126. {
  127. if (!has_open_menu())
  128. return;
  129. auto* topmost_menu = m_open_menu_stack.last().ptr();
  130. VERIFY(topmost_menu);
  131. auto* window = topmost_menu->menu_window();
  132. if (!window) {
  133. dbgln("MenuManager::handle_mouse_event: No menu window");
  134. return;
  135. }
  136. VERIFY(window->is_visible());
  137. bool event_is_inside_current_menu = window->rect().contains(mouse_event.position());
  138. if (event_is_inside_current_menu) {
  139. WindowManager::the().set_hovered_window(window);
  140. WindowManager::the().deliver_mouse_event(*window, mouse_event, true);
  141. return;
  142. }
  143. if (topmost_menu->hovered_item())
  144. topmost_menu->clear_hovered_item();
  145. if (mouse_event.type() == Event::MouseDown || mouse_event.type() == Event::MouseUp) {
  146. auto* window_menu_of = topmost_menu->window_menu_of();
  147. if (window_menu_of) {
  148. bool event_is_inside_taskbar_button = window_menu_of->taskbar_rect().contains(mouse_event.position());
  149. if (event_is_inside_taskbar_button && !topmost_menu->is_window_menu_open()) {
  150. topmost_menu->set_window_menu_open(true);
  151. return;
  152. }
  153. }
  154. if (mouse_event.type() == Event::MouseDown) {
  155. for (auto& menu : m_open_menu_stack) {
  156. if (!menu)
  157. continue;
  158. if (!menu->menu_window()->rect().contains(mouse_event.position()))
  159. continue;
  160. return;
  161. }
  162. MenuManager::the().close_everyone();
  163. topmost_menu->set_window_menu_open(false);
  164. }
  165. }
  166. if (mouse_event.type() == Event::MouseMove) {
  167. for (auto& menu : m_open_menu_stack) {
  168. if (!menu)
  169. continue;
  170. if (!menu->menu_window()->rect().contains(mouse_event.position()))
  171. continue;
  172. WindowManager::the().set_hovered_window(menu->menu_window());
  173. WindowManager::the().deliver_mouse_event(*menu->menu_window(), mouse_event, true);
  174. break;
  175. }
  176. }
  177. }
  178. void MenuManager::close_all_menus_from_client(Badge<ClientConnection>, ClientConnection& client)
  179. {
  180. if (!has_open_menu())
  181. return;
  182. if (m_open_menu_stack.first()->client() != &client)
  183. return;
  184. close_everyone();
  185. }
  186. void MenuManager::close_everyone()
  187. {
  188. for (auto& menu : m_open_menu_stack) {
  189. VERIFY(menu);
  190. menu->set_visible(false);
  191. menu->clear_hovered_item();
  192. }
  193. m_open_menu_stack.clear();
  194. clear_current_menu();
  195. }
  196. void MenuManager::close_everyone_not_in_lineage(Menu& menu)
  197. {
  198. Vector<Menu&> menus_to_close;
  199. for (auto& open_menu : m_open_menu_stack) {
  200. if (!open_menu)
  201. continue;
  202. if (&menu == open_menu.ptr() || open_menu->is_menu_ancestor_of(menu))
  203. continue;
  204. menus_to_close.append(*open_menu);
  205. }
  206. close_menus(menus_to_close);
  207. }
  208. void MenuManager::close_menus(Vector<Menu&>& menus)
  209. {
  210. for (auto& menu : menus) {
  211. if (&menu == m_current_menu)
  212. clear_current_menu();
  213. menu.set_visible(false);
  214. menu.clear_hovered_item();
  215. m_open_menu_stack.remove_first_matching([&](auto& entry) {
  216. return entry == &menu;
  217. });
  218. }
  219. }
  220. static void collect_menu_subtree(Menu& menu, Vector<Menu&>& menus)
  221. {
  222. menus.append(menu);
  223. for (size_t i = 0; i < menu.item_count(); ++i) {
  224. auto& item = menu.item(i);
  225. if (!item.is_submenu())
  226. continue;
  227. collect_menu_subtree(*item.submenu(), menus);
  228. }
  229. }
  230. void MenuManager::close_menu_and_descendants(Menu& menu)
  231. {
  232. Vector<Menu&> menus_to_close;
  233. collect_menu_subtree(menu, menus_to_close);
  234. close_menus(menus_to_close);
  235. }
  236. void MenuManager::set_hovered_menu(Menu* menu)
  237. {
  238. if (m_hovered_menu == menu)
  239. return;
  240. if (menu) {
  241. m_hovered_menu = menu->make_weak_ptr<Menu>();
  242. } else {
  243. // FIXME: This is quite aggressive. If we knew which window the previously hovered menu was in,
  244. // we could just invalidate that one instead of iterating all windows in the client.
  245. if (auto* client = m_hovered_menu->client()) {
  246. client->for_each_window([&](Window& window) {
  247. window.invalidate_menubar();
  248. return IterationDecision::Continue;
  249. });
  250. }
  251. m_hovered_menu = nullptr;
  252. }
  253. }
  254. void MenuManager::open_menu(Menu& menu, bool as_current_menu)
  255. {
  256. if (menu.is_open()) {
  257. if (as_current_menu || current_menu() != &menu) {
  258. // This menu is already open. If requested, or if the current
  259. // window doesn't match this one, then set it to this
  260. set_current_menu(&menu);
  261. }
  262. return;
  263. }
  264. m_open_menu_stack.append(menu);
  265. menu.set_visible(true);
  266. if (!menu.is_empty()) {
  267. menu.redraw_if_theme_changed();
  268. auto* window = menu.menu_window();
  269. VERIFY(window);
  270. window->set_visible(true);
  271. }
  272. if (as_current_menu || !current_menu()) {
  273. // Only make this menu the current menu if requested, or if no
  274. // other menu is current
  275. set_current_menu(&menu);
  276. }
  277. }
  278. void MenuManager::clear_current_menu()
  279. {
  280. Menu* previous_current_menu = m_current_menu;
  281. m_current_menu = nullptr;
  282. if (previous_current_menu) {
  283. // When closing the last menu, restore the previous active input window
  284. auto& wm = WindowManager::the();
  285. wm.restore_active_input_window(m_previous_input_window);
  286. if (auto* window = wm.window_with_active_menu()) {
  287. window->invalidate_menubar();
  288. }
  289. wm.set_window_with_active_menu(nullptr);
  290. }
  291. }
  292. void MenuManager::set_current_menu(Menu* menu)
  293. {
  294. if (!menu) {
  295. clear_current_menu();
  296. return;
  297. }
  298. VERIFY(is_open(*menu));
  299. if (menu == m_current_menu) {
  300. return;
  301. }
  302. Menu* previous_current_menu = m_current_menu;
  303. m_current_menu = menu;
  304. auto& wm = WindowManager::the();
  305. if (!previous_current_menu) {
  306. // When opening the first menu, store the current active input window
  307. if (auto* active_input = wm.active_input_window())
  308. m_previous_input_window = *active_input;
  309. else
  310. m_previous_input_window = nullptr;
  311. }
  312. wm.set_active_input_window(m_current_menu->menu_window());
  313. }
  314. Menu* MenuManager::previous_menu(Menu* current)
  315. {
  316. auto& wm = WindowManager::the();
  317. if (!wm.window_with_active_menu())
  318. return nullptr;
  319. Menu* found = nullptr;
  320. Menu* previous = nullptr;
  321. wm.window_with_active_menu()->menubar().for_each_menu([&](Menu& menu) {
  322. if (current == &menu) {
  323. found = previous;
  324. return IterationDecision::Break;
  325. }
  326. previous = &menu;
  327. return IterationDecision::Continue;
  328. });
  329. return found;
  330. }
  331. Menu* MenuManager::next_menu(Menu* current)
  332. {
  333. Menu* found = nullptr;
  334. bool is_next = false;
  335. auto& wm = WindowManager::the();
  336. if (!wm.window_with_active_menu())
  337. return nullptr;
  338. wm.window_with_active_menu()->menubar().for_each_menu([&](Menu& menu) {
  339. if (is_next) {
  340. found = &menu;
  341. return IterationDecision::Break;
  342. }
  343. if (current == &menu)
  344. is_next = true;
  345. return IterationDecision::Continue;
  346. });
  347. return found;
  348. }
  349. void MenuManager::did_change_theme()
  350. {
  351. ++m_theme_index;
  352. refresh();
  353. }
  354. }