





















(function () {
  'use strict';

  var T = function (k, d) { return window.VizI18n ? VizI18n.tr(k, d) : d; };

  var el = {};
  var open = false;

  

  var IS_MAC = /Mac|iP(hone|ad|od)/.test(navigator.platform || navigator.userAgent || '');

  

  var MODKEY = IS_MAC
    ? { MOD: '⌘', ALT: '⌥', SHIFT: '⇧' }
    : { MOD: 'Ctrl', ALT: 'Alt', SHIFT: 'Shift' };

  function token(name) {
    if (MODKEY[name]) return MODKEY[name];
    switch (name) {
      case 'SPACE': return T('viz_sc_k_space', '空格');
      case 'ENTER': return T('viz_sc_k_enter', '回车');
      case 'CLICK': return T('viz_sc_k_click', '点目标点');
      case 'LMB':   return T('viz_sc_k_lmb', '鼠标左键拖');
      case 'MMB':   return T('viz_sc_k_mmb', '鼠标中键拖');
      case 'WHEEL': return T('viz_sc_k_wheel', '滚轮');
      case 'RMB':   return T('viz_sc_k_rmb', '鼠标右键');
      default:      return name;
    }
  }

  function fmt(tpl) {
    return tpl.replace(/\{(\w+)\}/g, function (_, n) { return token(n); });
  }

  


  var GROUPS = [
    { t: ['viz_sc_grp_edit', '编辑'], rows: [
      ['{MOD} + S',      ['viz_sc_save',       '保存场景（未保存过则弹另存为）']],
      ['{ALT}',          ['viz_sc_alt_drag',   '按住不放：临时进入末端拖拽，松开即退出']],
      ['{MOD} + {CLICK}', ['viz_sc_ctrl_pick', '逐个多选目标点（用于右键「创建运动」）']],
      ['{SHIFT} + {CLICK}', ['viz_sc_shift_pick', '连选：从上次点的那条到这条，中间全选上']]
    ]},
    { t: ['viz_sc_grp_capture', '新增目标点时（点目标点区的「+」之后）'], rows: [
      ['{SPACE}', ['viz_sc_space', '记录当前位置为一个目标点']],
      ['{ENTER}', ['viz_sc_enter', '结束记录（手扶真机取点会同时重新使能电机）']]
    ]},
    { t: ['viz_sc_grp_view', '视图操作'], rows: [
      ['{LMB}',   ['viz_sc_rotate',  '旋转视角']],
      ['{MMB}',   ['viz_sc_pan',     '平移视角']],
      ['{WHEEL}', ['viz_sc_zoom',    '缩放视图']],
      ['{RMB}',   ['viz_sc_ctxmenu', '视口菜单（坐标系显隐、工具安装角标定等）']]
    ]}
  ];

  

  function injectStyle() {
    var css = [
      

      '#sc-backdrop{position:fixed;top:0;right:0;bottom:0;left:0;',
      '  z-index:46;background:rgba(0,0,0,.45);display:none;}',
      
      '#sc-pop{position:fixed;top:0;right:0;bottom:0;left:0;',
      '  margin:auto;height:fit-content;max-height:88vh;overflow:auto;',
      '  z-index:47;width:520px;max-width:92vw;',
      '  background:rgba(28,31,36,.98);border:1px solid #4da3ff;border-radius:6px;',
      '  box-shadow:0 6px 18px rgba(0,0,0,.5);display:none;font-size:12px;color:#c5c8c6;}',
      '#sc-pop .hd{display:flex;align-items:center;gap:8px;padding:12px 14px 6px;}',
      '#sc-pop h3{margin:0;flex:1 1 auto;font-size:13px;color:#ffcc44;font-weight:600;}',
      '#sc-pop .x{flex:0 0 auto;background:none;border:0;color:#8f979e;font-size:16px;',
      '  line-height:1;padding:0 4px;cursor:pointer;}',
      '#sc-pop .x:hover{color:#c5c8c6;}',
      '#sc-pop .note{padding:0 14px 8px;color:#8f979e;font-size:11px;line-height:1.5;}',
      '#sc-pop table{width:100%;border-collapse:collapse;margin:0 0 12px;}',
      '#sc-pop td{padding:5px 14px;vertical-align:top;line-height:1.5;}',
      

      '#sc-pop td.k{width:150px;white-space:nowrap;color:#9fd0ff;',
      '  font-family:Consolas,Monaco,monospace;}',
      '#sc-pop tr.grp td{padding-top:10px;color:#ffcc44;font-size:11px;',
      '  border-top:1px solid #3a4148;}',
      '#sc-pop tr.grp:first-child td{border-top:0;padding-top:2px;}'
    ].join('\n');
    var s = document.createElement('style');
    s.textContent = css;
    document.head.appendChild(s);
  }

  

  function build() {
    injectStyle();

    el.backdrop = document.createElement('div');
    el.backdrop.id = 'sc-backdrop';
    el.backdrop.addEventListener('click', close);

    el.pop = document.createElement('div');
    el.pop.id = 'sc-pop';

    var hd = document.createElement('div');
    hd.className = 'hd';
    el.title = document.createElement('h3');
    var x = document.createElement('button');
    x.className = 'x';
    x.textContent = '×';
    x.addEventListener('click', close);
    hd.appendChild(el.title);
    hd.appendChild(x);

    el.note = document.createElement('div');
    el.note.className = 'note';
    el.table = document.createElement('table');

    el.pop.appendChild(hd);
    el.pop.appendChild(el.note);
    el.pop.appendChild(el.table);

    document.body.appendChild(el.backdrop);
    document.body.appendChild(el.pop);

    

    document.addEventListener('keydown', function (ev) {
      if (!open) return;
      if (ev.key === 'Escape') {
        ev.stopPropagation();
        ev.preventDefault();
        close();
      }
    }, true);
  }

  function render() {
    if (!el.pop) return;
    el.title.textContent = T('viz_sc_title', '快捷键');
    el.note.textContent = T('viz_sc_note', '以下快捷键仅在「3D 可视化与离线编程」页生效。');
    el.table.innerHTML = '';
    GROUPS.forEach(function (g) {
      var gtr = el.table.insertRow();
      gtr.className = 'grp';
      var gtd = gtr.insertCell();
      gtd.colSpan = 2;
      gtd.textContent = T(g.t[0], g.t[1]);
      g.rows.forEach(function (r) {
        var tr = el.table.insertRow();
        var k = tr.insertCell();
        k.className = 'k';
        k.textContent = fmt(r[0]);
        tr.insertCell().textContent = T(r[1][0], r[1][1]);
      });
    });
  }

  function show() {
    if (!el.pop) return;
    render();                       
    el.backdrop.style.display = 'block';
    el.pop.style.display = 'block';
    open = true;
  }

  function close() {
    if (!el.pop) return;
    el.backdrop.style.display = 'none';
    el.pop.style.display = 'none';
    open = false;
  }

  window.ShortcutsUI = {
    init: function () {
      build();
      var btn = document.getElementById('btn-help');
      
      if (btn) btn.addEventListener('click', function () { open ? close() : show(); });
    },
    

    refresh: function () { if (open) render(); },
    open: show,
    close: close,
    isOpen: function () { return open; }
  };
})();
