From 6f9e0e3876a60562fdc20496767ebf17cbb89f02 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 12 Oct 2018 19:39:48 +0200 Subject: [PATCH] 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. --- Widgets/Event.h | 9 ++++- Widgets/Rect.h | 8 +++++ Widgets/RootWidget.cpp | 11 ++++-- Widgets/Widget.cpp | 7 +++- Widgets/Widget.h | 1 + Widgets/Window.cpp | 20 +++++++++-- Widgets/Window.h | 2 ++ Widgets/WindowManager.cpp | 71 ++++++++++++++++++++++++++++++--------- Widgets/WindowManager.h | 5 ++- 9 files changed, 111 insertions(+), 23 deletions(-) diff --git a/Widgets/Event.h b/Widgets/Event.h index 6de27b79e03..917a6960bc1 100644 --- a/Widgets/Event.h +++ b/Widgets/Event.h @@ -2,6 +2,7 @@ #include #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 { diff --git a/Widgets/Rect.h b/Widgets/Rect.h index d61ae16f7f8..98b0d865ff9 100644 --- a/Widgets/Rect.h +++ b/Widgets/Rect.h @@ -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; } diff --git a/Widgets/RootWidget.cpp b/Widgets/RootWidget.cpp index ed9c6ce891c..79666c110bf 100644 --- a/Widgets/RootWidget.cpp +++ b/Widgets/RootWidget.cpp @@ -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) diff --git a/Widgets/Widget.cpp b/Widgets/Widget.cpp index f5a66e79f11..a742ba64616 100644 --- a/Widgets/Widget.cpp +++ b/Widgets/Widget.cpp @@ -23,6 +23,11 @@ void Widget::setWindowRelativeRect(const Rect& rect) update(); } +void Widget::repaint(const Rect& rect) +{ + event(*make(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()); + EventLoop::main().postEvent(this, make(rect())); } Widget::HitTestResult Widget::hitTest(int x, int y) diff --git a/Widgets/Widget.h b/Widgets/Widget.h index 2979c529e1f..804f3332ec7 100644 --- a/Widgets/Widget.h +++ b/Widgets/Widget.h @@ -34,6 +34,7 @@ public: Rect rect() const { return { 0, 0, width(), height() }; } void update(); + void repaint(const Rect&); struct HitTestResult { Widget* widget { nullptr }; diff --git a/Widgets/Window.cpp b/Widgets/Window.cpp index 2e0b95746ee..4848a89c9b9 100644 --- a/Widgets/Window.cpp +++ b/Widgets/Window.cpp @@ -41,6 +41,11 @@ void Window::setRect(const Rect& rect) WindowManager::the().notifyRectChanged(*this, oldRect, m_rect); } +void Window::repaint() +{ + event(*make()); +} + void Window::event(Event& event) { if (event.isMouseEvent()) { @@ -58,12 +63,23 @@ void Window::event(Event& event) } if (event.isPaintEvent()) { + auto& pe = static_cast(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(m_mainWidget->rect())); + else + return m_mainWidget->event(event); + } return Object::event(event); } diff --git a/Widgets/Window.h b/Widgets/Window.h index f37f75c7571..1af1f96d7b6 100644 --- a/Widgets/Window.h +++ b/Widgets/Window.h @@ -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; diff --git a/Widgets/WindowManager.cpp b/Widgets/WindowManager.cpp index 6f1f235bb0d..15104a933ed 100644 --- a/Widgets/WindowManager.cpp +++ b/Widgets/WindowManager.cpp @@ -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()); + + 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(); diff --git a/Widgets/WindowManager.h b/Widgets/WindowManager.h index a81135f0fa4..08995bc00de 100644 --- a/Widgets/WindowManager.h +++ b/Widgets/WindowManager.h @@ -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; };