mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 13:47:34 +00:00
After moving a window, try to repaint a bit less.
Only repaint windows that intersect either the old or the new rect. Also only repaint those rects in the root widget.
This commit is contained in:
parent
74aa4d5345
commit
6f9e0e3876
9 changed files with 111 additions and 23 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
#include "Point.h"
|
#include "Point.h"
|
||||||
|
#include "Rect.h"
|
||||||
|
|
||||||
static const char* eventNames[] = {
|
static const char* eventNames[] = {
|
||||||
"Invalid",
|
"Invalid",
|
||||||
|
@ -61,10 +62,16 @@ public:
|
||||||
|
|
||||||
class PaintEvent final : public Event {
|
class PaintEvent final : public Event {
|
||||||
public:
|
public:
|
||||||
PaintEvent()
|
explicit PaintEvent(const Rect& rect = Rect())
|
||||||
: Event(Event::Paint)
|
: Event(Event::Paint)
|
||||||
|
, m_rect(rect)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const Rect& rect() const { return m_rect; }
|
||||||
|
private:
|
||||||
|
friend class WindowManager;
|
||||||
|
Rect m_rect;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ShowEvent final : public Event {
|
class ShowEvent final : public Event {
|
||||||
|
|
|
@ -67,6 +67,14 @@ public:
|
||||||
setY(top);
|
setY(top);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool intersects(const Rect& other) const
|
||||||
|
{
|
||||||
|
return left() < other.right()
|
||||||
|
&& other.left() < right()
|
||||||
|
&& top() < other.bottom()
|
||||||
|
&& other.top() < bottom();
|
||||||
|
}
|
||||||
|
|
||||||
int x() const { return location().x(); }
|
int x() const { return location().x(); }
|
||||||
int y() const { return location().y(); }
|
int y() const { return location().y(); }
|
||||||
int width() const { return m_width; }
|
int width() const { return m_width; }
|
||||||
|
|
|
@ -13,10 +13,15 @@ RootWidget::~RootWidget()
|
||||||
|
|
||||||
void RootWidget::onPaint(PaintEvent& event)
|
void RootWidget::onPaint(PaintEvent& event)
|
||||||
{
|
{
|
||||||
//printf("RootWidget::onPaint\n");
|
|
||||||
Painter painter(*this);
|
|
||||||
painter.fillRect(Rect(0, 0, 800, 600), Color(0x40, 0x40, 0x40));
|
|
||||||
Widget::onPaint(event);
|
Widget::onPaint(event);
|
||||||
|
|
||||||
|
printf("RootWidget::onPaint: %d,%d %dx%d\n",
|
||||||
|
event.rect().x(),
|
||||||
|
event.rect().y(),
|
||||||
|
event.rect().width(),
|
||||||
|
event.rect().height());
|
||||||
|
Painter painter(*this);
|
||||||
|
painter.fillRect(event.rect(), Color(0x40, 0x40, 0x40));
|
||||||
}
|
}
|
||||||
|
|
||||||
void RootWidget::onMouseMove(MouseEvent& event)
|
void RootWidget::onMouseMove(MouseEvent& event)
|
||||||
|
|
|
@ -23,6 +23,11 @@ void Widget::setWindowRelativeRect(const Rect& rect)
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Widget::repaint(const Rect& rect)
|
||||||
|
{
|
||||||
|
event(*make<PaintEvent>(rect));
|
||||||
|
}
|
||||||
|
|
||||||
void Widget::event(Event& event)
|
void Widget::event(Event& event)
|
||||||
{
|
{
|
||||||
switch (event.type()) {
|
switch (event.type()) {
|
||||||
|
@ -94,7 +99,7 @@ void Widget::update()
|
||||||
if (m_hasPendingPaintEvent)
|
if (m_hasPendingPaintEvent)
|
||||||
return;
|
return;
|
||||||
m_hasPendingPaintEvent = true;
|
m_hasPendingPaintEvent = true;
|
||||||
EventLoop::main().postEvent(this, make<PaintEvent>());
|
EventLoop::main().postEvent(this, make<PaintEvent>(rect()));
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget::HitTestResult Widget::hitTest(int x, int y)
|
Widget::HitTestResult Widget::hitTest(int x, int y)
|
||||||
|
|
|
@ -34,6 +34,7 @@ public:
|
||||||
Rect rect() const { return { 0, 0, width(), height() }; }
|
Rect rect() const { return { 0, 0, width(), height() }; }
|
||||||
|
|
||||||
void update();
|
void update();
|
||||||
|
void repaint(const Rect&);
|
||||||
|
|
||||||
struct HitTestResult {
|
struct HitTestResult {
|
||||||
Widget* widget { nullptr };
|
Widget* widget { nullptr };
|
||||||
|
|
|
@ -41,6 +41,11 @@ void Window::setRect(const Rect& rect)
|
||||||
WindowManager::the().notifyRectChanged(*this, oldRect, m_rect);
|
WindowManager::the().notifyRectChanged(*this, oldRect, m_rect);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Window::repaint()
|
||||||
|
{
|
||||||
|
event(*make<PaintEvent>());
|
||||||
|
}
|
||||||
|
|
||||||
void Window::event(Event& event)
|
void Window::event(Event& event)
|
||||||
{
|
{
|
||||||
if (event.isMouseEvent()) {
|
if (event.isMouseEvent()) {
|
||||||
|
@ -58,12 +63,23 @@ void Window::event(Event& event)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.isPaintEvent()) {
|
if (event.isPaintEvent()) {
|
||||||
|
auto& pe = static_cast<PaintEvent&>(event);
|
||||||
|
printf("Window[\"%s\"]: paintEvent %d,%d %dx%d\n", className(),
|
||||||
|
pe.rect().x(),
|
||||||
|
pe.rect().y(),
|
||||||
|
pe.rect().width(),
|
||||||
|
pe.rect().height());
|
||||||
|
|
||||||
if (isBeingDragged()) {
|
if (isBeingDragged()) {
|
||||||
// Ignore paint events during window drag.
|
// Ignore paint events during window drag.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (m_mainWidget)
|
if (m_mainWidget) {
|
||||||
return m_mainWidget->event(event);
|
if (pe.rect().isEmpty())
|
||||||
|
return m_mainWidget->event(*make<PaintEvent>(m_mainWidget->rect()));
|
||||||
|
else
|
||||||
|
return m_mainWidget->event(event);
|
||||||
|
}
|
||||||
return Object::event(event);
|
return Object::event(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,8 @@ public:
|
||||||
bool isBeingDragged() const { return m_isBeingDragged; }
|
bool isBeingDragged() const { return m_isBeingDragged; }
|
||||||
void setIsBeingDragged(bool b) { m_isBeingDragged = b; }
|
void setIsBeingDragged(bool b) { m_isBeingDragged = b; }
|
||||||
|
|
||||||
|
void repaint();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
String m_title;
|
String m_title;
|
||||||
Rect m_rect;
|
Rect m_rect;
|
||||||
|
|
|
@ -21,6 +21,23 @@ static inline Rect titleBarRectForWindow(const Window& window)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline Rect borderRectForWindow(const Window& window)
|
||||||
|
{
|
||||||
|
auto titleBarRect = titleBarRectForWindow(window);
|
||||||
|
return { titleBarRect.x() - 1,
|
||||||
|
titleBarRect.y() - 1,
|
||||||
|
titleBarRect.width() + 2,
|
||||||
|
windowFrameWidth + windowTitleBarHeight + window.rect().height() + 4
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline Rect outerRectForWindow(const Window& window)
|
||||||
|
{
|
||||||
|
auto rect = borderRectForWindow(window);
|
||||||
|
rect.inflate(2, 2);
|
||||||
|
return rect;
|
||||||
|
}
|
||||||
|
|
||||||
WindowManager& WindowManager::the()
|
WindowManager& WindowManager::the()
|
||||||
{
|
{
|
||||||
static WindowManager* s_the = new WindowManager;
|
static WindowManager* s_the = new WindowManager;
|
||||||
|
@ -49,7 +66,9 @@ 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());
|
||||||
|
|
||||||
Rect topRect = titleBarRectForWindow(window);
|
auto titleBarRect = titleBarRectForWindow(window);
|
||||||
|
auto outerRect = outerRectForWindow(window);
|
||||||
|
auto borderRect = borderRectForWindow(window);
|
||||||
|
|
||||||
Rect bottomRect {
|
Rect bottomRect {
|
||||||
window.x() - windowFrameWidth,
|
window.x() - windowFrameWidth,
|
||||||
|
@ -71,12 +90,6 @@ void WindowManager::paintWindowFrame(Window& window)
|
||||||
window.height()
|
window.height()
|
||||||
};
|
};
|
||||||
|
|
||||||
Rect borderRect {
|
|
||||||
topRect.x() - 1,
|
|
||||||
topRect.y() - 1,
|
|
||||||
topRect.width() + 2,
|
|
||||||
windowFrameWidth + windowTitleBarHeight + window.rect().height() + 4
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!m_lastDragRect.isEmpty()) {
|
if (!m_lastDragRect.isEmpty()) {
|
||||||
p.xorRect(m_lastDragRect, Color(255, 0, 0));
|
p.xorRect(m_lastDragRect, Color(255, 0, 0));
|
||||||
|
@ -84,22 +97,20 @@ void WindowManager::paintWindowFrame(Window& window)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_dragWindow == &window) {
|
if (m_dragWindow == &window) {
|
||||||
borderRect.inflate(2, 2);
|
p.xorRect(outerRect, Color(255, 0, 0));
|
||||||
p.xorRect(borderRect, Color(255, 0, 0));
|
m_lastDragRect = outerRect;
|
||||||
m_lastDragRect = borderRect;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
p.drawRect(borderRect, Color(255, 255, 255));
|
p.drawRect(borderRect, Color(255, 255, 255));
|
||||||
borderRect.inflate(2, 2);
|
p.drawRect(outerRect, m_windowBorderColor);
|
||||||
p.drawRect(borderRect, m_windowBorderColor);
|
|
||||||
|
|
||||||
p.fillRect(topRect, m_windowBorderColor);
|
p.fillRect(titleBarRect, m_windowBorderColor);
|
||||||
p.fillRect(bottomRect, m_windowBorderColor);
|
p.fillRect(bottomRect, m_windowBorderColor);
|
||||||
p.fillRect(leftRect, m_windowBorderColor);
|
p.fillRect(leftRect, m_windowBorderColor);
|
||||||
p.fillRect(rightRect, m_windowBorderColor);
|
p.fillRect(rightRect, m_windowBorderColor);
|
||||||
|
|
||||||
p.drawText(topRect, window.title(), Painter::TextAlignment::Center, m_windowTitleColor);
|
p.drawText(titleBarRect, window.title(), Painter::TextAlignment::Center, m_windowTitleColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowManager::addWindow(Window& window)
|
void WindowManager::addWindow(Window& window)
|
||||||
|
@ -124,6 +135,7 @@ void WindowManager::handleTitleBarMouseEvent(Window& window, MouseEvent& event)
|
||||||
m_dragWindow = &window;
|
m_dragWindow = &window;
|
||||||
m_dragOrigin = event.position();
|
m_dragOrigin = event.position();
|
||||||
m_dragWindowOrigin = window.position();
|
m_dragWindowOrigin = window.position();
|
||||||
|
m_dragStartRect = outerRectForWindow(window);
|
||||||
window.setIsBeingDragged(true);
|
window.setIsBeingDragged(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -136,14 +148,38 @@ void WindowManager::handleTitleBarMouseEvent(Window& window, MouseEvent& event)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WindowManager::repaintAfterMove(const Rect& oldRect, const Rect& newRect)
|
||||||
|
{
|
||||||
|
printf("[WM] repaint: [%d,%d %dx%d] -> [%d,%d %dx%d]\n",
|
||||||
|
oldRect.x(),
|
||||||
|
oldRect.y(),
|
||||||
|
oldRect.width(),
|
||||||
|
oldRect.height(),
|
||||||
|
newRect.x(),
|
||||||
|
newRect.y(),
|
||||||
|
newRect.width(),
|
||||||
|
newRect.height());
|
||||||
|
|
||||||
|
m_rootWidget->repaint(oldRect);
|
||||||
|
m_rootWidget->repaint(newRect);
|
||||||
|
for (auto* window : m_windows) {
|
||||||
|
if (outerRectForWindow(*window).intersects(oldRect) || outerRectForWindow(*window).intersects(newRect)) {
|
||||||
|
paintWindowFrame(*window);
|
||||||
|
window->repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void WindowManager::processMouseEvent(MouseEvent& event)
|
void WindowManager::processMouseEvent(MouseEvent& event)
|
||||||
{
|
{
|
||||||
if (event.type() == Event::MouseUp) {
|
if (event.type() == Event::MouseUp) {
|
||||||
if (m_dragWindow) {
|
if (m_dragWindow) {
|
||||||
printf("[WM] Finish dragging Window{%p}\n", m_dragWindow);
|
printf("[WM] Finish dragging Window{%p}\n", m_dragWindow);
|
||||||
m_dragWindow->setIsBeingDragged(false);
|
m_dragWindow->setIsBeingDragged(false);
|
||||||
|
m_dragEndRect = outerRectForWindow(*m_dragWindow);
|
||||||
m_dragWindow = nullptr;
|
m_dragWindow = nullptr;
|
||||||
EventLoop::main().postEvent(this, make<PaintEvent>());
|
|
||||||
|
repaintAfterMove(m_dragStartRect, m_dragEndRect);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -179,6 +215,11 @@ void WindowManager::processMouseEvent(MouseEvent& event)
|
||||||
void WindowManager::handlePaintEvent(PaintEvent& event)
|
void WindowManager::handlePaintEvent(PaintEvent& event)
|
||||||
{
|
{
|
||||||
//printf("[WM] paint event\n");
|
//printf("[WM] paint event\n");
|
||||||
|
if (event.rect().isEmpty()) {
|
||||||
|
event.m_rect.setWidth(AbstractScreen::the().width());
|
||||||
|
event.m_rect.setHeight(AbstractScreen::the().height());
|
||||||
|
}
|
||||||
|
|
||||||
m_rootWidget->event(event);
|
m_rootWidget->event(event);
|
||||||
paintWindowFrames();
|
paintWindowFrames();
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,8 @@ private:
|
||||||
void processMouseEvent(MouseEvent&);
|
void processMouseEvent(MouseEvent&);
|
||||||
void handleTitleBarMouseEvent(Window&, MouseEvent&);
|
void handleTitleBarMouseEvent(Window&, MouseEvent&);
|
||||||
void handlePaintEvent(PaintEvent&);
|
void handlePaintEvent(PaintEvent&);
|
||||||
|
void repaintAfterMove(const Rect& oldRect, const Rect& newRect);
|
||||||
|
|
||||||
virtual void event(Event&) override;
|
virtual void event(Event&) override;
|
||||||
|
|
||||||
Color m_windowBorderColor;
|
Color m_windowBorderColor;
|
||||||
|
@ -43,4 +44,6 @@ private:
|
||||||
Point m_dragOrigin;
|
Point m_dragOrigin;
|
||||||
Point m_dragWindowOrigin;
|
Point m_dragWindowOrigin;
|
||||||
Rect m_lastDragRect;
|
Rect m_lastDragRect;
|
||||||
|
Rect m_dragStartRect;
|
||||||
|
Rect m_dragEndRect;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue