2020-02-09 17:37:42 +00:00
|
|
|
|
/*
|
2020-12-25 11:15:16 +00:00
|
|
|
|
* Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
|
2021-03-30 20:41:14 +00:00
|
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
2020-02-09 17:37:42 +00:00
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-09 17:37:42 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "AppletManager.h"
|
|
|
|
|
#include <AK/QuickSort.h>
|
2021-03-30 21:30:50 +00:00
|
|
|
|
#include <LibCore/EventLoop.h>
|
2020-02-09 17:37:42 +00:00
|
|
|
|
#include <LibGfx/Painter.h>
|
|
|
|
|
#include <WindowServer/MenuManager.h>
|
|
|
|
|
|
|
|
|
|
namespace WindowServer {
|
|
|
|
|
|
|
|
|
|
static AppletManager* s_the;
|
|
|
|
|
Vector<String> order_vector;
|
|
|
|
|
|
|
|
|
|
AppletManager::AppletManager()
|
|
|
|
|
{
|
|
|
|
|
s_the = this;
|
|
|
|
|
|
2021-04-29 19:40:59 +00:00
|
|
|
|
auto wm_config = Core::ConfigFile::open("/etc/WindowServer.ini");
|
2020-02-09 17:37:42 +00:00
|
|
|
|
auto order = wm_config->read_entry("Applet", "Order");
|
|
|
|
|
order_vector = order.split(',');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AppletManager::~AppletManager()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AppletManager& AppletManager::the()
|
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
|
VERIFY(s_the);
|
2020-02-09 17:37:42 +00:00
|
|
|
|
return *s_the;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 20:41:14 +00:00
|
|
|
|
void AppletManager::set_position(const Gfx::IntPoint& position)
|
|
|
|
|
{
|
|
|
|
|
m_window->move_to(position);
|
|
|
|
|
m_window->set_visible(true);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 21:30:50 +00:00
|
|
|
|
void AppletManager::set_hovered_applet(Window* applet)
|
|
|
|
|
{
|
|
|
|
|
if (m_hovered_applet == applet)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (m_hovered_applet)
|
|
|
|
|
Core::EventLoop::current().post_event(*m_hovered_applet, make<Event>(Event::WindowLeft));
|
|
|
|
|
|
|
|
|
|
m_hovered_applet = applet;
|
|
|
|
|
|
|
|
|
|
if (m_hovered_applet)
|
|
|
|
|
Core::EventLoop::current().post_event(*m_hovered_applet, make<Event>(Event::WindowEntered));
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-09 17:37:42 +00:00
|
|
|
|
void AppletManager::event(Core::Event& event)
|
|
|
|
|
{
|
2021-03-30 21:30:50 +00:00
|
|
|
|
if (event.type() == Event::WindowLeft && m_hovered_applet) {
|
|
|
|
|
set_hovered_applet(nullptr);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 20:41:14 +00:00
|
|
|
|
if (!is<MouseEvent>(event))
|
|
|
|
|
return;
|
2020-02-09 17:37:42 +00:00
|
|
|
|
auto& mouse_event = static_cast<MouseEvent&>(event);
|
|
|
|
|
|
|
|
|
|
for (auto& applet : m_applets) {
|
|
|
|
|
if (!applet)
|
|
|
|
|
continue;
|
2021-03-30 20:41:14 +00:00
|
|
|
|
if (!applet->rect_in_applet_area().contains(mouse_event.position()))
|
2020-02-09 17:37:42 +00:00
|
|
|
|
continue;
|
2021-03-30 20:41:14 +00:00
|
|
|
|
auto local_event = mouse_event.translated(-applet->rect_in_applet_area().location());
|
2021-03-30 21:30:50 +00:00
|
|
|
|
set_hovered_applet(applet);
|
|
|
|
|
Core::EventLoop::current().post_event(*applet, make<MouseEvent>(local_event));
|
|
|
|
|
return;
|
2020-02-09 17:37:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AppletManager::add_applet(Window& applet)
|
|
|
|
|
{
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-29 22:26:13 +00:00
|
|
|
|
m_applets.append(applet);
|
2020-02-09 17:37:42 +00:00
|
|
|
|
|
2020-03-31 11:52:35 +00:00
|
|
|
|
// Prune any dead weak pointers from the applet list.
|
|
|
|
|
m_applets.remove_all_matching([](auto& entry) {
|
|
|
|
|
return entry.is_null();
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-03 15:01:37 +00:00
|
|
|
|
quick_sort(m_applets, [](auto& a, auto& b) {
|
2020-02-25 13:49:47 +00:00
|
|
|
|
auto index_a = order_vector.find_first_index(a->title());
|
|
|
|
|
auto index_b = order_vector.find_first_index(b->title());
|
2020-04-19 18:24:03 +00:00
|
|
|
|
return index_a.value_or(0) > index_b.value_or(0);
|
2020-02-09 17:37:42 +00:00
|
|
|
|
});
|
|
|
|
|
|
2021-03-30 20:41:14 +00:00
|
|
|
|
relayout();
|
2020-02-09 17:37:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 20:41:14 +00:00
|
|
|
|
void AppletManager::relayout()
|
2020-02-09 17:37:42 +00:00
|
|
|
|
{
|
2021-03-30 20:41:14 +00:00
|
|
|
|
constexpr int applet_spacing = 4;
|
2021-05-08 22:07:44 +00:00
|
|
|
|
constexpr int applet_window_height = 19;
|
2021-03-30 20:41:14 +00:00
|
|
|
|
int total_width = 0;
|
|
|
|
|
for (auto& existing_applet : m_applets) {
|
|
|
|
|
total_width += max(0, existing_applet->size().width()) + applet_spacing;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (total_width > 0)
|
|
|
|
|
total_width -= applet_spacing;
|
|
|
|
|
|
|
|
|
|
auto right_edge_x = total_width;
|
|
|
|
|
|
2020-02-09 17:37:42 +00:00
|
|
|
|
for (auto& existing_applet : m_applets) {
|
2021-03-30 20:41:14 +00:00
|
|
|
|
auto applet_size = existing_applet->size();
|
|
|
|
|
Gfx::IntRect new_applet_rect(right_edge_x - applet_size.width(), 0, applet_size.width(), applet_size.height());
|
|
|
|
|
|
|
|
|
|
Gfx::IntRect dummy_container_rect(0, 0, 0, applet_window_height);
|
|
|
|
|
new_applet_rect.center_vertically_within(dummy_container_rect);
|
|
|
|
|
|
|
|
|
|
existing_applet->set_rect_in_applet_area(new_applet_rect);
|
|
|
|
|
right_edge_x = existing_applet->rect_in_applet_area().x() - applet_spacing;
|
|
|
|
|
}
|
2020-02-09 17:37:42 +00:00
|
|
|
|
|
2021-03-30 20:41:14 +00:00
|
|
|
|
if (!m_window) {
|
|
|
|
|
m_window = Window::construct(*this, WindowType::AppletArea);
|
|
|
|
|
m_window->set_visible(false);
|
|
|
|
|
}
|
2020-02-09 17:37:42 +00:00
|
|
|
|
|
2021-03-30 20:41:14 +00:00
|
|
|
|
Gfx::IntRect rect { m_window->position(), Gfx::IntSize { total_width, applet_window_height } };
|
|
|
|
|
if (m_window->rect() == rect)
|
|
|
|
|
return;
|
|
|
|
|
m_window->set_rect(rect);
|
2021-04-04 18:22:45 +00:00
|
|
|
|
|
|
|
|
|
repaint();
|
|
|
|
|
|
2021-04-14 21:38:53 +00:00
|
|
|
|
WindowManager::the().tell_wms_applet_area_size_changed(rect.size());
|
2021-04-04 18:22:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AppletManager::repaint()
|
|
|
|
|
{
|
|
|
|
|
if (!m_window) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto rect = Gfx::IntRect { { 0, 0 }, m_window->size() };
|
|
|
|
|
|
2021-03-30 20:41:14 +00:00
|
|
|
|
if (!rect.is_empty()) {
|
|
|
|
|
Gfx::Painter painter(*m_window->backing_store());
|
2021-04-05 11:48:21 +00:00
|
|
|
|
painter.fill_rect(rect, WindowManager::the().palette().button());
|
2020-02-09 17:37:42 +00:00
|
|
|
|
}
|
2021-04-04 18:22:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AppletManager::did_change_theme()
|
|
|
|
|
{
|
|
|
|
|
repaint();
|
2020-02-09 17:37:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AppletManager::remove_applet(Window& applet)
|
|
|
|
|
{
|
|
|
|
|
m_applets.remove_first_matching([&](auto& entry) {
|
|
|
|
|
return &applet == entry.ptr();
|
|
|
|
|
});
|
2020-08-01 23:03:13 +00:00
|
|
|
|
|
2021-03-30 20:41:14 +00:00
|
|
|
|
relayout();
|
2020-02-09 17:37:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AppletManager::draw()
|
|
|
|
|
{
|
|
|
|
|
for (auto& applet : m_applets) {
|
|
|
|
|
if (!applet)
|
|
|
|
|
continue;
|
|
|
|
|
draw_applet(*applet);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AppletManager::draw_applet(const Window& applet)
|
|
|
|
|
{
|
|
|
|
|
if (!applet.backing_store())
|
|
|
|
|
return;
|
|
|
|
|
|
2021-03-30 20:41:14 +00:00
|
|
|
|
Gfx::Painter painter(*m_window->backing_store());
|
2020-08-20 23:20:30 +00:00
|
|
|
|
Gfx::PainterStateSaver saver(painter);
|
2021-03-30 20:41:14 +00:00
|
|
|
|
painter.add_clip_rect(applet.rect_in_applet_area());
|
2021-04-05 11:48:21 +00:00
|
|
|
|
painter.fill_rect(applet.rect_in_applet_area(), WindowManager::the().palette().button());
|
2021-03-30 20:41:14 +00:00
|
|
|
|
painter.blit(applet.rect_in_applet_area().location(), *applet.backing_store(), applet.backing_store()->rect());
|
2020-02-09 17:37:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 20:41:14 +00:00
|
|
|
|
void AppletManager::invalidate_applet(const Window& applet, const Gfx::IntRect&)
|
2020-02-09 17:37:42 +00:00
|
|
|
|
{
|
|
|
|
|
draw_applet(applet);
|
2021-03-30 20:41:14 +00:00
|
|
|
|
draw();
|
|
|
|
|
// FIXME: Invalidate only the exact rect we've been given.
|
|
|
|
|
if (m_window)
|
|
|
|
|
m_window->invalidate();
|
2020-02-09 17:37:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|