sidebar: waifus: refractor + fix spamming logs when clearing

This commit is contained in:
end-4
2024-02-19 21:57:14 +07:00
parent 5cc55e3425
commit e9a166d50a
+25 -38
View File
@@ -1,3 +1,6 @@
// TODO: execAsync(['identify', '-format', '{"w":%w,"h":%h}', imagePath])
// to detect img dimensions
const { Gdk, GdkPixbuf, Gio, GLib, Gtk } = imports.gi; const { Gdk, GdkPixbuf, Gio, GLib, Gtk } = imports.gi;
import Widget from 'resource:///com/github/Aylur/ags/widget.js'; import Widget from 'resource:///com/github/Aylur/ags/widget.js';
import * as Utils from 'resource:///com/github/Aylur/ags/utils.js'; import * as Utils from 'resource:///com/github/Aylur/ags/utils.js';
@@ -293,7 +296,7 @@ const waifuContent = Box({
const data = WaifuService.responses[id]; const data = WaifuService.responses[id];
if (!data) return; if (!data) return;
const imageBlock = box.attribute.map.get(id); const imageBlock = box.attribute.map.get(id);
imageBlock.attribute.update(data); imageBlock?.attribute.update(data);
}, 'updateResponse') }, 'updateResponse')
, ,
}); });
@@ -372,28 +375,30 @@ export const waifuCommands = Box({
} }
}); });
const clearChat = () => { const clearChat = () => { // destroy!!
waifuContent.attribute.map.clear(); waifuContent.attribute.map.forEach((value, key, map) => {
const kids = waifuContent.get_children(); value.destroy();
for (let i = 0; i < kids.length; i++) { value = null;
const child = kids[i]; });
if (child) child.destroy();
}
} }
const DummyTag = (width, height, url, color = '#9392A6') => { function newSimpleImageCall(name, url, width, height, dominantColor = '#9392A6') {
return { // Needs timeout or inits won't make it const timeSinceEpoch = Date.now();
const newImage = WaifuImage([`/${name}`]);
waifuContent.add(newImage);
waifuContent.attribute.map.set(timeSinceEpoch, newImage);
Utils.timeout(IMAGE_REVEAL_DELAY, () => newImage?.attribute.update({
status: 200, status: 200,
url: url, url: url,
extension: '', extension: '',
signature: 0, signature: timeSinceEpoch,
source: url, source: url,
dominant_color: color, dominant_color: dominantColor,
is_nsfw: false, is_nsfw: false,
width: width, width: width,
height: height, height: height,
tags: ['/test'], tags: [`/${name}`],
}; }, true));
} }
export const sendMessage = (text) => { export const sendMessage = (text) => {
@@ -401,30 +406,12 @@ export const sendMessage = (text) => {
// Commands // Commands
if (text.startsWith('/')) { if (text.startsWith('/')) {
if (text.startsWith('/clear')) clearChat(); if (text.startsWith('/clear')) clearChat();
else if (text.startsWith('/test')) { else if (text.startsWith('/test'))
const newImage = WaifuImage(['/test']); newSimpleImageCall('test', 'https://picsum.photos/600/400', 300, 200);
waifuContent.add(newImage); else if (text.startsWith('/chino'))
Utils.timeout(IMAGE_REVEAL_DELAY, () => newImage.attribute.update( newSimpleImageCall('chino', 'https://chino.pages.dev/chino', 300, 400, '#B2AEF3');
DummyTag(300, 200, 'https://picsum.photos/600/400'), else if (text.startsWith('/place'))
true newSimpleImageCall('place', 'https://placewaifu.com/image/400/600', 400, 600, '#F0A235');
));
}
else if (text.startsWith('/chino')) {
const newImage = WaifuImage(['/chino']);
waifuContent.add(newImage);
Utils.timeout(IMAGE_REVEAL_DELAY, () => newImage.attribute.update(
DummyTag(300, 400, 'https://chino.pages.dev/chino', '#B2AEF3'),
true
));
}
else if (text.startsWith('/place')) {
const newImage = WaifuImage(['/place']);
waifuContent.add(newImage);
Utils.timeout(IMAGE_REVEAL_DELAY, () => newImage.attribute.update(
DummyTag(400, 600, 'https://placewaifu.com/image/400/600', '#F0A235'),
true
));
}
} }
else WaifuService.fetch(text); else WaifuService.fetch(text);