add notifications

This commit is contained in:
Vladimir Mandic
2023-05-03 14:29:07 -04:00
parent 8cbce7ea19
commit 0af6c70b94
5 changed files with 22 additions and 21 deletions
+15 -15
View File
@@ -2,32 +2,32 @@
let lastHeadImg = null;
let notificationButton = null;
const regExpTempImage = /(?<=\/|\\)tmp[\w\d]{8}\.png$/gm;
onUiUpdate(function(){
if(notificationButton == null){
notificationButton = gradioApp().getElementById('request_notifications')
if (notificationButton != null) notificationButton.addEventListener('click', (evt) => Notification.requestPermission(), true);
if (!notificationButton) {
notificationButton = gradioApp().getElementById('request_notifications')
if (notificationButton) notificationButton.addEventListener('click', (evt) => Notification.requestPermission(), true);
}
const galleryPreviews = gradioApp().querySelectorAll('div[id^="tab_"][style*="display: block"] div[id$="_results"] .thumbnail-item > img');
if (galleryPreviews == null) return;
const headImg = galleryPreviews[0]?.src;
if (headImg == null || headImg == lastHeadImg) return;
if (headImg.search(regExpTempImage) != -1) return;
lastHeadImg = headImg;
// play notification sound if available
if (!galleryPreviews) return;
if (document.hasFocus()) return; // window is in focus so don't send notifications
const audioNotification = gradioApp().querySelector('#audio_notification audio');
if (audioNotification) audioNotification.play();
if (document.hasFocus()) return;
// Multiple copies of the images are in the DOM when one is selected. Dedup with a Set to get the real number generated.
const imgs = new Set(Array.from(galleryPreviews).map(img => img.src));
const headImg = galleryPreviews[0]?.src;
if (!headImg || headImg == lastHeadImg || headImg.endsWith('automatic.png')) return;
lastHeadImg = headImg;
console.log(headImg)
const imgs = new Set(Array.from(galleryPreviews).map(img => img.src)); // Multiple copies of the images are in the DOM when one is selected
const notification = new Notification(
'Stable Diffusion', {
'SD.Next', {
body: `Generated ${imgs.size > 1 ? imgs.size - opts.return_grid : 1} image${imgs.size > 1 ? 's' : ''}`,
icon: headImg,
image: headImg }
);
notification.onclick = function(_) {
notification.onclick = () => {
parent.focus();
this.close();
};
+4 -3
View File
@@ -37,9 +37,10 @@ function formatTime(secs) {
}
function setTitle(progress) {
var title = 'Stable Diffusion'
if(opts.show_progress_in_title && progress) title = '[' + progress.trim() + '] ' + title;
if(document.title != title) document.title = title;
var title = 'SD.Next'
console.log('progress:', progress)
if (progress) title += ' ' + progress.split(' ')[0].trim();
if (document.title != title) document.title = title;
}
function randomId() {