MenuManager.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/FileSystemPath.h>
  27. #include <AK/QuickSort.h>
  28. #include <LibCore/DirIterator.h>
  29. #include <LibGfx/Font.h>
  30. #include <LibGfx/Painter.h>
  31. #include <WindowServer/MenuManager.h>
  32. #include <WindowServer/Screen.h>
  33. #include <WindowServer/WindowManager.h>
  34. #include <unistd.h>
  35. //#define DEBUG_MENUS
  36. namespace WindowServer {
  37. static MenuManager* s_the;
  38. MenuManager& MenuManager::the()
  39. {
  40. ASSERT(s_the);
  41. return *s_the;
  42. }
  43. MenuManager::MenuManager()
  44. {
  45. s_the = this;
  46. m_username = getlogin();
  47. m_needs_window_resize = true;
  48. HashTable<String> seen_app_categories;
  49. {
  50. Core::DirIterator dt("/res/apps", Core::DirIterator::SkipDots);
  51. while (dt.has_next()) {
  52. auto af_name = dt.next_path();
  53. auto af_path = String::format("/res/apps/%s", af_name.characters());
  54. auto af = Core::ConfigFile::open(af_path);
  55. if (!af->has_key("App", "Name") || !af->has_key("App", "Executable"))
  56. continue;
  57. auto app_name = af->read_entry("App", "Name");
  58. auto app_executable = af->read_entry("App", "Executable");
  59. auto app_category = af->read_entry("App", "Category");
  60. auto app_icon_path = af->read_entry("Icons", "16x16");
  61. m_apps.append({ app_executable, app_name, app_icon_path, app_category });
  62. seen_app_categories.set(app_category);
  63. }
  64. }
  65. Vector<String> sorted_app_categories;
  66. for (auto& category : seen_app_categories)
  67. sorted_app_categories.append(category);
  68. quick_sort(sorted_app_categories.begin(), sorted_app_categories.end(), [](auto& a, auto& b) { return a < b; });
  69. u8 system_menu_name[] = { 0xc3, 0xb8, 0 };
  70. m_system_menu = Menu::construct(nullptr, -1, String((const char*)system_menu_name));
  71. // First we construct all the necessary app category submenus.
  72. for (const auto& category : sorted_app_categories) {
  73. if (m_app_category_menus.contains(category))
  74. continue;
  75. auto category_menu = Menu::construct(nullptr, 5000 + m_app_category_menus.size(), category);
  76. category_menu->on_item_activation = [this](auto& item) {
  77. if (item.identifier() >= 1 && item.identifier() <= 1u + m_apps.size() - 1) {
  78. if (fork() == 0) {
  79. const auto& bin = m_apps[item.identifier() - 1].executable;
  80. execl(bin.characters(), bin.characters(), nullptr);
  81. ASSERT_NOT_REACHED();
  82. }
  83. }
  84. };
  85. auto item = make<MenuItem>(*m_system_menu, -1, category);
  86. item->set_submenu_id(category_menu->menu_id());
  87. m_system_menu->add_item(move(item));
  88. m_app_category_menus.set(category, move(category_menu));
  89. }
  90. // Then we create and insert all the app menu items into the right place.
  91. int app_identifier = 1;
  92. for (const auto& app : m_apps) {
  93. RefPtr<Gfx::Bitmap> icon;
  94. if (!app.icon_path.is_empty())
  95. icon = Gfx::Bitmap::load_from_file(app.icon_path);
  96. auto parent_menu = m_app_category_menus.get(app.category).value_or(*m_system_menu);
  97. parent_menu->add_item(make<MenuItem>(*m_system_menu, app_identifier++, app.name, String(), true, false, false, icon));
  98. }
  99. m_system_menu->add_item(make<MenuItem>(*m_system_menu, MenuItem::Separator));
  100. m_themes_menu = Menu::construct(nullptr, 9000, "Themes");
  101. auto themes_menu_item = make<MenuItem>(*m_system_menu, 100, "Themes");
  102. themes_menu_item->set_submenu_id(m_themes_menu->menu_id());
  103. m_system_menu->add_item(move(themes_menu_item));
  104. {
  105. Core::DirIterator dt("/res/themes", Core::DirIterator::SkipDots);
  106. while (dt.has_next()) {
  107. auto theme_name = dt.next_path();
  108. auto theme_path = String::format("/res/themes/%s", theme_name.characters());
  109. m_themes.append({ FileSystemPath(theme_name).title(), theme_path });
  110. }
  111. quick_sort(m_themes.begin(), m_themes.end(), [](auto& a, auto& b) { return a.name < b.name; });
  112. }
  113. {
  114. int theme_identifier = 9000;
  115. for (auto& theme : m_themes) {
  116. m_themes_menu->add_item(make<MenuItem>(*m_themes_menu, theme_identifier++, theme.name));
  117. }
  118. }
  119. m_themes_menu->on_item_activation = [this](MenuItem& item) {
  120. auto& theme = m_themes[(int)item.identifier() - 9000];
  121. WindowManager::the().update_theme(theme.path, theme.name);
  122. ++m_theme_index;
  123. };
  124. m_system_menu->add_item(make<MenuItem>(*m_system_menu, MenuItem::Separator));
  125. m_system_menu->add_item(make<MenuItem>(*m_system_menu, 100, "Reload WM Config File"));
  126. m_system_menu->add_item(make<MenuItem>(*m_system_menu, MenuItem::Separator));
  127. m_system_menu->add_item(make<MenuItem>(*m_system_menu, 200, "About...", String(), true, false, false, Gfx::Bitmap::load_from_file("/res/icons/16x16/ladybug.png")));
  128. m_system_menu->add_item(make<MenuItem>(*m_system_menu, MenuItem::Separator));
  129. m_system_menu->add_item(make<MenuItem>(*m_system_menu, 300, "Shutdown..."));
  130. m_system_menu->on_item_activation = [this](MenuItem& item) {
  131. if (item.identifier() >= 1 && item.identifier() <= 1u + m_apps.size() - 1) {
  132. if (fork() == 0) {
  133. const auto& bin = m_apps[item.identifier() - 1].executable;
  134. execl(bin.characters(), bin.characters(), nullptr);
  135. ASSERT_NOT_REACHED();
  136. }
  137. }
  138. switch (item.identifier()) {
  139. case 100:
  140. WindowManager::the().reload_config(true);
  141. break;
  142. case 200:
  143. if (fork() == 0) {
  144. execl("/bin/About", "/bin/About", nullptr);
  145. ASSERT_NOT_REACHED();
  146. }
  147. return;
  148. case 300:
  149. if (fork() == 0) {
  150. execl("/bin/SystemDialog", "/bin/SystemDialog", "--shutdown", nullptr);
  151. ASSERT_NOT_REACHED();
  152. }
  153. return;
  154. }
  155. #ifdef DEBUG_MENUS
  156. dbg() << "Menu 1 item activated: " << item.text();
  157. #endif
  158. };
  159. // NOTE: This ensures that the system menu has the correct dimensions.
  160. set_current_menubar(nullptr);
  161. m_window = Window::construct(*this, WindowType::Menubar);
  162. m_window->set_rect(menubar_rect());
  163. }
  164. MenuManager::~MenuManager()
  165. {
  166. }
  167. bool MenuManager::is_open(const Menu& menu) const
  168. {
  169. for (int i = 0; i < m_open_menu_stack.size(); ++i) {
  170. if (&menu == m_open_menu_stack[i].ptr())
  171. return true;
  172. }
  173. return false;
  174. }
  175. const Gfx::Font& MenuManager::menu_font() const
  176. {
  177. return Gfx::Font::default_font();
  178. }
  179. const Gfx::Font& MenuManager::app_menu_font() const
  180. {
  181. return Gfx::Font::default_bold_font();
  182. }
  183. void MenuManager::draw()
  184. {
  185. auto& wm = WindowManager::the();
  186. auto palette = wm.palette();
  187. auto menubar_rect = this->menubar_rect();
  188. if (m_needs_window_resize) {
  189. int username_width = Gfx::Font::default_bold_font().width(m_username);
  190. m_username_rect = {
  191. menubar_rect.right() - menubar_menu_margin() / 2 - Gfx::Font::default_bold_font().width(m_username),
  192. menubar_rect.y(),
  193. username_width,
  194. menubar_rect.height()
  195. };
  196. int right_edge_x = m_username_rect.left() - 4;
  197. for (auto& existing_applet : m_applets) {
  198. if (!existing_applet)
  199. continue;
  200. Gfx::Rect new_applet_rect(right_edge_x - existing_applet->size().width(), 0, existing_applet->size().width(), existing_applet->size().height());
  201. Gfx::Rect dummy_menubar_rect(0, 0, 0, 18);
  202. new_applet_rect.center_vertically_within(dummy_menubar_rect);
  203. existing_applet->set_rect_in_menubar(new_applet_rect);
  204. right_edge_x = existing_applet->rect_in_menubar().x() - 4;
  205. }
  206. m_window->set_rect(menubar_rect);
  207. m_needs_window_resize = false;
  208. }
  209. Gfx::Painter painter(*window().backing_store());
  210. painter.fill_rect(menubar_rect, palette.window());
  211. painter.draw_line({ 0, menubar_rect.bottom() }, { menubar_rect.right(), menubar_rect.bottom() }, palette.threed_shadow1());
  212. int index = 0;
  213. for_each_active_menubar_menu([&](Menu& menu) {
  214. Color text_color = palette.window_text();
  215. if (is_open(menu)) {
  216. painter.fill_rect(menu.rect_in_menubar(), palette.menu_selection());
  217. painter.draw_rect(menu.rect_in_menubar(), palette.menu_selection().darkened());
  218. text_color = Color::White;
  219. }
  220. painter.draw_text(
  221. menu.text_rect_in_menubar(),
  222. menu.name(),
  223. index == 1 ? app_menu_font() : menu_font(),
  224. Gfx::TextAlignment::CenterLeft,
  225. text_color);
  226. ++index;
  227. return IterationDecision::Continue;
  228. });
  229. painter.draw_text(m_username_rect, m_username, Gfx::Font::default_bold_font(), Gfx::TextAlignment::CenterRight, palette.window_text());
  230. for (auto& applet : m_applets) {
  231. if (!applet)
  232. continue;
  233. draw_applet(*applet);
  234. }
  235. }
  236. void MenuManager::tick_clock()
  237. {
  238. refresh();
  239. }
  240. void MenuManager::refresh()
  241. {
  242. if (!m_window)
  243. return;
  244. draw();
  245. window().invalidate();
  246. }
  247. void MenuManager::event(Core::Event& event)
  248. {
  249. if (WindowManager::the().active_window_is_modal())
  250. return Core::Object::event(event);
  251. if (event.type() == Event::MouseMove || event.type() == Event::MouseUp || event.type() == Event::MouseDown || event.type() == Event::MouseWheel) {
  252. auto& mouse_event = static_cast<MouseEvent&>(event);
  253. for_each_active_menubar_menu([&](Menu& menu) {
  254. if (menu.rect_in_menubar().contains(mouse_event.position())) {
  255. handle_menu_mouse_event(menu, mouse_event);
  256. return IterationDecision::Break;
  257. }
  258. return IterationDecision::Continue;
  259. });
  260. for (auto& applet : m_applets) {
  261. if (!applet)
  262. continue;
  263. if (!applet->rect_in_menubar().contains(mouse_event.position()))
  264. continue;
  265. auto local_event = mouse_event.translated(-applet->rect_in_menubar().location());
  266. applet->event(local_event);
  267. }
  268. }
  269. if (static_cast<Event&>(event).is_key_event()) {
  270. auto& key_event = static_cast<const KeyEvent&>(event);
  271. if (key_event.type() == Event::KeyUp && key_event.key() == Key_Escape) {
  272. close_everyone();
  273. return;
  274. }
  275. if (event.type() == Event::KeyDown) {
  276. for_each_active_menubar_menu([&](Menu& menu) {
  277. if (is_open(menu))
  278. menu.dispatch_event(event);
  279. return IterationDecision::Continue;
  280. });
  281. }
  282. }
  283. return Core::Object::event(event);
  284. }
  285. void MenuManager::handle_menu_mouse_event(Menu& menu, const MouseEvent& event)
  286. {
  287. bool is_hover_with_any_menu_open = event.type() == MouseEvent::MouseMove
  288. && !m_open_menu_stack.is_empty()
  289. && (m_open_menu_stack.first()->menubar() || m_open_menu_stack.first() == m_system_menu.ptr());
  290. bool is_mousedown_with_left_button = event.type() == MouseEvent::MouseDown && event.button() == MouseButton::Left;
  291. bool should_open_menu = &menu != m_current_menu && (is_hover_with_any_menu_open || is_mousedown_with_left_button);
  292. if (is_mousedown_with_left_button)
  293. m_bar_open = !m_bar_open;
  294. if (should_open_menu && m_bar_open) {
  295. open_menu(menu);
  296. return;
  297. }
  298. if (!m_bar_open)
  299. close_everyone();
  300. }
  301. void MenuManager::set_needs_window_resize()
  302. {
  303. m_needs_window_resize = true;
  304. }
  305. void MenuManager::close_all_menus_from_client(Badge<ClientConnection>, ClientConnection& client)
  306. {
  307. if (m_open_menu_stack.is_empty())
  308. return;
  309. if (m_open_menu_stack.first()->client() != &client)
  310. return;
  311. close_everyone();
  312. }
  313. void MenuManager::close_everyone()
  314. {
  315. for (auto& menu : m_open_menu_stack) {
  316. if (menu && menu->menu_window())
  317. menu->menu_window()->set_visible(false);
  318. menu->clear_hovered_item();
  319. }
  320. m_open_menu_stack.clear();
  321. m_current_menu = nullptr;
  322. refresh();
  323. }
  324. void MenuManager::close_everyone_not_in_lineage(Menu& menu)
  325. {
  326. Vector<Menu*> menus_to_close;
  327. for (auto& open_menu : m_open_menu_stack) {
  328. if (!open_menu)
  329. continue;
  330. if (&menu == open_menu.ptr() || open_menu->is_menu_ancestor_of(menu))
  331. continue;
  332. menus_to_close.append(open_menu);
  333. }
  334. close_menus(menus_to_close);
  335. }
  336. void MenuManager::close_menus(const Vector<Menu*>& menus)
  337. {
  338. for (auto& menu : menus) {
  339. if (menu == m_current_menu)
  340. m_current_menu = nullptr;
  341. if (menu->menu_window())
  342. menu->menu_window()->set_visible(false);
  343. menu->clear_hovered_item();
  344. m_open_menu_stack.remove_first_matching([&](auto& entry) {
  345. return entry == menu;
  346. });
  347. }
  348. refresh();
  349. }
  350. static void collect_menu_subtree(Menu& menu, Vector<Menu*>& menus)
  351. {
  352. menus.append(&menu);
  353. for (int i = 0; i < menu.item_count(); ++i) {
  354. auto& item = menu.item(i);
  355. if (!item.is_submenu())
  356. continue;
  357. collect_menu_subtree(*const_cast<MenuItem&>(item).submenu(), menus);
  358. }
  359. }
  360. void MenuManager::close_menu_and_descendants(Menu& menu)
  361. {
  362. Vector<Menu*> menus_to_close;
  363. collect_menu_subtree(menu, menus_to_close);
  364. close_menus(menus_to_close);
  365. }
  366. void MenuManager::toggle_menu(Menu& menu)
  367. {
  368. if (is_open(menu)) {
  369. close_menu_and_descendants(menu);
  370. return;
  371. }
  372. open_menu(menu);
  373. }
  374. void MenuManager::open_menu(Menu& menu)
  375. {
  376. if (is_open(menu))
  377. return;
  378. if (!menu.is_empty()) {
  379. menu.redraw_if_theme_changed();
  380. auto& menu_window = menu.ensure_menu_window();
  381. menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() + 2 });
  382. menu_window.set_visible(true);
  383. }
  384. set_current_menu(&menu);
  385. refresh();
  386. }
  387. void MenuManager::set_current_menu(Menu* menu, bool is_submenu)
  388. {
  389. if (!is_submenu) {
  390. if (menu)
  391. close_everyone_not_in_lineage(*menu);
  392. else
  393. close_everyone();
  394. }
  395. if (!menu) {
  396. m_current_menu = nullptr;
  397. return;
  398. }
  399. m_open_menu_stack.append(menu->make_weak_ptr());
  400. m_current_menu = menu->make_weak_ptr();
  401. }
  402. void MenuManager::close_bar()
  403. {
  404. close_everyone();
  405. m_bar_open = false;
  406. }
  407. void MenuManager::add_applet(Window& applet)
  408. {
  409. int right_edge_x = m_username_rect.left() - 4;
  410. for (auto& existing_applet : m_applets) {
  411. if (existing_applet)
  412. right_edge_x = existing_applet->rect_in_menubar().x() - 4;
  413. }
  414. Gfx::Rect new_applet_rect(right_edge_x - applet.size().width(), 0, applet.size().width(), applet.size().height());
  415. Gfx::Rect dummy_menubar_rect(0, 0, 0, 18);
  416. new_applet_rect.center_vertically_within(dummy_menubar_rect);
  417. applet.set_rect_in_menubar(new_applet_rect);
  418. m_applets.append(applet.make_weak_ptr());
  419. }
  420. void MenuManager::remove_applet(Window& applet)
  421. {
  422. m_applets.remove_first_matching([&](auto& entry) {
  423. return &applet == entry.ptr();
  424. });
  425. }
  426. void MenuManager::draw_applet(const Window& applet)
  427. {
  428. if (!applet.backing_store())
  429. return;
  430. Gfx::Painter painter(*window().backing_store());
  431. painter.fill_rect(applet.rect_in_menubar(), WindowManager::the().palette().window());
  432. painter.blit(applet.rect_in_menubar().location(), *applet.backing_store(), applet.backing_store()->rect());
  433. }
  434. void MenuManager::invalidate_applet(const Window& applet, const Gfx::Rect& rect)
  435. {
  436. draw_applet(applet);
  437. window().invalidate(rect.translated(applet.rect_in_menubar().location()));
  438. }
  439. Gfx::Rect MenuManager::menubar_rect() const
  440. {
  441. return { 0, 0, Screen::the().rect().width(), 18 };
  442. }
  443. Menu* MenuManager::find_internal_menu_by_id(int menu_id)
  444. {
  445. if (m_themes_menu->menu_id() == menu_id)
  446. return m_themes_menu.ptr();
  447. for (auto& it : m_app_category_menus) {
  448. if (menu_id == it.value->menu_id())
  449. return it.value;
  450. }
  451. return nullptr;
  452. }
  453. void MenuManager::set_current_menubar(MenuBar* menubar)
  454. {
  455. if (menubar)
  456. m_current_menubar = menubar->make_weak_ptr();
  457. else
  458. m_current_menubar = nullptr;
  459. #ifdef DEBUG_MENUS
  460. dbg() << "[WM] Current menubar is now " << menubar;
  461. #endif
  462. Gfx::Point next_menu_location { MenuManager::menubar_menu_margin() / 2, 0 };
  463. int index = 0;
  464. for_each_active_menubar_menu([&](Menu& menu) {
  465. int text_width = index == 1 ? Gfx::Font::default_bold_font().width(menu.name()) : Gfx::Font::default_font().width(menu.name());
  466. 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 });
  467. menu.set_text_rect_in_menubar({ next_menu_location, { text_width, menubar_rect().height() } });
  468. next_menu_location.move_by(menu.rect_in_menubar().width(), 0);
  469. ++index;
  470. return IterationDecision::Continue;
  471. });
  472. refresh();
  473. }
  474. void MenuManager::close_menubar(MenuBar& menubar)
  475. {
  476. if (current_menubar() == &menubar)
  477. set_current_menubar(nullptr);
  478. }
  479. }