summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-03-28 11:16:33 +0200
committerAndreas Kling <kling@serenityos.org>2021-03-28 11:19:18 +0200
commit54c7110d9d02c0d48157e5075f5d08ece2b0b7ee (patch)
tree6be212db174f2588dd00d4a80983614a42cfd3fb /Userland/Libraries
parentc91bb729644ec994099fc375e485dd8524aad7c2 (diff)
downloadserenity-54c7110d9d02c0d48157e5075f5d08ece2b0b7ee.zip
LibCore: Add a way to install an event filter on a Core::Object
The event filter is consulted by Object::dispatch_event() and may decide that the event should not be delivered to the target object.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibCore/Object.cpp10
-rw-r--r--Userland/Libraries/LibCore/Object.h5
2 files changed, 13 insertions, 2 deletions
diff --git a/Userland/Libraries/LibCore/Object.cpp b/Userland/Libraries/LibCore/Object.cpp
index 854467a356..fc57e8be56 100644
--- a/Userland/Libraries/LibCore/Object.cpp
+++ b/Userland/Libraries/LibCore/Object.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -227,6 +227,9 @@ void Object::dispatch_event(Core::Event& e, Object* stay_within)
VERIFY(!stay_within || stay_within == this || stay_within->is_ancestor_of(*this));
auto* target = this;
do {
+ // If there's an event filter on this target, ask if it wants to swallow this event.
+ if (target->m_event_filter && !target->m_event_filter(e))
+ return;
target->event(e);
target = target->parent();
if (target == stay_within) {
@@ -262,4 +265,9 @@ void Object::register_property(const String& name, Function<JsonValue()> getter,
m_properties.set(name, make<Property>(name, move(getter), move(setter)));
}
+void Object::set_event_filter(Function<bool(Core::Event&)> filter)
+{
+ m_event_filter = move(filter);
+}
+
}
diff --git a/Userland/Libraries/LibCore/Object.h b/Userland/Libraries/LibCore/Object.h
index 8554adb4fe..05a33d3f6d 100644
--- a/Userland/Libraries/LibCore/Object.h
+++ b/Userland/Libraries/LibCore/Object.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -113,6 +113,8 @@ public:
void remove_child(Object&);
void remove_all_children();
+ void set_event_filter(Function<bool(Core::Event&)>);
+
void dump_tree(int indent = 0);
void deferred_invoke(Function<void(Object&)>);
@@ -169,6 +171,7 @@ private:
unsigned m_inspector_count { 0 };
HashMap<String, NonnullOwnPtr<Property>> m_properties;
NonnullRefPtrVector<Object> m_children;
+ Function<bool(Core::Event&)> m_event_filter;
};
}