WSMenu.cpp 19 KB

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