Menu.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Shannon Booth <shannon.ml.booth@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "Menu.h"
  28. #include "Event.h"
  29. #include "MenuItem.h"
  30. #include "MenuManager.h"
  31. #include "Screen.h"
  32. #include "Window.h"
  33. #include "WindowManager.h"
  34. #include <LibGfx/Bitmap.h>
  35. #include <LibGfx/CharacterBitmap.h>
  36. #include <LibGfx/Font.h>
  37. #include <LibGfx/Painter.h>
  38. #include <LibGfx/StylePainter.h>
  39. #include <LibGfx/Triangle.h>
  40. #include <WindowServer/ClientConnection.h>
  41. #include <WindowServer/WindowClientEndpoint.h>
  42. #include <ctype.h>
  43. namespace WindowServer {
  44. u32 find_ampersand_shortcut_character(const StringView& string)
  45. {
  46. Utf8View utf8_view { string };
  47. for (auto it = utf8_view.begin(); it != utf8_view.end(); ++it) {
  48. if (*it == '&') {
  49. ++it;
  50. if (it != utf8_view.end() && *it != '&')
  51. return *it;
  52. }
  53. }
  54. return 0;
  55. }
  56. Menu::Menu(ClientConnection* client, int menu_id, String name)
  57. : Core::Object(client)
  58. , m_client(client)
  59. , m_menu_id(menu_id)
  60. , m_name(move(name))
  61. {
  62. m_alt_shortcut_character = find_ampersand_shortcut_character(m_name);
  63. }
  64. Menu::~Menu()
  65. {
  66. }
  67. const Gfx::Font& Menu::font() const
  68. {
  69. return Gfx::FontDatabase::default_font();
  70. }
  71. static const char* s_checked_bitmap_data = {
  72. " "
  73. " # "
  74. " ## "
  75. " ### "
  76. " ## ### "
  77. " ##### "
  78. " ### "
  79. " # "
  80. " "
  81. };
  82. static const char* s_submenu_arrow_bitmap_data = {
  83. " "
  84. " # "
  85. " ## "
  86. " ### "
  87. " #### "
  88. " ### "
  89. " ## "
  90. " # "
  91. " "
  92. };
  93. static Gfx::CharacterBitmap* s_checked_bitmap;
  94. static const int s_checked_bitmap_width = 9;
  95. static const int s_checked_bitmap_height = 9;
  96. static const int s_submenu_arrow_bitmap_width = 9;
  97. static const int s_submenu_arrow_bitmap_height = 9;
  98. static const int s_item_icon_width = 16;
  99. static const int s_stripe_width = 24;
  100. int Menu::content_width() const
  101. {
  102. int widest_text = 0;
  103. int widest_shortcut = 0;
  104. for (auto& item : m_items) {
  105. if (item.type() != MenuItem::Text)
  106. continue;
  107. auto& use_font = item.is_default() ? Gfx::FontDatabase::default_bold_font() : font();
  108. int text_width = use_font.width(Gfx::parse_ampersand_string(item.text()));
  109. if (!item.shortcut_text().is_empty()) {
  110. int shortcut_width = use_font.width(item.shortcut_text());
  111. widest_shortcut = max(shortcut_width, widest_shortcut);
  112. }
  113. widest_text = max(widest_text, text_width);
  114. }
  115. int widest_item = widest_text + s_stripe_width;
  116. if (widest_shortcut)
  117. widest_item += padding_between_text_and_shortcut() + widest_shortcut;
  118. return max(widest_item, rect_in_window_menubar().width()) + horizontal_padding() + frame_thickness() * 2;
  119. }
  120. void Menu::redraw()
  121. {
  122. if (!menu_window())
  123. return;
  124. draw();
  125. menu_window()->invalidate();
  126. }
  127. Window& Menu::ensure_menu_window()
  128. {
  129. if (m_menu_window)
  130. return *m_menu_window;
  131. int width = this->content_width();
  132. Gfx::IntPoint next_item_location(frame_thickness(), frame_thickness());
  133. for (auto& item : m_items) {
  134. int height = 0;
  135. if (item.type() == MenuItem::Text)
  136. height = item_height();
  137. else if (item.type() == MenuItem::Separator)
  138. height = 8;
  139. item.set_rect({ next_item_location, { width - frame_thickness() * 2, height } });
  140. next_item_location.move_by(0, height);
  141. }
  142. int window_height_available = Screen::the().height() - frame_thickness() * 2;
  143. int max_window_height = (window_height_available / item_height()) * item_height() + frame_thickness() * 2;
  144. int content_height = m_items.is_empty() ? 0 : (m_items.last().rect().bottom() + 1) + frame_thickness();
  145. int window_height = min(max_window_height, content_height);
  146. if (window_height < content_height) {
  147. m_scrollable = true;
  148. m_max_scroll_offset = item_count() - window_height / item_height() + 2;
  149. }
  150. auto window = Window::construct(*this, WindowType::Menu);
  151. window->set_visible(false);
  152. window->set_rect(0, 0, width, window_height);
  153. m_menu_window = move(window);
  154. draw();
  155. return *m_menu_window;
  156. }
  157. int Menu::visible_item_count() const
  158. {
  159. if (!is_scrollable())
  160. return m_items.size();
  161. VERIFY(m_menu_window);
  162. // Make space for up/down arrow indicators
  163. return m_menu_window->height() / item_height() - 2;
  164. }
  165. void Menu::draw()
  166. {
  167. auto palette = WindowManager::the().palette();
  168. m_theme_index_at_last_paint = MenuManager::the().theme_index();
  169. VERIFY(menu_window());
  170. VERIFY(menu_window()->backing_store());
  171. Gfx::Painter painter(*menu_window()->backing_store());
  172. Gfx::IntRect rect { {}, menu_window()->size() };
  173. painter.draw_rect(rect, Color::Black);
  174. painter.fill_rect(rect.shrunken(2, 2), palette.menu_base());
  175. int width = this->content_width();
  176. if (!s_checked_bitmap)
  177. s_checked_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref();
  178. bool has_checkable_items = false;
  179. bool has_items_with_icon = false;
  180. for (auto& item : m_items) {
  181. has_checkable_items = has_checkable_items | item.is_checkable();
  182. has_items_with_icon = has_items_with_icon | !!item.icon();
  183. }
  184. Gfx::IntRect stripe_rect { frame_thickness(), frame_thickness(), s_stripe_width, menu_window()->height() - frame_thickness() * 2 };
  185. painter.fill_rect(stripe_rect, palette.menu_stripe());
  186. int visible_item_count = this->visible_item_count();
  187. if (is_scrollable()) {
  188. bool can_go_up = m_scroll_offset > 0;
  189. bool can_go_down = m_scroll_offset < m_max_scroll_offset;
  190. Gfx::IntRect up_indicator_rect { frame_thickness(), frame_thickness(), content_width(), item_height() };
  191. painter.draw_text(up_indicator_rect, "\xE2\xAC\x86", Gfx::TextAlignment::Center, can_go_up ? palette.menu_base_text() : palette.color(ColorRole::DisabledText));
  192. Gfx::IntRect down_indicator_rect { frame_thickness(), menu_window()->height() - item_height() - frame_thickness(), content_width(), item_height() };
  193. painter.draw_text(down_indicator_rect, "\xE2\xAC\x87", Gfx::TextAlignment::Center, can_go_down ? palette.menu_base_text() : palette.color(ColorRole::DisabledText));
  194. }
  195. for (int i = 0; i < visible_item_count; ++i) {
  196. auto& item = m_items.at(m_scroll_offset + i);
  197. if (item.type() == MenuItem::Text) {
  198. Color text_color = palette.menu_base_text();
  199. if (&item == hovered_item() && item.is_enabled()) {
  200. painter.fill_rect(item.rect(), palette.menu_selection());
  201. painter.draw_rect(item.rect(), palette.menu_selection().darkened());
  202. text_color = palette.menu_selection_text();
  203. } else if (!item.is_enabled()) {
  204. text_color = Color::MidGray;
  205. }
  206. Gfx::IntRect text_rect = item.rect().translated(stripe_rect.width() + 6, 0);
  207. if (item.is_checkable()) {
  208. if (item.is_exclusive()) {
  209. Gfx::IntRect radio_rect { item.rect().x() + 5, 0, 12, 12 };
  210. radio_rect.center_vertically_within(text_rect);
  211. Gfx::StylePainter::paint_radio_button(painter, radio_rect, palette, item.is_checked(), false);
  212. } else {
  213. Gfx::IntRect checkmark_rect { item.rect().x() + 7, 0, s_checked_bitmap_width, s_checked_bitmap_height };
  214. checkmark_rect.center_vertically_within(text_rect);
  215. Gfx::IntRect checkbox_rect = checkmark_rect.inflated(4, 4);
  216. painter.fill_rect(checkbox_rect, palette.base());
  217. Gfx::StylePainter::paint_frame(painter, checkbox_rect, palette, Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
  218. if (item.is_checked()) {
  219. painter.draw_bitmap(checkmark_rect.location(), *s_checked_bitmap, palette.button_text());
  220. }
  221. }
  222. } else if (item.icon()) {
  223. Gfx::IntRect icon_rect { item.rect().x() + 3, 0, s_item_icon_width, s_item_icon_width };
  224. icon_rect.center_vertically_within(text_rect);
  225. if (&item == hovered_item() && item.is_enabled()) {
  226. auto shadow_color = palette.menu_selection().darkened(0.7f);
  227. painter.blit_filtered(icon_rect.location().translated(1, 1), *item.icon(), item.icon()->rect(), [&shadow_color](auto) {
  228. return shadow_color;
  229. });
  230. icon_rect.move_by(-1, -1);
  231. }
  232. if (item.is_enabled())
  233. painter.blit(icon_rect.location(), *item.icon(), item.icon()->rect());
  234. else
  235. painter.blit_disabled(icon_rect.location(), *item.icon(), item.icon()->rect(), palette);
  236. }
  237. auto& previous_font = painter.font();
  238. if (item.is_default())
  239. painter.set_font(Gfx::FontDatabase::default_bold_font());
  240. painter.draw_ui_text(text_rect, item.text(), painter.font(), Gfx::TextAlignment::CenterLeft, text_color);
  241. if (!item.shortcut_text().is_empty()) {
  242. painter.draw_text(item.rect().translated(-right_padding(), 0), item.shortcut_text(), Gfx::TextAlignment::CenterRight, text_color);
  243. }
  244. painter.set_font(previous_font);
  245. if (item.is_submenu()) {
  246. 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();
  247. Gfx::IntRect submenu_arrow_rect {
  248. item.rect().right() - 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(), 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. }
  264. MenuItem* Menu::hovered_item() const
  265. {
  266. if (m_hovered_item_index == -1)
  267. return nullptr;
  268. return const_cast<MenuItem*>(&item(m_hovered_item_index));
  269. }
  270. void Menu::update_for_new_hovered_item(bool make_input)
  271. {
  272. auto* hovered_item = this->hovered_item();
  273. if (hovered_item && hovered_item->is_submenu()) {
  274. VERIFY(menu_window());
  275. MenuManager::the().close_everyone_not_in_lineage(*hovered_item->submenu());
  276. hovered_item->submenu()->do_popup(hovered_item->rect().top_right().translated(menu_window()->rect().location()), make_input, true);
  277. } else {
  278. MenuManager::the().close_everyone_not_in_lineage(*this);
  279. ensure_menu_window();
  280. set_visible(true);
  281. }
  282. redraw();
  283. }
  284. void Menu::open_hovered_item(bool leave_menu_open)
  285. {
  286. VERIFY(menu_window());
  287. VERIFY(menu_window()->is_visible());
  288. if (!hovered_item())
  289. return;
  290. if (hovered_item()->is_enabled())
  291. did_activate(*hovered_item(), leave_menu_open);
  292. if (!leave_menu_open)
  293. clear_hovered_item();
  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, false);
  301. submenu->set_hovered_index(0);
  302. VERIFY(submenu->hovered_item()->type() != MenuItem::Separator);
  303. }
  304. void Menu::handle_mouse_move_event(const MouseEvent& 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<const MouseEvent&>(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<const MouseEvent&>(event);
  334. m_scroll_offset += mouse_event.wheel_delta();
  335. m_scroll_offset = clamp(m_scroll_offset, 0, m_max_scroll_offset);
  336. int index = item_index_at(mouse_event.position());
  337. set_hovered_index(index);
  338. return;
  339. }
  340. if (event.type() == Event::KeyDown) {
  341. auto key = static_cast<KeyEvent&>(event).key();
  342. if (!(key == Key_Up || key == Key_Down || key == Key_Left || key == Key_Right || key == Key_Return))
  343. return;
  344. VERIFY(menu_window());
  345. VERIFY(menu_window()->is_visible());
  346. // Default to the first enabled, non-separator item on key press if one has not been selected yet
  347. if (!hovered_item()) {
  348. int counter = 0;
  349. for (const auto& item : m_items) {
  350. if (item.type() != MenuItem::Separator && item.is_enabled()) {
  351. set_hovered_index(counter, key == Key_Right);
  352. break;
  353. }
  354. counter++;
  355. }
  356. return;
  357. }
  358. if (key == Key_Up) {
  359. VERIFY(item(0).type() != MenuItem::Separator);
  360. if (is_scrollable() && m_hovered_item_index == 0)
  361. return;
  362. auto original_index = m_hovered_item_index;
  363. auto new_index = original_index;
  364. do {
  365. if (new_index == 0)
  366. new_index = m_items.size() - 1;
  367. else
  368. --new_index;
  369. if (new_index == original_index)
  370. return;
  371. } while (item(new_index).type() == MenuItem::Separator || !item(new_index).is_enabled());
  372. VERIFY(new_index >= 0);
  373. VERIFY(new_index <= static_cast<int>(m_items.size()) - 1);
  374. if (is_scrollable() && new_index < m_scroll_offset)
  375. --m_scroll_offset;
  376. set_hovered_index(new_index);
  377. return;
  378. }
  379. if (key == Key_Down) {
  380. VERIFY(item(0).type() != MenuItem::Separator);
  381. if (is_scrollable() && m_hovered_item_index == static_cast<int>(m_items.size()) - 1)
  382. return;
  383. auto original_index = m_hovered_item_index;
  384. auto new_index = original_index;
  385. do {
  386. if (new_index == static_cast<int>(m_items.size()) - 1)
  387. new_index = 0;
  388. else
  389. ++new_index;
  390. if (new_index == original_index)
  391. return;
  392. } while (item(new_index).type() == MenuItem::Separator || !item(new_index).is_enabled());
  393. VERIFY(new_index >= 0);
  394. VERIFY(new_index <= static_cast<int>(m_items.size()) - 1);
  395. if (is_scrollable() && new_index >= (m_scroll_offset + visible_item_count()))
  396. ++m_scroll_offset;
  397. set_hovered_index(new_index);
  398. return;
  399. }
  400. }
  401. Core::Object::event(event);
  402. }
  403. void Menu::clear_hovered_item()
  404. {
  405. if (!hovered_item())
  406. return;
  407. m_hovered_item_index = -1;
  408. redraw();
  409. }
  410. void Menu::start_activation_animation(MenuItem& item)
  411. {
  412. VERIFY(menu_window());
  413. VERIFY(menu_window()->backing_store());
  414. auto window = Window::construct(*this, WindowType::Menu);
  415. window->set_frameless(true);
  416. window->set_hit_testing_enabled(false);
  417. window->set_opacity(0.8f); // start out transparent so we don't have to recompute occlusions
  418. window->set_rect(item.rect().translated(m_menu_window->rect().location()));
  419. window->set_event_filter([](Core::Event&) {
  420. // ignore all events
  421. return false;
  422. });
  423. VERIFY(window->backing_store());
  424. Gfx::Painter painter(*window->backing_store());
  425. painter.blit({}, *menu_window()->backing_store(), item.rect(), 1.0f, false);
  426. window->invalidate();
  427. struct AnimationInfo {
  428. RefPtr<Core::Timer> timer;
  429. RefPtr<Window> window;
  430. u8 step { 8 }; // Must be even number!
  431. AnimationInfo(NonnullRefPtr<Window>&& window)
  432. : window(move(window))
  433. {
  434. }
  435. };
  436. auto animation = adopt_own(*new AnimationInfo(move(window)));
  437. auto& timer = animation->timer;
  438. timer = Core::Timer::create_repeating(50, [this, animation = animation.ptr(), animation_ref = move(animation)] {
  439. VERIFY(animation->step % 2 == 0);
  440. animation->step -= 2;
  441. if (animation->step == 0) {
  442. animation->window->set_visible(false);
  443. animation->timer->stop();
  444. animation->timer = nullptr; // break circular reference
  445. return;
  446. }
  447. float opacity = (float)animation->step / 10.0f;
  448. animation->window->set_opacity(opacity);
  449. });
  450. timer->start();
  451. }
  452. void Menu::did_activate(MenuItem& item, bool leave_menu_open)
  453. {
  454. if (item.type() == MenuItem::Type::Separator)
  455. return;
  456. if (!leave_menu_open)
  457. start_activation_animation(item);
  458. if (on_item_activation)
  459. on_item_activation(item);
  460. if (!leave_menu_open)
  461. MenuManager::the().close_everyone();
  462. if (m_client)
  463. m_client->post_message(Messages::WindowClient::MenuItemActivated(m_menu_id, item.identifier()));
  464. }
  465. bool Menu::activate_default()
  466. {
  467. for (auto& item : m_items) {
  468. if (item.type() == MenuItem::Type::Separator)
  469. continue;
  470. if (item.is_enabled() && item.is_default()) {
  471. did_activate(item, false);
  472. return true;
  473. }
  474. }
  475. return false;
  476. }
  477. MenuItem* Menu::item_with_identifier(unsigned identifier)
  478. {
  479. for (auto& item : m_items) {
  480. if (item.identifier() == identifier)
  481. return &item;
  482. }
  483. return nullptr;
  484. }
  485. int Menu::item_index_at(const Gfx::IntPoint& position)
  486. {
  487. int i = 0;
  488. for (auto& item : m_items) {
  489. if (item.rect().contains(position))
  490. return i;
  491. ++i;
  492. }
  493. return -1;
  494. }
  495. void Menu::close()
  496. {
  497. MenuManager::the().close_menu_and_descendants(*this);
  498. }
  499. void Menu::redraw_if_theme_changed()
  500. {
  501. if (m_theme_index_at_last_paint != MenuManager::the().theme_index())
  502. redraw();
  503. }
  504. void Menu::popup(const Gfx::IntPoint& position)
  505. {
  506. do_popup(position, true);
  507. }
  508. void Menu::do_popup(const Gfx::IntPoint& position, bool make_input, bool as_submenu)
  509. {
  510. if (is_empty()) {
  511. dbgln("Menu: Empty menu popup");
  512. return;
  513. }
  514. auto& window = ensure_menu_window();
  515. redraw_if_theme_changed();
  516. const int margin = 30;
  517. Gfx::IntPoint adjusted_pos = position;
  518. if (adjusted_pos.x() + window.width() >= Screen::the().width() - margin) {
  519. adjusted_pos = adjusted_pos.translated(-window.width(), 0);
  520. }
  521. if (adjusted_pos.y() + window.height() >= Screen::the().height() - margin) {
  522. adjusted_pos = adjusted_pos.translated(0, -window.height());
  523. if (as_submenu)
  524. adjusted_pos = adjusted_pos.translated(0, item_height());
  525. }
  526. window.move_to(adjusted_pos);
  527. set_visible(true);
  528. MenuManager::the().open_menu(*this, make_input);
  529. WindowManager::the().did_popup_a_menu({});
  530. }
  531. bool Menu::is_menu_ancestor_of(const Menu& other) const
  532. {
  533. for (auto& item : m_items) {
  534. if (!item.is_submenu())
  535. continue;
  536. auto& submenu = *item.submenu();
  537. if (&submenu == &other)
  538. return true;
  539. if (submenu.is_menu_ancestor_of(other))
  540. return true;
  541. }
  542. return false;
  543. }
  544. void Menu::set_visible(bool visible)
  545. {
  546. if (!menu_window())
  547. return;
  548. if (visible == menu_window()->is_visible())
  549. return;
  550. menu_window()->set_visible(visible);
  551. if (m_client)
  552. m_client->post_message(Messages::WindowClient::MenuVisibilityDidChange(m_menu_id, visible));
  553. }
  554. void Menu::add_item(NonnullOwnPtr<MenuItem> item)
  555. {
  556. if (auto alt_shortcut = find_ampersand_shortcut_character(item->text())) {
  557. m_alt_shortcut_character_to_item_indexes.ensure(tolower(alt_shortcut)).append(m_items.size());
  558. }
  559. m_items.append(move(item));
  560. }
  561. const Vector<size_t>* Menu::items_with_alt_shortcut(u32 alt_shortcut) const
  562. {
  563. auto it = m_alt_shortcut_character_to_item_indexes.find(tolower(alt_shortcut));
  564. if (it == m_alt_shortcut_character_to_item_indexes.end())
  565. return nullptr;
  566. return &it->value;
  567. }
  568. }