@restregular/dsh-powerup 0.7.0 → 0.8.1

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.
package/client.js CHANGED
@@ -69,7 +69,7 @@
69
69
 
70
70
  const NS = 'dsh-powerup';
71
71
  const SETTINGS_KEY = 'dsh-powerup:settings:v1';
72
- const VERSION = '0.7.0';
72
+ const VERSION = '0.8.1';
73
73
  const MENU_GAP = 8;
74
74
  const MENU_WIDTH = 248;
75
75
  /** Reference scheme understood by the Host's `powerup_step_read` tool. */
@@ -106,6 +106,15 @@
106
106
  'page.jobStopTitle': '后台任务停止原因',
107
107
  'page.jobStopDesc': '在会话顶部的后台任务面板里点停止按钮时,可以顺手填写停止原因;原因会附加在任务结束后注入会话的提示里。',
108
108
  'page.jobStopHow': '打开任意会话,点顶部的「后台任务」面板,在运行中的任务右侧点停止,填写原因(可留空)后确认。',
109
+ 'page.launcherTitle': '侧边栏启动器',
110
+ 'page.launcherDesc': '把左侧边栏的面板按钮变成自适应图标网格:列数跟着侧边栏宽度走,图标在上、文字在下;拖动图标可以排序,把一个图标拖到另一个图标的中间就会合成一个分组(像 Windows 开始菜单的文件夹),点分组会以浮层弹出里面的图标。',
111
+ 'page.launcherToggle': '启用图标网格',
112
+ 'page.launcherToggleHint': '关闭后侧边栏恢复宿主原本的列表样式;排序与分组仍会保留。',
113
+ 'launcher.groupDefault': '分组',
114
+ 'launcher.count': '{count} 个面板',
115
+ 'launcher.renameHint': '点击重命名',
116
+ 'launcher.dissolve': '解散分组',
117
+ 'launcher.removeItem': '移出分组',
109
118
  'page.footer': 'Power-Up {version} · 设置在浏览器本地保存',
110
119
  'page.localOnly': '仅影响当前浏览器',
111
120
  'action.copy': '复制内容',
@@ -283,6 +292,15 @@
283
292
  'page.jobStopTitle': 'Background job stop reason',
284
293
  'page.jobStopDesc': 'When you stop a running job from the background jobs panel in the session header, you can leave a reason; the reason is appended to the notice injected into the session when the job settles.',
285
294
  'page.jobStopHow': 'Open a session, expand the background jobs panel, press stop on a running job, type a reason (optional), then confirm.',
295
+ 'page.launcherTitle': 'Sidebar launcher',
296
+ 'page.launcherDesc': 'Turns the left sidebar panel buttons into an adaptive icon grid: the column count follows the sidebar width, icons sit above their labels, icons can be dragged to reorder, dropping one icon onto the middle of another forms a folder (like a Windows Start menu folder), and clicking a folder opens its contents in a flyout.',
297
+ 'page.launcherToggle': 'Enable the icon grid',
298
+ 'page.launcherToggleHint': 'When off, the sidebar keeps the host list; order and folders are remembered.',
299
+ 'launcher.groupDefault': 'Group',
300
+ 'launcher.count': '{count} panels',
301
+ 'launcher.renameHint': 'Click to rename',
302
+ 'launcher.dissolve': 'Ungroup',
303
+ 'launcher.removeItem': 'Remove from group',
286
304
  'page.footer': 'Power-Up {version} · Settings are stored in this browser',
287
305
  'page.localOnly': 'This browser only',
288
306
  'action.copy': 'Copy content',
@@ -464,7 +482,7 @@
464
482
  // Settings store (localStorage, per browser)
465
483
  // ------------------------------------------------------------------
466
484
  /** One-click update defaults: enable and restart unless the user turned one off. */
467
- const DEFAULT_SETTINGS = { enabled: true, oneClickEnable: true, oneClickRestart: true };
485
+ const DEFAULT_SETTINGS = { enabled: true, oneClickEnable: true, oneClickRestart: true, launcher: true };
468
486
 
469
487
  function readSettings() {
470
488
  try {
@@ -474,6 +492,7 @@
474
492
  enabled: parsed.enabled !== false,
475
493
  oneClickEnable: parsed.oneClickEnable !== false,
476
494
  oneClickRestart: parsed.oneClickRestart !== false,
495
+ launcher: parsed.launcher !== false,
477
496
  };
478
497
  }
479
498
  } catch {
@@ -516,6 +535,1137 @@
516
535
  return value;
517
536
  }
518
537
 
