Skip to content

Commit 02b447b

Browse files
committed
Automatically hide dropdown when blurred
1 parent df06920 commit 02b447b

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/gui/controllers/BlurRemove.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2021 LibreSprite Authors (cf. AUTHORS.md)
2+
// This file is released under the terms of the MIT license.
3+
// Read LICENSE.txt for more information.
4+
5+
#include <gui/Controller.hpp>
6+
#include <gui/Events.hpp>
7+
#include <gui/Node.hpp>
8+
9+
class BlurRemove : public ui::Controller {
10+
public:
11+
void attach() override {
12+
node()->addEventListener<ui::Blur, ui::BlurChild>(this);
13+
}
14+
15+
void eventHandler(const ui::Blur&) {
16+
if (auto focus = node()->getFocus(); !focus || !focus->isDescendantOf(node()->shared_from_this()))
17+
node()->remove();
18+
}
19+
20+
void eventHandler(const ui::BlurChild&) {
21+
if (auto focus = node()->getFocus(); !focus || !focus->isDescendantOf(node()->shared_from_this()))
22+
node()->remove();
23+
}
24+
};
25+
26+
static ui::Controller::Shared<BlurRemove> blurremove{"blur-remove"};

src/gui/controllers/DropDown.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ class DropDown : public ui::Controller {
2424
if (container)
2525
return container;
2626
container = ui::Node::fromXML("dropdowncontainer");
27+
container->load({
28+
{"steal-focus", true},
29+
{"controller", "blur-remove"}
30+
});
2731

2832
for (auto& opt : getOptions()) {
2933
auto entry = ui::Node::fromXML("menubutton");
3034
entry->load({
3135
{"label", opt},
3236
{"click", FunctionRef<void()>([=]{
33-
logI("Click ", opt);
3437
if (*value != opt) {
3538
container->remove();
3639
node()->load({{"value", opt}});

0 commit comments

Comments
 (0)