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