mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-05-05 06:32:30 +02:00
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.
This commit is contained in:
Notes:
sideshowbarker
2024-07-18 21:00:46 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/54c7110d9d0
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user