New official hooks for plugin: main window hooks

173 views
Skip to first unread message

XY Wong

unread,
Aug 3, 2023, 9:14:09 AM8/3/23
to zotero-dev
Hi everyone!

Two new hooks in the bootstrap.js  are available in Zotero 7 beta 27: onMainWindowLoad and onMainWindowUnload.

On some platforms, the main window can be opened and closed multiple times during a Zotero session, so any window-related activities, such as modifying the main UI, adding menus, or binding shortcuts must be performed by onMainWindowLoad so that new main windows contain your changes.

You must then remove all references to a window or objects within it when onMainWindowUnload is called, or else you'll risk creating a memory leak every time the window is closed. DOM elements added to a window will be automatically destroyed when the window is closed, so you only need to remove those in shutdown(), which you can do by cycling through all windows:


Best,
Xiangyu

iseexuhs

unread,
Aug 3, 2023, 1:47:27 PM8/3/23
to zotero-dev
This API is awesome.

I tested the following codes. The onMainWindowLoad() succeeded help to restart the plugin when reopened the Mac Zotero Window. It is great!

// Mac 上关闭 Zotero 主窗口后,重新打开窗口后,启动插件
function onMainWindowLoad({ id, version, resourceURI, rootURI = resourceURI.spec }) {
  startup({ id, version, resourceURI, rootURI});
}



Besides, I want to know whether it is necessary to call shutdown() in the onMainWindowUnload() to avoid memory leakage as the following codes shows? Or Maybe the shutdown() will be called automatically when the Mac Zotero MainWindow is closed, even if not  calling shutdown in onMainWindowUnload()?


// Mac 上关闭 Zotero 主窗口后,执行 shutdown 程序,防止内存泄漏
function onMainWindowUnload({ id, version, resourceURI, rootURI = resourceURI.spec }) {
  shutdown({ id, version, resourceURI, rootURI});
}

iseexuhs

unread,
Aug 3, 2023, 2:01:09 PM8/3/23
to zotero-dev
To verify it, I checked the memory usage changes of Zotero in Mac. When the Zotero mainWindow is closed (not quit), the memory usage is decreased much. So I guess shutdown() will be called automatically when the Mac Zotero mainWindow is closed.  am I right?

XY Wong

unread,
Aug 3, 2023, 10:33:51 PM8/3/23
to zotero-dev
1. No. shutdown is a plugin lifecycle hook; the hooks here are the main window hooks. They are unrelated.

2. shutdown is only called when your plugin is disabled (including Zotero exit)/removed.

3. You should not call shutdown on the main window unload. We plan to support multiple main windows, which means there will be multiple main windows alive at the same time. If one of them is closed, the plugin should still be alive.

iseexuhs

unread,
Aug 3, 2023, 11:46:06 PM8/3/23
to zotero-dev
get it. But I want to know, besides what you mentioned,  what other benefits multiple main windows could bring?

iseexuhs

unread,
Aug 3, 2023, 11:52:15 PM8/3/23
to zotero-dev
Also, I want to confirm again that the plugin is alive or not if only the Mac Zotero mainWindow is closed (not quit). 

Considering that now we can use the new hooks onMainWindowLoad() to reactivate the plugin after mainWindow Opening. So, if the mainWindow closing can make the plugin disable, it will be useful for memory usage?

在2023年8月4日星期五 UTC+8 10:33:51<XY Wong> 写道:

XY Wong

unread,
Aug 3, 2023, 11:53:28 PM8/3/23
to zotero-dev
People can have multiple data folders - This feature is still in the planning phase.

XY Wong

unread,
Aug 4, 2023, 12:05:29 AM8/4/23
to zotero-dev
As I said, the main window has nothing to do with the plugin lifecycle. Your plugin is always alive until it's disabled or removed.

iseexuhs

unread,
Aug 4, 2023, 12:09:15 AM8/4/23
to zotero-dev
get. Thanks a lot. 👍 The new hooks are great, and expect the multiple mainWindows for multiple data folders management.

volatile static

unread,
Aug 4, 2023, 12:30:49 AM8/4/23
to zotero-dev
Hi,
I wonder if you could add a hook function like `onPrefsPaneLoad`, that would be called once the registered prefsPane was loaded. For now, I use `<vbox id="zotero-prefpane-__addonName__" onload="Zotero.Notifier.trigger('redraw', 'setting', ['__addonName__'], window);">
` as a workaround.

在2023年8月3日星期四 UTC+8 21:14:09<XY Wong> 写道:

XY Wong

unread,
Aug 4, 2023, 12:34:36 AM8/4/23
to zotero-dev
I doubt if we really need this `prefpane` hook. `Zotero.PreferancePanes.register` accept the parameter `scripts`. You can load your script there.

XY Wong

unread,
Aug 4, 2023, 12:35:30 AM8/4/23
to zotero-dev
And this is off-topic. Please start a new thread if you want to have further discussion.

Emiliano Heyns

unread,
Sep 13, 2024, 9:32:50 AM9/13/24
to zotero-dev
is onMainWindowLoad always called on a plugin after it starts? The main window may already be up and running when a plugin starts.

XY Wong

unread,
Sep 13, 2024, 9:35:47 AM9/13/24
to zoter...@googlegroups.com
I think no, it’s only called when the main window is loaded. 

--
You received this message because you are subscribed to a topic in the Google Groups "zotero-dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/zotero-dev/zVn4k9dLoak/unsubscribe.
To unsubscribe from this group and all its topics, send an email to zotero-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/zotero-dev/33a2cc67-64a9-42dd-8828-73805256c6f6n%40googlegroups.com.

Emiliano Heyns

unread,
Sep 13, 2024, 9:38:33 AM9/13/24
to zotero-dev
Should Zotero.ItemPaneManager.registerSection be called every time onMainWindow loads? Or is registration sufficient and does Zotero take care it is run when a main windows loads? And when it is already loaded?

XY Wong

unread,
Sep 13, 2024, 9:42:33 AM9/13/24
to zoter...@googlegroups.com
Zotero will take care of the rendering of the custom sections and you only need to call it once, generally in onLoad.

Emiliano Heyns

unread,
Sep 13, 2024, 9:44:59 AM9/13/24
to zotero-dev
is onLoad a bootstrap event like startup and onMainWindowLoad?

XY Wong

unread,
Sep 13, 2024, 9:57:47 AM9/13/24
to zoter...@googlegroups.com
Oops, sorry, I meant to say the bootstrap hook startup. (The onLoad is a function name that I use in my plugins, just ignore it)

Reply all
Reply to author
Forward
0 new messages