From ceb373cf7167b9aed29bf34d847eecf8be778ab7 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 9 Jan 2019 05:38:13 +0100 Subject: [PATCH] More window manager hacking. FocusIn/FocusOut events. --- AK/AKString.h | 4 ++-- Widgets/Event.h | 2 ++ Widgets/RootWidget.cpp | 7 ------- Widgets/TextBox.cpp | 6 +++--- Widgets/Widget.cpp | 6 +++--- Widgets/Window.cpp | 14 +++++++------- Widgets/WindowManager.cpp | 1 + Widgets/test.cpp | 2 ++ 8 files changed, 20 insertions(+), 22 deletions(-) diff --git a/AK/AKString.h b/AK/AKString.h index 2f0a3e4b7a..54225a3176 100644 --- a/AK/AKString.h +++ b/AK/AKString.h @@ -65,9 +65,9 @@ public: bool is_null() const { return !m_impl; } bool is_empty() const { return length() == 0; } - unsigned length() const { return m_impl ? m_impl->length() : 0; } + size_t length() const { return m_impl ? m_impl->length() : 0; } const char* characters() const { return m_impl ? m_impl->characters() : nullptr; } - char operator[](unsigned i) const { ASSERT(m_impl); return (*m_impl)[i]; } + char operator[](size_t i) const { ASSERT(m_impl); return (*m_impl)[i]; } bool operator==(const String&) const; bool operator!=(const String& other) const { return !(*this == other); } diff --git a/Widgets/Event.h b/Widgets/Event.h index 4f1d2a139e..a6f5742c34 100644 --- a/Widgets/Event.h +++ b/Widgets/Event.h @@ -37,6 +37,8 @@ public: DeferredDestroy, WindowBecameInactive, WindowBecameActive, + FocusIn, + FocusOut, }; Event() { } diff --git a/Widgets/RootWidget.cpp b/Widgets/RootWidget.cpp index 00a5b604ab..0ffc1267e0 100644 --- a/Widgets/RootWidget.cpp +++ b/Widgets/RootWidget.cpp @@ -18,13 +18,6 @@ RootWidget::~RootWidget() void RootWidget::paintEvent(PaintEvent& event) { - Widget::paintEvent(event); - - printf("RootWidget::paintEvent: %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(0, 72, 96)); } diff --git a/Widgets/TextBox.cpp b/Widgets/TextBox.cpp index 264cdb1fc0..46f24702c5 100644 --- a/Widgets/TextBox.cpp +++ b/Widgets/TextBox.cpp @@ -35,13 +35,13 @@ void TextBox::paintEvent(PaintEvent&) Rect innerRect = rect(); innerRect.shrink(6, 6); - unsigned maxCharsToPaint = innerRect.width() / font().glyphWidth(); + size_t maxCharsToPaint = innerRect.width() / font().glyphWidth(); int firstVisibleChar = max((int)m_cursorPosition - (int)maxCharsToPaint, 0); - unsigned charsToPaint = min(m_text.length() - firstVisibleChar, maxCharsToPaint); + size_t charsToPaint = min(m_text.length() - firstVisibleChar, maxCharsToPaint); int y = innerRect.center().y() - font().glyphHeight() / 2; - for (unsigned i = 0; i < charsToPaint; ++i) { + for (size_t i = 0; i < charsToPaint; ++i) { char ch = m_text[firstVisibleChar + i]; if (ch == ' ') continue; diff --git a/Widgets/Widget.cpp b/Widgets/Widget.cpp index 0256179eff..4014072eb9 100644 --- a/Widgets/Widget.cpp +++ b/Widgets/Widget.cpp @@ -37,13 +37,13 @@ void Widget::event(Event& event) { switch (event.type()) { case Event::Paint: + m_hasPendingPaintEvent = false; if (auto* win = window()) { if (win->isBeingDragged()) return; if (!win->isVisible()) return; } - m_hasPendingPaintEvent = false; return paintEvent(static_cast(event)); case Event::Show: return showEvent(static_cast(event)); @@ -77,7 +77,7 @@ void Widget::paintEvent(PaintEvent& event) } for (auto* ch : children()) { auto* child = (Widget*)ch; - child->paintEvent(event); + child->event(event); } } @@ -117,7 +117,7 @@ void Widget::update() if (m_hasPendingPaintEvent) return; m_hasPendingPaintEvent = true; - EventLoop::main().postEvent(w, make(rect())); + EventLoop::main().postEvent(w, make(relativeRect())); } Widget::HitTestResult Widget::hitTest(int x, int y) diff --git a/Widgets/Window.cpp b/Widgets/Window.cpp index 7ecd53e8cc..7f2de932b8 100644 --- a/Widgets/Window.cpp +++ b/Widgets/Window.cpp @@ -74,7 +74,7 @@ void Window::event(Event& event) if (event.isPaintEvent()) { auto& pe = static_cast(event); - printf("Window[\"%s\"]: paintEvent %d,%d %dx%d\n", class_name(), + printf("Window[\"%s\"]: paintEvent %d,%d %dx%d\n", title().characters(), pe.rect().x(), pe.rect().y(), pe.rect().width(), @@ -123,15 +123,15 @@ void Window::setFocusedWidget(Widget* widget) { if (m_focusedWidget.ptr() == widget) return; - auto* previouslyFocusedWidget = m_focusedWidget.ptr(); - if (!widget) { + auto* previously_focused_widget = m_focusedWidget.ptr(); + if (!widget) m_focusedWidget = nullptr; - } else { + else { m_focusedWidget = widget->makeWeakPtr(); - m_focusedWidget->repaint(Rect()); + EventLoop::main().postEvent(m_focusedWidget.ptr(), make(Event::FocusIn)); } - if (previouslyFocusedWidget) - previouslyFocusedWidget->repaint(Rect()); + if (previously_focused_widget) + EventLoop::main().postEvent(previously_focused_widget, make(Event::FocusOut)); } void Window::close() diff --git a/Widgets/WindowManager.cpp b/Widgets/WindowManager.cpp index c7fad0eda8..b08300b844 100644 --- a/Widgets/WindowManager.cpp +++ b/Widgets/WindowManager.cpp @@ -139,6 +139,7 @@ void WindowManager::did_paint(Window& window) if (m_windows_in_order.tail() == &window) { ASSERT(window.backing()); framebuffer.blit(window.position(), *window.backing()); + framebuffer.flush(); printf("[WM] frontmost_only_compose_count: %u\n", ++m_frontmost_only_compose_count); return; } diff --git a/Widgets/test.cpp b/Widgets/test.cpp index ca72394d45..06deb0981f 100644 --- a/Widgets/test.cpp +++ b/Widgets/test.cpp @@ -100,10 +100,12 @@ int main(int argc, char** argv) win->setMainWidget(t); t->setFocus(true); +#if 0 auto* clockWin = new Window; clockWin->setTitle("Clock"); clockWin->setRect({ 500, 50, 100, 40 }); clockWin->setMainWidget(new ClockWidget); +#endif MsgBox(nullptr, "This is a message box!");