Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/events/eventManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class EventManager {
this.evenIds = {}; // event name -> {target, eventID}
this.monitoredWindows = {}; // windowID -> {eventName -> eventID}
this.unlockHandlerId = null;
this.positionCallbacks = new Set(); // Set of callbacks for position changes
}

attachGlobalEventOnce(eventName, target, callback) {
Expand All @@ -23,6 +24,7 @@ class EventManager {
event.target.disconnect(event.id);
});
this.detachUnlockScreenEvent();
this.positionCallbacks.clear();
this.evenIds = {};
}

Expand Down Expand Up @@ -61,6 +63,50 @@ class EventManager {
this.unlockHandlerId = null;
}
}

attachWindowPositionCallback(window, callback) {
const eventName = 'position-changed';
this.attachWindowEventOnce(eventName, window, callback);
this.positionCallbacks.add(callback);
return callback;
}

isPositionCallbackAttached(callback) {
return this.positionCallbacks.has(callback);
}

attachGlobalPositionCallback(target, callback) {
const eventName = 'position-changed';
this.attachGlobalEventOnce(eventName, target, callback);
this.positionCallbacks.add(callback);
return callback;
}

detachGlobalPositionCallback(callback) {
if (this.evenIds['position-changed'] && this.positionCallbacks.has(callback)) {
const event = this.evenIds['position-changed'];
event.target.disconnect(event.id);
delete this.evenIds['position-changed'];
this.positionCallbacks.delete(callback);
}
}

detachWindowPositionCallback(window, callback) {
const windowId = window.get_id();
const eventName = 'position-changed';

if (this.monitoredWindows[windowId] &&
this.monitoredWindows[windowId][eventName] !== undefined) {
window.disconnect(this.monitoredWindows[windowId][eventName]);
delete this.monitoredWindows[windowId][eventName];

// If no more events for this window, clean up the entry
if (Object.keys(this.monitoredWindows[windowId]).length === 0)
delete this.monitoredWindows[windowId];
}

this.positionCallbacks.delete(callback);
}
}

export default EventManager;