MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ subscribe()

bool MayaFlux::Portal::Forma::Context::subscribe ( uint32_t id,
Vruta::WindowEventFilter filter,
SubscriberFn fn )

Subscribe to the window events that reach an element, without replacing any of its own callbacks.

The on_* callbacks keep one callback per element and slot, so a second registration replaces the first. A subscription is added beside them: the element's own callback runs first, then every matching subscriber in the order they subscribed. unbind(id) removes the callbacks and every subscriber of the element together.

The filter is the one the window's event source uses, and an unset field matches anything. Events reach an element as follows:

  • MOUSE_BUTTON_PRESSED and MOUSE_BUTTON_RELEASED when the pointer is over it, with the button.
  • MOUSE_MOTION while the pointer is over it. A filter with no button receives this hover motion. A filter with a button receives the drag of that button, which continues outside the element until the release and starts on press, as on_drag does.
  • MOUSE_ENTERED and MOUSE_EXITED when the pointer enters or leaves it.
  • MOUSE_SCROLLED while the pointer is over it.
  • KEY_PRESSED, KEY_RELEASED and KEY_REPEAT of a key, while it has focus. A filter for these must name the key.
  • TEXT_INPUT while it has focus.
  • WINDOW_FOCUS_GAINED and WINDOW_FOCUS_LOST when it gains or loses focus.
Parameters
filterWhat to receive.
fnCalled for each matching event.
Returns
False, subscribing nothing, when receives() refuses the filter or fn is empty.

Definition at line 163 of file Context.cpp.

164{
165 if (!fn || !receives(filter))
166 return false;
167
168 if (filter.key_code) {
169 const IO::Keys key = *filter.key_code;
170 const int key_code = static_cast<int>(key);
171 if (m_subscribed_keys.insert(key_code).second) {
172 m_event_manager.add_event(
173 std::make_shared<Vruta::Event>(
174 subscribed_key_events(m_window, key,
175 [this, key](const Core::WindowEvent& event) { handle_subscribed_key(key, event); })),
176 m_name + "_subscribed_key_" + std::to_string(key_code));
177 }
178 }
179
180 m_callbacks[id].subscribers.push_back({ .filter = filter, .fn = std::move(fn) });
181 return true;
182}
std::unordered_set< int > m_subscribed_keys
Definition Context.hpp:463
void handle_subscribed_key(IO::Keys key, const Core::WindowEvent &event)
Definition Context.cpp:719
Vruta::EventManager & m_event_manager
Definition Context.hpp:467
std::shared_ptr< Core::Window > m_window
Definition Context.hpp:466
static bool receives(const Vruta::WindowEventFilter &filter) noexcept
Whether an element can receive what a filter asks for.
Definition Context.cpp:136
std::unordered_map< uint32_t, ElementCallbacks > m_callbacks
Definition Context.hpp:471

References handle_subscribed_key(), MayaFlux::Vruta::WindowEventFilter::key_code, m_callbacks, m_event_manager, m_name, m_subscribed_keys, m_window, and receives().

Referenced by operator=(), and MayaFlux::Portal::Forma::Detail::DocumentWalk< Source, Access >::wire().

Here is the call graph for this function:
Here is the caller graph for this function: