Menu.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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.threed_shadow1();
  221. shadow_color = palette.menu_selection().darkened();
  222. painter.blit_filtered(icon_rect.location(), *item.icon(), item.icon()->rect(), [&shadow_color](auto) {
  223. return shadow_color;
  224. });
  225. icon_rect.move_by(-1, -1);
  226. }
  227. painter.blit(icon_rect.location(), *item.icon(), item.icon()->rect());
  228. }
  229. auto& previous_font = painter.font();
  230. if (item.is_default())
  231. painter.set_font(Gfx::Font::default_bold_font());
  232. painter.draw_text(text_rect, item.text(), Gfx::TextAlignment::CenterLeft, text_color);
  233. if (!item.shortcut_text().is_empty()) {
  234. painter.draw_text(item.rect().translated(-right_padding(), 0), item.shortcut_text(), Gfx::TextAlignment::CenterRight, text_color);
  235. }
  236. painter.set_font(previous_font);
  237. if (item.is_submenu()) {
  238. 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();
  239. Gfx::IntRect submenu_arrow_rect {
  240. item.rect().right() - s_submenu_arrow_bitmap_width - 2,
  241. 0,
  242. s_submenu_arrow_bitmap_width,
  243. s_submenu_arrow_bitmap_height
  244. };
  245. submenu_arrow_rect.center_vertically_within(item.rect());
  246. painter.draw_bitmap(submenu_arrow_rect.location(), submenu_arrow_bitmap, text_color);
  247. }
  248. } else if (item.type() == MenuItem::Separator) {
  249. Gfx::IntPoint p1(item.rect().translated(stripe_rect.width() + 4, 0).x(), item.rect().center().y() - 1);
  250. Gfx::IntPoint p2(width - 7, item.rect().center().y() - 1);
  251. painter.draw_line(p1, p2, palette.threed_shadow1());
  252. painter.draw_line(p1.translated(0, 1), p2.translated(0, 1), palette.threed_highlight());
  253. }
  254. }
  255. }
  256. MenuItem* Menu::hovered_item() const
  257. {
  258. if (m_hovered_item_index == -1)
  259. return nullptr;
  260. return const_cast<MenuItem*>(&item(m_hovered_item_index));
  261. }
  262. void Menu::update_for_new_hovered_item(bool make_input)
  263. {
  264. if (hovered_item() && hovered_item()->is_submenu()) {
  265. ASSERT(menu_window());
  266. MenuManager::the().close_everyone_not_in_lineage(*hovered_item()->submenu());
  267. hovered_item()->submenu()->do_popup(hovered_item()->rect().top_right().translated(menu_window()->rect().location()), make_input);
  268. } else {
  269. MenuManager::the().close_everyone_not_in_lineage(*this);
  270. ensure_menu_window().set_visible(true);
  271. }
  272. redraw();
  273. }
  274. void Menu::open_hovered_item()
  275. {
  276. ASSERT(menu_window());
  277. ASSERT(menu_window()->is_visible());
  278. if (!hovered_item())
  279. return;
  280. if (hovered_item()->is_enabled())
  281. did_activate(*hovered_item());
  282. clear_hovered_item();
  283. }
  284. void Menu::descend_into_submenu_at_hovered_item()
  285. {
  286. ASSERT(hovered_item());
  287. auto submenu = hovered_item()->submenu();
  288. ASSERT(submenu);
  289. MenuManager::the().open_menu(*submenu, false);
  290. submenu->set_hovered_item(0);
  291. ASSERT(submenu->hovered_item()->type() != MenuItem::Separator);
  292. }
  293. void Menu::handle_mouse_move_event(const MouseEvent& mouse_event)
  294. {
  295. ASSERT(menu_window());
  296. MenuManager::the().set_current_menu(this);
  297. if (hovered_item() && hovered_item()->is_submenu()) {
  298. auto item = *hovered_item();
  299. auto submenu_top_left = item.rect().location() + Gfx::IntPoint { item.rect().width(), 0 };
  300. auto submenu_bottom_left = submenu_top_left + Gfx::IntPoint { 0, item.submenu()->menu_window()->height() };
  301. auto safe_hover_triangle = Gfx::Triangle { m_last_position_in_hover, submenu_top_left, submenu_bottom_left };
  302. m_last_position_in_hover = mouse_event.position();
  303. // Don't update the hovered item if mouse is moving towards a submenu
  304. if (safe_hover_triangle.contains(mouse_event.position()))
  305. return;
  306. }
  307. int index = item_index_at(mouse_event.position());
  308. if (m_hovered_item_index == index)
  309. return;
  310. m_hovered_item_index = index;
  311. update_for_new_hovered_item();
  312. return;
  313. }
  314. void Menu::event(Core::Event& event)
  315. {
  316. if (event.type() == Event::MouseMove) {
  317. handle_mouse_move_event(static_cast<const MouseEvent&>(event));
  318. return;
  319. }
  320. if (event.type() == Event::MouseUp) {
  321. open_hovered_item();
  322. return;
  323. }
  324. if (event.type() == Event::MouseWheel && is_scrollable()) {
  325. ASSERT(menu_window());
  326. auto& mouse_event = static_cast<const MouseEvent&>(event);
  327. m_scroll_offset += mouse_event.wheel_delta();
  328. m_scroll_offset = clamp(m_scroll_offset, 0, m_max_scroll_offset);
  329. int index = item_index_at(mouse_event.position());
  330. if (m_hovered_item_index == index)
  331. return;
  332. m_hovered_item_index = index;
  333. update_for_new_hovered_item();
  334. return;
  335. }
  336. if (event.type() == Event::KeyDown) {
  337. auto key = static_cast<KeyEvent&>(event).key();
  338. if (!(key == Key_Up || key == Key_Down || key == Key_Left || key == Key_Right || key == Key_Return))
  339. return;
  340. ASSERT(menu_window());
  341. ASSERT(menu_window()->is_visible());
  342. // Default to the first item on key press if one has not been selected yet
  343. if (!hovered_item()) {
  344. m_hovered_item_index = 0;
  345. update_for_new_hovered_item(key == Key_Right);
  346. return;
  347. }
  348. if (key == Key_Up) {
  349. ASSERT(m_items.at(0).type() != MenuItem::Separator);
  350. if (is_scrollable() && m_hovered_item_index == 0)
  351. return;
  352. auto original_index = m_hovered_item_index;
  353. do {
  354. if (m_hovered_item_index == 0)
  355. m_hovered_item_index = m_items.size() - 1;
  356. else
  357. --m_hovered_item_index;
  358. if (m_hovered_item_index == original_index)
  359. return;
  360. } while (hovered_item()->type() == MenuItem::Separator || !hovered_item()->is_enabled());
  361. ASSERT(m_hovered_item_index >= 0 && m_hovered_item_index <= static_cast<int>(m_items.size()) - 1);
  362. if (is_scrollable() && m_hovered_item_index < m_scroll_offset)
  363. --m_scroll_offset;
  364. update_for_new_hovered_item();
  365. return;
  366. }
  367. if (key == Key_Down) {
  368. ASSERT(m_items.at(0).type() != MenuItem::Separator);
  369. if (is_scrollable() && m_hovered_item_index == static_cast<int>(m_items.size()) - 1)
  370. return;
  371. auto original_index = m_hovered_item_index;
  372. do {
  373. if (m_hovered_item_index == static_cast<int>(m_items.size()) - 1)
  374. m_hovered_item_index = 0;
  375. else
  376. ++m_hovered_item_index;
  377. if (m_hovered_item_index == original_index)
  378. return;
  379. } while (hovered_item()->type() == MenuItem::Separator || !hovered_item()->is_enabled());
  380. ASSERT(m_hovered_item_index >= 0 && m_hovered_item_index <= static_cast<int>(m_items.size()) - 1);
  381. if (is_scrollable() && m_hovered_item_index >= (m_scroll_offset + visible_item_count()))
  382. ++m_scroll_offset;
  383. update_for_new_hovered_item();
  384. return;
  385. }
  386. }
  387. Core::Object::event(event);
  388. }
  389. void Menu::clear_hovered_item()
  390. {
  391. if (!hovered_item())
  392. return;
  393. m_hovered_item_index = -1;
  394. redraw();
  395. }
  396. void Menu::did_activate(MenuItem& item)
  397. {
  398. if (item.type() == MenuItem::Type::Separator)
  399. return;
  400. if (on_item_activation)
  401. on_item_activation(item);
  402. MenuManager::the().close_bar();
  403. if (m_client)
  404. m_client->post_message(Messages::WindowClient::MenuItemActivated(m_menu_id, item.identifier()));
  405. }
  406. bool Menu::activate_default()
  407. {
  408. for (auto& item : m_items) {
  409. if (item.type() == MenuItem::Type::Separator)
  410. continue;
  411. if (item.is_enabled() && item.is_default()) {
  412. did_activate(item);
  413. return true;
  414. }
  415. }
  416. return false;
  417. }
  418. MenuItem* Menu::item_with_identifier(unsigned identifier)
  419. {
  420. for (auto& item : m_items) {
  421. if (item.identifier() == identifier)
  422. return &item;
  423. }
  424. return nullptr;
  425. }
  426. int Menu::item_index_at(const Gfx::IntPoint& position)
  427. {
  428. int i = 0;
  429. for (auto& item : m_items) {
  430. if (item.rect().contains(position))
  431. return i;
  432. ++i;
  433. }
  434. return -1;
  435. }
  436. void Menu::close()
  437. {
  438. MenuManager::the().close_menu_and_descendants(*this);
  439. }
  440. void Menu::redraw_if_theme_changed()
  441. {
  442. if (m_theme_index_at_last_paint != MenuManager::the().theme_index())
  443. redraw();
  444. }
  445. void Menu::popup(const Gfx::IntPoint& position)
  446. {
  447. do_popup(position, true);
  448. }
  449. void Menu::do_popup(const Gfx::IntPoint& position, bool make_input)
  450. {
  451. if (is_empty()) {
  452. dbg() << "Menu: Empty menu popup";
  453. return;
  454. }
  455. auto& window = ensure_menu_window();
  456. redraw_if_theme_changed();
  457. const int margin = 30;
  458. Gfx::IntPoint adjusted_pos = position;
  459. if (adjusted_pos.x() + window.width() >= Screen::the().width() - margin) {
  460. adjusted_pos = adjusted_pos.translated(-window.width(), 0);
  461. }
  462. if (adjusted_pos.y() + window.height() >= Screen::the().height() - margin) {
  463. adjusted_pos = adjusted_pos.translated(0, -window.height());
  464. }
  465. if (adjusted_pos.y() < MenuManager::the().menubar_rect().height())
  466. adjusted_pos.set_y(MenuManager::the().menubar_rect().height());
  467. window.move_to(adjusted_pos);
  468. window.set_visible(true);
  469. MenuManager::the().open_menu(*this, make_input);
  470. WindowManager::the().did_popup_a_menu({});
  471. }
  472. bool Menu::is_menu_ancestor_of(const Menu& other) const
  473. {
  474. for (auto& item : m_items) {
  475. if (!item.is_submenu())
  476. continue;
  477. auto& submenu = *item.submenu();
  478. if (&submenu == &other)
  479. return true;
  480. if (submenu.is_menu_ancestor_of(other))
  481. return true;
  482. }
  483. return false;
  484. }
  485. }