Skip to content

Commit

Permalink
Merge pull request SnapDrop#497 from Haocen/patch-3
Browse files Browse the repository at this point in the history
QoL: Native notification improvements
  • Loading branch information
RobinLinus authored Oct 5, 2022
2 parents a96dde2 + 9543c47 commit 112bed3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
4 changes: 4 additions & 0 deletions client/scripts/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,10 @@ class Events {
static on(type, callback) {
return window.addEventListener(type, callback, false);
}

static off(type, callback) {
return window.removeEventListener(type, callback, false);
}
}


Expand Down
34 changes: 21 additions & 13 deletions client/scripts/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ class Notifications {
});
}

_notify(message, body, closeTimeout = 20000) {
_notify(message, body) {
const config = {
body: body,
icon: '/images/logo_transparent_128x128.png',
Expand All @@ -435,27 +435,35 @@ class Notifications {
}

// Notification is persistent on Android. We have to close it manually
if (closeTimeout) {
setTimeout(_ => notification.close(), closeTimeout);
}
const visibilitychangeHandler = () => {
if (document.visibilityState === 'visible') {
notification.close();
Events.off('visibilitychange', visibilitychangeHandler);
}
};
Events.on('visibilitychange', visibilitychangeHandler);

return notification;
}

_messageNotification(message) {
if (isURL(message)) {
const notification = this._notify(message, 'Click to open link');
this._bind(notification, e => window.open(message, '_blank', null, true));
} else {
const notification = this._notify(message, 'Click to copy text');
this._bind(notification, e => this._copyText(message, notification));
if (document.visibilityState !== 'visible') {
if (isURL(message)) {
const notification = this._notify(message, 'Click to open link');
this._bind(notification, e => window.open(message, '_blank', null, true));
} else {
const notification = this._notify(message, 'Click to copy text');
this._bind(notification, e => this._copyText(message, notification));
}
}
}

_downloadNotification(message) {
const notification = this._notify(message, 'Click to download');
if (!window.isDownloadSupported) return;
this._bind(notification, e => this._download(notification));
if (document.visibilityState !== 'visible') {
const notification = this._notify(message, 'Click to download');
if (!window.isDownloadSupported) return;
this._bind(notification, e => this._download(notification));
}
}

_download(notification) {
Expand Down

0 comments on commit 112bed3

Please sign in to comment.