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