MenuManager.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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/FileSystemPath.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. //#define DEBUG_MENUS
  39. namespace WindowServer {
  40. static MenuManager* s_the;
  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. }
  55. MenuManager::~MenuManager()
  56. {
  57. }
  58. bool MenuManager::is_open(const Menu& menu) const
  59. {
  60. for (size_t i = 0; i < m_open_menu_stack.size(); ++i) {
  61. if (&menu == m_open_menu_stack[i].ptr())
  62. return true;
  63. }
  64. return false;
  65. }
  66. const Gfx::Font& MenuManager::menu_font() const
  67. {
  68. return Gfx::Font::default_font();
  69. }
  70. const Gfx::Font& MenuManager::app_menu_font() const
  71. {
  72. return Gfx::Font::default_bold_font();
  73. }
  74. void MenuManager::draw()
  75. {
  76. auto& wm = WindowManager::the();
  77. auto palette = wm.palette();
  78. auto menubar_rect = this->menubar_rect();
  79. if (m_needs_window_resize) {
  80. m_window->set_rect(menubar_rect);
  81. AppletManager::the().calculate_applet_rects(window());
  82. m_needs_window_resize = false;
  83. }
  84. Gfx::Painter painter(*window().backing_store());
  85. painter.fill_rect(menubar_rect, palette.window());
  86. painter.draw_line({ 0, menubar_rect.bottom() }, { menubar_rect.right(), menubar_rect.bottom() }, palette.threed_shadow1());
  87. int index = 0;
  88. for_each_active_menubar_menu([&](Menu& menu) {
  89. Color text_color = palette.window_text();
  90. if (is_open(menu)) {
  91. painter.fill_rect(menu.rect_in_menubar(), palette.menu_selection());
  92. painter.draw_rect(menu.rect_in_menubar(), palette.menu_selection().darkened());
  93. text_color = palette.menu_selection_text();
  94. }
  95. painter.draw_text(
  96. menu.text_rect_in_menubar(),
  97. menu.name(),
  98. index == 1 ? app_menu_font() : menu_font(),
  99. Gfx::TextAlignment::CenterLeft,
  100. text_color);
  101. ++index;
  102. return IterationDecision::Continue;
  103. });
  104. AppletManager::the().draw();
  105. }
  106. void MenuManager::refresh()
  107. {
  108. if (!m_window)
  109. return;
  110. draw();
  111. window().invalidate();
  112. }
  113. void MenuManager::event(Core::Event& event)
  114. {
  115. if (WindowManager::the().active_window_is_modal())
  116. return Core::Object::event(event);
  117. if (static_cast<Event&>(event).is_mouse_event()) {
  118. handle_mouse_event(static_cast<MouseEvent&>(event));
  119. return;
  120. }
  121. if (static_cast<Event&>(event).is_key_event()) {
  122. auto& key_event = static_cast<const KeyEvent&>(event);
  123. if (key_event.type() == Event::KeyUp && key_event.key() == Key_Escape) {
  124. close_everyone();
  125. return;
  126. }
  127. if (event.type() == Event::KeyDown) {
  128. for_each_active_menubar_menu([&](Menu& menu) {
  129. if (is_open(menu))
  130. menu.dispatch_event(event);
  131. return IterationDecision::Continue;
  132. });
  133. }
  134. }
  135. return Core::Object::event(event);
  136. }
  137. void MenuManager::handle_mouse_event(MouseEvent& mouse_event)
  138. {
  139. bool handled_menubar_event = false;
  140. for_each_active_menubar_menu([&](Menu& menu) {
  141. if (menu.rect_in_menubar().contains(mouse_event.position())) {
  142. handle_menu_mouse_event(menu, mouse_event);
  143. handled_menubar_event = true;
  144. return IterationDecision::Break;
  145. }
  146. return IterationDecision::Continue;
  147. });
  148. if (handled_menubar_event)
  149. return;
  150. if (has_open_menu()) {
  151. auto* topmost_menu = m_open_menu_stack.last().ptr();
  152. ASSERT(topmost_menu);
  153. auto* window = topmost_menu->menu_window();
  154. ASSERT(window);
  155. ASSERT(window->is_visible());
  156. bool event_is_inside_current_menu = window->rect().contains(mouse_event.position());
  157. if (event_is_inside_current_menu) {
  158. WindowManager::the().set_hovered_window(window);
  159. auto translated_event = mouse_event.translated(-window->position());
  160. WindowManager::the().deliver_mouse_event(*window, translated_event);
  161. return;
  162. }
  163. if (topmost_menu->hovered_item())
  164. topmost_menu->clear_hovered_item();
  165. if (mouse_event.type() == Event::MouseDown || mouse_event.type() == Event::MouseUp) {
  166. auto* window_menu_of = topmost_menu->window_menu_of();
  167. if (window_menu_of) {
  168. bool event_is_inside_taskbar_button = window_menu_of->taskbar_rect().contains(mouse_event.position());
  169. if (event_is_inside_taskbar_button && !topmost_menu->is_window_menu_open()) {
  170. topmost_menu->set_window_menu_open(true);
  171. return;
  172. }
  173. }
  174. if (mouse_event.type() == Event::MouseDown) {
  175. close_bar();
  176. topmost_menu->set_window_menu_open(false);
  177. }
  178. }
  179. if (mouse_event.type() == Event::MouseMove) {
  180. for (auto& menu : m_open_menu_stack) {
  181. if (!menu)
  182. continue;
  183. if (!menu->menu_window()->rect().contains(mouse_event.position()))
  184. continue;
  185. WindowManager::the().set_hovered_window(menu->menu_window());
  186. auto translated_event = mouse_event.translated(-menu->menu_window()->position());
  187. WindowManager::the().deliver_mouse_event(*menu->menu_window(), translated_event);
  188. break;
  189. }
  190. }
  191. return;
  192. }
  193. AppletManager::the().dispatch_event(static_cast<Event&>(mouse_event));
  194. }
  195. void MenuManager::handle_menu_mouse_event(Menu& menu, const MouseEvent& event)
  196. {
  197. bool is_hover_with_any_menu_open = event.type() == MouseEvent::MouseMove
  198. && has_open_menu()
  199. && (m_open_menu_stack.first()->menubar() || m_open_menu_stack.first() == m_system_menu.ptr());
  200. bool is_mousedown_with_left_button = event.type() == MouseEvent::MouseDown && event.button() == MouseButton::Left;
  201. bool should_open_menu = &menu != m_current_menu && (is_hover_with_any_menu_open || is_mousedown_with_left_button);
  202. if (is_mousedown_with_left_button)
  203. m_bar_open = !m_bar_open;
  204. if (should_open_menu && m_bar_open) {
  205. open_menu(menu);
  206. return;
  207. }
  208. if (!m_bar_open)
  209. close_everyone();
  210. }
  211. void MenuManager::set_needs_window_resize()
  212. {
  213. m_needs_window_resize = true;
  214. }
  215. void MenuManager::close_all_menus_from_client(Badge<ClientConnection>, ClientConnection& client)
  216. {
  217. if (!has_open_menu())
  218. return;
  219. if (m_open_menu_stack.first()->client() != &client)
  220. return;
  221. close_everyone();
  222. }
  223. void MenuManager::close_everyone()
  224. {
  225. for (auto& menu : m_open_menu_stack) {
  226. if (menu && menu->menu_window())
  227. menu->menu_window()->set_visible(false);
  228. menu->clear_hovered_item();
  229. }
  230. m_open_menu_stack.clear();
  231. m_current_menu = nullptr;
  232. refresh();
  233. }
  234. void MenuManager::close_everyone_not_in_lineage(Menu& menu)
  235. {
  236. Vector<Menu*> menus_to_close;
  237. for (auto& open_menu : m_open_menu_stack) {
  238. if (!open_menu)
  239. continue;
  240. if (&menu == open_menu.ptr() || open_menu->is_menu_ancestor_of(menu))
  241. continue;
  242. menus_to_close.append(open_menu);
  243. }
  244. close_menus(menus_to_close);
  245. }
  246. void MenuManager::close_menus(const Vector<Menu*>& menus)
  247. {
  248. for (auto& menu : menus) {
  249. if (menu == m_current_menu)
  250. m_current_menu = nullptr;
  251. if (menu->menu_window())
  252. menu->menu_window()->set_visible(false);
  253. menu->clear_hovered_item();
  254. m_open_menu_stack.remove_first_matching([&](auto& entry) {
  255. return entry == menu;
  256. });
  257. }
  258. refresh();
  259. }
  260. static void collect_menu_subtree(Menu& menu, Vector<Menu*>& menus)
  261. {
  262. menus.append(&menu);
  263. for (int i = 0; i < menu.item_count(); ++i) {
  264. auto& item = menu.item(i);
  265. if (!item.is_submenu())
  266. continue;
  267. collect_menu_subtree(*const_cast<MenuItem&>(item).submenu(), menus);
  268. }
  269. }
  270. void MenuManager::close_menu_and_descendants(Menu& menu)
  271. {
  272. Vector<Menu*> menus_to_close;
  273. collect_menu_subtree(menu, menus_to_close);
  274. close_menus(menus_to_close);
  275. }
  276. void MenuManager::toggle_menu(Menu& menu)
  277. {
  278. if (is_open(menu)) {
  279. close_menu_and_descendants(menu);
  280. return;
  281. }
  282. open_menu(menu);
  283. }
  284. void MenuManager::open_menu(Menu& menu)
  285. {
  286. if (is_open(menu))
  287. return;
  288. if (!menu.is_empty()) {
  289. menu.redraw_if_theme_changed();
  290. auto& menu_window = menu.ensure_menu_window();
  291. menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() + 2 });
  292. menu_window.set_visible(true);
  293. }
  294. set_current_menu(&menu);
  295. refresh();
  296. }
  297. void MenuManager::set_current_menu(Menu* menu, bool is_submenu)
  298. {
  299. if (menu == m_current_menu)
  300. return;
  301. if (!is_submenu) {
  302. if (menu)
  303. close_everyone_not_in_lineage(*menu);
  304. else
  305. close_everyone();
  306. }
  307. if (!menu) {
  308. m_current_menu = nullptr;
  309. return;
  310. }
  311. m_current_menu = menu->make_weak_ptr();
  312. if (m_open_menu_stack.find([menu](auto& other) { return menu == other.ptr(); }).is_end())
  313. m_open_menu_stack.append(menu->make_weak_ptr());
  314. }
  315. void MenuManager::close_bar()
  316. {
  317. close_everyone();
  318. m_bar_open = false;
  319. }
  320. Gfx::Rect MenuManager::menubar_rect() const
  321. {
  322. return { 0, 0, Screen::the().rect().width(), 18 };
  323. }
  324. void MenuManager::set_current_menubar(MenuBar* menubar)
  325. {
  326. if (menubar)
  327. m_current_menubar = menubar->make_weak_ptr();
  328. else
  329. m_current_menubar = nullptr;
  330. #ifdef DEBUG_MENUS
  331. dbg() << "[WM] Current menubar is now " << menubar;
  332. #endif
  333. Gfx::Point next_menu_location { MenuManager::menubar_menu_margin() / 2, 0 };
  334. int index = 0;
  335. for_each_active_menubar_menu([&](Menu& menu) {
  336. int text_width = index == 1 ? Gfx::Font::default_bold_font().width(menu.name()) : Gfx::Font::default_font().width(menu.name());
  337. 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 });
  338. menu.set_text_rect_in_menubar({ next_menu_location, { text_width, menubar_rect().height() } });
  339. next_menu_location.move_by(menu.rect_in_menubar().width(), 0);
  340. ++index;
  341. return IterationDecision::Continue;
  342. });
  343. refresh();
  344. }
  345. void MenuManager::close_menubar(MenuBar& menubar)
  346. {
  347. if (current_menubar() == &menubar)
  348. set_current_menubar(nullptr);
  349. }
  350. void MenuManager::set_system_menu(Menu& menu)
  351. {
  352. m_system_menu = menu.make_weak_ptr();
  353. set_current_menubar(m_current_menubar);
  354. }
  355. void MenuManager::did_change_theme()
  356. {
  357. ++m_theme_index;
  358. refresh();
  359. }
  360. }