mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-23 01:32:14 -05:00
Implement basic focus.
This commit is contained in:
parent
44a32039be
commit
1929cb6b71
Notes:
sideshowbarker
2024-07-19 18:48:56 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/1929cb6b714
6 changed files with 45 additions and 4 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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 };
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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");
|
||||
|
|
Loading…
Reference in a new issue