Menu.cpp 25 KB

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