DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

this.EXPORTED_SYMBOLS = ["PanelMultiView"];

const {classes: Cc, interfaces: Ci, utils: Cu} = Components;

Cu.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "CustomizableWidgets",
  "resource:///modules/CustomizableWidgets.jsm");

/**
 * Simple implementation of the sliding window pattern; panels are added to a
 * linked list, in-order, and the currently shown panel is remembered using a
 * marker. The marker shifts as navigation between panels is continued, where
 * the panel at index 0 is always the starting point:
 *           ┌────┬────┬────┬────┐
 *           │▓▓▓▓│    │    │    │ Start
 *           └────┴────┴────┴────┘
 *      ┌────┬────┬────┬────┐
 *      │    │▓▓▓▓│    │    │      Forward
 *      └────┴────┴────┴────┘
 * ┌────┬────┬────┬────┐
 * │    │    │▓▓▓▓│    │           Forward
 * └────┴────┴────┴────┘
 *      ┌────┬────┬────┬────┐
 *      │    │▓▓▓▓│    │    │      Back
 *      └────┴────┴────┴────┘
 */
class SlidingPanelViews extends Array {
  constructor() {
    super();
    this._marker = 0;
  }

  /**
   * Get the index that points to the currently selected view.
   *
   * @return {Number}
   */
  get current() {
    return this._marker;
  }

  /**
   * Setter for the current index, which changes the order of elements and
   * updates the internal marker for the currently selected view.
   * We're manipulating the array directly to have it reflect the order of
   * navigation, instead of continuously growing the array with the next selected
   * view to keep memory usage within reasonable proportions. With this method,
   * the data structure grows no larger than the number of panels inside the
   * panelMultiView.
   *
   * @param  {Number} index Index of the item to move to the current position.
   * @return {Number} The new marker index.
   */
  set current(index) {
    if (index == this._marker) {
      // Never change a winning team.
      return index;
    }
    if (index == -1 || index > (this.length - 1)) {
      throw new Error(`SlidingPanelViews :: index ${index} out of bounds`);
    }

    let view = this.splice(index, 1)[0];
    if (this._marker > index) {
      // Correct the current marker if the view-to-select was removed somewhere
      // before it.
      --this._marker;
    }
    // Then add the view-to-select right after the currently selected view.
    this.splice(++this._marker, 0, view);
    return this._marker;
  }

  /**
   * Getter for the currently selected view node.
   *
   * @return {panelview}
   */
  get currentView() {
    return this[this._marker];
  }

  /**
   * Setter for the currently selected view node.
   *
   * @param  {panelview} view
   * @return {Number} Index of the currently selected view.
   */
  set currentView(view) {
    if (!view)
      return this.current;
    // This will throw an error if the view could not be found.
    return this.current = this.indexOf(view);
  }

  /**
   * Getter for the previous view, which is always positioned one position after
   * the current view.
   *
   * @return {panelview}
   */
  get previousView() {
    return this[this._marker + 1];
  }

  /**
   * Going back is an explicit action on the data structure, moving the marker
   * one step back.
   *
   * @return {Array} A list of two items: the newly selected view and the previous one.
   */
  back() {
    if (this._marker > 0)
      --this._marker;
    return [this.currentView, this.previousView];
  }

  /**
   * Reset the data structure to its original construct, removing all references
   * to view nodes.
   */
  clear() {
    this._marker = 0;
    this.splice(0, this.length);
  }
}

/**
 * This is the implementation of the panelUI.xml XBL binding, moved to this
 * module, to make it easier to fork the logic for the newer photon structure.
 * Goals are:
 * 1. to make it easier to programmatically extend the list of panels,
 * 2. allow for navigation between panels multiple levels deep and
 * 3. maintain the pre-photon structure with as little effort possible.
 *
 * @type {PanelMultiView}
 */
