Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, 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
28#include "Menu.h"
29#include "Event.h"
30#include "EventLoop.h"
31#include "MenuItem.h"
32#include "MenuManager.h"
33#include "Screen.h"
34#include "Window.h"
35#include "WindowManager.h"
36#include <LibGfx/Bitmap.h>
37#include <LibGfx/CharacterBitmap.h>
38#include <LibGfx/Font.h>
39#include <LibGfx/Painter.h>
40#include <LibGfx/StylePainter.h>
41#include <LibGfx/Triangle.h>
42#include <WindowServer/ClientConnection.h>
43#include <WindowServer/WindowClientEndpoint.h>
44
45namespace WindowServer {
46
47Menu::Menu(ClientConnection* client, int menu_id, const String& name)
48 : Core::Object(client)
49 , m_client(client)
50 , m_menu_id(menu_id)
51 , m_name(move(name))
52{
53}
54
55Menu::~Menu()
56{
57}
58
59const Gfx::Font& Menu::font() const
60{
61 return Gfx::Font::default_font();
62}
63
64static const char* s_checked_bitmap_data = {
65 " "
66 " # "
67 " ## "
68 " ### "
69 " ## ### "
70 " ##### "
71 " ### "
72 " # "
73 " "
74};
75
76static const char* s_submenu_arrow_bitmap_data = {
77 " "
78 " # "
79 " ## "
80 " ### "
81 " #### "
82 " ### "
83 " ## "
84 " # "
85 " "
86};
87
88static Gfx::CharacterBitmap* s_checked_bitmap;
89static const int s_checked_bitmap_width = 9;
90static const int s_checked_bitmap_height = 9;
91static const int s_submenu_arrow_bitmap_width = 9;
92static const int s_submenu_arrow_bitmap_height = 9;
93static const int s_item_icon_width = 16;
94static const int s_stripe_width = 23;
95
96int Menu::content_width() const
97{
98 int widest_text = 0;
99 int widest_shortcut = 0;
100 for (auto& item : m_items) {
101 if (item.type() != MenuItem::Text)
102 continue;
103 int text_width = font().width(item.text());
104 if (!item.shortcut_text().is_empty()) {
105 int shortcut_width = font().width(item.shortcut_text());
106 widest_shortcut = max(shortcut_width, widest_shortcut);
107 }
108 widest_text = max(widest_text, text_width);
109 }
110
111 int widest_item = widest_text + s_stripe_width;
112 if (widest_shortcut)
113 widest_item += padding_between_text_and_shortcut() + widest_shortcut;
114
115 return max(widest_item, rect_in_menubar().width()) + horizontal_padding() + frame_thickness() * 2;
116}
117
118void Menu::redraw()
119{
120 if (!menu_window())
121 return;
122 draw();
123 menu_window()->invalidate();
124}
125
126Window& Menu::ensure_menu_window()
127{
128 if (m_menu_window)
129 return *m_menu_window;
130
131 int width = this->content_width();
132
133 Gfx::Point next_item_location(frame_thickness(), frame_thickness());
134 for (auto& item : m_items) {
135 int height = 0;
136 if (item.type() == MenuItem::Text)
137 height = item_height();
138 else if (item.type() == MenuItem::Separator)
139 height = 8;
140 item.set_rect({ next_item_location, { width - frame_thickness() * 2, height } });
141 next_item_location.move_by(0, height);
142 }
143
144 int window_height_available = Screen::the().height() - MenuManager::the().menubar_rect().height() - frame_thickness() * 2;
145 int max_window_height = (window_height_available / item_height()) * item_height() + frame_thickness() * 2;
146 int content_height = m_items.is_empty() ? 0 : (m_items.last().rect().bottom() + 1) + frame_thickness();
147 int window_height = min(max_window_height, content_height);
148 if (window_height < content_height) {
149 m_scrollable = true;
150 m_max_scroll_offset = item_count() - window_height / item_height() + 2;
151 }
152
153 auto window = Window::construct(*this, WindowType::Menu);
154 window->set_rect(0, 0, width, window_height);
155 m_menu_window = move(window);
156 draw();
157
158 return *m_menu_window;
159}
160
161int Menu::visible_item_count() const
162{
163 if (!is_scrollable())
164 return m_items.size();
165 ASSERT(m_menu_window);
166 // Make space for up/down arrow indicators
167 return m_menu_window->height() / item_height() - 2;
168}
169
170void Menu::draw()
171{
172 auto palette = WindowManager::the().palette();
173 m_theme_index_at_last_paint = MenuManager::the().theme_index();
174
175 ASSERT(menu_window());
176 ASSERT(menu_window()->backing_store());
177 Gfx::Painter painter(*menu_window()->backing_store());
178
179 Gfx::Rect rect { {}, menu_window()->size() };
180 painter.fill_rect(rect.shrunken(6, 6), palette.menu_base());
181 Gfx::StylePainter::paint_window_frame(painter, rect, palette);
182 int width = this->content_width();
183
184 if (!s_checked_bitmap)
185 s_checked_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref();
186
187 bool has_checkable_items = false;
188 bool has_items_with_icon = false;
189 for (auto& item : m_items) {
190 has_checkable_items = has_checkable_items | item.is_checkable();
191 has_items_with_icon = has_items_with_icon | !!item.icon();
192 }
193
194 Gfx::Rect stripe_rect { frame_thickness(), frame_thickness(), s_stripe_width, menu_window()->height() - frame_thickness() * 2 };
195 painter.fill_rect(stripe_rect, palette.menu_stripe());
196 painter.draw_line(stripe_rect.top_right(), stripe_rect.bottom_right(), palette.menu_stripe().darkened());
197
198 int visible_item_count = this->visible_item_count();
199
200 if (is_scrollable()) {
201 bool can_go_up = m_scroll_offset > 0;
202 bool can_go_down = m_scroll_offset < m_max_scroll_offset;
203 Gfx::Rect up_indicator_rect { frame_thickness(), frame_thickness(), content_width(), item_height() };
204 painter.draw_text(up_indicator_rect, "\xc3\xb6", Gfx::TextAlignment::Center, can_go_up ? palette.menu_base_text() : palette.color(ColorRole::DisabledText));
205 Gfx::Rect down_indicator_rect { frame_thickness(), menu_window()->height() - item_height() - frame_thickness(), content_width(), item_height() };
206 painter.draw_text(down_indicator_rect, "\xc3\xb7", Gfx::TextAlignment::Center, can_go_down ? palette.menu_base_text() : palette.color(ColorRole::DisabledText));
207 }
208
209 for (int i = 0; i < visible_item_count; ++i) {
210 auto& item = m_items.at(m_scroll_offset + i);
211 if (item.type() == MenuItem::Text) {
212 Color text_color = palette.menu_base_text();
213 if (&item == hovered_item() && item.is_enabled()) {
214 painter.fill_rect(item.rect(), palette.menu_selection());
215 painter.draw_rect(item.rect(), palette.menu_selection().darkened());
216 text_color = palette.menu_selection_text();
217 } else if (!item.is_enabled()) {
218 text_color = Color::MidGray;
219 }
220 Gfx::Rect text_rect = item.rect().translated(stripe_rect.width() + 6, 0);
221 if (item.is_checkable()) {
222 if (item.is_exclusive()) {
223 Gfx::Rect radio_rect { item.rect().x() + 5, 0, 12, 12 };
224 radio_rect.center_vertically_within(text_rect);
225 Gfx::StylePainter::paint_radio_button(painter, radio_rect, palette, item.is_checked(), false);
226 } else {
227 Gfx::Rect checkmark_rect { item.rect().x() + 7, 0, s_checked_bitmap_width, s_checked_bitmap_height };
228 checkmark_rect.center_vertically_within(text_rect);
229 Gfx::Rect checkbox_rect = checkmark_rect.inflated(4, 4);
230 painter.fill_rect(checkbox_rect, palette.base());
231 Gfx::StylePainter::paint_frame(painter, checkbox_rect, palette, Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
232 if (item.is_checked()) {
233 painter.draw_bitmap(checkmark_rect.location(), *s_checked_bitmap, palette.button_text());
234 }
235 }
236 } else if (item.icon()) {
237 Gfx::Rect icon_rect { item.rect().x() + 3, 0, s_item_icon_width, s_item_icon_width };
238 icon_rect.center_vertically_within(text_rect);
239 painter.blit(icon_rect.location(), *item.icon(), item.icon()->rect());
240 }
241 painter.draw_text(text_rect, item.text(), 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 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::Rect 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::Point p1(item.rect().translated(stripe_rect.width() + 4, 0).x(), item.rect().center().y() - 1);
258 Gfx::Point 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
265MenuItem* Menu::hovered_item() const
266{
267 if (m_hovered_item_index == -1)
268 return nullptr;
269 return const_cast<MenuItem*>(&item(m_hovered_item_index));
270}
271
272void Menu::update_for_new_hovered_item()
273{
274 if (hovered_item() && hovered_item()->is_submenu()) {
275 MenuManager::the().close_everyone_not_in_lineage(*hovered_item()->submenu());
276 hovered_item()->submenu()->popup(hovered_item()->rect().top_right().translated(menu_window()->rect().location()), true);
277 } else {
278 MenuManager::the().close_everyone_not_in_lineage(*this);
279 MenuManager::the().set_current_menu(this);
280 menu_window()->set_visible(true);
281 }
282 redraw();
283}
284
285void Menu::open_hovered_item()
286{
287 ASSERT(menu_window());
288 ASSERT(menu_window()->is_visible());
289 if (!hovered_item())
290 return;
291 if (hovered_item()->is_enabled())
292 did_activate(*hovered_item());
293 clear_hovered_item();
294}
295
296void Menu::decend_into_submenu_at_hovered_item()
297{
298 ASSERT(hovered_item());
299 ASSERT(hovered_item()->is_submenu());
300 auto submenu = hovered_item()->submenu();
301 submenu->m_hovered_item_index = 0;
302 ASSERT(submenu->hovered_item()->type() != MenuItem::Separator);
303 submenu->update_for_new_hovered_item();
304 m_in_submenu = true;
305}
306
307void Menu::handle_mouse_move_event(const MouseEvent& mouse_event)
308{
309 ASSERT(menu_window());
310 if (hovered_item() && hovered_item()->is_submenu()) {
311
312 auto item = *hovered_item();
313 auto submenu_top_left = item.rect().location() + Gfx::Point { item.rect().width(), 0 };
314 auto submenu_bottom_left = submenu_top_left + Gfx::Point { 0, item.submenu()->menu_window()->height() };
315
316 auto safe_hover_triangle = Gfx::Triangle { m_last_position_in_hover, submenu_top_left, submenu_bottom_left };
317 m_last_position_in_hover = mouse_event.position();
318
319 // Don't update the hovered item if mouse is moving towards a submenu
320 if (safe_hover_triangle.contains(mouse_event.position()))
321 return;
322 }
323
324 int index = item_index_at(mouse_event.position());
325 if (m_hovered_item_index == index)
326 return;
327 m_hovered_item_index = index;
328
329 // FIXME: Tell parent menu (if it exists) that it is currently in a submenu
330 m_in_submenu = false;
331 update_for_new_hovered_item();
332 return;
333}
334
335void Menu::event(Core::Event& event)
336{
337 if (event.type() == Event::MouseMove) {
338 handle_mouse_move_event(static_cast<const MouseEvent&>(event));
339 return;
340 }
341
342 if (event.type() == Event::MouseUp) {
343 open_hovered_item();
344 return;
345 }
346
347 if (event.type() == Event::MouseWheel && is_scrollable()) {
348 ASSERT(menu_window());
349 auto& mouse_event = static_cast<const MouseEvent&>(event);
350 m_scroll_offset += mouse_event.wheel_delta();
351 m_scroll_offset = clamp(m_scroll_offset, 0, m_max_scroll_offset);
352
353 int index = item_index_at(mouse_event.position());
354 if (m_hovered_item_index == index)
355 return;
356
357 m_hovered_item_index = index;
358 update_for_new_hovered_item();
359 return;
360 }
361
362 if (event.type() == Event::KeyDown) {
363 auto key = static_cast<KeyEvent&>(event).key();
364
365 if (!(key == Key_Up || key == Key_Down || key == Key_Left || key == Key_Right || key == Key_Return))
366 return;
367
368 ASSERT(menu_window());
369 ASSERT(menu_window()->is_visible());
370
371 // Default to the first item on key press if one has not been selected yet
372 if (!hovered_item()) {
373 m_hovered_item_index = 0;
374 update_for_new_hovered_item();
375 return;
376 }
377
378 // Pass the event for the submenu that we are currently in to handle
379 if (m_in_submenu && key != Key_Left) {
380 ASSERT(hovered_item()->is_submenu());
381 hovered_item()->submenu()->dispatch_event(event);
382 return;
383 }
384
385 if (key == Key_Return) {
386 if (hovered_item()->is_submenu())
387 decend_into_submenu_at_hovered_item();
388 else
389 open_hovered_item();
390 return;
391 }
392
393 if (key == Key_Up) {
394 ASSERT(m_items.at(0).type() != MenuItem::Separator);
395
396 if (is_scrollable() && m_hovered_item_index == 0)
397 return;
398
399 do {
400 if (m_hovered_item_index == 0)
401 m_hovered_item_index = m_items.size() - 1;
402 else
403 --m_hovered_item_index;
404 } while (hovered_item()->type() == MenuItem::Separator);
405
406 ASSERT(m_hovered_item_index >= 0 && m_hovered_item_index <= static_cast<int>(m_items.size()) - 1);
407
408 if (is_scrollable() && m_hovered_item_index < m_scroll_offset)
409 --m_scroll_offset;
410
411 update_for_new_hovered_item();
412 return;
413 }
414
415 if (key == Key_Down) {
416 ASSERT(m_items.at(0).type() != MenuItem::Separator);
417
418 if (is_scrollable() && m_hovered_item_index == static_cast<int>(m_items.size()) - 1)
419 return;
420
421 do {
422 if (m_hovered_item_index == static_cast<int>(m_items.size()) - 1)
423 m_hovered_item_index = 0;
424 else
425 ++m_hovered_item_index;
426 } while (hovered_item()->type() == MenuItem::Separator);
427
428 ASSERT(m_hovered_item_index >= 0 && m_hovered_item_index <= static_cast<int>(m_items.size()) - 1);
429
430 if (is_scrollable() && m_hovered_item_index >= (m_scroll_offset + visible_item_count()))
431 ++m_scroll_offset;
432
433 update_for_new_hovered_item();
434 return;
435 }
436
437 if (key == Key_Left) {
438 if (!m_in_submenu)
439 return;
440
441 ASSERT(hovered_item()->is_submenu());
442 hovered_item()->submenu()->clear_hovered_item();
443 m_in_submenu = false;
444 return;
445 }
446
447 if (key == Key_Right) {
448 if (hovered_item()->is_submenu())
449 decend_into_submenu_at_hovered_item();
450 return;
451 }
452 }
453 Core::Object::event(event);
454}
455
456void Menu::clear_hovered_item()
457{
458 if (!hovered_item())
459 return;
460 m_hovered_item_index = -1;
461 m_in_submenu = false;
462 redraw();
463}
464
465void Menu::did_activate(MenuItem& item)
466{
467 if (item.type() == MenuItem::Type::Separator)
468 return;
469
470 if (on_item_activation)
471 on_item_activation(item);
472
473 MenuManager::the().close_bar();
474
475 if (m_client)
476 m_client->post_message(Messages::WindowClient::MenuItemActivated(m_menu_id, item.identifier()));
477}
478
479MenuItem* Menu::item_with_identifier(unsigned identifer)
480{
481 for (auto& item : m_items) {
482 if (item.identifier() == identifer)
483 return &item;
484 }
485 return nullptr;
486}
487
488int Menu::item_index_at(const Gfx::Point& position)
489{
490 int i = 0;
491 for (auto& item : m_items) {
492 if (item.rect().contains(position))
493 return i;
494 ++i;
495 }
496 return -1;
497}
498
499void Menu::close()
500{
501 MenuManager::the().close_menu_and_descendants(*this);
502}
503
504void Menu::redraw_if_theme_changed()
505{
506 if (m_theme_index_at_last_paint != MenuManager::the().theme_index())
507 redraw();
508}
509
510void Menu::popup(const Gfx::Point& position, bool is_submenu)
511{
512 ASSERT(!is_empty());
513
514 auto& window = ensure_menu_window();
515 redraw_if_theme_changed();
516
517 const int margin = 30;
518 Gfx::Point adjusted_pos = position;
519
520 if (adjusted_pos.x() + window.width() >= Screen::the().width() - margin) {
521 adjusted_pos = adjusted_pos.translated(-window.width(), 0);
522 }
523 if (adjusted_pos.y() + window.height() >= Screen::the().height() - margin) {
524 adjusted_pos = adjusted_pos.translated(0, -window.height());
525 }
526
527 if (adjusted_pos.y() < MenuManager::the().menubar_rect().height())
528 adjusted_pos.set_y(MenuManager::the().menubar_rect().height());
529
530 window.move_to(adjusted_pos);
531 window.set_visible(true);
532 MenuManager::the().set_current_menu(this, is_submenu);
533}
534
535bool Menu::is_menu_ancestor_of(const Menu& other) const
536{
537 for (auto& item : m_items) {
538 if (!item.is_submenu())
539 continue;
540 auto& submenu = *const_cast<MenuItem&>(item).submenu();
541 if (&submenu == &other)
542 return true;
543 if (submenu.is_menu_ancestor_of(other))
544 return true;
545 }
546 return false;
547}
548
549}