@lvce-editor/menu-worker 4.0.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -741,7 +741,7 @@ const getErrorResponse = (id, error, preparePrettyError, logError) => {
741
741
  const errorProperty = getErrorProperty(error, prettyError);
742
742
  return create$1$1(id, errorProperty);
743
743
  };
744
- const create$8 = (message, result) => {
744
+ const create$9 = (message, result) => {
745
745
  return {
746
746
  id: message.id,
747
747
  jsonrpc: Two$1,
@@ -750,7 +750,7 @@ const create$8 = (message, result) => {
750
750
  };
751
751
  const getSuccessResponse = (message, result) => {
752
752
  const resultProperty = result ?? null;
753
- return create$8(message, resultProperty);
753
+ return create$9(message, resultProperty);
754
754
  };
755
755
  const getErrorResponseSimple = (id, error) => {
756
756
  return {
@@ -844,7 +844,7 @@ const handleJsonRpcMessage = async (...args) => {
844
844
 
845
845
  const Two = '2.0';
846
846
 
847
- const create$7 = (method, params) => {
847
+ const create$8 = (method, params) => {
848
848
  return {
849
849
  jsonrpc: Two,
850
850
  method,
@@ -852,7 +852,7 @@ const create$7 = (method, params) => {
852
852
  };
853
853
  };
854
854
 
855
- const create$6 = (id, method, params) => {
855
+ const create$7 = (id, method, params) => {
856
856
  const message = {
857
857
  id,
858
858
  jsonrpc: Two,
@@ -863,12 +863,12 @@ const create$6 = (id, method, params) => {
863
863
  };
864
864
 
865
865
  let id$a = 0;
866
- const create$5 = () => {
866
+ const create$6 = () => {
867
867
  return ++id$a;
868
868
  };
869
869
 
870
870
  const registerPromise = map => {
871
- const id = create$5();
871
+ const id = create$6();
872
872
  const {
873
873
  promise,
874
874
  resolve
@@ -885,7 +885,7 @@ const invokeHelper = async (callbacks, ipc, method, params, useSendAndTransfer)
885
885
  id,
886
886
  promise
887
887
  } = registerPromise(callbacks);
888
- const message = create$6(id, method, params);
888
+ const message = create$7(id, method, params);
889
889
  if (useSendAndTransfer && ipc.sendAndTransfer) {
890
890
  ipc.sendAndTransfer(message);
891
891
  } else {
@@ -921,7 +921,7 @@ const createRpc = ipc => {
921
921
  * @deprecated
922
922
  */
923
923
  send(method, ...params) {
924
- const message = create$7(method, params);
924
+ const message = create$8(method, params);
925
925
  ipc.send(message);
926
926
  }
927
927
  };
@@ -961,7 +961,7 @@ const listen$1 = async (module, options) => {
961
961
  return ipc;
962
962
  };
963
963
 
964
- const create$4 = async ({
964
+ const create$5 = async ({
965
965
  commandMap,
966
966
  isMessagePortOpen = true,
967
967
  messagePort
@@ -979,7 +979,7 @@ const create$4 = async ({
979
979
  return rpc;
980
980
  };
981
981
 
982
- const create$3 = async ({
982
+ const create$4 = async ({
983
983
  commandMap,
984
984
  isMessagePortOpen,
985
985
  send
@@ -989,13 +989,55 @@ const create$3 = async ({
989
989
  port2
990
990
  } = new MessageChannel();
991
991
  await send(port1);
992
- return create$4({
992
+ return create$5({
993
993
  commandMap,
994
994
  isMessagePortOpen,
995
995
  messagePort: port2
996
996
  });
997
997
  };
998
998
 
999
+ const createSharedLazyRpc = factory => {
1000
+ let rpcPromise;
1001
+ const getOrCreate = () => {
1002
+ if (!rpcPromise) {
1003
+ rpcPromise = factory();
1004
+ }
1005
+ return rpcPromise;
1006
+ };
1007
+ return {
1008
+ async dispose() {
1009
+ const rpc = await getOrCreate();
1010
+ await rpc.dispose();
1011
+ },
1012
+ async invoke(method, ...params) {
1013
+ const rpc = await getOrCreate();
1014
+ return rpc.invoke(method, ...params);
1015
+ },
1016
+ async invokeAndTransfer(method, ...params) {
1017
+ const rpc = await getOrCreate();
1018
+ return rpc.invokeAndTransfer(method, ...params);
1019
+ },
1020
+ async send(method, ...params) {
1021
+ const rpc = await getOrCreate();
1022
+ rpc.send(method, ...params);
1023
+ }
1024
+ };
1025
+ };
1026
+
1027
+ const create$3 = async ({
1028
+ commandMap,
1029
+ isMessagePortOpen,
1030
+ send
1031
+ }) => {
1032
+ return createSharedLazyRpc(() => {
1033
+ return create$4({
1034
+ commandMap,
1035
+ isMessagePortOpen,
1036
+ send
1037
+ });
1038
+ });
1039
+ };
1040
+
999
1041
  const create$2 = async ({
1000
1042
  commandMap
1001
1043
  }) => {
@@ -1392,23 +1434,28 @@ const sendMessagePortToRendererProcess = async port => {
1392
1434
  const command = 'HandleMessagePort.handleMessagePort';
1393
1435
  await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToRendererProcess', port, command, DebugWorker);
1394
1436
  };
1437
+ const sendMessagePortToTextMeasurementWorker = async port => {
1438
+ const command = 'TextMeasurement.handleMessagePort';
1439
+ await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToTextMeasurementWorker', port, command, 0);
1440
+ };
1395
1441
 
1396
1442
  const RendererWorker = {
1397
1443
  __proto__: null,
1398
1444
  invoke: invoke$2,
1399
1445
  invokeAndTransfer,
1400
1446
  sendMessagePortToRendererProcess,
1447
+ sendMessagePortToTextMeasurementWorker,
1401
1448
  set: set$2
1402
1449
  };
1403
1450
 
1404
- const send = port => {
1451
+ const send$1 = port => {
1405
1452
  return sendMessagePortToRendererProcess(port);
1406
1453
  };
1407
1454
  const createRendererProcessRpc = async () => {
1408
1455
  try {
1409
- const rpc = await create$3({
1456
+ const rpc = await create$4({
1410
1457
  commandMap: {},
1411
- send
1458
+ send: send$1
1412
1459
  });
1413
1460
  return rpc;
1414
1461
  } catch (error) {
@@ -1470,6 +1517,54 @@ const closeSubMenu = async () => {
1470
1517
  await invoke$3(/* Menu.hideSubMenu */7903, /* parentIndex */parentMenu.focusedIndex);
1471
1518
  };
1472
1519
 
1520
+ const send = port => {
1521
+ return sendMessagePortToTextMeasurementWorker(port);
1522
+ };
1523
+ const createTextMeasurementWorkerRpc = async () => {
1524
+ try {
1525
+ const rpc = await create$3({
1526
+ commandMap: {},
1527
+ send
1528
+ });
1529
+ return rpc;
1530
+ } catch (error) {
1531
+ throw new VError(error, `Failed to create text measurement worker rpc`);
1532
+ }
1533
+ };
1534
+
1535
+ let rpcPromise;
1536
+ const getRpc = () => {
1537
+ rpcPromise ||= createTextMeasurementWorkerRpc();
1538
+ return rpcPromise;
1539
+ };
1540
+ const measureTextWidths = async (texts, fontWeight, fontSize, fontFamily, letterSpacing, isMonospaceFont, charWidth) => {
1541
+ const rpc = await getRpc();
1542
+ return rpc.invoke('TextMeasurement.measureTextWidths', texts, fontWeight, fontSize, fontFamily, letterSpacing, isMonospaceFont, charWidth);
1543
+ };
1544
+
1545
+ const FontWeight = 400;
1546
+ const FontSize = 13;
1547
+ const FontFamily = 'system-ui, Ubuntu, Droid Sans, sans-serif';
1548
+ const LetterSpacing = 0;
1549
+ const IsMonospaceFont = false;
1550
+ const CharWidth = 8;
1551
+ const getLabels = items => {
1552
+ return items.map(item => item.label).filter(label => typeof label === 'string' && label);
1553
+ };
1554
+ const getMenuLabelWidths = async items => {
1555
+ const labels = getLabels(items);
1556
+ return measureTextWidths(labels, FontWeight, FontSize, FontFamily, LetterSpacing, IsMonospaceFont, CharWidth);
1557
+ };
1558
+
1559
+ const MENU_MIN_WIDTH = 150;
1560
+ const MENU_ITEM_HORIZONTAL_PADDING = 56;
1561
+ const MENU_ITEM_HORIZONTAL_MARGIN = 8;
1562
+ const getMenuMeasuredWidth = async items => {
1563
+ const labelWidths = await getMenuLabelWidths(items);
1564
+ const maxLabelWidth = Math.max(0, ...labelWidths);
1565
+ return Math.ceil(Math.max(MENU_MIN_WIDTH, maxLabelWidth + MENU_ITEM_HORIZONTAL_PADDING + MENU_ITEM_HORIZONTAL_MARGIN));
1566
+ };
1567
+
1473
1568
  const Separator$1 = 1;
1474
1569
  const None = 0;
1475
1570
  const SubMenu = 4;
@@ -1480,7 +1575,7 @@ const RestoreFocus = 6;
1480
1575
  const Ignore = 7;
1481
1576
 
1482
1577
  // TODO lazyload menuEntries and use Command.execute (maybe)
1483
- const CONTEXT_MENU_WIDTH$1 = 250;
1578
+ const MENU_TARGET_GAP = 5;
1484
1579
 
1485
1580
  // TODO difference between focusing with mouse or keyboard
1486
1581
  // with mouse -> open submenu
@@ -1511,8 +1606,8 @@ const getMenuHeight$1 = items => {
1511
1606
  }
1512
1607
  return height;
1513
1608
  };
1514
- const getMenuBounds = (x, y, items) => {
1515
- const menuWidth = CONTEXT_MENU_WIDTH$1;
1609
+ const getMenuBounds = async (x, y, items) => {
1610
+ const menuWidth = await getMenuMeasuredWidth(items);
1516
1611
  const menuHeight = getMenuHeight$1(items);
1517
1612
  const layoutState = {
1518
1613
  points: [0, 0]
@@ -1523,7 +1618,7 @@ const getMenuBounds = (x, y, items) => {
1523
1618
  // TODO what about separators?
1524
1619
 
1525
1620
  if (x + menuWidth > windowWidth) {
1526
- x -= menuWidth;
1621
+ x -= menuWidth + MENU_TARGET_GAP;
1527
1622
  x = Math.max(x, 0);
1528
1623
  }
1529
1624
  if (y + menuHeight > windowHeight) {
@@ -1873,8 +1968,9 @@ const MenuEntriesHelp = {
1873
1968
  id: id$6
1874
1969
  };
1875
1970
 
1876
- const getRecentlyOpened = () => {
1877
- return invoke$2(/* RecentlyOpened.getRecentlyOpened */'RecentlyOpened.getRecentlyOpened');
1971
+ const getRecentlyOpened = async () => {
1972
+ const recentlyOpened = await invoke$2(/* RecentlyOpened.getRecentlyOpened */'RecentlyOpened.getRecentlyOpened');
1973
+ return recentlyOpened;
1878
1974
  };
1879
1975
 
1880
1976
  const File = 'file://';
@@ -2464,7 +2560,7 @@ const MENU_WIDTH = 150;
2464
2560
 
2465
2561
  const show$2 = async (x, y, id, mouseBlocking = false, ...args) => {
2466
2562
  const items = await getMenuEntriesWithKeyBindings(id, ...args);
2467
- const bounds = getMenuBounds(x, y, items);
2563
+ const bounds = await getMenuBounds(x, y, items);
2468
2564
  const menu = addMenuInternal({
2469
2565
  focusedIndex: -1,
2470
2566
  id,
@@ -2479,7 +2575,7 @@ const show$2 = async (x, y, id, mouseBlocking = false, ...args) => {
2479
2575
  };
2480
2576
  const show2$2 = async (uid, menuId, x, y, mouseBlocking = false, ...args) => {
2481
2577
  const items = await getMenuEntriesWithKeyBindings2(uid, menuId, ...args);
2482
- const bounds = getMenuBounds(x, y, items);
2578
+ const bounds = await getMenuBounds(x, y, items);
2483
2579
  const menu = addMenuInternal({
2484
2580
  focusedIndex: -1,
2485
2581
  id: menuId,
@@ -2771,7 +2867,7 @@ const getMenuEntries = (id, platform) => {
2771
2867
  // TODO more tests
2772
2868
 
2773
2869
  const getMenuShowCommands = async (items, menuId, x, y, mouseBlocking = false) => {
2774
- const bounds = getMenuBounds(x, y, items);
2870
+ const bounds = await getMenuBounds(x, y, items);
2775
2871
  const menu = addMenuInternal({
2776
2872
  focusedIndex: -1,
2777
2873
  id: menuId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/menu-worker",
3
- "version": "4.0.0",
3
+ "version": "4.2.0",
4
4
  "description": "Menu Worker",
5
5
  "repository": {
6
6
  "type": "git",