[
  { "type": "button",
    "init": "(button) => {
      let view;
      function showLabel() {
        button.text = (view.keyMode == King.LocalMode) ? qsTr('Local Mode') : qsTr('Remote Mode');
        button.visible = true;
      }
      function update(activeView) {
        if (view != activeView) {
          if (view) view.keyModeChanged.disconnect(showLabel);
          if (view = activeView) showLabel(); else button.visible = false;
          if (view) view.keyModeChanged.connect(showLabel);
        }
      }
      function changeMode() {
        if (view = terminal.activeView()) {
          view.keyMode = (view.keyMode == King.LocalMode) ? King.RemoteMode : King.LocalMode;
        }
      }
      button.clicked.connect(changeMode);
      terminal.viewActivated.connect(update);
      update(terminal.view());
    }"
  },
  { "type": "button",
    "init": "(button) => {
      let view;
      function showButton(rows, columns) {
        button.text = qsTr('Window %1×%2').arg(rows | 0).arg(columns | 0);
        button.visible = true;
      }
      function update(activeView) {
        if (view != activeView) {
          if (view) view.screenSizeChanged.disconnect(showButton);
          if (view = activeView) showButton(view.windowRows, view.windowColumns); else button.visible = false;
          if (view) view.screenSizeChanged.connect(showButton);
        }
      }
      terminal.viewActivated.connect(update);
      update(terminal.view());
    }"
  },
  { "type": "button",
    "init": "(button) => {
      let view;
      function pad(count) {
        let spaces = '  ';
        return count + spaces.substring(0, spaces.length - String(count).length);
      }
      function showButton() {
        button.text = qsTr('Ln %1 Ch %2').arg(pad(view.caretLine + 1)).arg(pad(view.caretLineOffset));
        button.visible = true;
      }
      function update(activeView) {
        if (view != activeView) {
          if (view) view.caretPositionChanged.disconnect(showButton);
          if (view = activeView) showButton(); else button.visible = false;
          if (view) view.caretPositionChanged.connect(showButton);
        }
      }
      terminal.viewActivated.connect(update);
      update(terminal.view());
    }"
  },
  { "type": "button",
    "init": "(button) => {
      let view;
      let action = window.findAction('Terminal.SchemeList');

      function showButton() {
        button.text = view.scheme();
        button.visible = true;
      }
      function update(activeView) {
        if (view != activeView) {
          if (view) view.schemeChanged.disconnect(showButton);
          if (view = activeView) showButton(); else button.visible = false;
          if (view) view.schemeChanged.connect(showButton);
        }
      }
      button.popupMode = King.InstantPopup;
      button.menu = window.createMenu(button);

      button.menu.addAction(action);

      terminal.viewActivated.connect(update);
      update(terminal.view());
    }"
  },
  { "type": "label",
    "init": "(label) => {
      window.setInterval(label, () => {
        let date = new Date();
        label.text = date.toLocaleString();
      }, 2000);
    }"
  },
  { "type": "label",
    "init": "(label) => {
      label.text = '<a href=\"https://www.github.com/kingtoolbox/windterm/issues\" style=\"text-decoration:none;\">🐛 WindTerm Issues</a>';
    }"
  },
  { "type": "button",
    "init": "(button) => {
      button.text = '🔒 ' + qsTr('Lock Screen')
      button.visible = true;

      button.clicked.connect(function() {
        window.lockScreen();
      });
    }"
  }
]