Menu.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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 "Menu.h"
  8. #include "Event.h"
  9. #include "MenuItem.h"
  10. #include "MenuManager.h"
  11. #include "Screen.h"
  12. #include "Window.h"
  13. #include "WindowManager.h"
  14. #include <AK/CharacterTypes.h>
  15. #include <LibGfx/Bitmap.h>
  16. #include <LibGfx/CharacterBitmap.h>
  17. #include <LibGfx/Font.h>
  18. #include <LibGfx/Painter.h>
  19. #include <LibGfx/StylePainter.h>
  20. #include <LibGfx/Triangle.h>
  21. #include <WindowServer/ClientConnection.h>
  22. #include <WindowServer/WindowClientEndpoint.h>
  23. namespace WindowServer {
  24. u32 find_ampersand_shortcut_character(const StringView& string)
  25. {
  26. Utf8View utf8_view { string };
  27. for (auto it = utf8_view.begin(); it != utf8_view.end(); ++it) {
  28. if (*it == '&') {
  29. ++it;
  30. if (it != utf8_view.end() && *it != '&')
  31. return *it;
  32. }
  33. }
  34. return 0;
  35. }
  36. Menu::Menu(ClientConnection* client, int menu_id, String name)
  37. : Core::Object(client)
  38. , m_client(client)
  39. , m_menu_id(menu_id)
  40. , m_name(move(name))
  41. {
  42. m_alt_shortcut_character = find_ampersand_shortcut_character(m_name);
  43. }
  44. Menu::~Menu()
  45. {
  46. }
  47. const Gfx::Font& Menu::font() const
  48. {
  49. return Gfx::FontDatabase::default_font();
  50. }
  51. static const char* s_checked_bitmap_data = {
  52. " "
  53. " # "
  54. " ## "
  55. " ### "
  56. " ## ### "
  57. " ##### "
  58. " ### "
  59. " # "
  60. " "
  61. };
  62. static const char* s_submenu_arrow_bitmap_data = {
  63. " "
  64. " # "
  65. " ## "
  66. " ### "
  67. " #### "
  68. " ### "
  69. " ## "
  70. " # "
  71. " "
  72. };
  73. static Gfx::CharacterBitmap* s_checked_bitmap;
  74. static const int s_checked_bitmap_width = 9;
  75. static const int s_checked_bitmap_height = 9;
  76. static const int s_submenu_arrow_bitmap_width = 9;
  77. static const int s_submenu_arrow_bitmap_height = 9;
  78. static const int s_item_icon_width = 16;
  79. static const int s_stripe_width = 24;
  80. int Menu::content_width() const
  81. {
  82. int widest_text = 0;
  83. int widest_shortcut = 0;
  84. for (auto& item : m_items) {
  85. if (item.type() != MenuItem::Text)
  86. continue;
  87. auto& use_font = item.is_default() ? font().bold_variant() : font();
  88. int text_width = use_font.width(Gfx::parse_ampersand_string(item.text()));
  89. if (!item.shortcut_text().is_empty()) {
  90. int shortcut_width = use_font.width(item.shortcut_text());
  91. widest_shortcut = max(shortcut_width, widest_shortcut);
  92. }
  93. widest_text = max(widest_text, text_width);
  94. }
  95. int widest_item = widest_text + s_stripe_width;
  96. if (widest_shortcut)
  97. widest_item += padding_between_text_and_shortcut() + widest_shortcut;
  98. return max(widest_item, rect_in_window_menubar().width()) + horizontal_padding() + frame_thickness() * 2;
  99. }
  100. void Menu::redraw()
  101. {
  102. if (!menu_window())
  103. return;
  104. draw();
  105. menu_window()->invalidate();
  106. }
  107. Window& Menu::ensure_menu_window()
  108. {
  109. if (m_menu_window)
  110. return *m_menu_window;
  111. int width = this->content_width();
  112. Gfx::IntPoint next_item_location(frame_thickness(), frame_thickness());
  113. for (auto& item : m_items) {
  114. int height = 0;
  115. if (item.type() == MenuItem::Text)
  116. height = item_height();
  117. else if (item.type() == MenuItem::Separator)
  118. height = 8;
  119. item.set_rect({ next_item_location, { width - frame_thickness() * 2, height } });
  120. next_item_location.translate_by(0, height);
  121. }
  122. int window_height_available = Screen::the().height() - frame_thickness() * 2;
  123. int max_window_height = (window_height_available / item_height()) * item_height() + frame_thickness() * 2;
  124. int content_height = m_items.is_empty() ? 0 : (m_items.last().rect().bottom() + 1) + frame_thickness();
  125. int window_height = min(max_window_height, content_height);
  126. if (window_height < content_height) {
  127. m_scrollable = true;
  128. m_max_scroll_offset = item_count() - window_height / item_height() + 2;
  129. }
  130. auto window = Window::construct(*this, WindowType::Menu);
  131. window->set_visible(false);
  132. window->set_rect(0, 0, width, window_height);
  133. m_menu_window = move(window);
  134. draw();
  135. return *m_menu_window;
  136. }
  137. size_t Menu::visible_item_count() const
  138. {
  139. if (!is_scrollable())
  140. return m_items.size();
  141. VERIFY(m_menu_window);
  142. // Make space for up/down arrow indicators
  143. return m_menu_window->height() / item_height() - 2;
  144. }
  145. void Menu::draw()
  146. {
  147. auto palette = WindowManager::the().palette();
  148. m_theme_index_at_last_paint = MenuManager::the().theme_index();
  149. VERIFY(menu_window());
  150. VERIFY(menu_window()->backing_store());
  151. Gfx::Painter painter(*menu_window()->backing_store());
  152. Gfx::IntRect rect { {}, menu_window()->size() };
  153. painter.draw_rect(rect, Color::Black);
  154. painter.fill_rect(rect.shrunken(2, 2), palette.menu_base());
  155. int width = this->content_width();
  156. if (!s_checked_bitmap)
  157. s_checked_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref();
  158. bool has_checkable_items = false;
  159. bool has_items_with_icon = false;
  160. for (auto& item : m_items) {
  161. has_checkable_items = has_checkable_items | item.is_checkable();
  162. has_items_with_icon = has_items_with_icon | !!item.icon();
  163. }
  164. Gfx::IntRect stripe_rect { frame_thickness(), frame_thickness(), s_stripe_width, menu_window()->height() - frame_thickness() * 2 };
  165. painter.fill_rect(stripe_rect, palette.menu_stripe());
  166. int visible_item_count = this->visible_item_count();
  167. if (is_scrollable()) {
  168. bool can_go_up = m_scroll_offset > 0;
  169. bool can_go_down = m_scroll_offset < m_max_scroll_offset;
  170. Gfx::IntRect up_indicator_rect { frame_thickness(), frame_thickness(), content_width(), item_height() };
  171. painter.draw_text(up_indicator_rect, "\xE2\xAC\x86", Gfx::TextAlignment::Center, can_go_up ? palette.menu_base_text() : palette.color(ColorRole::DisabledText));
  172. Gfx::IntRect down_indicator_rect { frame_thickness(), menu_window()->height() - item_height() - frame_thickness(), content_width(), item_height() };
  173. painter.draw_text(down_indicator_rect, "\xE2\xAC\x87", Gfx::TextAlignment::Center, can_go_down ? palette.menu_base_text() : palette.color(ColorRole::DisabledText));
  174. }
  175. for (int i = 0; i < visible_item_count; ++i) {
  176. auto& item = m_items.at(m_scroll_offset + i);
  177. if (item.type() == MenuItem::Text) {
  178. Color text_color = palette.menu_base_text();
  179. if (&item == hovered_item() && item.is_enabled()) {
  180. painter.fill_rect(item.rect(), palette.menu_selection());
  181. painter.draw_rect(item.rect(), palette.menu_selection().darkened());
  182. text_color = palette.menu_selection_text();
  183. } else if (!item.is_enabled()) {
  184. text_color = Color::MidGray;
  185. }
  186. Gfx::IntRect text_rect = item.rect().translated(stripe_rect.width() + 6, 0);
  187. if (item.is_checkable()) {
  188. if (item.is_exclusive()) {
  189. Gfx::IntRect radio_rect { item.rect().x() + 5, 0, 12, 12 };
  190. radio_rect.center_vertically_within(text_rect);
  191. Gfx::StylePainter::paint_radio_button(painter, radio_rect, palette, item.is_checked(), false);
  192. } else {
  193. Gfx::IntRect checkmark_rect { item.rect().x() + 7, 0, s_checked_bitmap_width, s_checked_bitmap_height };
  194. checkmark_rect.center_vertically_within(text_rect);
  195. Gfx::IntRect checkbox_rect = checkmark_rect.inflated(4, 4);
  196. painter.fill_rect(checkbox_rect, palette.base());
  197. Gfx::StylePainter::paint_frame(painter, checkbox_rect, palette, Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
  198. if (item.is_checked()) {
  199. painter.draw_bitmap(checkmark_rect.location(), *s_checked_bitmap, palette.button_text());
  200. }
  201. }
  202. } else if (item.icon()) {
  203. Gfx::IntRect icon_rect { item.rect().x() + 3, 0, s_item_icon_width, s_item_icon_width };
  204. icon_rect.center_vertically_within(text_rect);
  205. if (&item == hovered_item() && item.is_enabled()) {
  206. auto shadow_color = palette.menu_selection().darkened(0.7f);
  207. painter.blit_filtered(icon_rect.location().translated(1, 1), *item.icon(), item.icon()->rect(), [&shadow_color](auto) {
  208. return shadow_color;
  209. });
  210. icon_rect.translate_by(-1, -1);
  211. }
  212. if (item.is_enabled())
  213. painter.blit(icon_rect.location(), *item.icon(), item.icon()->rect());
  214. else
  215. painter.blit_disabled(icon_rect.location(), *item.icon(), item.icon()->rect(), palette);
  216. }
  217. auto& previous_font = painter.font();
  218. if (item.is_default())
  219. painter.set_font(previous_font.bold_variant());
  220. painter.draw_ui_text(text_rect, item.text(), painter.font(), Gfx::TextAlignment::CenterLeft, text_color);
  221. if (!item.shortcut_text().is_empty()) {
  222. painter.draw_text(item.rect().translated(-right_padding(), 0), item.shortcut_text(), Gfx::TextAlignment::CenterRight, text_color);
  223. }
  224. painter.set_font(previous_font);
  225. if (item.is_submenu()) {
  226. static auto& submenu_arrow_bitmap = Gfx::CharacterBitmap::create_from_ascii(s_submenu_arrow_bitmap_data, s_submenu_arrow_bitmap_width, s_submenu_arrow_bitmap_height).leak_ref();
  227. Gfx::IntRect submenu_arrow_rect {
  228. item.rect().right() - s_submenu_arrow_bitmap_width - 2,
  229. 0,
  230. s_submenu_arrow_bitmap_width,
  231. s_submenu_arrow_bitmap_height
  232. };
  233. submenu_arrow_rect.center_vertically_within(item.rect());
  234. painter.draw_bitmap(submenu_arrow_rect.location(), submenu_arrow_bitmap, text_color);
  235. }
  236. } else if (item.type() == MenuItem::Separator) {
  237. Gfx::IntPoint p1(item.rect().translated(stripe_rect.width() + 4, 0).x(), item.rect().center().y() - 1);
  238. Gfx::IntPoint p2(width - 7, item.rect().center().y() - 1);
  239. painter.draw_line(p1, p2, palette.threed_shadow1());
  240. painter.draw_line(p1.translated(0, 1), p2.translated(0, 1), palette.threed_highlight());
  241. }
  242. }
  243. }
  244. MenuItem* Menu::hovered_item() const
  245. {
  246. if (m_hovered_item_index == -1)
  247. return nullptr;
  248. return const_cast<MenuItem*>(&item(m_hovered_item_index));
  249. }
  250. void Menu::update_for_new_hovered_item(bool make_input)
  251. {
  252. if (auto* hovered_item = this->hovered_item()) {
  253. if (hovered_item->is_submenu()) {
  254. VERIFY(menu_window());
  255. MenuManager::the().close_everyone_not_in_lineage(*hovered_item->submenu());
  256. hovered_item->submenu()->do_popup(hovered_item->rect().top_right().translated(menu_window()->rect().location()), make_input, true);
  257. } else {
  258. MenuManager::the().close_everyone_not_in_lineage(*this);
  259. ensure_menu_window();
  260. set_visible(true);
  261. }
  262. }
  263. redraw();
  264. }
  265. void Menu::open_hovered_item(bool leave_menu_open)
  266. {
  267. VERIFY(menu_window());
  268. VERIFY(menu_window()->is_visible());
  269. if (!hovered_item())
  270. return;
  271. if (hovered_item()->is_enabled())
  272. did_activate(*hovered_item(), leave_menu_open);
  273. if (!leave_menu_open)
  274. clear_hovered_item();
  275. }
  276. void Menu::descend_into_submenu_at_hovered_item()
  277. {
  278. VERIFY(hovered_item());
  279. auto submenu = hovered_item()->submenu();
  280. VERIFY(submenu);
  281. MenuManager::the().open_menu(*submenu, false);
  282. submenu->set_hovered_index(0);
  283. VERIFY(submenu->hovered_item()->type() != MenuItem::Separator);
  284. }
  285. void Menu::handle_mouse_move_event(const MouseEvent& mouse_event)
  286. {
  287. VERIFY(menu_window());
  288. MenuManager::the().set_current_menu(this);
  289. if (hovered_item() && hovered_item()->is_submenu()) {
  290. auto item = *hovered_item();
  291. auto submenu_top_left = item.rect().location() + Gfx::IntPoint { item.rect().width(), 0 };
  292. auto submenu_bottom_left = submenu_top_left + Gfx::IntPoint { 0, item.submenu()->menu_window()->height() };
  293. auto safe_hover_triangle = Gfx::Triangle { m_last_position_in_hover, submenu_top_left, submenu_bottom_left };
  294. m_last_position_in_hover = mouse_event.position();
  295. // Don't update the hovered item if mouse is moving towards a submenu
  296. if (safe_hover_triangle.contains(mouse_event.position()))
  297. return;
  298. }
  299. int index = item_index_at(mouse_event.position());
  300. set_hovered_index(index);
  301. }
  302. void Menu::event(Core::Event& event)
  303. {
  304. if (event.type() == Event::MouseMove) {
  305. handle_mouse_move_event(static_cast<const MouseEvent&>(event));
  306. return;
  307. }
  308. if (event.type() == Event::MouseUp) {
  309. open_hovered_item(static_cast<MouseEvent&>(event).modifiers() & KeyModifier::Mod_Ctrl);
  310. return;
  311. }
  312. if (event.type() == Event::MouseWheel && is_scrollable()) {
  313. VERIFY(menu_window());
  314. auto& mouse_event = static_cast<const MouseEvent&>(event);
  315. m_scroll_offset += mouse_event.wheel_delta();
  316. m_scroll_offset = clamp(m_scroll_offset, 0, m_max_scroll_offset);
  317. int index = item_index_at(mouse_event.position());
  318. set_hovered_index(index);
  319. return;
  320. }
  321. if (event.type() == Event::KeyDown) {
  322. auto key = static_cast<KeyEvent&>(event).key();
  323. if (!(key == Key_Up || key == Key_Down || key == Key_Left || key == Key_Right || key == Key_Return))
  324. return;
  325. VERIFY(menu_window());
  326. VERIFY(menu_window()->is_visible());
  327. if (!hovered_item()) {
  328. if (key == Key_Up) {
  329. // Default to the last enabled, non-separator item on key press if one has not been selected yet
  330. for (auto i = static_cast<int>(m_items.size()) - 1; i >= 0; i--) {
  331. auto& item = m_items.at(i);
  332. if (item.type() != MenuItem::Separator && item.is_enabled()) {
  333. set_hovered_index(i, key == Key_Right);
  334. break;
  335. }
  336. }
  337. } else {
  338. // Default to the first enabled, non-separator item on key press if one has not been selected yet
  339. int counter = 0;
  340. for (const auto& item : m_items) {
  341. if (item.type() != MenuItem::Separator && item.is_enabled()) {
  342. set_hovered_index(counter, key == Key_Right);
  343. break;
  344. }
  345. counter++;
  346. }
  347. }
  348. return;
  349. }
  350. if (key == Key_Up) {
  351. VERIFY(item(0).type() != MenuItem::Separator);
  352. if (is_scrollable() && m_hovered_item_index == 0)
  353. return;
  354. auto original_index = m_hovered_item_index;
  355. auto new_index = original_index;
  356. do {
  357. if (new_index == 0)
  358. new_index = m_items.size() - 1;
  359. else
  360. --new_index;
  361. if (new_index == original_index)
  362. return;
  363. } while (item(new_index).type() == MenuItem::Separator || !item(new_index).is_enabled());
  364. VERIFY(new_index >= 0);
  365. VERIFY(new_index <= static_cast<int>(m_items.size()) - 1);
  366. if (is_scrollable() && new_index < m_scroll_offset)
  367. --m_scroll_offset;
  368. set_hovered_index(new_index);
  369. return;
  370. }
  371. if (key == Key_Down) {
  372. VERIFY(item(0).type() != MenuItem::Separator);
  373. if (is_scrollable() && m_hovered_item_index == static_cast<int>(m_items.size()) - 1)
  374. return;
  375. auto original_index = m_hovered_item_index;
  376. auto new_index = original_index;
  377. do {
  378. if (new_index == static_cast<int>(m_items.size()) - 1)
  379. new_index = 0;
  380. else
  381. ++new_index;
  382. if (new_index == original_index)
  383. return;
  384. } while (item(new_index).type() == MenuItem::Separator || !item(new_index).is_enabled());
  385. VERIFY(new_index >= 0);
  386. VERIFY(new_index <= static_cast<int>(m_items.size()) - 1);
  387. if (is_scrollable() && new_index >= (m_scroll_offset + static_cast<int>(visible_item_count())))
  388. ++m_scroll_offset;
  389. set_hovered_index(new_index);
  390. return;
  391. }
  392. }
  393. Core::Object::event(event);
  394. }
  395. void Menu::clear_hovered_item()
  396. {
  397. set_hovered_index(-1);
  398. }
  399. void Menu::start_activation_animation(MenuItem& item)
  400. {
  401. VERIFY(menu_window());
  402. VERIFY(menu_window()->backing_store());
  403. auto window = Window::construct(*this, WindowType::Menu);
  404. window->set_frameless(true);
  405. window->set_hit_testing_enabled(false);
  406. window->set_opacity(0.8f); // start out transparent so we don't have to recompute occlusions
  407. window->set_rect(item.rect().translated(m_menu_window->rect().location()));
  408. window->set_event_filter([](Core::Event&) {
  409. // ignore all events
  410. return false;
  411. });
  412. VERIFY(window->backing_store());
  413. Gfx::Painter painter(*window->backing_store());
  414. painter.blit({}, *menu_window()->backing_store(), item.rect(), 1.0f, false);
  415. window->invalidate();
  416. struct AnimationInfo {
  417. RefPtr<Core::Timer> timer;
  418. RefPtr<Window> window;
  419. u8 step { 8 }; // Must be even number!
  420. AnimationInfo(NonnullRefPtr<Window>&& window)
  421. : window(move(window))
  422. {
  423. }
  424. };
  425. auto animation = adopt_own(*new AnimationInfo(move(window)));
  426. auto& timer = animation->timer;
  427. timer = Core::Timer::create_repeating(50, [this, animation = animation.ptr(), animation_ref = move(animation)] {
  428. VERIFY(animation->step % 2 == 0);
  429. animation->step -= 2;
  430. if (animation->step == 0) {
  431. animation->window->set_visible(false);
  432. animation->timer->stop();
  433. animation->timer = nullptr; // break circular reference
  434. return;
  435. }
  436. float opacity = (float)animation->step / 10.0f;
  437. animation->window->set_opacity(opacity);
  438. });
  439. timer->start();
  440. }
  441. void Menu::did_activate(MenuItem& item, bool leave_menu_open)
  442. {
  443. if (item.type() == MenuItem::Type::Separator)
  444. return;
  445. if (!leave_menu_open)
  446. start_activation_animation(item);
  447. if (on_item_activation)
  448. on_item_activation(item);
  449. if (!leave_menu_open)
  450. MenuManager::the().close_everyone();
  451. if (m_client)
  452. m_client->async_menu_item_activated(m_menu_id, item.identifier());
  453. }
  454. bool Menu::activate_default()
  455. {
  456. for (auto& item : m_items) {
  457. if (item.type() == MenuItem::Type::Separator)
  458. continue;
  459. if (item.is_enabled() && item.is_default()) {
  460. did_activate(item, false);
  461. return true;
  462. }
  463. }
  464. return false;
  465. }
  466. MenuItem* Menu::item_with_identifier(unsigned identifier)
  467. {
  468. for (auto& item : m_items) {
  469. if (item.identifier() == identifier)
  470. return &item;
  471. }
  472. return nullptr;
  473. }
  474. int Menu::item_index_at(const Gfx::IntPoint& position)
  475. {
  476. int i = 0;
  477. for (auto& item : m_items) {
  478. if (item.rect().contains(position)) {
  479. if (item.type() == MenuItem::Type::Separator)
  480. return -1;
  481. return i;
  482. }
  483. ++i;
  484. }
  485. return -1;
  486. }
  487. void Menu::close()
  488. {
  489. MenuManager::the().close_menu_and_descendants(*this);
  490. }
  491. void Menu::redraw_if_theme_changed()
  492. {
  493. if (m_theme_index_at_last_paint != MenuManager::the().theme_index())
  494. redraw();
  495. }
  496. void Menu::popup(const Gfx::IntPoint& position)
  497. {
  498. do_popup(position, true);
  499. }
  500. void Menu::do_popup(const Gfx::IntPoint& position, bool make_input, bool as_submenu)
  501. {
  502. if (is_empty()) {
  503. dbgln("Menu: Empty menu popup");
  504. return;
  505. }
  506. auto& window = ensure_menu_window();
  507. redraw_if_theme_changed();
  508. const int margin = 30;
  509. Gfx::IntPoint adjusted_pos = position;
  510. if (adjusted_pos.x() + window.width() >= Screen::the().width() - margin) {
  511. adjusted_pos = adjusted_pos.translated(-window.width(), 0);
  512. } else {
  513. adjusted_pos.set_x(adjusted_pos.x() + 1);
  514. }
  515. if (adjusted_pos.y() + window.height() >= Screen::the().height() - margin) {
  516. adjusted_pos = adjusted_pos.translated(0, -min(window.height(), adjusted_pos.y()));
  517. if (as_submenu)
  518. adjusted_pos = adjusted_pos.translated(0, item_height());
  519. }
  520. window.move_to(adjusted_pos);
  521. set_visible(true);
  522. MenuManager::the().open_menu(*this, make_input);
  523. WindowManager::the().did_popup_a_menu({});
  524. }
  525. bool Menu::is_menu_ancestor_of(const Menu& other) const
  526. {
  527. for (auto& item : m_items) {
  528. if (!item.is_submenu())
  529. continue;
  530. auto& submenu = *item.submenu();
  531. if (&submenu == &other)
  532. return true;
  533. if (submenu.is_menu_ancestor_of(other))
  534. return true;
  535. }
  536. return false;
  537. }
  538. void Menu::set_visible(bool visible)
  539. {
  540. if (!menu_window())
  541. return;
  542. if (visible == menu_window()->is_visible())
  543. return;
  544. menu_window()->set_visible(visible);
  545. if (m_client)
  546. m_client->async_menu_visibility_did_change(m_menu_id, visible);
  547. }
  548. void Menu::add_item(NonnullOwnPtr<MenuItem> item)
  549. {
  550. if (auto alt_shortcut = find_ampersand_shortcut_character(item->text())) {
  551. m_alt_shortcut_character_to_item_indices.ensure(to_ascii_lowercase(alt_shortcut)).append(m_items.size());
  552. }
  553. m_items.append(move(item));
  554. }
  555. const Vector<size_t>* Menu::items_with_alt_shortcut(u32 alt_shortcut) const
  556. {
  557. auto it = m_alt_shortcut_character_to_item_indices.find(to_ascii_lowercase(alt_shortcut));
  558. if (it == m_alt_shortcut_character_to_item_indices.end())
  559. return nullptr;
  560. return &it->value;
  561. }
  562. void Menu::set_hovered_index(int index, bool make_input)
  563. {
  564. if (m_hovered_item_index == index)
  565. return;
  566. if (auto* old_hovered_item = hovered_item()) {
  567. if (client())
  568. client()->async_menu_item_left(m_menu_id, old_hovered_item->identifier());
  569. }
  570. m_hovered_item_index = index;
  571. update_for_new_hovered_item(make_input);
  572. if (auto* new_hovered_item = hovered_item()) {
  573. if (client())
  574. client()->async_menu_item_entered(m_menu_id, new_hovered_item->identifier());
  575. }
  576. }
  577. bool Menu::is_open() const
  578. {
  579. return MenuManager::the().is_open(*this);
  580. }
  581. }