From 44a32039be5d6b5ff1f6b5eed75300723edb06a9 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 13 Oct 2018 17:42:24 +0200 Subject: Let the WindowManager track the active window. We draw the active window in a different color, obviously. :^) --- Widgets/Window.cpp | 6 ++++++ Widgets/Window.h | 2 ++ Widgets/WindowManager.cpp | 55 ++++++++++++++++++++++++++++++++++++++--------- Widgets/WindowManager.h | 12 +++++++++-- 4 files changed, 63 insertions(+), 12 deletions(-) (limited to 'Widgets') diff --git a/Widgets/Window.cpp b/Widgets/Window.cpp index eb701f2970..c3886f993b 100644 --- a/Widgets/Window.cpp +++ b/Widgets/Window.cpp @@ -85,3 +85,9 @@ void Window::event(Event& event) return Object::event(event); } + +bool Window::isActive() const +{ + return WindowManager::the().activeWindow() == this; +} + diff --git a/Widgets/Window.h b/Widgets/Window.h index 1af1f96d7b..bfb49b36fb 100644 --- a/Widgets/Window.h +++ b/Widgets/Window.h @@ -37,6 +37,8 @@ public: void repaint(); + bool isActive() const; + private: String m_title; Rect m_rect; diff --git a/Widgets/WindowManager.cpp b/Widgets/WindowManager.cpp index 1cb8557e77..a84cc81094 100644 --- a/Widgets/WindowManager.cpp +++ b/Widgets/WindowManager.cpp @@ -46,8 +46,11 @@ WindowManager& WindowManager::the() WindowManager::WindowManager() { - m_windowBorderColor = Color(0, 64, 192); - m_windowTitleColor = Color::White; + m_activeWindowBorderColor = Color(0, 64, 192); + m_activeWindowTitleColor = Color::White; + + m_inactiveWindowBorderColor = Color(64, 64, 64); + m_inactiveWindowTitleColor = Color::White; } WindowManager::~WindowManager() @@ -102,26 +105,33 @@ void WindowManager::paintWindowFrame(Window& window) return; } - p.drawRect(borderRect, Color::White); - p.drawRect(outerRect, m_windowBorderColor); + auto titleColor = &window == activeWindow() ? m_activeWindowTitleColor : m_inactiveWindowTitleColor; + auto borderColor = &window == activeWindow() ? m_activeWindowBorderColor : m_inactiveWindowBorderColor; + + p.drawRect(borderRect, Color::MidGray); + p.drawRect(outerRect, borderColor); - p.fillRect(titleBarRect, m_windowBorderColor); - p.fillRect(bottomRect, m_windowBorderColor); - p.fillRect(leftRect, m_windowBorderColor); - p.fillRect(rightRect, m_windowBorderColor); + p.fillRect(titleBarRect, borderColor); + p.fillRect(bottomRect, borderColor); + p.fillRect(leftRect, borderColor); + p.fillRect(rightRect, borderColor); - p.drawText(titleBarRect, window.title(), Painter::TextAlignment::Center, m_windowTitleColor); + p.drawText(titleBarRect, window.title(), Painter::TextAlignment::Center, titleColor); } void WindowManager::addWindow(Window& window) { m_windows.set(&window); + if (!activeWindow()) + setActiveWindow(&window); } void WindowManager::removeWindow(Window& window) { ASSERT(m_windows.contains(&window)); m_windows.remove(&window); + if (!activeWindow() && !m_windows.isEmpty()) + setActiveWindow(*m_windows.begin()); } void WindowManager::notifyTitleChanged(Window& window) @@ -149,7 +159,7 @@ void WindowManager::handleTitleBarMouseEvent(Window& window, MouseEvent& event) byte r = (((double)rand()) / (double)RAND_MAX) * 255.0; byte g = (((double)rand()) / (double)RAND_MAX) * 255.0; byte b = (((double)rand()) / (double)RAND_MAX) * 255.0; - m_windowBorderColor = Color(r, g, b); + m_activeWindowBorderColor = Color(r, g, b); paintWindowFrame(window); #endif } @@ -204,11 +214,15 @@ void WindowManager::processMouseEvent(MouseEvent& event) // FIXME: Respect z-order of windows... for (auto* window : m_windows) { if (titleBarRectForWindow(*window).contains(event.position())) { + if (event.type() == Event::MouseDown) + setActiveWindow(window); handleTitleBarMouseEvent(*window, event); return; } if (window->rect().contains(event.position())) { + if (event.type() == Event::MouseDown) + setActiveWindow(window); // FIXME: Re-use the existing event instead of crafting a new one? auto localEvent = make(event.type(), event.x() - window->rect().x(), event.y() - window->rect().y(), event.button()); window->event(*localEvent); @@ -260,3 +274,24 @@ void WindowManager::setRootWidget(Widget* widget) m_rootWidget = widget; EventLoop::main().postEvent(m_rootWidget, make()); } + +void WindowManager::setActiveWindow(Window* window) +{ + if (window == m_activeWindow.ptr()) + return; + + auto* previouslyActiveWindow = m_activeWindow.ptr(); + + m_activeWindow = window->makeWeakPtr(); + + if (previouslyActiveWindow) { + paintWindowFrame(*previouslyActiveWindow); + previouslyActiveWindow->repaint(); + } + + if (m_activeWindow) { + paintWindowFrame(*m_activeWindow); + m_activeWindow->repaint(); + } +} + diff --git a/Widgets/WindowManager.h b/Widgets/WindowManager.h index f73a3325b0..f53c29740e 100644 --- a/Widgets/WindowManager.h +++ b/Widgets/WindowManager.h @@ -24,6 +24,9 @@ public: Widget* rootWidget() { return m_rootWidget; } void setRootWidget(Widget*); + Window* activeWindow() { return m_activeWindow.ptr(); } + void setActiveWindow(Window*); + private: WindowManager(); ~WindowManager(); @@ -35,13 +38,18 @@ private: virtual void event(Event&) override; - Color m_windowBorderColor; - Color m_windowTitleColor; + Color m_activeWindowBorderColor; + Color m_activeWindowTitleColor; + + Color m_inactiveWindowBorderColor; + Color m_inactiveWindowTitleColor; void paintWindowFrame(Window&); HashTable m_windows; Widget* m_rootWidget { nullptr }; + WeakPtr m_activeWindow; + WeakPtr m_dragWindow; Point m_dragOrigin; -- cgit v1.2.3