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 // Prune any dead weak pointers from the applet list.
75 m_applets.remove_all_matching([](auto& entry) {
76 return entry.is_null();
77 });
78
79 quick_sort(m_applets, [](auto& a, auto& b) {
80 auto index_a = order_vector.find_first_index(a->title());
81 auto index_b = order_vector.find_first_index(b->title());
82 ASSERT(index_a.has_value());
83 ASSERT(index_b.has_value());
84 return index_a.value() > index_b.value();
85 });
86
87 calculate_applet_rects(MenuManager::the().window());
88}
89
90void AppletManager::calculate_applet_rects(Window& window)
91{
92 auto menubar_rect = window.rect();
93 int right_edge_x = menubar_rect.width() - 4;
94 for (auto& existing_applet : m_applets) {
95
96 Gfx::Rect new_applet_rect(right_edge_x - existing_applet->size().width(), 0, existing_applet->size().width(), existing_applet->size().height());
97 Gfx::Rect dummy_menubar_rect(0, 0, 0, 18);
98 new_applet_rect.center_vertically_within(dummy_menubar_rect);
99
100 existing_applet->set_rect_in_menubar(new_applet_rect);
101 right_edge_x = existing_applet->rect_in_menubar().x() - 4;
102 }
103}
104
105void AppletManager::remove_applet(Window& applet)
106{
107 m_applets.remove_first_matching([&](auto& entry) {
108 return &applet == entry.ptr();
109 });
110}
111
112void AppletManager::draw()
113{
114 for (auto& applet : m_applets) {
115 if (!applet)
116 continue;
117 draw_applet(*applet);
118 }
119}
120
121void AppletManager::draw_applet(const Window& applet)
122{
123 if (!applet.backing_store())
124 return;
125
126 Gfx::Painter painter(*MenuManager::the().window().backing_store());
127 painter.fill_rect(applet.rect_in_menubar(), WindowManager::the().palette().window());
128 painter.blit(applet.rect_in_menubar().location(), *applet.backing_store(), applet.backing_store()->rect());
129}
130
131void AppletManager::invalidate_applet(const Window& applet, const Gfx::Rect& rect)
132{
133 draw_applet(applet);
134 MenuManager::the().window().invalidate(rect.translated(applet.rect_in_menubar().location()));
135}
136
137}