1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 14:48:11 +00:00
serenity/Widgets/Window.cpp
Andreas Kling b0e3f73375 Start refactoring the windowing system to use an event loop.
Userspace programs can now open /dev/gui_events and read a stream of GUI_Event
structs one at a time.

I was stuck on a stupid problem where we'd reenter Scheduler::yield() due to
having one of the has_data_available_for_reading() implementations using locks.
2019-01-14 14:42:49 +01:00

116 lines
2.9 KiB
C++

#include "Window.h"
#include "WindowManager.h"
#include "Event.h"
#include "EventLoop.h"
#include "Widget.h"
#include "Process.h"
Window::Window(Process& process, int window_id)
: m_process(process)
, m_window_id(window_id)
{
WindowManager::the().addWindow(*this);
}
Window::~Window()
{
WindowManager::the().removeWindow(*this);
}
void Window::setTitle(String&& title)
{
if (m_title == title)
return;
m_title = move(title);
WindowManager::the().notifyTitleChanged(*this);
}
void Window::setRect(const Rect& rect)
{
if (m_rect == rect)
return;
auto oldRect = m_rect;
m_rect = rect;
dbgprintf("Window::setRect %d,%d %dx%d\n", m_rect.x(), m_rect.y(), m_rect.width(), m_rect.height());
m_backing = GraphicsBitmap::create(m_rect.size());
WindowManager::the().notifyRectChanged(*this, oldRect, m_rect);
}
void Window::repaint(const Rect& rect)
{
event(*make<PaintEvent>(rect));
}
void Window::update(const Rect& rect)
{
EventLoop::main().postEvent(this, make<PaintEvent>(rect));
}
// FIXME: Just use the same types.
static GUI_MouseButton to_api(MouseButton button)
{
switch (button) {
case MouseButton::None: return GUI_MouseButton::NoButton;
case MouseButton::Left: return GUI_MouseButton::Left;
case MouseButton::Right: return GUI_MouseButton::Right;
case MouseButton::Middle: return GUI_MouseButton::Middle;
}
}
void Window::event(Event& event)
{
GUI_Event gui_event;
gui_event.window_id = window_id();
switch (event.type()) {
case Event::Paint:
gui_event.type = GUI_Event::Type::Paint;
gui_event.paint.rect = static_cast<PaintEvent&>(event).rect();
break;
case Event::MouseMove:
gui_event.type = GUI_Event::Type::MouseMove;
gui_event.mouse.position = static_cast<MouseEvent&>(event).position();
break;
case Event::MouseDown:
gui_event.type = GUI_Event::Type::MouseDown;
gui_event.mouse.position = static_cast<MouseEvent&>(event).position();
gui_event.mouse.button = to_api(static_cast<MouseEvent&>(event).button());
break;
case Event::MouseUp:
gui_event.type = GUI_Event::Type::MouseUp;
gui_event.mouse.position = static_cast<MouseEvent&>(event).position();
gui_event.mouse.button = to_api(static_cast<MouseEvent&>(event).button());
break;
}
if (gui_event.type == GUI_Event::Type::Invalid)
return;
{
LOCKER(m_process.gui_events_lock());
m_process.gui_events().append(move(gui_event));
}
}
void Window::did_paint()
{
WindowManager::the().did_paint(*this);
}
bool Window::isActive() const
{
return WindowManager::the().activeWindow() == this;
}
bool Window::isVisible() const
{
return WindowManager::the().isVisible(const_cast<Window&>(*this));
}
void Window::close()
{
WindowManager::the().removeWindow(*this);
deleteLater();
}