Menu.cpp 26 KB

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