Web Inspector: sidebar panels shouldn't be added as subviews unless visible
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / Sidebar.js
index 20d0d18..3768485 100644 (file)
  * THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-WebInspector.Sidebar = class Sidebar extends WebInspector.View
+WI.Sidebar = class Sidebar extends WI.View
 {
     constructor(element, side, sidebarPanels, role, label, hasNavigationBar)
     {
         super(element);
 
-        console.assert(!side || side === WebInspector.Sidebar.Sides.Left || side === WebInspector.Sidebar.Sides.Right);
-        this._side = side || WebInspector.Sidebar.Sides.Left;
+        console.assert(!side || side === WI.Sidebar.Sides.Left || side === WI.Sidebar.Sides.Right);
+        this._side = side || WI.Sidebar.Sides.Left;
         this._collapsed = true;
 
-        this.element.classList.add("sidebar", this._side, WebInspector.Sidebar.CollapsedStyleClassName);
+        this.element.classList.add("sidebar", this._side, WI.Sidebar.CollapsedStyleClassName);
 
         this.element.setAttribute("role", role || "group");
         if (label)
@@ -42,12 +42,12 @@ WebInspector.Sidebar = class Sidebar extends WebInspector.View
         if (hasNavigationBar) {
             this.element.classList.add("has-navigation-bar");
 
-            this._navigationBar = new WebInspector.NavigationBar(null, null, "tablist");
-            this._navigationBar.addEventListener(WebInspector.NavigationBar.Event.NavigationItemSelected, this._navigationItemSelected, this);
+            this._navigationBar = new WI.SidebarNavigationBar(null, null, "tablist");
+            this._navigationBar.addEventListener(WI.NavigationBar.Event.NavigationItemSelected, this._navigationItemSelected, this);
             this.addSubview(this._navigationBar);
         }
 
-        this._resizer = new WebInspector.Resizer(WebInspector.Resizer.RuleOrientation.Vertical, this);
+        this._resizer = new WI.Resizer(WI.Resizer.RuleOrientation.Vertical, this);
         this.element.insertBefore(this._resizer.element, this.element.firstChild);
 
         this._sidebarPanels = [];
@@ -62,56 +62,52 @@ WebInspector.Sidebar = class Sidebar extends WebInspector.View
 
     addSidebarPanel(sidebarPanel)
     {
-        console.assert(sidebarPanel instanceof WebInspector.SidebarPanel);
-        if (!(sidebarPanel instanceof WebInspector.SidebarPanel))
-            return null;
+        this.insertSidebarPanel(sidebarPanel, this._sidebarPanels.length);
+    }
+
+    insertSidebarPanel(sidebarPanel, index)
+    {
+        console.assert(sidebarPanel instanceof WI.SidebarPanel);
+        if (!(sidebarPanel instanceof WI.SidebarPanel))
+            return;
 
         console.assert(!sidebarPanel.parentSidebar);
         if (sidebarPanel.parentSidebar)
-            return null;
+            return;
 
-        this._sidebarPanels.push(sidebarPanel);
-        this.addSubview(sidebarPanel);
+        console.assert(index >= 0 && index <= this._sidebarPanels.length);
+        this._sidebarPanels.splice(index, 0, sidebarPanel);
 
         if (this._navigationBar) {
             console.assert(sidebarPanel.navigationItem);
-            this._navigationBar.addNavigationItem(sidebarPanel.navigationItem);
+            this._navigationBar.insertNavigationItem(sidebarPanel.navigationItem, index);
         }
-
-        sidebarPanel.added();
-
-        return sidebarPanel;
     }
 
     removeSidebarPanel(sidebarPanelOrIdentifierOrIndex)
     {
         var sidebarPanel = this.findSidebarPanel(sidebarPanelOrIdentifierOrIndex);
         if (!sidebarPanel)
-            return null;
-
-        sidebarPanel.selected = false;
+            return;
 
         if (sidebarPanel.visible) {
             sidebarPanel.hidden();
             sidebarPanel.visibilityDidChange();
         }
 
+        sidebarPanel.selected = false;
+
         if (this._selectedSidebarPanel === sidebarPanel) {
             var index = this._sidebarPanels.indexOf(sidebarPanel);
             this.selectedSidebarPanel = this._sidebarPanels[index - 1] || this._sidebarPanels[index + 1] || null;
         }
 
         this._sidebarPanels.remove(sidebarPanel);
-        this.removeSubview(sidebarPanel);
 
         if (this._navigationBar) {
             console.assert(sidebarPanel.navigationItem);
             this._navigationBar.removeNavigationItem(sidebarPanel.navigationItem);
         }
-
-        sidebarPanel.removed();
-
-        return sidebarPanel;
     }
 
     get selectedSidebarPanel()
@@ -126,14 +122,10 @@ WebInspector.Sidebar = class Sidebar extends WebInspector.View
             return;
 
         if (this._selectedSidebarPanel) {
-            var wasVisible = this._selectedSidebarPanel.visible;
-
+            this._selectedSidebarPanel.hidden();
+            this._selectedSidebarPanel.visibilityDidChange();
             this._selectedSidebarPanel.selected = false;
-
-            if (wasVisible) {
-                this._selectedSidebarPanel.hidden();
-                this._selectedSidebarPanel.visibilityDidChange();
-            }
+            this.removeSubview(this._selectedSidebarPanel);
         }
 
         this._selectedSidebarPanel = sidebarPanel || null;
@@ -142,31 +134,27 @@ WebInspector.Sidebar = class Sidebar extends WebInspector.View
             this._navigationBar.selectedNavigationItem = sidebarPanel ? sidebarPanel.navigationItem : null;
 
         if (this._selectedSidebarPanel) {
+            this.addSubview(this._selectedSidebarPanel);
             this._selectedSidebarPanel.selected = true;
-
-            if (this._selectedSidebarPanel.visible) {
-                this._selectedSidebarPanel.shown();
-                this._selectedSidebarPanel.visibilityDidChange();
-            }
+            this._selectedSidebarPanel.shown();
+            this._selectedSidebarPanel.visibilityDidChange();
         }
 
-        this.dispatchEventToListeners(WebInspector.Sidebar.Event.SidebarPanelSelected);
+        this.dispatchEventToListeners(WI.Sidebar.Event.SidebarPanelSelected);
     }
 
     get minimumWidth()
     {
         if (this._navigationBar)
-            return Math.max(WebInspector.Sidebar.AbsoluteMinimumWidth, this._navigationBar.minimumWidth);
+            return Math.max(WI.Sidebar.AbsoluteMinimumWidth, this._navigationBar.minimumWidth);
         if (this._selectedSidebarPanel)
-            return Math.max(WebInspector.Sidebar.AbsoluteMinimumWidth, this._selectedSidebarPanel.minimumWidth);
-        return WebInspector.Sidebar.AbsoluteMinimumWidth;
+            return Math.max(WI.Sidebar.AbsoluteMinimumWidth, this._selectedSidebarPanel.minimumWidth);
+        return WI.Sidebar.AbsoluteMinimumWidth;
     }
 
     get maximumWidth()
     {
-        // FIXME: This is kind of arbitrary and ideally would be a more complex calculation based on the
-        // available space for the sibling elements.
-        return Math.round(window.innerWidth / 3);
+        return WI.getMaximumSidebarWidth(this);
     }
 
     get width()
@@ -193,7 +181,7 @@ WebInspector.Sidebar = class Sidebar extends WebInspector.View
             return;
 
         this._collapsed = flag || false;
-        this.element.classList.toggle(WebInspector.Sidebar.CollapsedStyleClassName);
+        this.element.classList.toggle(WI.Sidebar.CollapsedStyleClassName);
 
         if (!this._collapsed && this._navigationBar)
             this._navigationBar.needsLayout();
@@ -207,8 +195,8 @@ WebInspector.Sidebar = class Sidebar extends WebInspector.View
             this._selectedSidebarPanel.visibilityDidChange();
         }
 
