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:
Andreas Kling 2018-10-12 19:39:48 +02:00
parent 74aa4d5345
commit 6f9e0e3876
Notes: sideshowbarker 2024-07-19 18:49:56 +09:00
9 changed files with 111 additions and 23 deletions

View file

@ -2,6 +2,7 @@
#include <AK/Types.h>
#include "Point.h"
#include "Rect.h"
static const char* eventNames[] = {
"Invalid",
@ -61,10 +62,16 @@ public:
class PaintEvent final : public Event {
public:
PaintEvent()
explicit PaintEvent(const Rect& rect = Rect())
: 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 {

View file

@ -67,6 +67,14 @@ public:
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 y() const { return location().y(); }
int width() const { return m_width; }

View file

@ -13,10 +13,15 @@ RootWidget::~RootWidget()
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);
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)

View file

@ -23,6 +23,11 @@ void Widget::setWindowRelativeRect(const Rect& rect)
update();
}
void Widget::repaint(const Rect& rect)
{
event(*make<PaintEvent>(rect));
}
void Widget::event(Event& event)
{
switch (event.type()) {
@ -94,7 +99,7 @@ void Widget::update()
if (m_hasPendingPaintEvent)
return;
m_hasPendingPaintEvent = true;
EventLoop::main().postEvent(this, make<PaintEvent>());
EventLoop::main().postEvent(this, make<PaintEvent>(rect()));
}
Widget::HitTestResult Widget::hitTest(int x, int y)

View file

@ -34,6 +34,7 @@ public:
Rect rect() const { return { 0, 0, width(), height() }; }
void update();
void repaint(const Rect&);
struct HitTestResult {
Widget* widget { nullptr };

View file

@ -41,6 +41,11 @@ void Window::setRect(const Rect& rect)
WindowManager::the().notifyRectChanged(*this, oldRect, m_rect);
}
void Window::repaint()
{
event(*make<PaintEvent>());
}
void Window::event(Event& event)
{
if (event.isMouseEvent()) {
@ -58,12 +63,23 @@ void Window::event(Event& event)
}
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()) {
// Ignore paint events during window drag.
return;
}
if (m_mainWidget)
return m_mainWidget->event(event);
if (m_mainWidget) {
if (pe.rect().isEmpty())
return m_mainWidget->event(*make<PaintEvent>(m_mainWidget->rect()));
else
return m_mainWidget->event(event);
}
return Object::event(event);
}

View file

@ -35,6 +35,8 @@ public:
bool isBeingDragged() const { return m_isBeingDragged; }
void setIsBeingDragged(bool b) { m_isBeingDragged = b; }
void repaint();
private:
String m_title;
Rect m_rect;

View file

@ -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()
{
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());
Rect topRect = titleBarRectForWindow(window);
auto titleBarRect = titleBarRectForWindow(window);
auto outerRect = outerRectForWindow(window);
auto borderRect = borderRectForWindow(window);
Rect bottomRect {
window.x() - windowFrameWidth,
@ -71,12 +90,6 @@ void WindowManager::paintWindowFrame(Window& window)
window.height()
};
Rect borderRect {
topRect.x() - 1,
topRect.y() - 1,
topRect.width() + 2,
windowFrameWidth + windowTitleBarHeight + window.rect().height() + 4
};
if (!m_lastDragRect.isEmpty()) {
p.xorRect(m_lastDragRect, Color(255, 0, 0));
@ -84,22 +97,20 @@ void WindowManager::paintWindowFrame(Window& window)
}
if (m_dragWindow == &window) {
borderRect.inflate(2, 2);
p.xorRect(borderRect, Color(255, 0, 0));
m_lastDragRect = borderRect;
p.xorRect(outerRect, Color(255, 0, 0));
m_lastDragRect = outerRect;
return;
}
p.drawRect(borderRect, Color(255, 255, 255));
borderRect.inflate(2, 2);
p.drawRect(borderRect, m_windowBorderColor);
p.drawRect(outerRect, m_windowBorderColor);
p.fillRect(topRect, m_windowBorderColor);
p.fillRect(titleBarRect, m_windowBorderColor);
p.fillRect(bottomRect, m_windowBorderColor);
p.fillRect(leftRect, 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)
@ -124,6 +135,7 @@ void WindowManager::handleTitleBarMouseEvent(Window& window, MouseEvent& event)
m_dragWindow = &window;
m_dragOrigin = event.position();
m_dragWindowOrigin = window.position();
m_dragStartRect = outerRectForWindow(window);
window.setIsBeingDragged(true);
return;
}
@ -136,14 +148,38 @@ void WindowManager::handleTitleBarMouseEvent(Window& window, MouseEvent& event)
#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)
{
if (event.type() == Event::MouseUp) {
if (m_dragWindow) {
printf("[WM] Finish dragging Window{%p}\n", m_dragWindow);
m_dragWindow->setIsBeingDragged(false);
m_dragEndRect = outerRectForWindow(*m_dragWindow);
m_dragWindow = nullptr;
EventLoop::main().postEvent(this, make<PaintEvent>());
repaintAfterMove(m_dragStartRect, m_dragEndRect);
return;
}
}
@ -179,6 +215,11 @@ void WindowManager::processMouseEvent(MouseEvent& event)
void WindowManager::handlePaintEvent(PaintEvent& event)
{
//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);
paintWindowFrames();

View file

@ -29,7 +29,8 @@ private:
void processMouseEvent(MouseEvent&);
void handleTitleBarMouseEvent(Window&, MouseEvent&);
void handlePaintEvent(PaintEvent&);
void repaintAfterMove(const Rect& oldRect, const Rect& newRect);
virtual void event(Event&) override;
Color m_windowBorderColor;
@ -43,4 +44,6 @@ private:
Point m_dragOrigin;
Point m_dragWindowOrigin;
Rect m_lastDragRect;
Rect m_dragStartRect;
Rect m_dragEndRect;
};