1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:07:35 +00:00

Add WindowActivated and WindowDeactivated events.

Use this to implement different looking Terminal cursors depending on
the window active state.
This commit is contained in:
Andreas Kling 2019-01-17 17:38:04 +01:00
parent 135ff48bb9
commit dad58db757
9 changed files with 66 additions and 42 deletions

View file

@ -18,6 +18,8 @@ static const char* WSEvent_names[] = {
"Timer",
"WM_Compose",
"WM_Invalidate",
"WindowActivated",
"WindowDeactivated",
};
class WSEvent {
@ -35,6 +37,8 @@ public:
Timer,
WM_Compose,
WM_Invalidate,
WindowActivated,
WindowDeactivated,
};
WSEvent() { }
@ -67,22 +71,6 @@ private:
Rect m_rect;
};
class ShowEvent final : public WSEvent {
public:
ShowEvent()
: WSEvent(WSEvent::Show)
{
}
};
class HideEvent final : public WSEvent {
public:
HideEvent()
: WSEvent(WSEvent::Hide)
{
}
};
enum class MouseButton : byte {
None = 0,
Left,
@ -142,10 +130,3 @@ private:
Point m_position;
MouseButton m_button { MouseButton::None };
};
class TimerEvent final : public WSEvent {
public:
TimerEvent() : WSEvent(WSEvent::Timer) { }
~TimerEvent() { }
};

View file

@ -77,6 +77,12 @@ void WSWindow::event(WSEvent& event)
case WSEvent::WM_Invalidate:
WSWindowManager::the().invalidate(*this);
return;
case WSEvent::WindowActivated:
gui_event.type = GUI_Event::Type::WindowActivated;
break;
case WSEvent::WindowDeactivated:
gui_event.type = GUI_Event::Type::WindowDeactivated;
break;
}
if (gui_event.type == GUI_Event::Type::Invalid)

View file

@ -172,7 +172,7 @@ void WSWindowManager::addWindow(WSWindow& window)
m_windows.set(&window);
m_windows_in_order.append(&window);
if (!activeWindow())
setActiveWindow(&window);
set_active_window(&window);
}
void WSWindowManager::move_to_front(WSWindow& window)
@ -192,7 +192,7 @@ void WSWindowManager::removeWindow(WSWindow& window)
m_windows.remove(&window);
m_windows_in_order.remove(&window);
if (!activeWindow() && !m_windows.is_empty())
setActiveWindow(*m_windows.begin());
set_active_window(*m_windows.begin());
}
void WSWindowManager::notifyTitleChanged(WSWindow& window)
@ -253,7 +253,7 @@ void WSWindowManager::processMouseEvent(MouseEvent& event)
if (titleBarRectForWindow(window->rect()).contains(event.position())) {
if (event.type() == WSEvent::MouseDown) {
move_to_front(*window);
setActiveWindow(window);
set_active_window(window);
}
handleTitleBarMouseEvent(*window, event);
return;
@ -262,7 +262,7 @@ void WSWindowManager::processMouseEvent(MouseEvent& event)
if (window->rect().contains(event.position())) {
if (event.type() == WSEvent::MouseDown) {
move_to_front(*window);
setActiveWindow(window);
set_active_window(window);
}
// FIXME: Re-use the existing event instead of crafting a new one?
auto localEvent = make<MouseEvent>(event.type(), event.x() - window->rect().x(), event.y() - window->rect().y(), event.button());
@ -342,8 +342,8 @@ void WSWindowManager::event(WSEvent& event)
if (event.isKeyEvent()) {
// FIXME: This is a good place to hook key events globally. :)
if (m_activeWindow)
return m_activeWindow->event(event);
if (m_active_window)
return m_active_window->event(event);
return;
}
@ -354,17 +354,21 @@ void WSWindowManager::event(WSEvent& event)
}
}
void WSWindowManager::setActiveWindow(WSWindow* window)
void WSWindowManager::set_active_window(WSWindow* window)
{
LOCKER(m_lock);
if (window == m_activeWindow.ptr())
if (window == m_active_window.ptr())
return;
if (auto* previously_active_window = m_activeWindow.ptr())
if (auto* previously_active_window = m_active_window.ptr()) {
WSEventLoop::the().post_event(previously_active_window, make<WSEvent>(WSEvent::WindowDeactivated));
invalidate(*previously_active_window);
m_activeWindow = window->makeWeakPtr();
if (m_activeWindow)
invalidate(*m_activeWindow);
}
m_active_window = window->makeWeakPtr();
if (m_active_window) {
WSEventLoop::the().post_event(m_active_window.ptr(), make<WSEvent>(WSEvent::WindowActivated));
invalidate(*m_active_window);
}
}
void WSWindowManager::invalidate()

View file

@ -25,7 +25,7 @@ public:
void notifyTitleChanged(WSWindow&);
void notifyRectChanged(WSWindow&, const Rect& oldRect, const Rect& newRect);
WSWindow* activeWindow() { return m_activeWindow.ptr(); }
WSWindow* activeWindow() { return m_active_window.ptr(); }
void move_to_front(WSWindow&);
@ -45,7 +45,7 @@ private:
void processMouseEvent(MouseEvent&);
void handleTitleBarMouseEvent(WSWindow&, MouseEvent&);
void setActiveWindow(WSWindow*);
void set_active_window(WSWindow*);
virtual void event(WSEvent&) override;
@ -64,7 +64,7 @@ private:
HashTable<WSWindow*> m_windows;
InlineLinkedList<WSWindow> m_windows_in_order;
WeakPtr<WSWindow> m_activeWindow;
WeakPtr<WSWindow> m_active_window;
WeakPtr<WSWindow> m_dragWindow;