this.PanelMultiView = class {
  get document() {
    return this.node.ownerDocument;
  }

  get window() {
    return this.node.ownerGlobal;
  }

  get _panel() {
    return this.node.parentNode;
  }

  get showingSubView() {
    return this.node.getAttribute("viewtype") == "subview";
  }
  get _mainViewId() {
    return this.node.getAttribute("mainViewId");
  }
  set _mainViewId(val) {
    this.node.setAttribute("mainViewId", val);
    return val;
  }
  get _mainView() {
    return this._mainViewId ? this.document.getElementById(this._mainViewId) : null;
  }
  get showingSubViewAsMainView() {
    return this.node.getAttribute("mainViewIsSubView") == "true";
  }

  get ignoreMutations() {
    return this._ignoreMutations;
  }
  set ignoreMutations(val) {
    this._ignoreMutations = val;
    if (!val && this._panel.state == "open") {
      if (this.showingSubView) {
        this._syncContainerWithSubView();
      } else {
        this._syncContainerWithMainView();
      }
    }
  }

  get _transitioning() {
    return this.__transitioning;
  }
  set _transitioning(val) {
    this.__transitioning = val;
    if (val) {
      this.node.setAttribute("transitioning", "true");
    } else {
      this.node.removeAttribute("transitioning");
    }
  }

  get panelViews() {
    // If there's a dedicated subViews container, we're not in the right binding
    // to use SlidingPanelViews.
    if (this._subViews)
      return null;

    if (this._panelViews)
      return this._panelViews;

    this._panelViews = new SlidingPanelViews();
    this._panelViews.push(...this.node.getElementsByTagName("panelview"));
    return this._panelViews;
  }
  get _dwu() {
    if (this.__dwu)
      return this.__dwu;
    return this.__dwu = this.window.QueryInterface(Ci.nsIInterfaceRequestor)
                                   .getInterface(Ci.nsIDOMWindowUtils);
  }
  get _currentSubView() {
    return this.panelViews ? this.panelViews.currentView : this.__currentSubView;
  }
  set _currentSubView(panel) {
    if (this.panelViews)
      this.panelViews.currentView = panel;
    else
      this.__currentSubView = panel;
    return panel;
  }
  get _keyNavigationMap() {
    if (!this.__keyNavigationMap)
      this.__keyNavigationMap = new Map();
    return this.__keyNavigationMap;
  }

  constructor(xulNode) {
    this.node = xulNode;

    this._currentSubView = this._anchorElement = this._subViewObserver = null;
    this._mainViewHeight = 0;
    this.__transitioning = this._ignoreMutations = false;

    const {document, window} = this;

    this._clickCapturer =
      document.getAnonymousElementByAttribute(this.node, "anonid", "clickCapturer");
    this._viewContainer =
      document.getAnonymousElementByAttribute(this.node, "anonid", "viewContainer");
    this._mainViewContainer =
      document.getAnonymousElementByAttribute(this.node, "anonid", "mainViewContainer");
    this._subViews =
      document.getAnonymousElementByAttribute(this.node, "anonid", "subViews");
    this._viewStack =
      document.getAnonymousElementByAttribute(this.node, "anonid", "viewStack");

    this._panel.addEventListener("popupshowing", this);
    this._panel.addEventListener("popuphidden", this);
    if (this.panelViews) {
      let cs = window.getComputedStyle(document.documentElement);
      // Set CSS-determined attributes now to prevent a layout flush when we do
      // it when transitioning between panels.
      this._dir = cs.direction;
      this.setMainView(this.panelViews.currentView);
      this.showMainView();
    } else {
      this._panel.addEventListener("popupshown", this);
      this._clickCapturer.addEventListener("click", this);
      this._subViews.addEventListener("overflow", this);
      this._mainViewContainer.addEventListener("overflow", this);

      // Get a MutationObserver ready to react to subview size changes. We
      // only attach this MutationObserver when a subview is being displayed.
      this._subViewObserver = new window.MutationObserver(this._syncContainerWithSubView.bind(this));
      this._mainViewObserver = new window.MutationObserver(this._syncContainerWithMainView.bind(this));

      this._mainViewContainer.setAttribute("panelid", this._panel.id);

      if (this._mainView) {
        this.setMainView(this._mainView);
      }
    }

    this.node.setAttribute("viewtype", "main");

    // Proxy these public properties and methods, as used elsewhere by various
    // parts of the browser, to this instance.
    ["_mainView", "ignoreMutations", "showingSubView"].forEach(property => {
      Object.defineProperty(this.node, property, {
        enumerable: true,
        get: () => this[property],
        set: (val) => this[property] = val
      });
    });
    ["goBack", "setHeightToFit", "setMainView", "showMainView", "showSubView"].forEach(method => {
      Object.defineProperty(this.node, method, {
        enumerable: true,
        value: (...args) => this[method](...args)
      });
    });
  }

  destructor() {
    if (this._mainView) {
      this._mainView.removeAttribute("mainview");
    }
    if (this.panelViews) {
      this.panelViews.clear();
    } else {
      this._mainViewObserver.disconnect();
      this._subViewObserver.disconnect();
      this._subViews.removeEventListener("overflow", this);
      this._mainViewContainer.removeEventListener("overflow", this);
      this._clickCapturer.removeEventListener("click", this);
    }
    this._panel.removeEventListener("popupshowing", this);
    this._panel.removeEventListener("popupshown", this);
    this._panel.removeEventListener("popuphidden", this);
    this.node = this._clickCapturer = this._viewContainer = this._mainViewContainer =
      this._subViews = this._viewStack = this.__dwu = null;
  }

  goBack(target) {
    let [current, previous] = this.panelViews.back();
    return this.showSubView(current, target, previous);
  }

  /**
   * Checks whether it is possible to navigate backwards currently.
   * Since the visibility of the back button is dependent - right now - on the
   * fact that there's a view title set, we use that heuristic to determine this
   * capability.
   *
   * @param  {panelview} view View to check, defaults to the currently active view.
   * @return {Boolean}
   */
  _canGoBack(view = this._currentSubView) {
    return !!view.getAttribute("title");
  }

  setMainView(aNewMainView) {
    if (this.panelViews) {
      // If the new main view is not yet in the zeroth position, make sure it's
      // inserted there.
      if (aNewMainView.parentNode != this._viewStack && this._viewStack.firstChild != aNewMainView) {
        this._viewStack.insertBefore(aNewMainView, this._viewStack.firstChild);
      }
    } else {
      if (this._mainView) {
        this._mainViewObserver.disconnect();
        this._subViews.appendChild(this._mainView);
        this._mainView.removeAttribute("mainview");
      }
      this._mainViewId = aNewMainView.id;
      aNewMainView.setAttribute("mainview", "true");
      this._mainViewContainer.appendChild(aNewMainView);
    }
  }

  showMainView() {
    if (this.panelViews) {
      this.showSubView(this._mainViewId);
    } else {
      if (this.showingSubView) {
        let viewNode = this._currentSubView;
        let evt = new this.window.CustomEvent("ViewHiding", { bubbles: true, cancelable: true });
        viewNode.dispatchEvent(evt);

        viewNode.removeAttribute("current");
        this._currentSubView = null;

        this._subViewObserver.disconnect();

        this._setViewContainerHeight(this._mainViewHeight);

        this.node.setAttribute("viewtype", "main");
      }

      this._shiftMainView();
    }
  }

  showSubView(aViewId, aAnchor, aPreviousView, aAdopted = false) {
    const {document, window} = this;
    return (async () => {
      // Support passing in the node directly.
      let viewNode = typeof aViewId == "string" ? this.node.querySelector("#" + aViewId) : aViewId;
      if (!viewNode) {
        viewNode = document.getElementById(aViewId);
        if (viewNode) {
          if (this.panelViews) {
            this._viewStack.appendChild(viewNode);
            this.panelViews.push(viewNode);
          } else {
            this._subViews.appendChild(viewNode);
          }
        } else {
          throw new Error(`Subview ${aViewId} doesn't exist!`);
        }
      }

      let reverse = !!aPreviousView;
      let previousViewNode = aPreviousView || this._currentSubView;
      let playTransition = (!!previousViewNode && previousViewNode != viewNode);

      let dwu, previousRect;
      if (playTransition || this.panelViews) {
        dwu = this._dwu;
        previousRect = previousViewNode.__lastKnownBoundingRect =
          dwu.getBoundsWithoutFlushing(previousViewNode);
        if (this.panelViews && !this._mainViewWidth) {
          this._mainViewWidth = previousRect.width;
          let top = dwu.getBoundsWithoutFlushing(previousViewNode.firstChild).top;
          let bottom = dwu.getBoundsWithoutFlushing(previousViewNode.lastChild).bottom;
          this._viewVerticalPadding = previousRect.height - (bottom - top);
        }
      }

      // Emit the ViewShowing event so that the widget definition has a chance
      // to lazily populate the subview with things.
      let detail = {
        blockers: new Set(),
        addBlocker(aPromise) {
          this.blockers.add(aPromise);
        },
      };

      // Make sure that new panels always have a title set.
      if (this.panelViews && aAdopted && aAnchor) {
        if (aAnchor && !viewNode.hasAttribute("title"))
          viewNode.setAttribute("title", aAnchor.getAttribute("label"));
        viewNode.classList.add("PanelUI-subView");
        let custWidget = CustomizableWidgets.find(widget => widget.viewId == viewNode.id);
        if (custWidget) {
          if (custWidget.onInit)
            custWidget.onInit(aAnchor);
          custWidget.onViewShowing({ target: aAnchor, detail });
        }
      }
      viewNode.setAttribute("current", true);
      if (playTransition && this.panelViews)
        viewNode.style.maxWidth = viewNode.style.minWidth = this._mainViewWidth + "px";

      let evt = new window.CustomEvent("ViewShowing", { bubbles: true, cancelable: true, detail });
      viewNode.dispatchEvent(evt);

      let cancel = evt.defaultPrevented;
      if (detail.blockers.size) {
        try {
          let results = await Promise.all(detail.blockers);
          cancel = cancel || results.some(val => val === false);
        } catch (e) {
          Cu.reportError(e);
          cancel = true;
        }
      }

      if (cancel) {
        return;
      }

      this._currentSubView = viewNode;

      // Now we have to transition the panel. There are a few parts to this:
      //
      // 1) The main view content gets shifted so that the center of the anchor
      //    node is at the left-most edge of the panel.
      // 2) The subview deck slides in so that it takes up almost all of the
      //    panel.
      // 3) If the subview is taller then the main panel contents, then the panel
      //    must grow to meet that new height. Otherwise, it must shrink.
      //
      // All three of these actions make use of CSS transformations, so they
      // should all occur simultaneously.
      this.node.setAttribute("viewtype", "subview");

      if (this.panelViews && playTransition) {
        // Sliding the next subview in means that the previous panelview stays
        // where it is and the active panelview slides in from the left in LTR
        // mode, right in RTL mode.
        let onTransitionEnd = () => {
          evt = new window.CustomEvent("ViewHiding", { bubbles: true, cancelable: true });
          previousViewNode.dispatchEvent(evt);
          previousViewNode.removeAttribute("current");
        };

        // There's absolutely no need to show off our epic animation skillz when
        // the panel's not even open.
        if (this._panel.state != "open") {
          onTransitionEnd();
          return;
        }

        if (aAnchor)
          aAnchor.setAttribute("open", true);
        this._viewContainer.style.height = previousRect.height + "px";
        this._viewContainer.style.width = previousRect.width + "px";

        this._transitioning = true;
        this._viewContainer.setAttribute("transition-reverse", reverse);
        let nodeToAnimate = reverse ? previousViewNode : viewNode;

        if (!reverse) {
          // We set the margin here to make sure the view is positioned next
          // to the view that is currently visible. The animation is taken
          // care of by transitioning the `transform: translateX()` property
          // instead.
          // Once the transition finished, we clean both properties up.
          nodeToAnimate.style.marginInlineStart = previousRect.width + "px";
        }

        // Set the transition style and listen for its end to clean up and
        // make sure the box sizing becomes dynamic again.
        // Somehow, putting these properties in PanelUI.css doesn't work for
        // newly shown nodes in a XUL parent node.
        nodeToAnimate.style.transition = "transform ease-" + (reverse ? "in" : "out") +
          " var(--panelui-subview-transition-duration)";
        nodeToAnimate.style.willChange = "transform";
        nodeToAnimate.style.borderInlineStart = "1px solid var(--panel-separator-color)";

        // Wait until after the first paint to ensure setting 'current=true'
        // has taken full effect; once both views are visible, we want to
        // correctly measure rects using `dwu.getBoundsWithoutFlushing`.
        window.addEventListener("MozAfterPaint", () => {
          let viewRect = viewNode.__lastKnownBoundingRect;
          if (!viewRect) {
            viewRect = dwu.getBoundsWithoutFlushing(viewNode);
            if (!reverse) {
              // When showing two nodes at the same time (one partly out of view,
              // but that doesn't seem to make a difference in this case) inside
              // a XUL node container, the flexible box layout on the vertical
              // axis gets confused. I.e. it lies.
              // So what we need to resort to here is count the height of each
              // individual child element of the view.
              viewRect.height = [viewNode.header, ...viewNode.children].reduce((acc, node) => {
                return acc + dwu.getBoundsWithoutFlushing(node).height;
              }, this._viewVerticalPadding);
            }
          }

          // Set the viewContainer dimensions to make sure only the current view
          // is visible.
          this._viewContainer.style.height = viewRect.height + "px";
          this._viewContainer.style.width = viewRect.width + "px";

          // The 'magic' part: build up the amount of pixels to move right or left.
          let moveToLeft = (this._dir == "rtl" && !reverse) || (this._dir == "ltr" && reverse);
          let movementX = reverse ? viewRect.width : previousRect.width;
          let moveX = (moveToLeft ? "" : "-") + movementX;
          nodeToAnimate.style.transform = "translateX(" + moveX + "px)";
          // We're setting the width property to prevent flickering during the
          // sliding animation with smaller views.
          nodeToAnimate.style.width = viewRect.width + "px";

          let listener;
          let seen = 0;
          this._viewContainer.addEventListener("transitionend", listener = ev => {
            if (ev.target == this._viewContainer && ev.propertyName == "height") {
              // Myeah, panel layout auto-resizing is a funky thing. We'll wait
              // another few milliseconds to remove the width and height 'fixtures',
              // to be sure we don't flicker annoyingly.
              // NB: HACK! Bug 1363756 is there to fix this.
              window.setTimeout(() => {
                this._viewContainer.style.removeProperty("height");
                this._viewContainer.style.removeProperty("width");
              }, 500);
              ++seen;
            } else if (ev.target == nodeToAnimate && ev.propertyName == "transform") {
              onTransitionEnd();
              this._transitioning = false;
              this._resetKeyNavigation(previousViewNode);

              // Take another breather, just like before, to wait for the 'current'
              // attribute removal to take effect. This prevents a flicker.
              // The cleanup we do doesn't affect the display anymore, so we're not
              // too fussed about the timing here.
              window.addEventListener("MozAfterPaint", () => {
                nodeToAnimate.style.removeProperty("border-inline-start");
                nodeToAnimate.style.removeProperty("transition");
                nodeToAnimate.style.removeProperty("transform");
                nodeToAnimate.style.removeProperty("width");

                if (!reverse)
                  viewNode.style.removeProperty("margin-inline-start");
                if (aAnchor)
                  aAnchor.removeAttribute("open");

                this._viewContainer.removeAttribute("transition-reverse");
              }, { once: true });
              ++seen;
            }
            if (seen == 2)
              this._viewContainer.removeEventListener("transitionend", listener);
          });
        }, { once: true });
      } else if (!this.panelViews) {
        this._shiftMainView(aAnchor);

        this._mainViewHeight = this._viewStack.clientHeight;

        let newHeight = this._heightOfSubview(viewNode, this._subViews);
        this._setViewContainerHeight(newHeight);

        this._subViewObserver.observe(viewNode, {
          attributes: true,
          characterData: true,
          childList: true,
          subtree: true
        });
      }
    })();
  }

  _setViewContainerHeight(aHeight) {
    let container = this._viewContainer;
    this._transitioning = true;

    let onTransitionEnd = () => {
      container.removeEventListener("transitionend", onTransitionEnd);
      this._transitioning = false;
    };

    container.addEventListener("transitionend", onTransitionEnd);
    container.style.height = `${aHeight}px`;
  }

  _shiftMainView(aAnchor) {
    if (aAnchor) {
      // We need to find the edge of the anchor, relative to the main panel.
      // Then we need to add half the width of the anchor. This is the target
      // that we need to transition to.
      let anchorRect = aAnchor.getBoundingClientRect();
      let mainViewRect = this._mainViewContainer.getBoundingClientRect();
      let center = aAnchor.clientWidth / 2;
      let direction = aAnchor.ownerGlobal.getComputedStyle(aAnchor).direction;
      let edge;
      if (direction == "ltr") {
        edge = anchorRect.left - mainViewRect.left;
      } else {
        edge = mainViewRect.right - anchorRect.right;
      }

      // If the anchor is an element on the far end of the mainView we
      // don't want to shift the mainView too far, we would reveal empty
      // space otherwise.
      let cstyle = this.window.getComputedStyle(this.document.documentElement);
      let exitSubViewGutterWidth =
        cstyle.getPropertyValue("--panel-ui-exit-subview-gutter-width");
      let maxShift = mainViewRect.width - parseInt(exitSubViewGutterWidth);
      let target = Math.min(maxShift, edge + center);

      let neg = direction == "ltr" ? "-" : "";
      this._mainViewContainer.style.transform = `translateX(${neg}${target}px)`;
      aAnchor.setAttribute("panel-multiview-anchor", true);
    } else {
      this._mainViewContainer.style.transform = "";
      if (this.anchorElement)
        this.anchorElement.removeAttribute("panel-multiview-anchor");
    }
    this.anchorElement = aAnchor;
  }

  handleEvent(aEvent) {
    if (aEvent.type.startsWith("popup") && aEvent.target != this._panel) {
      // Shouldn't act on e.g. context menus being shown from within the panel.
      return;
    }
    switch (aEvent.type) {
      case "click":
        if (aEvent.originalTarget == this._clickCapturer) {
          this.showMainView();
        }
        break;
      case "keydown":
        this._keyNavigation(aEvent);
        break;
      case "mousemove":
        this._resetKeyNavigation();
        break;
      case "overflow":
        if (!this.panelViews && aEvent.target.localName == "vbox") {
          // Resize the right view on the next tick.
          if (this.showingSubView) {
            this.window.setTimeout(this._syncContainerWithSubView.bind(this), 0);
          } else if (!this.transitioning) {
            this.window.setTimeout(this._syncContainerWithMainView.bind(this), 0);
          }
        }
        break;
      case "popupshowing":
        this.node.setAttribute("panelopen", "true");
        // Bug 941196 - The panel can get taller when opening a subview. Disabling
        // autoPositioning means that the panel won't jump around if an opened
        // subview causes the panel to exceed the dimensions of the screen in the
        // direction that the panel originally opened in. This property resets
        // every time the popup closes, which is why we have to set it each time.
        this._panel.autoPosition = false;

        if (!this.panelViews) {
          this._syncContainerWithMainView();
          this._mainViewObserver.observe(this._mainView, {
            attributes: true,
            characterData: true,
            childList: true,
            subtree: true
          });
        } else {
          this.window.addEventListener("keydown", this);
          this._panel.addEventListener("mousemove", this);
        }
        break;
      case "popupshown":
        this._setMaxHeight();
        break;
      case "popuphidden":
        this.node.removeAttribute("panelopen");
        this._mainView.style.removeProperty("height");
        this.showMainView();
        if (!this.panelViews) {
          this._mainViewObserver.disconnect();
        } else {
          this.window.removeEventListener("keydown", this);
          this._panel.removeEventListener("mousemove", this);
          this._resetKeyNavigation();
        }
        break;
    }
  }

  /**
   * Allow for navigating subview buttons using the arrow keys and the Enter key.
   * The Up and Down keys can be used to navigate the list up and down and the
   * Enter, Right or Left - depending on the text direction - key can be used to
   * simulate a click on the currently selected button.
   * The Right or Left key - depending on the text direction - can be used to
   * navigate to the previous view, functioning as a shortcut for the view's
   * back button.
   * Thus, in LTR mode:
   *  - The Right key functions the same as the Enter key, simulating a click
   *  - The Left key triggers a navigation back to the previous view.
   *
   * @param {KeyEvent} event
   */
  _keyNavigation(event) {
    if (this._transitioning)
      return;

    let view = this._currentSubView;
    let navMap = this._keyNavigationMap.get(view);
    if (!navMap) {
      navMap = {};
      this._keyNavigationMap.set(view, navMap);
    }

    let buttons = navMap.buttons;
    if (!buttons || !buttons.length) {
      buttons = navMap.buttons = this._getNavigableElements(view);
      // Set the 'tabindex' attribute on the buttons to make sure they're focussable.
      for (let button of buttons) {
        if (button.classList.contains("subviewbutton-back"))
          continue;
        // If we've been here before, forget about it!
        if (button.hasAttribute("tabindex"))
          break;
        button.setAttribute("tabindex", 0);
      }
    }
    if (!buttons.length)
      return;

    let stop = () => {
      event.stopPropagation();
      event.preventDefault();
    };

    let keyCode = event.code;
    switch (keyCode) {
      case "ArrowDown":
      case "ArrowUp": {
        stop();
        let isDown = (keyCode == "ArrowDown");
        let maxIdx = buttons.length - 1;
        let buttonIndex = isDown ? 0 : maxIdx;
        if (typeof navMap.selected == "number") {
          if (isDown) {
            buttonIndex = ++navMap.selected;
            if (buttonIndex > maxIdx)
              buttonIndex = 0;
          } else {
            buttonIndex = --navMap.selected;
            if (buttonIndex < 0)
              buttonIndex = maxIdx;
          }
        }
        let button = buttons[buttonIndex];
        button.focus();
        navMap.selected = buttonIndex;
        break;
      }
      case "ArrowLeft":
      case "ArrowRight": {
        stop();
        let dir = this._dir;
        if ((dir == "ltr" && keyCode == "ArrowLeft") ||
            (dir == "rtl" && keyCode == "ArrowRight")) {
          if (this._canGoBack(view))
            this.goBack(view.backButton);
          break;
        }
        // If the current button is _not_ one that points to a subview, pressing
        // the arrow key shouldn't do anything.
        if (!navMap.selected || !buttons[navMap.selected].classList.contains("subviewbutton-nav"))
          break;
        // Fall-through...
      }
      case "Enter": {
        let button = buttons[navMap.selected];
        if (!button)
          break;
        stop();
        // Unfortunately, 'tabindex' doesn't not execute the default action, so
        // we explicitly do this here.
        button.click();
        break;
      }
    }
  }

  /**
   * Clear all traces of keyboard navigation happening right now.
   *
   * @param {panelview} view View to reset the key navigation attributes of.
   *                         Defaults to `this._currentSubView`.
   */
  _resetKeyNavigation(view = this._currentSubView) {
    let navMap = this._keyNavigationMap.get(view);
    this._keyNavigationMap.clear();
    if (!navMap)
      return;

    let buttons = this._getNavigableElements(view);
    if (!buttons.length)
      return;

    let button = buttons[navMap.selected];
    if (button)
      button.blur();
  }

  /**
   * Retrieve the button elements from a view node that can be used for navigation
   * using the keyboard; enabled buttons and the back button, if visible.
   *
   * @param  {nsIDOMNode} view
   * @return {Array}
   */
  _getNavigableElements(view) {
    let buttons = Array.from(view.querySelectorAll(".subviewbutton:not([disabled])"));
    if (this._canGoBack(view))
      buttons.unshift(view.backButton);
    return buttons;
  }

  _shouldSetPosition() {
    return this.node.getAttribute("nosubviews") == "true";
  }

  _shouldSetHeight() {
    return this.node.getAttribute("nosubviews") != "true";
  }

  _setMaxHeight() {
    if (!this._shouldSetHeight())
      return;

    // Ignore the mutation that'll fire when we set the height of
    // the main view.
    this.ignoreMutations = true;
    this._mainView.style.height = this.node.getBoundingClientRect().height + "px";
    this.ignoreMutations = false;
  }

  _adjustContainerHeight() {
    if (!this.ignoreMutations && !this.showingSubView && !this._transitioning) {
      let height;
      if (this.showingSubViewAsMainView) {
        height = this._heightOfSubview(this._mainView);
      } else {
        height = this._mainView.scrollHeight;
      }
      this._viewContainer.style.height = height + "px";
    }
  }

  _syncContainerWithSubView() {
    // Check that this panel is still alive:
    if (!this._panel || !this._panel.parentNode) {
      return;
    }

    if (!this.ignoreMutations && this.showingSubView) {
      let newHeight = this._heightOfSubview(this._currentSubView, this._subViews);
      this._viewContainer.style.height = newHeight + "px";
    }
  }

  _syncContainerWithMainView() {
    // Check that this panel is still alive:
    if (!this._panel || !this._panel.parentNode) {
      return;
    }

    if (this._shouldSetPosition()) {
      this._panel.adjustArrowPosition();
    }

    if (this._shouldSetHeight()) {
      this._adjustContainerHeight();
    }
  }

  /**
   * Call this when the height of one of your views (the main view or a
   * subview) changes and you want the heights of the multiview and panel
   * to be the same as the view's height.
   * If the caller can give a hint of the expected height change with the
   * optional aExpectedChange parameter, it prevents flicker.
   */
  setHeightToFit(aExpectedChange) {
    // Set the max-height to zero, wait until the height is actually
    // updated, and then remove it.  If it's not removed, weird things can
    // happen, like widgets in the panel won't respond to clicks even
    // though they're visible.
    const {window} = this;
    let count = 5;
    let height = window.getComputedStyle(this.node).height;
    if (aExpectedChange)
      this.node.style.maxHeight = (parseInt(height, 10) + aExpectedChange) + "px";
    else
      this.node.style.maxHeight = "0";
    let interval = window.setInterval(() => {
      if (height != window.getComputedStyle(this.node).height || --count == 0) {
        window.clearInterval(interval);
        this.node.style.removeProperty("max-height");
      }
    }, 0);
  }

  _heightOfSubview(aSubview, aContainerToCheck) {
    function getFullHeight(element) {
      // XXXgijs: unfortunately, scrollHeight rounds values, and there's no alternative
      // that works with overflow: auto elements. Fortunately for us,
      // we have exactly 1 (potentially) scrolling element in here (the subview body),
      // and rounding 1 value is OK - rounding more than 1 and adding them means we get
      // off-by-1 errors. Now we might be off by a subpixel, but we care less about that.
      // So, use scrollHeight *only* if the element is vertically scrollable.
      let height;
      let elementCS;
      if (element.scrollTopMax) {
        height = element.scrollHeight;
        // Bounding client rects include borders, scrollHeight doesn't:
        elementCS = win.getComputedStyle(element);
        height += parseFloat(elementCS.borderTopWidth) +
                  parseFloat(elementCS.borderBottomWidth);
      } else {
        height = element.getBoundingClientRect().height;
        if (height > 0) {
          elementCS = win.getComputedStyle(element);
        }
      }
      if (elementCS) {
        // Include margins - but not borders or paddings because they
        // were dealt with above.
        height += parseFloat(elementCS.marginTop) + parseFloat(elementCS.marginBottom);
      }
      return height;
    }
    let win = aSubview.ownerGlobal;
    let body = aSubview.querySelector(".panel-subview-body");
    let height = getFullHeight(body || aSubview);
    if (body) {
      let header = aSubview.querySelector(".panel-subview-header");
      let footer = aSubview.querySelector(".panel-subview-footer");
      height += (header ? getFullHeight(header) : 0) +
                (footer ? getFullHeight(footer) : 0);
    }
    if (aContainerToCheck) {
      let containerCS = win.getComputedStyle(aContainerToCheck);
      height += parseFloat(containerCS.paddingTop) + parseFloat(containerCS.paddingBottom);
    }
    return Math.ceil(height);
  }
}