Skip to content

Commit d631df8

Browse files
authored
feat(aot) Add intersection observer (#168)
1 parent 0c0ab40 commit d631df8

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

alwaysontop/constants.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ module.exports = {
2121
HIDE: 'aot-hide',
2222
OPEN: 'aot-open',
2323
SHOW: 'aot-show',
24-
SHOW_MAIN_WINDOW: 'aot-main-window-show'
24+
SHOW_MAIN_WINDOW: 'aot-main-window-show',
25+
SHOW_AOT_WINDOW: 'show-aot-window',
26+
HIDE_AOT_WINDOW: 'hide-aot-window'
2527
},
2628
STORAGE: {
2729
AOT_X: 'aot-x',

alwaysontop/main/index.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ const onNewWindow = (event, url, frameName, disposition, options) => {
8888
};
8989

9090
/**
91-
* Handle blur event on mainWindow
91+
* Handle show aot event.
9292
*/
93-
const onBlur = () => {
94-
logInfo('handling main window blur');
93+
const showAot = () => {
94+
logInfo('show aot handler');
9595

9696
let state;
9797

@@ -106,10 +106,10 @@ const onBlur = () => {
106106
};
107107

108108
/**
109-
* Handle blur event on mainWindow
109+
* Handle hide aot event.
110110
*/
111-
const onFocus = () => {
112-
logInfo('handling main window focus');
111+
const hideAot = () => {
112+
logInfo('hide aot handler');
113113

114114
hideWindow();
115115
};
@@ -129,8 +129,8 @@ const onClose = () => {
129129
const addWindowHandlers = () => {
130130
logInfo(`adding main window event handlers`);
131131

132-
mainWindow.on('blur', onBlur);
133-
mainWindow.on('focus', onFocus);
132+
mainWindow.on('blur', showAot);
133+
mainWindow.on('focus', hideAot);
134134

135135
// this might be redundant, since child windows will be closed anyway
136136
mainWindow.on('close', onClose);
@@ -142,8 +142,8 @@ const addWindowHandlers = () => {
142142
const removeWindowHandlers = () => {
143143
logInfo(`removing main window event handlers`);
144144

145-
mainWindow.removeListener('blur', onBlur);
146-
mainWindow.removeListener('focus', onFocus);
145+
mainWindow.removeListener('blur', showAot);
146+
mainWindow.removeListener('focus', hideAot);
147147
mainWindow.removeListener('close', onClose);
148148
};
149149

@@ -194,6 +194,12 @@ const onStateChange = (event, { value }) => {
194194
// this will switch focus to main window, which in turns triggers hide on aot
195195
mainWindow.show();
196196
break;
197+
case STATES.SHOW_AOT_WINDOW:
198+
showAot();
199+
break;
200+
case STATES.HIDE_AOT_WINDOW:
201+
hideAot();
202+
break;
197203
default:
198204
break;
199205
}

alwaysontop/render/index.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ class AlwaysOnTop extends EventEmitter {
3737
super();
3838

3939
this._api = api;
40-
4140
this._closeWindow = this._closeWindow.bind(this);
4241
this._dismiss = this._dismiss.bind(this);
4342
this._onConferenceJoined = this._onConferenceJoined.bind(this);
4443
this._onStateChange = this._onStateChange.bind(this);
4544
this._switchToMainWindow = this._switchToMainWindow.bind(this);
4645
this._updateLargeVideoSrc = this._updateLargeVideoSrc.bind(this);
46+
this._onIntersection = this._onIntersection.bind(this);
47+
this._intersectionObserver = new IntersectionObserver(this._onIntersection);
4748

4849
this._api.on('_willDispose', this._closeWindow);
4950
this._api.on('videoConferenceJoined', this._onConferenceJoined);
@@ -75,6 +76,25 @@ class AlwaysOnTop extends EventEmitter {
7576
ipcRenderer.on(EVENTS.UPDATE_STATE, this._onStateChange);
7677

7778
sendStateUpdate(STATES.CONFERENCE_JOINED);
79+
80+
this._intersectionObserver.observe(this._api.getIFrame());
81+
}
82+
83+
/**
84+
* Handles intersection events for the instance's IntersectionObserver
85+
*
86+
* @param {IntersectionObserverEntry[]} entries
87+
* @param {IntersectionObserver} observer
88+
*/
89+
_onIntersection(entries) {
90+
logInfo('handling main window intersection');
91+
const singleEntry = entries.pop();
92+
93+
if (singleEntry.isIntersecting) {
94+
sendStateUpdate(STATES.HIDE_AOT_WINDOW);
95+
} else {
96+
sendStateUpdate(STATES.SHOW_AOT_WINDOW);
97+
}
7898
}
7999

80100
/**
@@ -193,6 +213,7 @@ class AlwaysOnTop extends EventEmitter {
193213
_closeWindow() {
194214
logInfo(`closing window and cleaning listeners`);
195215

216+
this._intersectionObserver.unobserve(this._api.getIFrame());
196217
this.emit(EXTERNAL_EVENTS.ALWAYSONTOP_WILL_CLOSE);
197218

198219
sendStateUpdate(STATES.CLOSE);

0 commit comments

Comments
 (0)