Implement basic focus.

This commit is contained in:
Andreas Kling 2018-10-13 17:52:47 +02:00
parent 44a32039be
commit 1929cb6b71
Notes: sideshowbarker 2024-07-19 18:48:56 +09:00
6 changed files with 45 additions and 4 deletions

View file

@ -120,3 +120,16 @@ void Widget::setWindow(Window* window)
return;
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 repaint(const Rect&);
bool isFocused() const;
void setFocus(bool);
struct HitTestResult {
Widget* widget { nullptr };
int localX { 0 };

View file

@ -83,6 +83,12 @@ void Window::event(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);
}
@ -91,3 +97,14 @@ bool Window::isActive() const
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
#include <AK/String.h>
#include "Object.h"
#include "Rect.h"
#include <AK/String.h>
#include <AK/WeakPtr.h>
class Widget;
@ -39,10 +40,15 @@ public:
bool isActive() const;
Widget* focusedWidget() { return m_focusedWidget.ptr(); }
void setFocusedWidget(Widget*);
private:
String m_title;
Rect m_rect;
Widget* m_mainWidget { nullptr };
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));
if (event.isKeyEvent()) {
// FIXME: Implement proper focus.
Widget* focusedWidget = g_tw;
return focusedWidget->event(event);
// FIXME: This is a good place to hook key events globally. :)
if (m_activeWindow)
return m_activeWindow->event(event);
return Object::event(event);
}
if (event.isPaintEvent())

View file

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