mirror of
https://github.com/RGBCube/serenity
synced 2025-07-23 18:17:41 +00:00
Small refactor.
This commit is contained in:
parent
02f4d6ef8e
commit
f2fa7b615f
3 changed files with 35 additions and 18 deletions
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
|
#include "Point.h"
|
||||||
|
|
||||||
static const char* eventNames[] = {
|
static const char* eventNames[] = {
|
||||||
"Invalid",
|
"Invalid",
|
||||||
|
@ -37,6 +38,9 @@ public:
|
||||||
|
|
||||||
const char* name() const { return eventNames[(unsigned)m_type]; }
|
const char* name() const { return eventNames[(unsigned)m_type]; }
|
||||||
|
|
||||||
|
bool isMouseEvent() const { return m_type == MouseMove || m_type == MouseDown || m_type == MouseUp; }
|
||||||
|
bool isKeyEvent() const { return m_type == KeyUp || m_type == KeyDown; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit Event(Type type) : m_type(type) { }
|
explicit Event(Type type) : m_type(type) { }
|
||||||
|
|
||||||
|
@ -101,19 +105,18 @@ class MouseEvent : public Event {
|
||||||
public:
|
public:
|
||||||
MouseEvent(Type type, int x, int y, MouseButton button = MouseButton::None)
|
MouseEvent(Type type, int x, int y, MouseButton button = MouseButton::None)
|
||||||
: Event(type)
|
: Event(type)
|
||||||
, m_x(x)
|
, m_position(x, y)
|
||||||
, m_y(y)
|
|
||||||
, m_button(button)
|
, m_button(button)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
int x() const { return m_x; }
|
Point position() const { return m_position; }
|
||||||
int y() const { return m_y; }
|
int x() const { return m_position.x(); }
|
||||||
|
int y() const { return m_position.y(); }
|
||||||
MouseButton button() const { return m_button; }
|
MouseButton button() const { return m_button; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int m_x { 0 };
|
Point m_position;
|
||||||
int m_y { 0 };
|
|
||||||
MouseButton m_button { MouseButton::None };
|
MouseButton m_button { MouseButton::None };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -108,21 +108,32 @@ void WindowManager::notifyRectChanged(Window& window, const Rect& oldRect, const
|
||||||
newRect.height());
|
newRect.height());
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowManager::event(Event& event)
|
void WindowManager::processMouseEvent(MouseEvent& event)
|
||||||
{
|
{
|
||||||
if (event.type() == Event::MouseMove
|
// First step: hit test windows.
|
||||||
|| event.type() == Event::MouseDown
|
for (auto* window : m_windows) {
|
||||||
|| event.type() == Event::MouseUp) {
|
// FIXME: z-order...
|
||||||
auto& me = static_cast<MouseEvent&>(event);
|
if (window->rect().contains(event.position())) {
|
||||||
|
// FIXME: Re-use the existing event instead of crafting a new one?
|
||||||
auto result = m_rootWidget->hitTest(me.x(), me.y());
|
auto localEvent = make<MouseEvent>(event.type(), event.x() - window->rect().x(), event.y() - window->rect().y(), event.button());
|
||||||
//printf("hit test for %d,%d found: %s{%p} %d,%d\n", me.x(), me.y(), result.widget->className(), result.widget, result.localX, result.localY);
|
return;
|
||||||
auto localEvent = make<MouseEvent>(event.type(), result.localX, result.localY, me.button());
|
}
|
||||||
result.widget->event(*localEvent);
|
|
||||||
return Object::event(event);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.type() == Event::KeyDown || event.type() == Event::KeyUp) {
|
// Otherwise: send it to root the root widget...
|
||||||
|
auto result = m_rootWidget->hitTest(event.x(), event.y());
|
||||||
|
//printf("hit test for %d,%d found: %s{%p} %d,%d\n", me.x(), me.y(), result.widget->className(), result.widget, result.localX, result.localY);
|
||||||
|
// FIXME: Re-use the existing event instead of crafting a new one?
|
||||||
|
auto localEvent = make<MouseEvent>(event.type(), result.localX, result.localY, event.button());
|
||||||
|
result.widget->event(*localEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowManager::event(Event& event)
|
||||||
|
{
|
||||||
|
if (event.isMouseEvent())
|
||||||
|
return processMouseEvent(static_cast<MouseEvent&>(event));
|
||||||
|
|
||||||
|
if (event.isKeyEvent()) {
|
||||||
// FIXME: Implement proper focus.
|
// FIXME: Implement proper focus.
|
||||||
Widget* focusedWidget = g_tw;
|
Widget* focusedWidget = g_tw;
|
||||||
return focusedWidget->event(event);
|
return focusedWidget->event(event);
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include "Rect.h"
|
#include "Rect.h"
|
||||||
#include <AK/HashTable.h>
|
#include <AK/HashTable.h>
|
||||||
|
|
||||||
|
class MouseEvent;
|
||||||
class Widget;
|
class Widget;
|
||||||
class Window;
|
class Window;
|
||||||
|
|
||||||
|
@ -24,6 +25,8 @@ private:
|
||||||
WindowManager();
|
WindowManager();
|
||||||
~WindowManager();
|
~WindowManager();
|
||||||
|
|
||||||
|
void processMouseEvent(MouseEvent&);
|
||||||
|
|
||||||
virtual void event(Event&) override;
|
virtual void event(Event&) override;
|
||||||
|
|
||||||
void paintWindowFrame(Window&);
|
void paintWindowFrame(Window&);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue