mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-22 17:31:58 -05:00
Move windowing stuff from AbstractScreen to WindowManager.
This commit is contained in:
parent
415c4b90c5
commit
02f4d6ef8e
6 changed files with 59 additions and 58 deletions
|
@ -3,12 +3,9 @@
|
||||||
#include "Event.h"
|
#include "Event.h"
|
||||||
#include "Widget.h"
|
#include "Widget.h"
|
||||||
#include <AK/Assertions.h>
|
#include <AK/Assertions.h>
|
||||||
#include "TerminalWidget.h"
|
|
||||||
|
|
||||||
static AbstractScreen* s_the;
|
static AbstractScreen* s_the;
|
||||||
|
|
||||||
extern TerminalWidget* g_tw;
|
|
||||||
|
|
||||||
AbstractScreen& AbstractScreen::the()
|
AbstractScreen& AbstractScreen::the()
|
||||||
{
|
{
|
||||||
ASSERT(s_the);
|
ASSERT(s_the);
|
||||||
|
@ -28,35 +25,3 @@ AbstractScreen::~AbstractScreen()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void AbstractScreen::event(Event& event)
|
|
||||||
{
|
|
||||||
if (event.type() == Event::MouseMove
|
|
||||||
|| event.type() == Event::MouseDown
|
|
||||||
|| event.type() == Event::MouseUp) {
|
|
||||||
auto& me = static_cast<MouseEvent&>(event);
|
|
||||||
//printf("AbstractScreen::onMouseMove: %d, %d\n", me.x(), me.y());
|
|
||||||
auto result = m_rootWidget->hitTest(me.x(), me.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);
|
|
||||||
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) {
|
|
||||||
// FIXME: Implement proper focus.
|
|
||||||
Widget* focusedWidget = g_tw;
|
|
||||||
return focusedWidget->event(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Object::event(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
void AbstractScreen::setRootWidget(Widget* widget)
|
|
||||||
{
|
|
||||||
// FIXME: Should we support switching root widgets?
|
|
||||||
ASSERT(!m_rootWidget);
|
|
||||||
ASSERT(widget);
|
|
||||||
|
|
||||||
m_rootWidget = widget;
|
|
||||||
EventLoop::main().postEvent(m_rootWidget, make<ShowEvent>());
|
|
||||||
}
|
|
||||||
|
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
#include "Object.h"
|
#include "Object.h"
|
||||||
|
|
||||||
class Widget;
|
|
||||||
|
|
||||||
class AbstractScreen : public Object {
|
class AbstractScreen : public Object {
|
||||||
public:
|
public:
|
||||||
virtual ~AbstractScreen();
|
virtual ~AbstractScreen();
|
||||||
|
@ -11,20 +9,13 @@ public:
|
||||||
unsigned width() const { return m_width; }
|
unsigned width() const { return m_width; }
|
||||||
unsigned height() const { return m_height; }
|
unsigned height() const { return m_height; }
|
||||||
|
|
||||||
Widget* rootWidget() { return m_rootWidget; }
|
|
||||||
void setRootWidget(Widget*);
|
|
||||||
|
|
||||||
static AbstractScreen& the();
|
static AbstractScreen& the();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
AbstractScreen(unsigned width, unsigned height);
|
AbstractScreen(unsigned width, unsigned height);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void event(Event&) override;
|
|
||||||
|
|
||||||
unsigned m_width { 0 };
|
unsigned m_width { 0 };
|
||||||
unsigned m_height { 0 };
|
unsigned m_height { 0 };
|
||||||
|
|
||||||
Widget* m_rootWidget { nullptr };
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
#include "EventLoopSDL.h"
|
#include "EventLoopSDL.h"
|
||||||
#include "Event.h"
|
#include "Event.h"
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include "AbstractScreen.h"
|
|
||||||
#include "Widget.h"
|
#include "Widget.h"
|
||||||
#include "TerminalWidget.h"
|
#include "TerminalWidget.h"
|
||||||
|
#include "WindowManager.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int g_fd;
|
int g_fd;
|
||||||
|
@ -46,23 +46,23 @@ void EventLoopSDL::waitForEvent()
|
||||||
if (sdlEvent.window.event == SDL_WINDOWEVENT_EXPOSED) {
|
if (sdlEvent.window.event == SDL_WINDOWEVENT_EXPOSED) {
|
||||||
// Spam paint events whenever we get exposed.
|
// Spam paint events whenever we get exposed.
|
||||||
// This is obviously not ideal, but the SDL backend here is just a prototype anyway.
|
// This is obviously not ideal, but the SDL backend here is just a prototype anyway.
|
||||||
postEvent(AbstractScreen::the().rootWidget(), make<PaintEvent>());
|
postEvent(&WindowManager::the(), make<PaintEvent>());
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
case SDL_MOUSEMOTION:
|
case SDL_MOUSEMOTION:
|
||||||
postEvent(&AbstractScreen::the(), make<MouseEvent>(Event::MouseMove, sdlEvent.motion.x, sdlEvent.motion.y));
|
postEvent(&WindowManager::the(), make<MouseEvent>(Event::MouseMove, sdlEvent.motion.x, sdlEvent.motion.y));
|
||||||
return;
|
return;
|
||||||
case SDL_MOUSEBUTTONDOWN:
|
case SDL_MOUSEBUTTONDOWN:
|
||||||
postEvent(&AbstractScreen::the(), make<MouseEvent>(Event::MouseDown, sdlEvent.button.x, sdlEvent.button.y, toMouseButton(sdlEvent.button.button)));
|
postEvent(&WindowManager::the(), make<MouseEvent>(Event::MouseDown, sdlEvent.button.x, sdlEvent.button.y, toMouseButton(sdlEvent.button.button)));
|
||||||
return;
|
return;
|
||||||
case SDL_MOUSEBUTTONUP:
|
case SDL_MOUSEBUTTONUP:
|
||||||
postEvent(&AbstractScreen::the(), make<MouseEvent>(Event::MouseUp, sdlEvent.button.x, sdlEvent.button.y, toMouseButton(sdlEvent.button.button)));
|
postEvent(&WindowManager::the(), make<MouseEvent>(Event::MouseUp, sdlEvent.button.x, sdlEvent.button.y, toMouseButton(sdlEvent.button.button)));
|
||||||
return;
|
return;
|
||||||
case SDL_KEYDOWN:
|
case SDL_KEYDOWN:
|
||||||
postEvent(&AbstractScreen::the(), make<KeyEvent>(Event::KeyDown, toKey(sdlEvent.key.keysym)));
|
postEvent(&WindowManager::the(), make<KeyEvent>(Event::KeyDown, toKey(sdlEvent.key.keysym)));
|
||||||
return;
|
return;
|
||||||
case SDL_KEYUP:
|
case SDL_KEYUP:
|
||||||
postEvent(&AbstractScreen::the(), make<KeyEvent>(Event::KeyUp, toKey(sdlEvent.key.keysym)));
|
postEvent(&WindowManager::the(), make<KeyEvent>(Event::KeyUp, toKey(sdlEvent.key.keysym)));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,10 @@
|
||||||
#include "Widget.h"
|
#include "Widget.h"
|
||||||
#include "Window.h"
|
#include "Window.h"
|
||||||
#include "AbstractScreen.h"
|
#include "AbstractScreen.h"
|
||||||
|
#include "TerminalWidget.h"
|
||||||
|
#include "EventLoop.h"
|
||||||
|
|
||||||
|
extern TerminalWidget* g_tw;
|
||||||
|
|
||||||
WindowManager& WindowManager::the()
|
WindowManager& WindowManager::the()
|
||||||
{
|
{
|
||||||
|
@ -26,7 +30,7 @@ void WindowManager::paintWindowFrames()
|
||||||
|
|
||||||
void WindowManager::paintWindowFrame(Window& window)
|
void WindowManager::paintWindowFrame(Window& window)
|
||||||
{
|
{
|
||||||
Painter p(*AbstractScreen::the().rootWidget());
|
Painter p(*m_rootWidget);
|
||||||
|
|
||||||
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());
|
||||||
|
|
||||||
|
@ -103,3 +107,40 @@ void WindowManager::notifyRectChanged(Window& window, const Rect& oldRect, const
|
||||||
newRect.width(),
|
newRect.width(),
|
||||||
newRect.height());
|
newRect.height());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WindowManager::event(Event& event)
|
||||||
|
{
|
||||||
|
if (event.type() == Event::MouseMove
|
||||||
|
|| event.type() == Event::MouseDown
|
||||||
|
|| event.type() == Event::MouseUp) {
|
||||||
|
auto& me = static_cast<MouseEvent&>(event);
|
||||||
|
|
||||||
|
auto result = m_rootWidget->hitTest(me.x(), me.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);
|
||||||
|
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) {
|
||||||
|
// FIXME: Implement proper focus.
|
||||||
|
Widget* focusedWidget = g_tw;
|
||||||
|
return focusedWidget->event(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.type() == Event::Paint) {
|
||||||
|
return m_rootWidget->event(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object::event(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowManager::setRootWidget(Widget* widget)
|
||||||
|
{
|
||||||
|
// FIXME: Should we support switching root widgets?
|
||||||
|
ASSERT(!m_rootWidget);
|
||||||
|
ASSERT(widget);
|
||||||
|
|
||||||
|
m_rootWidget = widget;
|
||||||
|
EventLoop::main().postEvent(m_rootWidget, make<ShowEvent>());
|
||||||
|
}
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
class Window;
|
|
||||||
|
|
||||||
#include "Object.h"
|
#include "Object.h"
|
||||||
#include "Rect.h"
|
#include "Rect.h"
|
||||||
#include <AK/HashTable.h>
|
#include <AK/HashTable.h>
|
||||||
|
|
||||||
|
class Widget;
|
||||||
|
class Window;
|
||||||
|
|
||||||
class WindowManager : public Object {
|
class WindowManager : public Object {
|
||||||
public:
|
public:
|
||||||
static WindowManager& the();
|
static WindowManager& the();
|
||||||
|
@ -16,11 +17,16 @@ public:
|
||||||
void notifyTitleChanged(Window&);
|
void notifyTitleChanged(Window&);
|
||||||
void notifyRectChanged(Window&, const Rect& oldRect, const Rect& newRect);
|
void notifyRectChanged(Window&, const Rect& oldRect, const Rect& newRect);
|
||||||
|
|
||||||
|
Widget* rootWidget() { return m_rootWidget; }
|
||||||
|
void setRootWidget(Widget*);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
WindowManager();
|
WindowManager();
|
||||||
~WindowManager();
|
~WindowManager();
|
||||||
|
|
||||||
void paintWindowFrame(Window&);
|
virtual void event(Event&) override;
|
||||||
|
|
||||||
|
void paintWindowFrame(Window&);
|
||||||
HashTable<Window*> m_windows;
|
HashTable<Window*> m_windows;
|
||||||
|
Widget* m_rootWidget { nullptr };
|
||||||
};
|
};
|
||||||
|
|
|
@ -16,9 +16,7 @@ int main(int c, char** v)
|
||||||
EventLoopSDL loop;
|
EventLoopSDL loop;
|
||||||
|
|
||||||
RootWidget w;
|
RootWidget w;
|
||||||
fb.setRootWidget(&w);
|
WindowManager::the().setRootWidget(&w);
|
||||||
|
|
||||||
WindowManager::the();
|
|
||||||
|
|
||||||
auto* l1 = new Label(&w);
|
auto* l1 = new Label(&w);
|
||||||
l1->setRect(Rect(100, 100, 300, 20));
|
l1->setRect(Rect(100, 100, 300, 20));
|
||||||
|
|
Loading…
Reference in a new issue