From 382cf55b32d9b9d75b7a9f9fa99d451d8a74f125 Mon Sep 17 00:00:00 2001 From: itagagaki Date: Tue, 13 Dec 2022 20:52:37 +0900 Subject: [PATCH] More improve startup --- content/folderPane.js | 176 +++++++++++++++++++++++------------------- manifest.json | 2 +- 2 files changed, 97 insertions(+), 81 deletions(-) diff --git a/content/folderPane.js b/content/folderPane.js index 54527f0..c6fc011 100644 --- a/content/folderPane.js +++ b/content/folderPane.js @@ -1,9 +1,5 @@ (async function () { - - /* Utility */ - const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); - /* For folder sorting */ const Cc = Components.classes; @@ -161,27 +157,108 @@ }; myPrefObserver.register(); - /* Refresh pane */ - let refreshCount = 0; - for (let win of Services.wm.getEnumerator("mail:3pane")) { - ++refreshCount; + /* Startup folder */ + let startup_folder = tbsf_prefs.getStringPref("startup_folder"); + if (startup_folder) { + tblog.debug("startup folder: "+startup_folder); + } else { + tblog.debug("No startup folder specified"); } - tblog.debug("Refresh pane ("+refreshCount+")"); - function refreshPane(win) { - try { - win.gFolderTreeView._rebuild(); - --refreshCount; - } catch (e) { - setTimeout(refreshPane, 5, win); + + if (startup_folder && ThunderbirdMajorVersion < 98) { + /* + On Thunderbird 97 and earlier, it was possible for add-ons to intervene in + the startup behavior of Thunderbird. + */ + const oldRestoreTab = mailTabType.modes.folder.restoreTab; + let inRestoreTab = false; + mailTabType.modes.folder.restoreTab = function (x, y) { + tblog.debug("restoreTab"); + inRestoreTab = true; + oldRestoreTab.call(this, x, y); + inRestoreTab = false; + }; + const oldSelectFolder = gFolderTreeView.selectFolder; + const change_folder = startup_folder; + let firstRun = true; + gFolderTreeView.selectFolder = function (x, y) { + tblog.debug("selectFolder firstRun:"+firstRun.toString()+" inRestoreTab:"+inRestoreTab.toString()); + if (firstRun && inRestoreTab) { + const folder = MailUtils.getExistingFolder(change_folder); + if (folder) { + oldSelectFolder.call(this, folder, true); + /* Ensures that the selected folder is on the screen. */ + const selected = gFolderTreeView.getSelectedFolders()[0]; + if (selected) { + gFolderTreeView._treeElement.ensureRowIsVisible(gFolderTreeView.getIndexOfFolder(selected)); + } + } else { + oldSelectFolder.call(this, x, y); + } + firstRun = false; + } else { + oldSelectFolder.call(this, x, y); + } } + tblog.debug("Overriding selectFolder"); + startup_folder = null; } - for (let win of Services.wm.getEnumerator("mail:3pane")) { - refreshPane(win); + + /* + Refresh pane and select folder + + The start of this add-on may be too early to call gFolderTreeView._rebuild() + and MailUtils.getExistingFolder(). + + So I structured a 10-times retry loop to counter any exceptions or failures + that may occur due to that. + */ + async function selectFolder(win, startup_folder) { + let tries = 0; + let ms = 100; + + // Stop after 10 tries. Or use other break condition, like total time spend. + while (tries < 10) { + try { + /* + Refresh pane -- possible exception. + */ + win.gFolderTreeView._rebuild(); + if (startup_folder) { + /* + Select folder + Since Thunderbird 98, add-on startup has been delayed until + Thunderbird is mostly done. So there is no way other than + immediately selecting the folder. However, there is a report of a + case where getExistingFolder returns null for the existing folder. + */ + let folder = MailUtils.getExistingFolder(startup_folder); + if (folder && gFolderTreeView.selectFolder(folder, true)) { + return true; + } + } else { + return true; + } + } catch (e) { + // Nothing. + } + tries++; + await new Promise(resolve => setTimeout(resolve, ms)); + } + return false; } - while (refreshCount > 0) { - await sleep(10); + + let selectPromises = []; + for (let win of Services.wm.getEnumerator("mail:3pane")) { + selectPromises.push(selectFolder(win, startup_folder)); } + let results = await Promise.all(selectPromises); + tblog.debug("Refreshing the pane" + + (startup_folder ? " and selecting folder" : "") + + ": " + + (results ? "Success" : "Failure")); + /* Ensures that the selected folder is on the screen. */ { const selected = gFolderTreeView.getSelectedFolders()[0]; @@ -190,67 +267,6 @@ } } - /* For default startup folder */ - const startup_folder = tbsf_prefs.getStringPref("startup_folder"); - if (startup_folder) { - tblog.debug("startup folder: "+startup_folder); - if (ThunderbirdMajorVersion < 98) { - /* - On Thunderbird 97 or older, it was possible for add-ons - to intervene in the startup behavior of Thunderbird. - */ - const oldRestoreTab = mailTabType.modes.folder.restoreTab; - let inRestoreTab = false; - mailTabType.modes.folder.restoreTab = function (x, y) { - tblog.debug("restoreTab"); - inRestoreTab = true; - oldRestoreTab.call(this, x, y); - inRestoreTab = false; - }; - const oldSelectFolder = gFolderTreeView.selectFolder; - let firstRun = true; - gFolderTreeView.selectFolder = function (x, y) { - tblog.debug("selectFolder firstRun:"+firstRun.toString()+" inRestoreTab:"+inRestoreTab.toString()); - if (firstRun && inRestoreTab) { - const folder = MailUtils.getExistingFolder(startup_folder); - if (folder) - oldSelectFolder.call(this, folder, true); - else - oldSelectFolder.call(this, x, y); - firstRun = false; - } else { - oldSelectFolder.call(this, x, y); - } - } - } else { - /* - Since Thunderbird 98, add-on startup has been delayed until Thunderbird is mostly done. - So there is no way other than immediately selecting the folder. - */ - const retryMax = 10; - for (let retry = 1; retry <= retryMax; retry++) { - let folder = MailUtils.getExistingFolder(startup_folder); - if (folder) { - if (gFolderTreeView.selectFolder(folder, true)) { - tblog.debug("selectFolder succeeded"); - } else { - tblog.debug("selectFolder failed"); - } - break; - } else { - if (retry < retryMax) { - tblog.debug(startup_folder+" not found ("+retry+") ..retry"); - await sleep(200); - } else { - tblog.debug(startup_folder+" not found ("+retry+") ..giving up"); - } - } - } - } - } else { - tblog.debug("No startup folder specified"); - } - tblog.debug("Init done"); })() diff --git a/manifest.json b/manifest.json index db14652..6732263 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "Manually sort folders", "description": "__MSG_extensionDescription__", - "version": "2.2.5", + "version": "2.2.6", "author": "Jonathan Protzenko and Itagaki Fumihiko", "homepage_url": "https://github.com/protz/Manually-Sort-Folders/wiki", "applications": {