Serenity Operating System
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
27#include "AppletManager.h"
28#include <AK/QuickSort.h>
29#include <LibGfx/Painter.h>
30#include <WindowServer/MenuManager.h>
31
32namespace WindowServer {
33
34static AppletManager* s_the;
35Vector<String> order_vector;
36
37AppletManager::AppletManager()
38{
39 s_the = this;
40
41 auto wm_config = Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini");
42 auto order = wm_config->read_entry("Applet", "Order");
43 order_vector = order.split(',');
44}
45
46AppletManager::~AppletManager()
47{
48}
49
50AppletManager& AppletManager::the()
51{
52 ASSERT(s_the);
53 return *s_the;
54}
55
56void AppletManager::event(Core::Event& event)
57{
58 auto& mouse_event = static_cast<MouseEvent&>(event);
59
60 for (auto& applet : m_applets) {
61 if (!applet)
62 continue;
63 if (!applet->rect_in_menubar().contains(mouse_event.position()))
64 continue;
65 auto local_event = mouse_event.translated(-applet->rect_in_menubar().location());
66 applet->event(local_event);
67 }
68}
69
70void AppletManager::add_applet(Window& applet)
71{
72 m_applets.append(applet.make_weak_ptr());
73
74 quick_sort(m_applets.begin(), m_applets.end(), [](auto& a, auto& b) {
75 auto index_a = order_vector.find_first_index(a->title());
76 auto index_b = order_vector.find_first_index(b->title());
77 ASSERT(index_a.has_value());
78 ASSERT(index_b.has_value());
79 return index_a.value() > index_b.value();
80 });
81
82 calculate_applet_rects(MenuManager::the().window());
83}
84
85void AppletManager::calculate_applet_rects(Window& window)
86{
87 auto menubar_rect = window.rect();
88 int right_edge_x = menubar_rect.width() - 4;
89 for (auto& existing_applet : m_applets) {
90
91 Gfx::Rect new_applet_rect(right_edge_x - existing_applet->size().width(), 0, existing_applet->size().width(), existing_applet->size().height());
92 Gfx::Rect dummy_menubar_rect(0, 0, 0, 18);
93 new_applet_rect.center_vertically_within(dummy_menubar_rect);
94
95 existing_applet->set_rect_in_menubar(new_applet_rect);
96 right_edge_x = existing_applet->rect_in_menubar().x() - 4;
97 }
98}
99
100void AppletManager::remove_applet(Window& applet)
101{
102 m_applets.remove_first_matching([&](auto& entry) {
103 return &applet == entry.ptr();
104 });
105}
106
107void AppletManager::draw()
108{
109 for (auto& applet : m_applets) {
110 if (!applet)
111 continue;
112 draw_applet(*applet);
113 }
114}
115
116void AppletManager::draw_applet(const Window& applet)
117{
118 if (!applet.backing_store())
119 return;
120
121 Gfx::Painter painter(*MenuManager::the().window().backing_store());
122 painter.fill_rect(applet.rect_in_menubar(), WindowManager::the().palette().window());
123 painter.blit(applet.rect_in_menubar().location(), *applet.backing_store(), applet.backing_store()->rect());
124}
125
126void AppletManager::invalidate_applet(const Window& applet, const Gfx::Rect& rect)
127{
128 draw_applet(applet);
129 MenuManager::the().window().invalidate(rect.translated(applet.rect_in_menubar().location()));
130}
131
132}