538
+ // ------------------------------------------------------------------
539
+ // Sidebar launcher: the panel list as an adaptive icon grid
540
+ // ------------------------------------------------------------------
541
+ /** localStorage key holding the launcher's order and folders. */
542
+ const LAUNCHER_STORAGE_KEY = 'dsh-powerup:launcher:v1';
543
+ /** Prefix that marks a top-level entry as a folder instead of a panel. */
544
+ const LAUNCHER_GROUP_PREFIX = 'group:';
545
+ /** Pixel distance before a press turns into a drag. */
546
+ const LAUNCHER_DRAG_THRESHOLD = 6;
547
+
548
+ function launcherGroupEntry(groupId) {
549
+ return LAUNCHER_GROUP_PREFIX + groupId;
550
+ }
551
+
552
+ function launcherEntryGroupId(entry) {
553
+ return typeof entry === 'string' && entry.startsWith(LAUNCHER_GROUP_PREFIX)
554
+ ? entry.slice(LAUNCHER_GROUP_PREFIX.length)
555
+ : null;
556
+ }
557
+
558
+ function makeLauncherGroupId() {
559
+ return 'g' + Date.now().toString(36) + Math.random().toString(36).slice(2, 6);
560
+ }
561
+
562
+ /**
563
+ * Reconcile the saved launcher state with the panels that exist right now.
564
+ * Unknown panel keys (unregistered, unpinned) are dropped; panels without
565
+ * a saved position are appended in host order; folders left empty vanish.
566
+ * Pure so the smoke test can cover the merge rules without a DOM.
567
+ * @param {unknown} raw stored value (already parsed)
568
+ * @param {string[]} panelKeys panel keys in host registration order
569
+ * @returns {{order: string[], groups: Record<string, {id: string, name: string, items: string[]}>}}
570
+ */
571
+ function normalizeLauncherState(raw, panelKeys) {
572
+ const known = new Set(Array.isArray(panelKeys) ? panelKeys : []);
573
+ if (raw === null || typeof raw !== 'object') return { order: [...known], groups: {} };
574
+ const groups = {};
575
+ const assigned = new Set();
576
+ const rawGroups = raw.groups !== null && typeof raw.groups === 'object' ? raw.groups : {};
577
+ for (const [groupId, value] of Object.entries(rawGroups)) {
578
+ if (value === null || typeof value !== 'object' || typeof groupId !== 'string' || groupId === '') continue;
579
+ const items = [];
580
+ const list = Array.isArray(value.items) ? value.items : [];
581
+ for (const key of list) {
582
+ if (typeof key !== 'string' || !known.has(key) || assigned.has(key)) continue;
583
+ assigned.add(key);
584
+ items.push(key);
585
+ }
586
+ if (items.length === 0) continue;
587
+ groups[groupId] = {
588
+ id: groupId,
589
+ name: typeof value.name === 'string' ? value.name : '',
590
+ items,
591
+ };
592
+ }
593
+ const order = [];
594
+ const seen = new Set();
595
+ const rawOrder = Array.isArray(raw.order) ? raw.order : [];
596
+ for (const entry of rawOrder) {
597
+ if (typeof entry !== 'string' || seen.has(entry)) continue;
598
+ const groupId = launcherEntryGroupId(entry);
599
+ if (groupId !== null) {
600
+ if (Object.prototype.hasOwnProperty.call(groups, groupId)) {
601
+ order.push(entry);
602
+ seen.add(entry);
603
+ }
604
+ continue;
605
+ }
606
+ if (!known.has(entry) || assigned.has(entry)) continue;
607
+ order.push(entry);
608
+ seen.add(entry);
609
+ }
610
+ for (const key of known) {
611
+ if (assigned.has(key) || seen.has(key)) continue;
612
+ order.push(key);
613
+ seen.add(key);
614
+ }
615
+ for (const groupId of Object.keys(groups)) {
616
+ const entry = launcherGroupEntry(groupId);
617
+ if (seen.has(entry)) continue;
618
+ order.push(entry);
619
+ seen.add(entry);
620
+ }
621
+ return { order, groups };
622
+ }
623
+
624
+ /**
625
+ * DOM-driven controller for the sidebar launcher.
626
+ *
627
+ * It works on the host's own `<nav>` panel list: it adds a marker class,
628
+ * turns the list into an `auto-fill` grid, injects synthetic folder tiles,
629
+ * hides the host rows that moved into a folder, and drives drag sorting
630
+ * and the folder flyout with plain DOM (React owns the rows, so every
631
+ * change here is idempotent and re-applied after the host re-renders).
632
+ *
633
+ * Everything it needs from the plugin arrives through `deps`, which keeps
634
+ * this region self-contained and testable.
635
+ * @param {{t?: Function, format?: Function, isEnabled?: Function, read?: Function, write?: Function, listPanels?: Function}} deps
636
+ */
637
+ function createLauncherCore(deps) {
638
+ const options = deps ?? {};
639
+ const t = typeof options.t === 'function' ? options.t : (key) => key;
640
+ const formatText = typeof options.format === 'function'
641
+ ? options.format
642
+ : (template, params) => String(template).replace(/\{(\w+)\}/g, (match, key) =>
643
+ params !== undefined && params !== null && key in params ? String(params[key]) : match);
644
+ const isEnabled = typeof options.isEnabled === 'function' ? options.isEnabled : () => true;
645
+ const readRaw = typeof options.read === 'function' ? options.read : () => null;
646
+ const writeRaw = typeof options.write === 'function' ? options.write : () => {};
647
+ const listPanels = typeof options.listPanels === 'function' ? options.listPanels : () => [];
648
+
649
+ let nav = null;
650
+ let docObserver = null;
651
+ let navObserver = null;
652
+ let healthTimer = null;
653
+ let queued = 0;
654
+ let syncing = false;
655
+ let started = false;
656
+ let state = { order: [], groups: {} };
657
+ let pendingLoad = true;
658
+ let hostTiles = [];
659
+ let folderButtons = new Map();
660
+ let dropLine = null;
661
+ let drag = null;
662
+ let popup = null;
663
+
664
+ function warn(message, error) {
665
+ try {
666
+ console.warn('[dsh-powerup] ' + message, error?.message ?? error);
667
+ } catch {
668
+ /* Console output must never break the launcher. */
669
+ }
670
+ }
671
+
672
+ function hasDocument() {
673
+ return typeof document !== 'undefined'
674
+ && document !== null
675
+ && typeof document.querySelector === 'function'
676
+ && document.body !== null
677
+ && document.body !== undefined;
678
+ }
679
+
680
+ function scheduleSync() {
681
+ if (queued !== 0 || !started) return;
682
+ if (typeof requestAnimationFrame === 'function') {
683
+ queued = requestAnimationFrame(() => {
684
+ queued = 0;
685
+ sync();
686
+ });
687
+ return;
688
+ }
689
+ if (typeof window !== 'undefined' && window !== null && typeof window.setTimeout === 'function') {
690
+ queued = window.setTimeout(() => {
691
+ queued = 0;
692
+ sync();
693
+ }, 0);
694
+ }
695
+ }
696
+
697
+ function navLooksLikePanelList(candidate) {
698
+ if (candidate === null || candidate === undefined) return false;
699
+ if (typeof candidate.children !== 'object' || candidate.children === null) return false;
700
+ for (const child of [...candidate.children]) {
701
+ if (child.tagName === 'BUTTON') return true;
702
+ if (child.tagName === 'DIV' && typeof child.querySelector === 'function' && child.querySelector(':scope > button') !== null) return true;
703
+ }
704
+ return false;
705
+ }
706
+
707
+ function queryNav() {
708
+ if (!hasDocument()) return null;
709
+ const marked = document.querySelector('nav[class*="panelList"], nav.dsh-powerup-launcher');
710
+ if (marked !== null && marked !== undefined) return marked;
711
+ const candidates = typeof document.querySelectorAll === 'function' ? document.querySelectorAll('nav') : [];
712
+ for (const candidate of candidates) {
713
+ if (navLooksLikePanelList(candidate)) return candidate;
714
+ }
715
+ return null;
716
+ }
717
+
718
+ function observeDocument() {
719
+ if (docObserver !== null || typeof MutationObserver !== 'function' || !hasDocument()) return;
720
+ const root = document.body ?? document.documentElement;
721
+ if (root === null || root === undefined) return;
722
+ docObserver = new MutationObserver(() => {
723
+ const next = queryNav();
724
+ if (next === null) return;
725
+ if (docObserver !== null) {
726
+ docObserver.disconnect();
727
+ docObserver = null;
728
+ }
729
+ attachNav(next);
730
+ });
731
+ docObserver.observe(root, { childList: true, subtree: true });
732
+ }
733
+
734
+ function attachNav(element) {
735
+ nav = element;
736
+ element.classList.add('dsh-powerup-launcher');
737
+ if (navObserver !== null) navObserver.disconnect();
738
+ if (typeof MutationObserver === 'function') {
739
+ navObserver = new MutationObserver(() => {
740
+ if (!syncing) scheduleSync();
741
+ });
742
+ navObserver.observe(element, { childList: true, subtree: true, attributes: true, attributeFilter: ['aria-current'] });
743
+ }
744
+ sync();
745
+ }
746
+
747
+ /** Resolve the host panel rows (and the "more" launcher) into tile records. */
748
+ function collectHostTiles(container) {
749
+ const byLabel = new Map();
750
+ let panels = [];
751
+ try {
752
+ panels = listPanels() ?? [];
753
+ } catch {
754
+ panels = [];
755
+ }
756
+ if (Array.isArray(panels)) {
757
+ for (const panel of panels) {
758
+ if (panel === null || typeof panel !== 'object') continue;
759
+ if (typeof panel.id !== 'string' || typeof panel.label !== 'string') continue;
760
+ if (!byLabel.has(panel.label)) byLabel.set(panel.label, panel.id);
761
+ }
762
+ }
763
+ const tiles = [];
764
+ const usedKeys = new Set();
765
+ for (const child of [...container.children]) {
766
+ if (child.hasAttribute('data-powerup-folder')) continue;
767
+ if (typeof child.classList === 'object' && child.classList !== null && child.classList.contains('dsh-powerup-drop-line')) continue;
768
+ let gridEl = child;
769
+ let buttonEl = null;
770
+ if (child.tagName === 'BUTTON') buttonEl = child;
771
+ else if (child.tagName === 'DIV') {
772
+ buttonEl = typeof child.querySelector === 'function' ? child.querySelector(':scope > button') : null;
773
+ if (buttonEl === null) continue;
774
+ } else continue;
775
+ const label = buttonEl.getAttribute('aria-label') ?? '';
776
+ if (label === '') continue;
777
+ const isMore = child.tagName === 'DIV' || buttonEl.getAttribute('aria-haspopup') === 'menu';
778
+ let key = isMore ? '__more' : (byLabel.get(label) ?? 'label:' + label);
779
+ if (key !== '__more' && usedKeys.has(key)) {
780
+ let suffix = 2;
781
+ while (usedKeys.has(key + '~' + suffix)) suffix += 1;
782
+ key = key + '~' + suffix;
783
+ }
784
+ usedKeys.add(key);
785
+ gridEl.setAttribute('data-powerup-grid', key);
786
+ if (isMore) gridEl.setAttribute('data-powerup-more', '');
787
+ else gridEl.removeAttribute('data-powerup-more');
788
+ const glyph = typeof gridEl.querySelector === 'function'
789
+ ? gridEl.querySelector('[class*="panelGlyph"], span[aria-hidden="true"]')
790
+ : null;
791
+ tiles.push({
792
+ key,
793
+ label,
794
+ gridEl,
795
+ buttonEl,
796
+ isMore,
797
+ glyphHtml: glyph === null ? '' : glyph.innerHTML,
798
+ active: false,
799
+ });
800
+ }
801
+ return tiles;
802
+ }
803
+
804
+ function detectWide(tiles) {
805
+ for (const tile of tiles) {
806
+ if (tile.isMore) continue;
807
+ if (typeof tile.buttonEl.querySelector === 'function' && tile.buttonEl.querySelector('[class*="panelTitle"]') !== null) return true;
808
+ if (tile.buttonEl.children.length >= 2) return true;
809
+ }
810
+ return false;
811
+ }
812
+
813
+ function tileByKeyMap() {
814
+ const map = new Map();
815
+ for (const tile of hostTiles) map.set(tile.key, tile);
816
+ return map;
817
+ }
818
+
819
+ function buildFolderTile(groupId, group, tiles) {
820
+ let button = folderButtons.get(groupId);
821
+ if (button === undefined || button.isConnected !== true) {
822
+ button = document.createElement('button');
823
+ button.type = 'button';
824
+ button.className = 'dsh-powerup-folder';
825
+ button.setAttribute('data-powerup-folder', groupId);
826
+ button.addEventListener('click', (event) => {
827
+ event.preventDefault();
828
+ event.stopPropagation();
829
+ if (drag !== null && drag.active) return;
830
+ toggleFolder(groupId, button);
831
+ });
832
+ nav.appendChild(button);
833
+ folderButtons.set(groupId, button);
834
+ }
835
+ button.setAttribute('data-powerup-folder', groupId);
836
+ button.setAttribute('aria-label', group.name === '' ? t('launcher.groupDefault') : group.name);
837
+ button.setAttribute('title', group.name === '' ? t('launcher.groupDefault') : group.name);
838
+ const signature = group.name + '\u0000' + group.items.join('\u0000');
839
+ if (button.getAttribute('data-powerup-signature') !== signature) {
840
+ button.setAttribute('data-powerup-signature', signature);
841
+ button.textContent = '';
842
+ const stack = document.createElement('span');
843
+ stack.className = 'dsh-powerup-folder-stack';
844
+ for (const key of group.items.slice(0, 4)) {
845
+ const tile = tiles.get(key);
846
+ const mini = document.createElement('span');
847
+ mini.className = 'dsh-powerup-folder-mini';
848
+ mini.innerHTML = tile === undefined ? '' : tile.glyphHtml;
849
+ stack.appendChild(mini);
850
+ }
851
+ if (group.items.length === 1) stack.classList.add('is-single');
852
+ const name = document.createElement('span');
853
+ name.className = 'dsh-powerup-folder-name';
854
+ name.textContent = group.name === '' ? t('launcher.groupDefault') : group.name;
855
+ button.append(stack, name);
856
+ }
857
+ return button;
858
+ }
859
+
860
+ function hideDropLine() {
861
+ if (dropLine !== null) dropLine.style.display = 'none';
862
+ if (nav !== null && typeof nav.querySelectorAll === 'function') {
863
+ for (const element of nav.querySelectorAll('.dsh-powerup-merge-target')) element.classList.remove('dsh-powerup-merge-target');
864
+ }
865
+ }
866
+
867
+ function clearVisuals() {
868
+ closePopup();
869
+ endDrag();
870
+ hideDropLine();
871
+ folderButtons.forEach((button) => button.remove());
872
+ folderButtons = new Map();
873
+ if (dropLine !== null) {
874
+ dropLine.remove();
875
+ dropLine = null;
876
+ }
877
+ if (nav !== null) {
878
+ nav.classList.remove('dsh-powerup-launcher', 'dsh-powerup-launcher-wide');
879
+ for (const tile of hostTiles) {
880
+ tile.gridEl.classList.remove('dsh-powerup-dragging', 'dsh-powerup-merge-target');
881
+ tile.gridEl.style.order = '';
882
+ tile.gridEl.style.display = '';
883
+ }
884
+ }
885
+ hostTiles = [];
886
+ }
887
+
888
+ function readPersisted() {
889
+ try {
890
+ const text = readRaw();
891
+ if (typeof text !== 'string' || text === '') return null;
892
+ return JSON.parse(text);
893
+ } catch {
894
+ return null;
895
+ }
896
+ }
897
+
898
+ function sync() {
899
+ if (!started || syncing || !hasDocument()) return;
900
+ if (nav === null || nav.isConnected !== true) {
901
+ const next = queryNav();
902
+ if (next === null) {
903
+ if (docObserver === null) observeDocument();
904
+ return;
905
+ }
906
+ attachNav(next);
907
+ return;
908
+ }
909
+ syncing = true;
910
+ try {
911
+ if (isEnabled() !== true) {
912
+ clearVisuals();
913
+ return;
914
+ }
915
+ nav.classList.add('dsh-powerup-launcher');
916
+ hostTiles = collectHostTiles(nav);
917
+ const wide = detectWide(hostTiles);
918
+ nav.classList.toggle('dsh-powerup-launcher-wide', wide);
919
+ const panelKeys = hostTiles.filter((tile) => !tile.isMore).map((tile) => tile.key);
920
+ if (pendingLoad) {
921
+ pendingLoad = false;
922
+ state = normalizeLauncherState(readPersisted(), panelKeys);
923
+ } else {
924
+ state = normalizeLauncherState(state, panelKeys);
925
+ }
926
+ const tiles = tileByKeyMap();
927
+ const memberOf = new Map();
928
+ for (const group of Object.values(state.groups)) {
929
+ for (const key of group.items) memberOf.set(key, group.id);
930
+ }
931
+ const orderIndex = new Map(state.order.map((entry, index) => [entry, index]));
932
+ for (const tile of hostTiles) {
933
+ tile.active = tile.buttonEl.getAttribute('aria-current') === 'page';
934
+ const hidden = wide && memberOf.has(tile.key);
935
+ /* Inline display (not a class): React rewrites className when the
936
+ active panel changes and would drop an added class. */
937
+ tile.gridEl.style.display = hidden ? 'none' : '';
938
+ if (tile.isMore) tile.gridEl.style.order = String(state.order.length + 4);
939
+ else {
940
+ const position = orderIndex.get(tile.key);
941
+ tile.gridEl.style.order = position === undefined ? '' : String(position);
942
+ }
943
+ }
944
+ const live = new Set();
945
+ for (const [groupId, group] of Object.entries(state.groups)) {
946
+ live.add(groupId);
947
+ const button = buildFolderTile(groupId, group, tiles);
948
+ button.style.display = wide ? '' : 'none';
949
+ const position = orderIndex.get(launcherGroupEntry(groupId));
950
+ button.style.order = position === undefined ? String(state.order.length + 2) : String(position);
951
+ const active = group.items.some((key) => tiles.get(key)?.active === true);
952
+ button.classList.toggle('is-active', active);
953
+ }
954
+ for (const [groupId, button] of [...folderButtons]) {
955
+ if (live.has(groupId)) continue;
956
+ button.remove();
957
+ folderButtons.delete(groupId);
958
+ }
959
+ if (popup !== null) {
960
+ if (state.groups[popup.groupId] === undefined) closePopup();
961
+ else renderPopup();
962
+ }
963
+ } catch (error) {
964
+ warn('launcher sync failed:', error);
965
+ } finally {
966
+ syncing = false;
967
+ }
968
+ }
969
+
970
+ function save() {
971
+ try {
972
+ writeRaw(JSON.stringify(state));
973
+ } catch (error) {
974
+ warn('launcher state could not be saved:', error);
975
+ }
976
+ }
977
+
978
+ // ----------------------------------------------------------------
979
+ // Folder flyout
980
+ // ----------------------------------------------------------------
981
+ function toggleFolder(groupId, anchor) {
982
+ if (popup !== null && popup.groupId === groupId) {
983
+ closePopup();
984
+ return;
985
+ }
986
+ openFolder(groupId, anchor);
987
+ }
988
+
989
+ function openFolder(groupId, anchor) {
990
+ if (!hasDocument() || state.groups[groupId] === undefined) return;
991
+ closePopup();
992
+ const element = document.createElement('div');
993
+ element.className = 'dsh-powerup-folder-pop';
994
+ element.setAttribute('role', 'dialog');
995
+ element.setAttribute('aria-label', state.groups[groupId].name === '' ? t('launcher.groupDefault') : state.groups[groupId].name);
996
+ element.setAttribute('data-powerup-folder-pop', groupId);
997
+ document.body.appendChild(element);
998
+ popup = { element, groupId, anchor };
999
+ renderPopup();
1000
+ positionPopup();
1001
+ const reveal = () => {
1002
+ if (popup !== null && popup.element === element) element.classList.add('is-open');
1003
+ };
1004
+ if (typeof requestAnimationFrame === 'function') requestAnimationFrame(reveal);
1005
+ else reveal();
1006
+ document.addEventListener('pointerdown', onPopupOutside, true);
1007
+ document.addEventListener('keydown', onPopupKey, true);
1008
+ if (typeof window !== 'undefined' && window !== null && typeof window.addEventListener === 'function') {
1009
+ window.addEventListener('resize', closePopup, true);
1010
+ window.addEventListener('scroll', onPopupScroll, true);
1011
+ }
1012
+ }
1013
+
1014
+ function closePopup() {
1015
+ if (popup === null) return;
1016
+ const element = popup.element;
1017
+ popup = null;
1018
+ document.removeEventListener('pointerdown', onPopupOutside, true);
1019
+ document.removeEventListener('keydown', onPopupKey, true);
1020
+ if (typeof window !== 'undefined' && window !== null && typeof window.removeEventListener === 'function') {
1021
+ window.removeEventListener('resize', closePopup, true);
1022
+ window.removeEventListener('scroll', onPopupScroll, true);
1023
+ }
1024
+ element.classList.remove('is-open');
1025
+ const remove = () => {
1026
+ if (element.parentNode !== null) element.remove();
1027
+ };
1028
+ if (typeof window !== 'undefined' && window !== null && typeof window.setTimeout === 'function') {
1029
+ window.setTimeout(remove, 170);
1030
+ } else {
1031
+ remove();
1032
+ }
1033
+ }
1034
+
1035
+ function onPopupOutside(event) {
1036
+ if (popup === null) return;
1037
+ const target = event.target;
1038
+ if (target === null || target === undefined) return;
1039
+ if (typeof popup.element.contains === 'function' && popup.element.contains(target)) return;
1040
+ if (popup.anchor !== null && popup.anchor !== undefined && typeof popup.anchor.contains === 'function' && popup.anchor.contains(target)) return;
1041
+ closePopup();
1042
+ }
1043
+
1044
+ function onPopupKey(event) {
1045
+ if (popup === null || event.key !== 'Escape') return;
1046
+ const target = event.target;
1047
+ const tag = target !== null && target !== undefined && typeof target.tagName === 'string' ? target.tagName : '';
1048
+ if (tag === 'INPUT' && typeof popup.element.contains === 'function' && popup.element.contains(target)) return;
1049
+ closePopup();
1050
+ }
1051
+
1052
+ function onPopupScroll(event) {
1053
+ if (popup === null) return;
1054
+ if (typeof popup.element.contains === 'function' && popup.element.contains(event.target)) return;
1055
+ closePopup();
1056
+ }
1057
+
1058
+ function positionPopup() {
1059
+ if (popup === null) return;
1060
+ const anchor = popup.anchor;
1061
+ const element = popup.element;
1062
+ if (anchor === null || anchor === undefined || typeof anchor.getBoundingClientRect !== 'function') return;
1063
+ const rect = anchor.getBoundingClientRect();
1064
+ if (rect.width === 0 && rect.height === 0) return;
1065
+ const width = element.offsetWidth || 0;
1066
+ const height = element.offsetHeight || 0;
1067
+ const viewWidth = (typeof window !== 'undefined' && window !== null && window.innerWidth) || 1280;
1068
+ const viewHeight = (typeof window !== 'undefined' && window !== null && window.innerHeight) || 900;
1069
+ const gap = 8;
1070
+ const margin = 10;
1071
+ let left = rect.right + gap;
1072
+ let originX = 'left';
1073
+ if (left + width > viewWidth - margin) {
1074
+ const alternate = rect.left - gap - width;
1075
+ if (alternate >= margin) {
1076
+ left = alternate;
1077
+ originX = 'right';
1078
+ } else {
1079
+ left = Math.max(margin, viewWidth - width - margin);
1080
+ }
1081
+ }
1082
+ const top = Math.max(margin, Math.min(rect.top - 6, viewHeight - height - margin));
1083
+ element.style.left = Math.round(left) + 'px';
1084
+ element.style.top = Math.round(top) + 'px';
1085
+ element.style.transformOrigin = originX + ' 22%';
1086
+ }
1087
+
1088
+ function renderPopup() {
1089
+ if (popup === null) return;
1090
+ const group = state.groups[popup.groupId];
1091
+ if (group === undefined) {
1092
+ closePopup();
1093
+ return;
1094
+ }
1095
+ const element = popup.element;
1096
+ element.textContent = '';
1097
+ const head = document.createElement('div');
1098
+ head.className = 'dsh-powerup-folder-pop-head';
1099
+ const title = document.createElement('button');
1100
+ title.type = 'button';
1101
+ title.className = 'dsh-powerup-folder-pop-title';
1102
+ title.textContent = group.name === '' ? t('launcher.groupDefault') : group.name;
1103
+ title.setAttribute('title', t('launcher.renameHint'));
1104
+ title.addEventListener('click', beginRename);
1105
+ const count = document.createElement('span');
1106
+ count.className = 'dsh-powerup-folder-pop-count';
1107
+ count.textContent = formatText(t('launcher.count'), { count: group.items.length });
1108
+ const dissolve = document.createElement('button');
1109
+ dissolve.type = 'button';
1110
+ dissolve.className = 'dsh-powerup-folder-pop-action';
1111
+ dissolve.textContent = t('launcher.dissolve');
1112
+ dissolve.addEventListener('click', () => dissolveGroup(popup === null ? null : popup.groupId));
1113
+ head.append(title, count, dissolve);
1114
+ const grid = document.createElement('div');
1115
+ grid.className = 'dsh-powerup-folder-grid';
1116
+ const tiles = tileByKeyMap();
1117
+ for (const key of group.items) {
1118
+ const tile = tiles.get(key);
1119
+ if (tile === undefined) continue;
1120
+ const item = document.createElement('div');
1121
+ item.className = 'dsh-powerup-folder-item';
1122
+ item.setAttribute('role', 'button');
1123
+ item.setAttribute('tabindex', '0');
1124
+ item.setAttribute('aria-label', tile.label);
1125
+ const glyph = document.createElement('span');
1126
+ glyph.className = 'dsh-powerup-folder-item-glyph';
1127
+ glyph.innerHTML = tile.glyphHtml;
1128
+ const label = document.createElement('span');
1129
+ label.className = 'dsh-powerup-folder-item-label';
1130
+ label.textContent = tile.label;
1131
+ const remove = document.createElement('span');
1132
+ remove.className = 'dsh-powerup-folder-item-remove';
1133
+ remove.setAttribute('role', 'button');
1134
+ remove.setAttribute('tabindex', '0');
1135
+ remove.setAttribute('aria-label', t('launcher.removeItem'));
1136
+ remove.setAttribute('title', t('launcher.removeItem'));
1137
+ remove.textContent = '\u00d7';
1138
+ remove.addEventListener('click', (event) => {
1139
+ event.preventDefault();
1140
+ event.stopPropagation();
1141
+ removeFromGroup(popup === null ? null : popup.groupId, key);
1142
+ });
1143
+ remove.addEventListener('keydown', (event) => {
1144
+ if (event.key !== 'Enter' && event.key !== ' ') return;
1145
+ event.preventDefault();
1146
+ event.stopPropagation();
1147
+ removeFromGroup(popup === null ? null : popup.groupId, key);
1148
+ });
1149
+ item.append(glyph, label, remove);
1150
+ item.addEventListener('click', () => selectPanel(tile));
1151
+ item.addEventListener('keydown', (event) => {
1152
+ if (event.key !== 'Enter' && event.key !== ' ') return;
1153
+ event.preventDefault();
1154
+ selectPanel(tile);
1155
+ });
1156
+ grid.appendChild(item);
1157
+ }
1158
+ element.append(head, grid);
1159
+ positionPopup();
1160
+ }
1161
+
1162
+ function beginRename() {
1163
+ if (popup === null) return;
1164
+ const group = state.groups[popup.groupId];
1165
+ if (group === undefined) return;
1166
+ const current = popup.element.querySelector('.dsh-powerup-folder-pop-title');
1167
+ if (current === null) return;
1168
+ const input = document.createElement('input');
1169
+ input.type = 'text';
1170
+ input.className = 'dsh-powerup-folder-pop-input';
1171
+ input.value = group.name;
1172
+ input.placeholder = t('launcher.groupDefault');
1173
+ input.setAttribute('maxlength', '24');
1174
+ const commit = () => {
1175
+ if (popup === null) return;
1176
+ const target = state.groups[popup.groupId];
1177
+ if (target === undefined) return;
1178
+ const name = input.value.trim().slice(0, 24);
1179
+ if (name === target.name) {
1180
+ renderPopup();
1181
+ return;
1182
+ }
1183
+ state = { order: state.order, groups: { ...state.groups, [popup.groupId]: { ...target, name } } };
1184
+ save();
1185
+ sync();
1186
+ };
1187
+ input.addEventListener('keydown', (event) => {
1188
+ if (event.key === 'Enter') {
1189
+ event.preventDefault();
1190
+ commit();
1191
+ } else if (event.key === 'Escape') {
1192
+ event.preventDefault();
1193
+ event.stopPropagation();
1194
+ renderPopup();
1195
+ }
1196
+ });
1197
+ input.addEventListener('blur', commit);
1198
+ current.replaceWith(input);
1199
+ if (typeof input.focus === 'function') {
1200
+ input.focus();
1201
+ if (typeof input.select === 'function') input.select();
1202
+ }
1203
+ }
1204
+
1205
+ function selectPanel(tile) {
1206
+ try {
1207
+ tile.buttonEl.click();
1208
+ } catch (error) {
1209
+ warn('panel could not be selected:', error);
1210
+ }
1211
+ closePopup();
1212
+ }
1213
+
1214
+ function removeFromGroup(groupId, key) {
1215
+ if (groupId === null || typeof groupId !== 'string' || typeof key !== 'string') return;
1216
+ const group = state.groups[groupId];
1217
+ if (group === undefined) return;
1218
+ const items = group.items.filter((entry) => entry !== key);
1219
+ const order = state.order.filter((entry) => entry !== key);
1220
+ const groups = { ...state.groups };
1221
+ if (items.length === 0) {
1222
+ delete groups[groupId];
1223
+ state = { order: order.filter((entry) => entry !== launcherGroupEntry(groupId)), groups };
1224
+ } else {
1225
+ groups[groupId] = { ...group, items };
1226
+ order.push(key);
1227
+ state = { order, groups };
1228
+ }
1229
+ save();
1230
+ sync();
1231
+ }
1232
+
1233
+ function dissolveGroup(groupId) {
1234
+ if (groupId === null || typeof groupId !== 'string') return;
1235
+ const group = state.groups[groupId];
1236
+ if (group === undefined) return;
1237
+ const groups = { ...state.groups };
1238
+ delete groups[groupId];
1239
+ const order = state.order
1240
+ .filter((entry) => entry !== launcherGroupEntry(groupId))
1241
+ .concat(group.items.filter((key) => !state.order.includes(key)));
1242
+ state = { order, groups };
1243
+ save();
1244
+ closePopup();
1245
+ sync();
1246
+ }
1247
+
1248
+ // ----------------------------------------------------------------
1249
+ // Drag sorting and folder merging
1250
+ // ----------------------------------------------------------------
1251
+ function topLevelCandidates(excludeKey) {
1252
+ const candidates = [];
1253
+ for (const entry of state.order) {
1254
+ if (entry === excludeKey) continue;
1255
+ const groupId = launcherEntryGroupId(entry);
1256
+ if (groupId !== null) {
1257
+ const button = folderButtons.get(groupId);
1258
+ if (button !== undefined && button.isConnected === true) candidates.push({ key: entry, el: button, kind: 'folder' });
1259
+ continue;
1260
+ }
1261
+ const tile = hostTiles.find((candidate) => candidate.key === entry);
1262
+ if (tile === undefined) continue;
1263
+ if (tile.gridEl.style.display === 'none') continue;
1264
+ candidates.push({ key: entry, el: tile.gridEl, kind: 'panel' });
1265
+ }
1266
+ return candidates;
1267
+ }
1268
+
1269
+ function describeTile(target) {
1270
+ if (target === null || target === undefined || typeof target.closest !== 'function') return null;
1271
+ const button = target.closest('button');
1272
+ if (button === null || nav === null || !nav.contains(button)) return null;
1273
+ const folderId = button.getAttribute('data-powerup-folder');
1274
+ if (folderId !== null) {
1275
+ const group = state.groups[folderId];
1276
+ if (group === undefined) return null;
1277
+ return { kind: 'folder', key: launcherGroupEntry(folderId), gridEl: button, label: group.name };
1278
+ }
1279
+ const tile = hostTiles.find((candidate) => candidate.buttonEl === button);
1280
+ if (tile === undefined || tile.isMore) return null;
1281
+ return { kind: 'panel', key: tile.key, gridEl: tile.gridEl, label: tile.label };
1282
+ }
1283
+
1284
+ function onPointerDown(event) {
1285
+ if (!started || event.button !== undefined && event.button !== 0) return;
1286
+ if (nav === null || !nav.classList.contains('dsh-powerup-launcher-wide')) return;
1287
+ if (popup !== null && typeof popup.element.contains === 'function' && popup.element.contains(event.target)) return;
1288
+ const descriptor = describeTile(event.target);
1289
+ if (descriptor === null) return;
1290
+ drag = {
1291
+ descriptor,
1292
+ startX: event.clientX,
1293
+ startY: event.clientY,
1294
+ active: false,
1295
+ ghost: null,
1296
+ offsetX: 0,
1297
+ offsetY: 0,
1298
+ drop: null,
1299
+ };
1300
+ document.addEventListener('pointermove', onPointerMove, { capture: true, passive: false });
1301
+ document.addEventListener('pointerup', onPointerUp, true);
1302
+ document.addEventListener('pointercancel', onPointerUp, true);
1303
+ document.addEventListener('keydown', onDragKey, true);
1304
+ }
1305
+
1306
+ function onDragKey(event) {
1307
+ if (event.key !== 'Escape') return;
1308
+ endDrag();
1309
+ }
1310
+
1311
+ function beginDrag() {
1312
+ if (drag === null) return;
1313
+ drag.active = true;
1314
+ closePopup();
1315
+ const gridEl = drag.descriptor.gridEl;
1316
+ const rect = gridEl.getBoundingClientRect();
1317
+ const ghost = gridEl.cloneNode(true);
1318
+ ghost.className = 'dsh-powerup-drag-ghost';
1319
+ ghost.removeAttribute('data-powerup-grid');
1320
+ ghost.removeAttribute('data-powerup-more');
1321
+ ghost.removeAttribute('data-powerup-host');
1322
+ ghost.removeAttribute('aria-label');
1323
+ ghost.style.width = rect.width + 'px';
1324
+ ghost.style.height = rect.height + 'px';
1325
+ ghost.style.left = rect.left + 'px';
1326
+ ghost.style.top = rect.top + 'px';
1327
+ ghost.style.order = '';
1328
+ document.body.appendChild(ghost);
1329
+ drag.ghost = ghost;
1330
+ drag.offsetX = drag.startX - rect.left;
1331
+ drag.offsetY = drag.startY - rect.top;
1332
+ gridEl.classList.add('dsh-powerup-dragging');
1333
+ hideDropLine();
1334
+ }
1335
+
1336
+ function moveGhost(x, y) {
1337
+ if (drag === null || drag.ghost === null) return;
1338
+ drag.ghost.style.left = Math.round(x - drag.offsetX) + 'px';
1339
+ drag.ghost.style.top = Math.round(y - drag.offsetY) + 'px';
1340
+ }
1341
+
1342
+ function onPointerMove(event) {
1343
+ if (drag === null) return;
1344
+ if (!drag.active) {
1345
+ const dx = event.clientX - drag.startX;
1346
+ const dy = event.clientY - drag.startY;
1347
+ if (Math.sqrt(dx * dx + dy * dy) < LAUNCHER_DRAG_THRESHOLD) return;
1348
+ beginDrag();
1349
+ }
1350
+ if (typeof event.preventDefault === 'function') event.preventDefault();
1351
+ moveGhost(event.clientX, event.clientY);
1352
+ updateDrop(event.clientX, event.clientY);
1353
+ }
1354
+
1355
+ function updateDrop(x, y) {
1356
+ if (drag === null || !drag.active || nav === null) return;
1357
+ const navRect = nav.getBoundingClientRect();
1358
+ if (x < navRect.left - 24 || x > navRect.right + 24 || y < navRect.top - 24) {
1359
+ /* Released far outside the launcher: drop is a no-op, not an append. */
1360
+ drag.drop = null;
1361
+ hideDropLine();
1362
+ return;
1363
+ }
1364
+ const candidates = topLevelCandidates(drag.descriptor.key);
1365
+ let drop = null;
1366
+ for (const candidate of candidates) {
1367
+ if (candidate.el === undefined || typeof candidate.el.getBoundingClientRect !== 'function') continue;
1368
+ const rect = candidate.el.getBoundingClientRect();
1369
+ if (rect.width === 0 && rect.height === 0) continue;
1370
+ if (x < rect.left || x > rect.right || y < rect.top || y > rect.bottom) continue;
1371
+ const centerX = rect.left + rect.width / 2;
1372
+ const centerY = rect.top + rect.height / 2;
1373
+ const nearCenter = Math.abs(x - centerX) <= rect.width * 0.28 && Math.abs(y - centerY) <= rect.height * 0.32;
1374
+ if (nearCenter) {
1375
+ drop = { kind: 'merge', target: candidate };
1376
+ } else {
1377
+ drop = { kind: 'insert', index: candidates.indexOf(candidate) + (x < centerX ? 0 : 1), anchor: rect, after: x >= centerX };
1378
+ }
1379
+ break;
1380
+ }
1381
+ if (drop === null) {
1382
+ let index = candidates.length;
1383
+ for (let cursor = 0; cursor < candidates.length; cursor += 1) {
1384
+ const rect = candidates[cursor].el.getBoundingClientRect();
1385
+ if (y < rect.top) {
1386
+ index = cursor;
1387
+ break;
1388
+ }
1389
+ if (y <= rect.bottom && x < rect.left + rect.width / 2) {
1390
+ index = cursor;
1391
+ break;
1392
+ }
1393
+ }
1394
+ const anchor = index < candidates.length && candidates[index] !== undefined
1395
+ ? candidates[index].el.getBoundingClientRect()
1396
+ : (candidates.length > 0 ? candidates[candidates.length - 1].el.getBoundingClientRect() : null);
1397
+ drop = { kind: 'insert', index, anchor, after: index >= candidates.length };
1398
+ }
1399
+ drag.drop = drop;
1400
+ paintDrop(drop, candidates);
1401
+ }
1402
+
1403
+ function paintDrop(drop, candidates) {
1404
+ if (nav === null) return;
1405
+ for (const candidate of candidates) candidate.el.classList.remove('dsh-powerup-merge-target');
1406
+ if (drop.kind === 'merge') {
1407
+ hideDropLine();
1408
+ if (drop.target !== undefined && drop.target.el !== undefined) drop.target.el.classList.add('dsh-powerup-merge-target');
1409
+ return;
1410
+ }
1411
+ let rect = drop.anchor;
1412
+ let after = drop.after === true;
1413
+ if (rect === null || rect === undefined) {
1414
+ if (drop.index < candidates.length && candidates[drop.index] !== undefined) rect = candidates[drop.index].el.getBoundingClientRect();
1415
+ else if (candidates.length > 0) {
1416
+ rect = candidates[candidates.length - 1].el.getBoundingClientRect();
1417
+ after = true;
1418
+ }
1419
+ }
1420
+ if (rect === null || rect === undefined) {
1421
+ hideDropLine();
1422
+ return;
1423
+ }
1424
+ if (dropLine === null) {
1425
+ dropLine = document.createElement('div');
1426
+ dropLine.className = 'dsh-powerup-drop-line';
1427
+ dropLine.setAttribute('aria-hidden', 'true');
1428
+ nav.appendChild(dropLine);
1429
+ }
1430
+ const navRect = nav.getBoundingClientRect();
1431
+ dropLine.style.left = Math.round((after ? rect.right + 1 : rect.left - 1) - navRect.left) + 'px';
1432
+ dropLine.style.top = Math.round(rect.top - navRect.top) + 'px';
1433
+ dropLine.style.height = Math.round(rect.height) + 'px';
1434
+ dropLine.style.display = 'block';
1435
+ }
1436
+
1437
+ function endDrag() {
1438
+ document.removeEventListener('pointermove', onPointerMove, { capture: true, passive: false });
1439
+ document.removeEventListener('pointerup', onPointerUp, true);
1440
+ document.removeEventListener('pointercancel', onPointerUp, true);
1441
+ document.removeEventListener('keydown', onDragKey, true);
1442
+ if (drag !== null) {
1443
+ if (drag.ghost !== null) drag.ghost.remove();
1444
+ if (drag.descriptor !== undefined && drag.descriptor.gridEl !== undefined) drag.descriptor.gridEl.classList.remove('dsh-powerup-dragging');
1445
+ }
1446
+ hideDropLine();
1447
+ drag = null;
1448
+ }
1449
+
1450
+ function suppressNextClick() {
1451
+ const swallow = (event) => {
1452
+ if (typeof event.preventDefault === 'function') event.preventDefault();
1453
+ if (typeof event.stopPropagation === 'function') event.stopPropagation();
1454
+ };
1455
+ document.addEventListener('click', swallow, true);
1456
+ if (typeof window !== 'undefined' && window !== null && typeof window.setTimeout === 'function') {
1457
+ window.setTimeout(() => document.removeEventListener('click', swallow, true), 80);
1458
+ } else {
1459
+ document.removeEventListener('click', swallow, true);
1460
+ }
1461
+ }
1462
+
1463
+ function onPointerUp() {
1464
+ if (drag === null) return;
1465
+ const active = drag.active;
1466
+ const descriptor = drag.descriptor;
1467
+ const drop = drag.drop;
1468
+ endDrag();
1469
+ if (!active) return;
1470
+ suppressNextClick();
1471
+ if (drop !== null && drop.kind === 'merge' && drop.target !== undefined) {
1472
+ performMerge(descriptor.key, drop.target.key);
1473
+ } else if (drop !== null && drop.kind === 'insert') {
1474
+ performReorder(descriptor.key, drop.index);
1475
+ } else {
1476
+ sync();
1477
+ }
1478
+ }
1479
+
1480
+ function performReorder(key, index) {
1481
+ const order = state.order.filter((entry) => entry !== key);
1482
+ const clamped = Math.max(0, Math.min(index, order.length));
1483
+ order.splice(clamped, 0, key);
1484
+ state = { order: dedupeEntries(order), groups: state.groups };
1485
+ save();
1486
+ sync();
1487
+ }
1488
+
1489
+ function dedupeEntries(entries) {
1490
+ const seen = new Set();
1491
+ const result = [];
1492
+ for (const entry of entries) {
1493
+ if (typeof entry !== 'string' || seen.has(entry)) continue;
1494
+ seen.add(entry);
1495
+ result.push(entry);
1496
+ }
1497
+ return result;
1498
+ }
1499
+
1500
+ function performMerge(sourceKey, targetKey) {
1501
+ if (sourceKey === targetKey) return;
1502
+ const sourceGroupId = launcherEntryGroupId(sourceKey);
1503
+ const targetGroupId = launcherEntryGroupId(targetKey);
1504
+ if (sourceGroupId !== null && sourceGroupId === targetGroupId) return;
1505
+ const groups = { ...state.groups };
1506
+ let order = [...state.order];
1507
+ let sourceItems = [];
1508
+ let sourceName = '';
1509
+ if (sourceGroupId !== null) {
1510
+ const source = groups[sourceGroupId];
1511
+ if (source === undefined) return;
1512
+ sourceItems = [...source.items];
1513
+ sourceName = source.name;
1514
+ delete groups[sourceGroupId];
1515
+ } else {
1516
+ sourceItems = [sourceKey];
1517
+ }
1518
+ order = order.filter((entry) => entry !== sourceKey);
1519
+ if (targetGroupId !== null) {
1520
+ const target = groups[targetGroupId];
1521
+ if (target === undefined) return;
1522
+ const items = [...target.items];
1523
+ for (const key of sourceItems) if (!items.includes(key)) items.push(key);
1524
+ groups[targetGroupId] = { ...target, items, name: target.name !== '' ? target.name : sourceName };
1525
+ } else {
1526
+ const groupId = sourceGroupId !== null ? sourceGroupId : makeLauncherGroupId();
1527
+ const items = [];
1528
+ for (const key of sourceItems) if (!items.includes(key)) items.push(key);
1529
+ if (!items.includes(targetKey)) items.push(targetKey);
1530
+ groups[groupId] = { id: groupId, name: sourceName, items };
1531
+ const position = order.indexOf(targetKey);
1532
+ if (position >= 0) order[position] = launcherGroupEntry(groupId);
1533
+ else order.push(launcherGroupEntry(groupId));
1534
+ }
1535
+ state = { order: dedupeEntries(order), groups };
1536
+ save();
1537
+ sync();
1538
+ }
1539
+
1540
+ function start() {
1541
+ if (started || !hasDocument()) return;
1542
+ started = true;
1543
+ document.addEventListener('pointerdown', onPointerDown, true);
1544
+ const element = queryNav();
1545
+ if (element !== null) attachNav(element);
1546
+ else observeDocument();
1547
+ if (typeof window !== 'undefined' && window !== null && typeof window.setInterval === 'function') {
1548
+ healthTimer = window.setInterval(() => {
1549
+ if (!started) return;
1550
+ if (nav !== null && nav.isConnected === true) return;
1551
+ const next = queryNav();
1552
+ if (next !== null) attachNav(next);
1553
+ else if (docObserver === null) observeDocument();
1554
+ }, 2000);
1555
+ }
1556
+ sync();
1557
+ }
1558
+
1559
+ function stop() {
1560
+ started = false;
1561
+ if (queued !== 0) {
1562
+ try {
1563
+ if (typeof cancelAnimationFrame === 'function') cancelAnimationFrame(queued);
1564
+ else if (typeof window !== 'undefined' && window !== null && typeof window.clearTimeout === 'function') window.clearTimeout(queued);
1565
+ } catch {
1566
+ /* A cancelled frame is already gone. */
1567
+ }
1568
+ queued = 0;
1569
+ }
1570
+ if (healthTimer !== null) {
1571
+ try {
1572
+ if (typeof window.clearInterval === 'function') window.clearInterval(healthTimer);
1573
+ } catch {
1574
+ /* Nothing else to clear. */
1575
+ }
1576
+ healthTimer = null;
1577
+ }
1578
+ if (navObserver !== null) {
1579
+ navObserver.disconnect();
1580
+ navObserver = null;
1581
+ }
1582
+ if (docObserver !== null) {
1583
+ docObserver.disconnect();
1584
+ docObserver = null;
1585
+ }
1586
+ if (typeof document !== 'undefined' && document !== null && typeof document.removeEventListener === 'function') {
1587
+ document.removeEventListener('pointerdown', onPointerDown, true);
1588
+ }
1589
+ clearVisuals();
1590
+ state = { order: [], groups: {} };
1591
+ pendingLoad = true;
1592
+ nav = null;
1593
+ }
1594
+
1595
+ return {
1596
+ start,
1597
+ stop,
1598
+ sync,
1599
+ // Exposed for the smoke test only; the plugin never calls these.
1600
+ __debug: {
1601
+ getState: () => state,
1602
+ setState: (next) => {
1603
+ state = normalizeLauncherState(next, hostTiles.filter((tile) => !tile.isMore).map((tile) => tile.key));
1604
+ },
1605
+ getNav: () => nav,
1606
+ },
1607
+ };
1608
+ }
1609
+
1610
+ /**
1611
+ * Start the sidebar launcher upgrade. The core owns all DOM work; this
1612
+ * adapter supplies translations, the settings flag, storage, and the
1613
+ * registered panel ids that key the saved order and folders.
1614
+ * @param {object} ctx client plugin context
1615
+ * @returns {object | null} the core, or null when the host has no DOM
1616
+ */
1617
+ function installLauncher(ctx) {
1618
+ const listPanels = () => {
1619
+ try {
1620
+ const slots = ctx.get('slots');
1621
+ if (slots === undefined || slots === null || typeof slots.entriesOfSlot !== 'function') return [];
1622
+ const entries = slots.entriesOfSlot('sidebar.panellist');
1623
+ if (!Array.isArray(entries)) return [];
1624
+ return entries.map((entry) => {
1625
+ const options = entry?.options ?? {};
1626
+ let label;
1627
+ try {
1628
+ label = typeof options.label === 'function' ? options.label() : options.label;
1629
+ } catch {
1630
+ label = undefined;
1631
+ }
1632
+ return { id: options.id, label };
1633
+ });
1634
+ } catch {
1635
+ return [];
1636
+ }
1637
+ };
1638
+ const core = createLauncherCore({
1639
+ t: (key, params) => translateCurrent(key, params),
1640
+ format,
1641
+ isEnabled: () => getSettings().launcher !== false,
1642
+ read: () => {
1643
+ try {
1644
+ return localStorage.getItem(LAUNCHER_STORAGE_KEY);
1645
+ } catch {
1646
+ return null;
1647
+ }
1648
+ },
1649
+ write: (value) => {
1650
+ try {
1651
+ localStorage.setItem(LAUNCHER_STORAGE_KEY, value);
1652
+ } catch {
1653
+ /* The in-memory order still works for this page. */
1654
+ }
1655
+ },
1656
+ listPanels,
1657
+ });
1658
+ ctx.effect(() => {
1659
+ core.start();
1660
+ const off = subscribeSettings(() => core.sync());
1661
+ return () => {
1662
+ off();
1663
+ core.stop();
1664
+ };
1665
+ }, 'dsh-powerup: sidebar launcher');
1666
+ return core;
1667
+ }
1668
+
519
1669
  // ------------------------------------------------------------------