-        this.dispatchEventToListeners(WebInspector.Sidebar.Event.CollapsedStateDidChange);
-        this.dispatchEventToListeners(WebInspector.Sidebar.Event.WidthDidChange);
+        this.dispatchEventToListeners(WI.Sidebar.Event.CollapsedStateDidChange);
+        this.dispatchEventToListeners(WI.Sidebar.Event.WidthDidChange);
     }
 
     get sidebarPanels()
@@ -225,7 +213,7 @@ WebInspector.Sidebar = class Sidebar extends WebInspector.View
     {
         var sidebarPanel = null;
 
-        if (sidebarPanelOrIdentifierOrIndex instanceof WebInspector.SidebarPanel) {
+        if (sidebarPanelOrIdentifierOrIndex instanceof WI.SidebarPanel) {
             if (this._sidebarPanels.includes(sidebarPanelOrIdentifierOrIndex))
                 sidebarPanel = sidebarPanelOrIdentifierOrIndex;
         } else if (typeof sidebarPanelOrIdentifierOrIndex === "number") {
@@ -251,12 +239,27 @@ WebInspector.Sidebar = class Sidebar extends WebInspector.View
 
     resizerDragging(resizer, positionDelta)
     {
-        if (this._side === WebInspector.Sidebar.Sides.Left)
+        if (this._side === WI.Sidebar.Sides.Left)
+            positionDelta *= -1;
+
+        if (WI.resolvedLayoutDirection() === WI.LayoutDirection.RTL)
             positionDelta *= -1;
 
         var newWidth = positionDelta + this._widthBeforeResize;
         this.width = newWidth;
-        this.collapsed = (newWidth < (this.minimumWidth / 2));
+        this.collapsed = newWidth < (this.minimumWidth / 2);
+    }
+
+    resizerDragEnded(resizer)
+    {
+        if (this._widthBeforeResize === this.width)
+            return;
+
+        if (!this.collapsed && this._navigationBar)
+            this._navigationBar.sizeDidChange();
+
+        if (!this.collapsed && this._selectedSidebarPanel)
+            this._selectedSidebarPanel.sizeDidChange();
     }
 
     // Private
@@ -267,13 +270,16 @@ WebInspector.Sidebar = class Sidebar extends WebInspector.View
         newWidth = Math.ceil(Number.constrain(newWidth, this.minimumWidth + 1, this.maximumWidth));
         this.element.style.width = `${newWidth}px`;
 
-        if (!this.collapsed && this._navigationBar)
-            this._navigationBar.needsLayout();
+        if (this.collapsed)
+            return;
 
-        if (!this.collapsed && this._selectedSidebarPanel)
-            this._selectedSidebarPanel.updateLayout(WebInspector.View.LayoutReason.Resize);
+        if (this._navigationBar)
+            this._navigationBar.updateLayoutIfNeeded(WI.View.LayoutReason.Resize);
+
+        if (this._selectedSidebarPanel)
+            this._selectedSidebarPanel.updateLayoutIfNeeded(WI.View.LayoutReason.Resize);
 
-        this.dispatchEventToListeners(WebInspector.Sidebar.Event.WidthDidChange, {newWidth});
+        this.dispatchEventToListeners(WI.Sidebar.Event.WidthDidChange, {newWidth});
     }
 
     _navigationItemSelected(event)
@@ -282,15 +288,15 @@ WebInspector.Sidebar = class Sidebar extends WebInspector.View
     }
 };
 
-WebInspector.Sidebar.CollapsedStyleClassName = "collapsed";
-WebInspector.Sidebar.AbsoluteMinimumWidth = 200;
+WI.Sidebar.CollapsedStyleClassName = "collapsed";
+WI.Sidebar.AbsoluteMinimumWidth = 200;
 
-WebInspector.Sidebar.Sides = {
+WI.Sidebar.Sides = {
     Right: "right",
     Left: "left"
 };
 
-WebInspector.Sidebar.Event = {
+WI.Sidebar.Event = {
     SidebarPanelSelected: "sidebar-panel-selected",
     CollapsedStateDidChange: "sidebar-collapsed-state-did-change",
     WidthDidChange: "sidebar-width-did-change",