Menu.cpp 19 KB

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