520
1670
  // Session-scoped bridge
521
1671
  // ------------------------------------------------------------------
@@ -567,6 +1717,27 @@
567
1717
  }, children);
568
1718
  }
569
1719
 
1720
+ function LauncherIcon({ size = 16 }) {
1721
+ return h(Icon, { size },
1722
+ h('rect', {
1723
+ x: '2.6', y: '2.6', width: '4.6', height: '4.6', rx: '1.3',
1724
+ stroke: 'currentColor', strokeWidth: '1.2',
1725
+ }),
1726
+ h('rect', {
1727
+ x: '8.8', y: '2.6', width: '4.6', height: '4.6', rx: '1.3',
1728
+ stroke: 'currentColor', strokeWidth: '1.2',
1729
+ }),
1730
+ h('rect', {
1731
+ x: '2.6', y: '8.8', width: '4.6', height: '4.6', rx: '1.3',
1732
+ stroke: 'currentColor', strokeWidth: '1.2',
1733
+ }),
1734
+ h('path', {
1735
+ d: 'M9.4 11.1h3.4M11.1 9.4v3.4',
1736
+ stroke: 'currentColor', strokeWidth: '1.2', strokeLinecap: 'round',
1737
+ }),
1738
+ );
1739
+ }
1740
+
570
1741
  function PowerUpLineIcon({ size = 16 }) {
571
1742
  return h(Icon, { size },
572
1743
  h('path', {
@@ -797,6 +1968,64 @@
797
1968
  // ------------------------------------------------------------------
798
1969
  // CSS — host tokens only (except the brand SVG artwork)
799
1970
  // ------------------------------------------------------------------
1971
+ /**
1972
+ * Sidebar launcher styles. Every rule is scoped under the marker class we
1973
+ * add to the host panel list, so the untouched host rules still describe
1974
+ * the collapsed 56px rail (we remove `.dsh-powerup-launcher-wide` there).
1975
+ * The grid uses `auto-fill` + `minmax`, so the column count follows the
1976
+ * sidebar width instead of a fixed number.
1977
+ */
1978
+ const LAUNCHER_CSS = `
1979
+ nav.dsh-powerup-launcher.dsh-powerup-launcher-wide{position:relative;display:grid!important;grid-template-columns:repeat(auto-fill,minmax(58px,1fr));grid-auto-rows:min-content;gap:2px;align-content:start;align-items:stretch;margin:0 0 10px;padding:2px 0}
1980
+ nav.dsh-powerup-launcher.dsh-powerup-launcher-wide>button,nav.dsh-powerup-launcher.dsh-powerup-launcher-wide>[data-powerup-more]>button,.dsh-powerup-folder{box-sizing:border-box;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:3px;width:auto;min-width:0;height:auto;min-height:52px;margin:0;padding:6px 2px 5px;border:0;border-radius:var(--dsw-radius-md,8px);background:transparent;color:var(--dsw-alias-label-primary);font:inherit;font-size:11px;line-height:14px;text-align:center;cursor:pointer;user-select:none;-webkit-user-select:none;transition:background .12s ease,color .12s ease,transform .12s ease}
1981
+ nav.dsh-powerup-launcher.dsh-powerup-launcher-wide>button:hover,nav.dsh-powerup-launcher.dsh-powerup-launcher-wide>[data-powerup-more]>button:hover,.dsh-powerup-folder:hover{background:var(--dsw-alias-interactive-bg-hover)}
1982
+ nav.dsh-powerup-launcher.dsh-powerup-launcher-wide>button:active,nav.dsh-powerup-launcher.dsh-powerup-launcher-wide>[data-powerup-more]>button:active,.dsh-powerup-folder:active{transform:scale(.96)}
1983
+ nav.dsh-powerup-launcher.dsh-powerup-launcher-wide>button[class*="panelActive"],nav.dsh-powerup-launcher.dsh-powerup-launcher-wide>[data-powerup-more]>button[aria-expanded=true]{background:var(--dsw-alias-interactive-bg-hover)}
1984
+ nav.dsh-powerup-launcher.dsh-powerup-launcher-wide>[data-powerup-more]{display:flex!important;flex-direction:column;min-width:0;margin:0}
1985
+ nav.dsh-powerup-launcher.dsh-powerup-launcher-wide [class*="panelGlyph"]{display:inline-flex;align-items:center;justify-content:center;width:24px;height:24px;flex:none}
1986
+ nav.dsh-powerup-launcher.dsh-powerup-launcher-wide [class*="panelGlyph"] svg{width:20px;height:20px}
1987
+ nav.dsh-powerup-launcher.dsh-powerup-launcher-wide [class*="panelTitle"]{display:block;width:100%;max-width:100%;color:var(--dsw-alias-label-secondary);font-size:11px;line-height:14px;font-weight:400;text-align:center;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
1988
+ nav.dsh-powerup-launcher.dsh-powerup-launcher-wide>button:hover [class*="panelTitle"]{color:var(--dsw-alias-label-primary)}
1989
+ .dsh-powerup-folder{position:relative;list-style:none;flex:none}
1990
+ .dsh-powerup-folder-stack{display:grid;grid-template-columns:repeat(2,1fr);grid-template-rows:repeat(2,1fr);gap:1px;width:24px;height:24px;padding:2px;box-sizing:border-box;border-radius:6px;background:color-mix(in srgb,var(--dsw-alias-label-primary) 9%,transparent);flex:none}
1991
+ .dsh-powerup-folder-mini{display:inline-flex;align-items:center;justify-content:center;min-width:0;min-height:0;overflow:hidden;color:var(--dsw-alias-label-primary);opacity:.85}
1992
+ .dsh-powerup-folder-mini svg{width:9px;height:9px}
1993
+ .dsh-powerup-folder-stack.is-single{grid-template-columns:1fr;grid-template-rows:1fr}
1994
+ .dsh-powerup-folder-stack.is-single .dsh-powerup-folder-mini svg{width:14px;height:14px}
1995
+ .dsh-powerup-folder-name{display:block;width:100%;max-width:100%;color:var(--dsw-alias-label-secondary);font-size:11px;line-height:14px;text-align:center;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
1996
+ .dsh-powerup-folder.is-active .dsh-powerup-folder-name{color:var(--dsw-alias-label-primary)}
1997
+ .dsh-powerup-folder.is-active .dsh-powerup-folder-stack{background:color-mix(in srgb,var(--dsw-alias-state-business-primary) 22%,transparent)}
1998
+ .dsh-powerup-dragging{opacity:.32}
1999
+ .dsh-powerup-merge-target{background:color-mix(in srgb,var(--dsw-alias-state-business-primary) 17%,transparent)!important;box-shadow:inset 0 0 0 1px var(--dsw-alias-state-business-primary)}
2000
+ .dsh-powerup-drop-line{position:absolute;z-index:6;display:none;width:2px;border-radius:999px;background:var(--dsw-alias-state-business-primary);box-shadow:0 0 0 1px color-mix(in srgb,var(--dsw-alias-state-business-primary) 26%,transparent);pointer-events:none}
2001
+ .dsh-powerup-drag-ghost{position:fixed;z-index:1800;box-sizing:border-box;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:3px;padding:6px 2px 5px;border-radius:var(--dsw-radius-md,8px);background:var(--dsw-alias-bg-layer-2);box-shadow:var(--dsw-elevation-prominent);color:var(--dsw-alias-label-primary);font-size:11px;line-height:14px;pointer-events:none;opacity:.96;transform:scale(1.05);transition:opacity .1s ease}
2002
+ .dsh-powerup-drag-ghost [class*="panelGlyph"]{display:inline-flex;align-items:center;justify-content:center;width:24px;height:24px;flex:none}
2003
+ .dsh-powerup-drag-ghost [class*="panelGlyph"] svg{width:20px;height:20px}
2004
+ .dsh-powerup-drag-ghost [class*="panelTitle"]{display:block;width:100%;max-width:100%;color:var(--dsw-alias-label-secondary);font-size:11px;line-height:14px;text-align:center;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
2005
+ .dsh-powerup-folder-pop{position:fixed;z-index:1800;box-sizing:border-box;display:flex;flex-direction:column;gap:6px;width:max-content;min-width:196px;max-width:min(440px,calc(100vw - 24px));max-height:min(72vh,440px);padding:8px;border:.5px solid var(--dsw-alias-border-l2);border-radius:var(--dsw-radius-lg,10px);background:var(--dsw-alias-bg-layer-2);box-shadow:var(--dsw-elevation-prominent);color:var(--dsw-alias-label-primary);font-size:12px;line-height:16px;opacity:0;transform:scale(.92);transition:opacity .13s ease,transform .17s cubic-bezier(.22,.9,.3,1.06)}
2006
+ .dsh-powerup-folder-pop.is-open{opacity:1;transform:scale(1)}
2007
+ .dsh-powerup-folder-pop-head{display:flex;align-items:center;gap:6px;min-width:0;padding:0 2px}
2008
+ .dsh-powerup-folder-pop-title{flex:1 1 auto;min-width:0;padding:2px 5px;border:0;border-radius:var(--dsw-radius-sm,6px);background:transparent;color:var(--dsw-alias-label-primary);font:inherit;font-size:12px;line-height:16px;font-weight:500;text-align:left;cursor:text;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
2009
+ .dsh-powerup-folder-pop-title:hover{background:var(--dsw-alias-interactive-bg-hover)}
2010
+ .dsh-powerup-folder-pop-input{flex:1 1 auto;min-width:0;height:22px;padding:0 5px;border:.5px solid var(--dsw-alias-state-business-primary);border-radius:var(--dsw-radius-sm,6px);background:var(--dsw-alias-bg-layer-3);color:var(--dsw-alias-label-primary);font:inherit;font-size:12px;line-height:20px;outline:none}
2011
+ .dsh-powerup-folder-pop-count{flex:none;color:var(--dsw-alias-label-tertiary);font-size:11px;white-space:nowrap}
2012
+ .dsh-powerup-folder-pop-action{flex:none;padding:2px 6px;border:0;border-radius:var(--dsw-radius-sm,6px);background:transparent;color:var(--dsw-alias-label-tertiary);font:inherit;font-size:11px;line-height:16px;cursor:pointer;white-space:nowrap}
2013
+ .dsh-powerup-folder-pop-action:hover{background:var(--dsw-alias-interactive-bg-hover);color:var(--dsw-alias-label-primary)}
2014
+ .dsh-powerup-folder-grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(76px,1fr));gap:2px;align-content:start;max-height:min(60vh,340px);overflow:auto;padding:2px}
2015
+ .dsh-powerup-folder-item{position:relative;box-sizing:border-box;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:3px;min-width:0;min-height:54px;padding:6px 2px 5px;border:0;border-radius:var(--dsw-radius-md,8px);background:transparent;color:var(--dsw-alias-label-primary);font:inherit;font-size:11px;line-height:14px;cursor:pointer;transition:background .12s ease}
2016
+ .dsh-powerup-folder-item:hover{background:var(--dsw-alias-interactive-bg-hover)}
2017
+ .dsh-powerup-folder-item:focus{outline:none}
2018
+ .dsh-powerup-folder-item:focus-visible{outline:var(--dsw-focus-ring-width,2px) solid var(--dsw-focus-ring-color,var(--dsw-alias-state-business-primary));outline-offset:-1px}
2019
+ .dsh-powerup-folder-item-glyph{display:inline-flex;align-items:center;justify-content:center;width:24px;height:24px;flex:none;color:var(--dsw-alias-label-secondary)}
2020
+ .dsh-powerup-folder-item-glyph svg{width:20px;height:20px}
2021
+ .dsh-powerup-folder-item-label{display:block;width:100%;max-width:100%;color:var(--dsw-alias-label-secondary);text-align:center;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
2022
+ .dsh-powerup-folder-item:hover .dsh-powerup-folder-item-label{color:var(--dsw-alias-label-primary)}
2023
+ .dsh-powerup-folder-item-remove{position:absolute;top:1px;right:2px;display:inline-flex;align-items:center;justify-content:center;width:15px;height:15px;padding:0;border:0;border-radius:50%;background:var(--dsw-alias-bg-layer-2);color:var(--dsw-alias-label-tertiary);font:inherit;font-size:11px;line-height:15px;cursor:pointer;opacity:0;transition:opacity .12s ease}
2024
+ .dsh-powerup-folder-item:hover .dsh-powerup-folder-item-remove,.dsh-powerup-folder-item-remove:focus-visible{opacity:1}
2025
+ .dsh-powerup-folder-item-remove:hover{color:var(--dsw-alias-label-primary)}
2026
+ @media (prefers-reduced-motion:reduce){nav.dsh-powerup-launcher.dsh-powerup-launcher-wide>button,nav.dsh-powerup-launcher.dsh-powerup-launcher-wide>[data-powerup-more]>button,.dsh-powerup-folder,.dsh-powerup-folder-pop,.dsh-powerup-drag-ghost,.dsh-powerup-drop-line{transition:none}}
2027
+ `;
2028
+
800
2029
  const CSS = `
801
2030
  .dsh-powerup-page{box-sizing:border-box;height:100%;overflow-y:auto;padding:28px 24px 52px;color:var(--dsw-alias-label-primary);font-size:13px;line-height:1.55;--pu-accent:var(--dsw-alias-brand-primary)}
802
2031
  .dsh-powerup-page *{box-sizing:border-box}
@@ -998,7 +2227,7 @@
998
2227
  try {
999
2228
  const style = document.createElement('style');
1000
2229
  style.setAttribute('data-dsh-powerup', '');
1001
- style.textContent = CSS;
2230
+ style.textContent = CSS + '\n' + LAUNCHER_CSS;
1002
2231
  document.head.appendChild(style);
1003
2232
  ctx.effect(() => () => style.remove(), 'dsh-powerup: styles');
1004
2233
  } catch (error) {
@@ -3946,6 +5175,18 @@
3946
5175
  ),
3947
5176
  ),
3948
5177
  ),
5178
+ h('section', { className: 'dsh-powerup-card' },
5179
+ h('div', { className: 'dsh-powerup-card-head' },
5180
+ h('div', { className: 'dsh-powerup-card-icon' }, h(LauncherIcon, { size: 18 })),
5181
+ h('div', { className: 'dsh-powerup-card-copy' },
5182
+ h('h2', null, t('page.launcherTitle')),
5183
+ h('p', null, t('page.launcherDesc')),
5184
+ ),
5185
+ ),
5186
+ h('div', { className: 'dsh-powerup-settings' },
5187
+ settingRow('launcher', settings.launcher !== false, t('page.launcherToggle'), t('page.launcherToggleHint')),
5188
+ ),
5189
+ ),
3949
5190
  h('section', { className: 'dsh-powerup-card' },
3950
5191
  h('div', { className: 'dsh-powerup-card-head' },
3951
5192
  h('div', { className: 'dsh-powerup-card-icon' }, h(QuickIcon, { size: 18 })),
@@ -4035,6 +5276,7 @@
4035
5276
  function apply(ctx) {
4036
5277
  bindLocale(ctx);
4037
5278
  installStyles(ctx);
5279
+ installLauncher(ctx);
4038
5280
 
4039
5281
  const slots = ctx.get('slots');
4040
5282
  if (slots === undefined || typeof slots.inject !== 'function' || typeof slots.register !== 'function') {
@@ -4126,7 +5368,13 @@
4126
5368
  __test: {
4127
5369
  DICT,
4128
5370
  CSS,
5371
+ LAUNCHER_CSS,
5372
+ LAUNCHER_STORAGE_KEY,
5373
+ normalizeLauncherState,
5374
+ createLauncherCore,
5375
+ installLauncher,
4129
5376
  PowerUpLineIcon,
5377
+ LauncherIcon,
4130
5378
  UpdateIcon,
4131
5379
  QuickIcon,
4132
5380
  PowerUpBridge,