mirror of
https://github.com/SerenityOS/serenity
synced 2024-07-22 02:26:11 +00:00
The WindowManager can now react to mouse events on the window title bar.
This commit is contained in:
parent
5d125e40d9
commit
22721e6729
|
@ -40,6 +40,7 @@ public:
|
||||||
|
|
||||||
bool isMouseEvent() const { return m_type == MouseMove || m_type == MouseDown || m_type == MouseUp; }
|
bool isMouseEvent() const { return m_type == MouseMove || m_type == MouseDown || m_type == MouseUp; }
|
||||||
bool isKeyEvent() const { return m_type == KeyUp || m_type == KeyDown; }
|
bool isKeyEvent() const { return m_type == KeyUp || m_type == KeyDown; }
|
||||||
|
bool isPaintEvent() const { return m_type == Paint; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit Event(Type type) : m_type(type) { }
|
explicit Event(Type type) : m_type(type) { }
|
||||||
|
|
|
@ -16,7 +16,6 @@ void RootWidget::onPaint(PaintEvent& event)
|
||||||
//printf("RootWidget::onPaint\n");
|
//printf("RootWidget::onPaint\n");
|
||||||
Painter painter(*this);
|
Painter painter(*this);
|
||||||
painter.fillRect(Rect(0, 0, 800, 600), Color(0x40, 0x40, 0x40));
|
painter.fillRect(Rect(0, 0, 800, 600), Color(0x40, 0x40, 0x40));
|
||||||
WindowManager::the().paintWindowFrames();
|
|
||||||
Widget::onPaint(event);
|
Widget::onPaint(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,19 @@
|
||||||
|
|
||||||
extern TerminalWidget* g_tw;
|
extern TerminalWidget* g_tw;
|
||||||
|
|
||||||
|
static const int windowFrameWidth = 2;
|
||||||
|
static const int windowTitleBarHeight = 16;
|
||||||
|
|
||||||
|
static inline Rect titleBarRectForWindow(const Window& window)
|
||||||
|
{
|
||||||
|
return {
|
||||||
|
window.x() - windowFrameWidth,
|
||||||
|
window.y() - windowTitleBarHeight - windowFrameWidth,
|
||||||
|
window.width() + windowFrameWidth * 2,
|
||||||
|
windowTitleBarHeight + windowFrameWidth
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
WindowManager& WindowManager::the()
|
WindowManager& WindowManager::the()
|
||||||
{
|
{
|
||||||
static WindowManager* s_the = new WindowManager;
|
static WindowManager* s_the = new WindowManager;
|
||||||
|
@ -16,6 +29,8 @@ WindowManager& WindowManager::the()
|
||||||
|
|
||||||
WindowManager::WindowManager()
|
WindowManager::WindowManager()
|
||||||
{
|
{
|
||||||
|
m_windowBorderColor = Color(0x00, 0x00, 0x80);
|
||||||
|
m_windowTitleColor = Color(0xff, 0xff, 0xff);
|
||||||
}
|
}
|
||||||
|
|
||||||
WindowManager::~WindowManager()
|
WindowManager::~WindowManager()
|
||||||
|
@ -34,14 +49,7 @@ void WindowManager::paintWindowFrame(Window& window)
|
||||||
|
|
||||||
printf("WM: paintWindowFrame {%p}, rect: %d,%d %dx%d\n", &window, window.rect().x(), window.rect().y(), window.rect().width(), window.rect().height());
|
printf("WM: paintWindowFrame {%p}, rect: %d,%d %dx%d\n", &window, window.rect().x(), window.rect().y(), window.rect().width(), window.rect().height());
|
||||||
|
|
||||||
static const int windowFrameWidth = 2;
|
Rect topRect = titleBarRectForWindow(window);
|
||||||
static const int windowTitleBarHeight = 16;
|
|
||||||
|
|
||||||
Rect topRect {
|
|
||||||
window.x() - windowFrameWidth,
|
|
||||||
window.y() - windowTitleBarHeight - windowFrameWidth,
|
|
||||||
window.width() + windowFrameWidth * 2,
|
|
||||||
windowTitleBarHeight + windowFrameWidth };
|
|
||||||
|
|
||||||
Rect bottomRect {
|
Rect bottomRect {
|
||||||
window.x() - windowFrameWidth,
|
window.x() - windowFrameWidth,
|
||||||
|
@ -63,25 +71,23 @@ void WindowManager::paintWindowFrame(Window& window)
|
||||||
window.height()
|
window.height()
|
||||||
};
|
};
|
||||||
|
|
||||||
static const Color windowBorderColor(0x00, 0x00, 0x80);
|
|
||||||
static const Color windowTitleColor(0xff, 0xff, 0xff);
|
|
||||||
|
|
||||||
Rect borderRect {
|
Rect borderRect {
|
||||||
topRect.x() - 1,
|
topRect.x() - 1,
|
||||||
topRect.y() - 1,
|
topRect.y() - 1,
|
||||||
topRect.width() + 2,
|
topRect.width() + 2,
|
||||||
windowFrameWidth + windowTitleBarHeight + window.rect().height() + 4
|
windowFrameWidth + windowTitleBarHeight + window.rect().height() + 4
|
||||||
};
|
};
|
||||||
|
|
||||||
p.drawRect(borderRect, Color(255, 255, 255));
|
p.drawRect(borderRect, Color(255, 255, 255));
|
||||||
borderRect.inflate(2, 2);
|
borderRect.inflate(2, 2);
|
||||||
p.drawRect(borderRect, windowBorderColor);
|
p.drawRect(borderRect, m_windowBorderColor);
|
||||||
|
|
||||||
p.fillRect(topRect, windowBorderColor);
|
p.fillRect(topRect, m_windowBorderColor);
|
||||||
p.fillRect(bottomRect, windowBorderColor);
|
p.fillRect(bottomRect, m_windowBorderColor);
|
||||||
p.fillRect(leftRect, windowBorderColor);
|
p.fillRect(leftRect, m_windowBorderColor);
|
||||||
p.fillRect(rightRect, windowBorderColor);
|
p.fillRect(rightRect, m_windowBorderColor);
|
||||||
|
|
||||||
p.drawText(topRect, window.title(), Painter::TextAlignment::Center, windowTitleColor);
|
p.drawText(topRect, window.title(), Painter::TextAlignment::Center, m_windowTitleColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowManager::addWindow(Window& window)
|
void WindowManager::addWindow(Window& window)
|
||||||
|
@ -108,11 +114,24 @@ void WindowManager::notifyRectChanged(Window& window, const Rect& oldRect, const
|
||||||
newRect.height());
|
newRect.height());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WindowManager::handleTitleBarMouseEvent(Window& window, MouseEvent& event)
|
||||||
|
{
|
||||||
|
byte r = (((double)rand()) / (double)RAND_MAX) * 255.0;
|
||||||
|
byte g = (((double)rand()) / (double)RAND_MAX) * 255.0;
|
||||||
|
byte b = (((double)rand()) / (double)RAND_MAX) * 255.0;
|
||||||
|
m_windowBorderColor = Color(r, g, b);
|
||||||
|
paintWindowFrame(window);
|
||||||
|
}
|
||||||
|
|
||||||
void WindowManager::processMouseEvent(MouseEvent& event)
|
void WindowManager::processMouseEvent(MouseEvent& event)
|
||||||
{
|
{
|
||||||
// First step: hit test windows.
|
// FIXME: Respect z-order of windows...
|
||||||
for (auto* window : m_windows) {
|
for (auto* window : m_windows) {
|
||||||
// FIXME: z-order...
|
if (titleBarRectForWindow(*window).contains(event.position())) {
|
||||||
|
handleTitleBarMouseEvent(*window, event);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (window->rect().contains(event.position())) {
|
if (window->rect().contains(event.position())) {
|
||||||
// FIXME: Re-use the existing event instead of crafting a new one?
|
// FIXME: Re-use the existing event instead of crafting a new one?
|
||||||
auto localEvent = make<MouseEvent>(event.type(), event.x() - window->rect().x(), event.y() - window->rect().y(), event.button());
|
auto localEvent = make<MouseEvent>(event.type(), event.x() - window->rect().x(), event.y() - window->rect().y(), event.button());
|
||||||
|
@ -129,6 +148,13 @@ void WindowManager::processMouseEvent(MouseEvent& event)
|
||||||
result.widget->event(*localEvent);
|
result.widget->event(*localEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WindowManager::handlePaintEvent(PaintEvent& event)
|
||||||
|
{
|
||||||
|
printf("[WM] paint event\n");
|
||||||
|
m_rootWidget->event(event);
|
||||||
|
paintWindowFrames();
|
||||||
|
}
|
||||||
|
|
||||||
void WindowManager::event(Event& event)
|
void WindowManager::event(Event& event)
|
||||||
{
|
{
|
||||||
if (event.isMouseEvent())
|
if (event.isMouseEvent())
|
||||||
|
@ -140,9 +166,8 @@ void WindowManager::event(Event& event)
|
||||||
return focusedWidget->event(event);
|
return focusedWidget->event(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.type() == Event::Paint) {
|
if (event.isPaintEvent())
|
||||||
return m_rootWidget->event(event);
|
return handlePaintEvent(static_cast<PaintEvent&>(event));
|
||||||
}
|
|
||||||
|
|
||||||
return Object::event(event);
|
return Object::event(event);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,16 +2,17 @@
|
||||||
|
|
||||||
#include "Object.h"
|
#include "Object.h"
|
||||||
#include "Rect.h"
|
#include "Rect.h"
|
||||||
|
#include "Color.h"
|
||||||
#include <AK/HashTable.h>
|
#include <AK/HashTable.h>
|
||||||
|
|
||||||
class MouseEvent;
|
class MouseEvent;
|
||||||
|
class PaintEvent;
|
||||||
class Widget;
|
class Widget;
|
||||||
class Window;
|
class Window;
|
||||||
|
|
||||||
class WindowManager : public Object {
|
class WindowManager : public Object {
|
||||||
public:
|
public:
|
||||||
static WindowManager& the();
|
static WindowManager& the();
|
||||||
|
|
||||||
void addWindow(Window&);
|
void addWindow(Window&);
|
||||||
void paintWindowFrames();
|
void paintWindowFrames();
|
||||||
|
|
||||||
|
@ -26,9 +27,14 @@ private:
|
||||||
~WindowManager();
|
~WindowManager();
|
||||||
|
|
||||||
void processMouseEvent(MouseEvent&);
|
void processMouseEvent(MouseEvent&);
|
||||||
|
void handleTitleBarMouseEvent(Window&, MouseEvent&);
|
||||||
|
void handlePaintEvent(PaintEvent&);
|
||||||
|
|
||||||
virtual void event(Event&) override;
|
virtual void event(Event&) override;
|
||||||
|
|
||||||
|
Color m_windowBorderColor;
|
||||||
|
Color m_windowTitleColor;
|
||||||
|
|
||||||
void paintWindowFrame(Window&);
|
void paintWindowFrame(Window&);
|
||||||
HashTable<Window*> m_windows;
|
HashTable<Window*> m_windows;
|
||||||
Widget* m_rootWidget { nullptr };
|
Widget* m_rootWidget { nullptr };
|
||||||
|
|
Loading…
Reference in a new issue