AppletManager.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2020-2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "AppletManager.h"
  27. #include <AK/QuickSort.h>
  28. #include <LibGfx/Painter.h>
  29. #include <WindowServer/MenuManager.h>
  30. namespace WindowServer {
  31. static AppletManager* s_the;
  32. Vector<String> order_vector;
  33. AppletManager::AppletManager()
  34. {
  35. s_the = this;
  36. auto wm_config = Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini");
  37. auto order = wm_config->read_entry("Applet", "Order");
  38. order_vector = order.split(',');
  39. }
  40. AppletManager::~AppletManager()
  41. {
  42. }
  43. AppletManager& AppletManager::the()
  44. {
  45. ASSERT(s_the);
  46. return *s_the;
  47. }
  48. void AppletManager::event(Core::Event& event)
  49. {
  50. auto& mouse_event = static_cast<MouseEvent&>(event);
  51. for (auto& applet : m_applets) {
  52. if (!applet)
  53. continue;
  54. if (!applet->rect_in_menubar().contains(mouse_event.position()))
  55. continue;
  56. auto local_event = mouse_event.translated(-applet->rect_in_menubar().location());
  57. applet->event(local_event);
  58. }
  59. }
  60. void AppletManager::add_applet(Window& applet)
  61. {
  62. m_applets.append(applet.make_weak_ptr());
  63. quick_sort(m_applets.begin(), m_applets.end(), [](auto& a, auto& b) {
  64. int index_a = order_vector.find_first_index(a->title());
  65. int index_b = order_vector.find_first_index(b->title());
  66. return index_a > index_b;
  67. });
  68. calculate_applet_rects(MenuManager::the().window());
  69. }
  70. void AppletManager::calculate_applet_rects(Window& window)
  71. {
  72. auto menubar_rect = window.rect();
  73. int right_edge_x = menubar_rect.width() - 4;
  74. for (auto& existing_applet : m_applets) {
  75. Gfx::Rect new_applet_rect(right_edge_x - existing_applet->size().width(), 0, existing_applet->size().width(), existing_applet->size().height());
  76. Gfx::Rect dummy_menubar_rect(0, 0, 0, 18);
  77. new_applet_rect.center_vertically_within(dummy_menubar_rect);
  78. existing_applet->set_rect_in_menubar(new_applet_rect);
  79. right_edge_x = existing_applet->rect_in_menubar().x() - 4;
  80. }
  81. }
  82. void AppletManager::remove_applet(Window& applet)
  83. {
  84. m_applets.remove_first_matching([&](auto& entry) {
  85. return &applet == entry.ptr();
  86. });
  87. }
  88. void AppletManager::draw()
  89. {
  90. for (auto& applet : m_applets) {
  91. if (!applet)
  92. continue;
  93. draw_applet(*applet);
  94. }
  95. }
  96. void AppletManager::draw_applet(const Window& applet)
  97. {
  98. if (!applet.backing_store())
  99. return;
  100. Gfx::Painter painter(*MenuManager::the().window().backing_store());
  101. painter.fill_rect(applet.rect_in_menubar(), WindowManager::the().palette().window());
  102. painter.blit(applet.rect_in_menubar().location(), *applet.backing_store(), applet.backing_store()->rect());
  103. }
  104. void AppletManager::invalidate_applet(const Window& applet, const Gfx::Rect& rect)
  105. {
  106. draw_applet(applet);
  107. MenuManager::the().window().invalidate(rect.translated(applet.rect_in_menubar().location()));
  108. }
  109. }