MenuManager.cpp 13 KB

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