Menu.cpp 20 KB

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