Skip to content

Commit df06920

Browse files
committed
Facilitate querying for the currently focused UI node
1 parent afeaaa4 commit df06920

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

src/gui/Node.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ namespace ui {
5656
}
5757

5858
virtual void eventHandler(const Focus& event) {
59-
parent->processEvent(FocusChild{this});
59+
if (parent)
60+
parent->processEvent(FocusChild{this});
6061
}
6162

6263
virtual void eventHandler(const Blur& event) {
63-
parent->processEvent(BlurChild{this});
64+
if (parent)
65+
parent->processEvent(BlurChild{this});
6466
}
6567

6668
public:
@@ -141,6 +143,10 @@ namespace ui {
141143
}
142144
}
143145

146+
virtual std::shared_ptr<ui::Node> getFocus() {
147+
return parent ? parent->getFocus() : nullptr;
148+
}
149+
144150
virtual bool hasFocus(std::shared_ptr<ui::Node> child = nullptr) {
145151
if (parent) {
146152
return parent->hasFocus(child ?: shared_from_this());

src/gui/Window.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace ui {
2222
Node::doResize();
2323
}
2424

25-
std::shared_ptr<ui::Node> Window::getFocused() {
25+
std::shared_ptr<ui::Node> Window::getFocus() {
2626
if (focusTarget.expired()) {
2727
focus(findChildByPredicate([](ui::Node* node){
2828
return *node->stealFocus;
@@ -117,10 +117,9 @@ namespace ui {
117117
if (!guiEvent.target)
118118
return;
119119

120-
focus(guiEvent.target->shared_from_this());
121-
122-
if (guiEvent.target)
123-
guiEvent.target->processEvent(guiEvent);
120+
auto target = guiEvent.target->shared_from_this();
121+
focus(target);
122+
target->processEvent(guiEvent);
124123
}
125124
}
126125

@@ -147,7 +146,7 @@ namespace ui {
147146

148147
auto shared = guiEvent.target->shared_from_this();
149148

150-
if (auto focus = getFocused()) {
149+
if (auto focus = getFocus()) {
151150
if (focus.get() == guiEvent.target) {
152151
guiEvent.target->processEvent(ui::Click{focus.get(), eventX, eventY, event.buttons});
153152
}
@@ -162,13 +161,15 @@ namespace ui {
162161
hoverWindow = this;
163162
ui::MouseWheel guiEvent{nullptr, mouseX, mouseY, mouseButtons, event.wheelX, event.wheelY};
164163
guiEvent.target = findEventTarget(guiEvent);
165-
if (guiEvent.target)
166-
guiEvent.target->processEvent(guiEvent);
164+
if (guiEvent.target) {
165+
auto shared = guiEvent.target->shared_from_this();
166+
shared->processEvent(guiEvent);
167+
}
167168
}
168169
}
169170

170171
void Window::on(msg::TextEvent& event) {
171-
if (auto focus = getFocused()) {
172+
if (auto focus = getFocus()) {
172173
focus->processEvent(ui::TextEvent{
173174
focus.get(),
174175
event.text,
@@ -178,7 +179,7 @@ namespace ui {
178179
}
179180

180181
void Window::on(msg::KeyDown& event) {
181-
if (auto focus = getFocused()) {
182+
if (auto focus = getFocus()) {
182183
focus->processEvent(ui::KeyDown{
183184
focus.get(),
184185
event.scancode,
@@ -190,7 +191,7 @@ namespace ui {
190191
}
191192

192193
void Window::on(msg::KeyUp& event) {
193-
if (auto focus = getFocused()) {
194+
if (auto focus = getFocus()) {
194195
focus->processEvent(ui::KeyUp{
195196
focus.get(),
196197
event.scancode,

src/gui/Window.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class Window : public Node {
2929
msg::PollActiveWindow> pub{this};
3030
std::weak_ptr<ui::Node> mouseOverTarget;
3131
std::weak_ptr<ui::Node> focusTarget;
32-
std::shared_ptr<ui::Node> getFocused();
3332

3433
static inline ui::Window* hoverWindow = nullptr;
3534
static inline std::weak_ptr<ui::Node> dragTarget;
@@ -53,6 +52,7 @@ class Window : public Node {
5352
void postInject() override;
5453
void resize() override;
5554
void doResize() override;
55+
std::shared_ptr<ui::Node> getFocus() override;
5656
bool hasFocus(std::shared_ptr<ui::Node> child) override;
5757
void focus(std::shared_ptr<ui::Node> child) override;
5858
void blur(std::shared_ptr<ui::Node> child) override;

0 commit comments

Comments
 (0)