Menubar.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Badge.h>
  7. #include <AK/IDAllocator.h>
  8. #include <LibGUI/Menu.h>
  9. #include <LibGUI/MenuItem.h>
  10. #include <LibGUI/Menubar.h>
  11. #include <LibGUI/WindowServerConnection.h>
  12. namespace GUI {
  13. static IDAllocator s_menubar_id_allocator;
  14. Menubar::Menubar()
  15. {
  16. }
  17. Menubar::~Menubar()
  18. {
  19. unrealize_menubar();
  20. }
  21. Menu& Menubar::add_menu(String name)
  22. {
  23. auto& menu = add<Menu>(move(name));
  24. m_menus.append(menu);
  25. return menu;
  26. }
  27. int Menubar::realize_menubar()
  28. {
  29. auto menubar_id = s_menubar_id_allocator.allocate();
  30. WindowServerConnection::the().async_create_menubar(menubar_id);
  31. return menubar_id;
  32. }
  33. void Menubar::unrealize_menubar()
  34. {
  35. if (m_menubar_id == -1)
  36. return;
  37. WindowServerConnection::the().async_destroy_menubar(m_menubar_id);
  38. m_menubar_id = -1;
  39. }
  40. void Menubar::notify_added_to_window(Badge<Window>)
  41. {
  42. VERIFY(m_menubar_id == -1);
  43. m_menubar_id = realize_menubar();
  44. VERIFY(m_menubar_id != -1);
  45. for (auto& menu : m_menus) {
  46. int menu_id = menu.realize_menu();
  47. VERIFY(menu_id != -1);
  48. WindowServerConnection::the().async_add_menu_to_menubar(m_menubar_id, menu_id);
  49. }
  50. }
  51. void Menubar::notify_removed_from_window(Badge<Window>)
  52. {
  53. unrealize_menubar();
  54. }
  55. }