1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:27:43 +00:00

Implement basic focus.

This commit is contained in:
Andreas Kling 2018-10-13 17:52:47 +02:00
parent 44a32039be
commit 1929cb6b71
6 changed files with 45 additions and 4 deletions

View file

@ -120,3 +120,16 @@ void Widget::setWindow(Window* window)
return; return;
m_window = window; m_window = window;
} }
bool Widget::isFocused() const
{
return m_window && m_window->focusedWidget() == this;
}
void Widget::setFocus(bool focus)
{
if (focus == isFocused())
return;
if (m_window)
m_window->setFocusedWidget(this);
}

View file

@ -36,6 +36,9 @@ public:
void update(); void update();
void repaint(const Rect&); void repaint(const Rect&);
bool isFocused() const;
void setFocus(bool);
struct HitTestResult { struct HitTestResult {
Widget* widget { nullptr }; Widget* widget { nullptr };
int localX { 0 }; int localX { 0 };

View file

@ -83,6 +83,12 @@ void Window::event(Event& event)
return Object::event(event); return Object::event(event);
} }
if (event.isKeyEvent()) {
if (m_focusedWidget)
return m_focusedWidget->event(event);
return Object::event(event);
}
return Object::event(event); return Object::event(event);
} }
@ -91,3 +97,14 @@ bool Window::isActive() const
return WindowManager::the().activeWindow() == this; return WindowManager::the().activeWindow() == this;
} }
void Window::setFocusedWidget(Widget* widget)
{
if (m_focusedWidget.ptr() == widget)
return;
if (!widget) {
m_focusedWidget = nullptr;
return;
}
m_focusedWidget = widget->makeWeakPtr();
}

View file

@ -1,8 +1,9 @@
#pragma once #pragma once
#include <AK/String.h>
#include "Object.h" #include "Object.h"
#include "Rect.h" #include "Rect.h"
#include <AK/String.h>
#include <AK/WeakPtr.h>
class Widget; class Widget;
@ -39,10 +40,15 @@ public:
bool isActive() const; bool isActive() const;
Widget* focusedWidget() { return m_focusedWidget.ptr(); }
void setFocusedWidget(Widget*);
private: private:
String m_title; String m_title;
Rect m_rect; Rect m_rect;
Widget* m_mainWidget { nullptr }; Widget* m_mainWidget { nullptr };
bool m_isBeingDragged { false }; bool m_isBeingDragged { false };
WeakPtr<Widget> m_focusedWidget;
}; };

View file

@ -254,9 +254,10 @@ void WindowManager::event(Event& event)
return processMouseEvent(static_cast<MouseEvent&>(event)); return processMouseEvent(static_cast<MouseEvent&>(event));
if (event.isKeyEvent()) { if (event.isKeyEvent()) {
// FIXME: Implement proper focus. // FIXME: This is a good place to hook key events globally. :)
Widget* focusedWidget = g_tw; if (m_activeWindow)
return focusedWidget->event(event); return m_activeWindow->event(event);
return Object::event(event);
} }
if (event.isPaintEvent()) if (event.isPaintEvent())

View file

@ -80,6 +80,7 @@ int main(int argc, char** argv)
auto* t = new TerminalWidget(nullptr); auto* t = new TerminalWidget(nullptr);
win->setMainWidget(t); win->setMainWidget(t);
t->setFocus(true);
auto* clockWin = new Window; auto* clockWin = new Window;
clockWin->setTitle("Clock"); clockWin->setTitle("Clock");