Skip to content

Commit

Permalink
feat: add performance limit
Browse files Browse the repository at this point in the history
  • Loading branch information
rugk committed Feb 28, 2021
1 parent 8f59b28 commit 6466b50
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/background/modules/ContextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { COMMUNICATION_MESSAGE_TYPE } from "/common/modules/data/BrowserCommunic
import { menuStructure, SEPARATOR_ID, TRANSFORMATION_TYPE } from "/common/modules/data/Fonts.js";

const menus = browser.menus || browser.contextMenus; // fallback for Thunderbird
const PREVIEW_STRING_CUT_LENGTH = 1000; // a setting that may improve performance by not calculating invisible parts of the context menu

let lastCachedUnicodeFontSettings = null;

Expand Down Expand Up @@ -51,6 +52,11 @@ async function handleMenuShown(info) {
await menus.removeAll();
return menus.refresh();
}
// shorten preview text as it may not be shown anyway
if (text.length > PREVIEW_STRING_CUT_LENGTH) {
// to be sure, we append … anyway, in case some strange OS has a tooltip for context menus or so
text = `${text.substr(0, PREVIEW_STRING_CUT_LENGTH)}…`;
}
text = text.normalize();

const menuIsShown = info.menuIds.length > 0;
Expand Down Expand Up @@ -156,9 +162,7 @@ export async function init() {
BrowserCommunication.addListener(COMMUNICATION_MESSAGE_TYPE.UNICODE_FONT, async (request) => {
lastCachedUnicodeFontSettings = request.optionValue;

if (!refreshMenu) {
await menus.removeAll();
}
await menus.removeAll();
return buildMenu(request.optionValue);
});
}

0 comments on commit 6466b50

Please sign in to comment.