Skip to content

Commit

Permalink
Use document focus instead of page visibility API (#644)
Browse files Browse the repository at this point in the history
For marking messages as unread and for triggering notifications, use
`document.hasFocus()` instead of the page visibility API.

This means that messages are only marked as read if the browser is in
the foreground and focussed. Otherwise, a notification is triggered.

Refs #211 and #17.
  • Loading branch information
dbrgn authored Oct 23, 2018
1 parent 242635c commit 77c50b7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 61 deletions.
8 changes: 7 additions & 1 deletion public/libs/angular-inview/angular-inview.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ function inViewDirective ($parse) {
viewportEventSignal = viewportEventSignal.merge(signalFromEvent(document, 'visibilitychange'));
}

// Merged with the page focus/blur events
if (options.considerPageFocus) {
viewportEventSignal = viewportEventSignal.merge(signalFromEvent(window, 'focus blur'));
}

// Merge with container's events signal
if (container) {
viewportEventSignal = viewportEventSignal.merge(container.eventsSignal);
Expand Down Expand Up @@ -100,8 +105,9 @@ function inViewDirective ($parse) {
var elementRect = offsetRect(element[0].getBoundingClientRect(), options.offset);
var isVisible = !!(element[0].offsetWidth || element[0].offsetHeight || element[0].getClientRects().length);
var documentVisible = !options.considerPageVisibility || document.visibilityState === 'visible' || document.hidden === false;
var documentFocussed = !options.considerPageFocus || document.hasFocus();
var info = {
inView: documentVisible && isVisible && intersectRect(elementRect, viewportRect),
inView: documentVisible && documentFocussed && isVisible && intersectRect(elementRect, viewportRect),
event: event,
element: element,
elementRect: elementRect,
Expand Down
2 changes: 1 addition & 1 deletion src/partials/messenger.conversation.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ <h2 class="md-flex" translate>messenger.PRIVATE_CHAT</h2>
<li ng-repeat="message in ctrl.messages" id="message-{{message.id}}">
<eee-message eee-receiver="ctrl.receiver" eee-type="ctrl.type" eee-message="message"
in-view="$inview && !ctrl.locked && ctrl.msgRead(message)"
in-view-options="{ considerPageVisibility: true }"></eee-message>
in-view-options="{ considerPageFocus: true }"></eee-message>
</li>
<li ng-if="ctrl.isTyping()" class="typing-indicator">
<!-- Non status messages -->
Expand Down
58 changes: 0 additions & 58 deletions src/services/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,67 +25,13 @@ export class BrowserService {
private browser: BrowserInfo;
private $log: ng.ILogService;
private $window: ng.IWindowService;
private isPageVisible = true;
private supportsExtendedLocaleCompareCache: boolean;

public static $inject = ['$log', '$window'];
constructor($log: ng.ILogService, $window: ng.IWindowService) {
// Angular services
this.$log = $log;
this.$window = $window;
this.initializePageVisibility();
}

private initializePageVisibility() {
const onChange = (isVisible: any) => {
if (this.isPageVisible !== isVisible) {
this.isPageVisible = isVisible;
}
};

let pageHiddenKey = 'hidden';

// add default visibility change listener
let defaultListener;
if (pageHiddenKey in document) {
defaultListener = 'visibilitychange';
} else if ('mozHidden' in document) {
pageHiddenKey = 'mozHidden';
defaultListener = 'mozvisibilitychange';
} else if ('webkitHidden' in document) {
pageHiddenKey = 'webkitHidden';
defaultListener = 'webkitvisibilitychange';
} else if ('msHidden' in document) {
pageHiddenKey = 'msHidden';
defaultListener = 'msvisibilitychange';
}

document.addEventListener(defaultListener, function() {
onChange(!this[pageHiddenKey]);
});

// configure other document and window events
const map = {
focus: true,
blur: false,
};

for (const event in map) {
if (map[event] !== undefined) {
document.addEventListener(event, () => {
onChange(map[event]);
}, false);

window.addEventListener(event, () => {
onChange(map[event]);
}, false);
}
}

// initial visible state set
if (document[pageHiddenKey] !== undefined ) {
onChange(!document[pageHiddenKey]);
}
}

public getBrowser(): BrowserInfo {
Expand Down Expand Up @@ -183,10 +129,6 @@ export class BrowserService {
return this.browser;
}

public isVisible() {
return this.isPageVisible;
}

/**
* Return whether the current browser supports the WebRTC task or not.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/services/webclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3212,7 +3212,7 @@ export class WebClientService {
sender: threema.Receiver,
): void {
// Ignore message from active receivers (and if the browser tab is visible)
if (this.browserService.isVisible()
if (document.hasFocus()
&& this.receiverService.compare(conversation, this.receiverService.getActive())) {
return;
}
Expand Down

0 comments on commit 77c50b7

Please sign in to comment.