MenuManager.cpp 14 KB

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