add: system Notification

This commit is contained in:
xiangyu 2023-01-05 21:49:24 +08:00
parent 329e6dab88
commit 25befec9c6
2 changed files with 58 additions and 19 deletions

View File

@ -1,6 +1,7 @@
import { BasicExampleFactory, UIExampleFactory } from "./modules/examples";
import {
changeProgressWindowLine,
isProgressWindow,
showProgressWindow,
} from "./modules/progressWindow";
import { config } from "../package.json";
@ -14,7 +15,9 @@ async function onStartup() {
config.addonName,
getString("startup.begin"),
"default",
-1
{
closeTime: -1,
}
);
changeProgressWindowLine(progWin, { newProgress: 0 });
@ -51,7 +54,9 @@ async function onStartup() {
newProgress: 100,
newText: `[100%] ${getString("startup.finish")}`,
});
progWin.startCloseTimer(5000);
if (isProgressWindow(progWin)) {
(progWin as _ZoteroProgressWindow).startCloseTimer(5000);
}
}
function onShutdown(): void {

View File

@ -10,10 +10,34 @@ export function showProgressWindow(
header: string,
context: string,
type: "success" | "fail" | "default" = "default",
t: number = 5000
): _ZoteroProgressWindow {
options: {
closeTime?: number;
backend?: "Zotero" | "system";
} = {
closeTime: 5000,
backend: "Zotero",
}
): _ZoteroProgressWindow | Notification {
// Currently Zotero 7 doesn't support progress window.
// Use system backend on Zotero 7.
if (options.backend === "system" || ztoolkit.Compat.isZotero7()) {
Zotero.Prefs.set("alerts.useSystemBackend", true, true);
const notification = new (ztoolkit.Compat.getGlobal(
"Notification"
) as typeof Notification)(header, {
body: context,
icon: progressWindowIcon[type],
tag: config.addonName,
});
if (options.closeTime) {
(ztoolkit.Compat.getGlobal("setTimeout") as typeof setTimeout)(() => {
notification.close();
}, options.closeTime);
}
return notification;
} else {
// A simple wrapper of the Zotero ProgressWindow
let progressWindow = new Zotero.ProgressWindow({
const progressWindow = new Zotero.ProgressWindow({
closeOnClick: true,
}) as _ZoteroProgressWindow;
progressWindow.changeHeadline(header);
@ -23,20 +47,24 @@ export function showProgressWindow(
context
);
progressWindow.show();
if (t > 0) {
progressWindow.startCloseTimer(t);
if (options.closeTime) {
progressWindow.startCloseTimer(options.closeTime);
}
return progressWindow;
}
}
export function changeProgressWindowLine(
progressWindow: _ZoteroProgressWindow,
progressWindow: _ZoteroProgressWindow | Notification,
options: {
newText?: string;
newIcon?: string;
newProgress?: number;
}
) {
if (!isProgressWindow(progressWindow)) {
return;
}
// @ts-ignore
const progress = progressWindow.progress as _ZoteroItemProgress;
if (!progress) {
@ -46,3 +74,9 @@ export function changeProgressWindowLine(
options.newIcon && progress.setIcon(options.newIcon);
options.newProgress && progress.setProgress(options.newProgress);
}
export function isProgressWindow(
progressWindow: _ZoteroProgressWindow | Notification
) {
return !(progressWindow as Notification).title;
}