Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * 2. Redistributions in binary form must reproduce the above |
| 12 | * copyright notice, this list of conditions and the following disclaimer |
| 13 | * in the documentation and/or other materials provided with the |
| 14 | * distribution. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS |
| 17 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC. |
| 20 | * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 29 | import {Constraints} from './Geometry.js'; |
| 30 | import {Events as ResizerWidgetEvents, SimpleResizerWidget} from './ResizerWidget.js'; |
| 31 | import {ToolbarButton} from './Toolbar.js'; |
| 32 | import {Widget} from './Widget.js'; |
| 33 | import {Events as ZoomManagerEvents} from './ZoomManager.js'; |
| 34 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 35 | /** |
| 36 | * @unrestricted |
| 37 | */ |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 38 | export class SplitWidget extends Widget { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 39 | /** |
| 40 | * @param {boolean} isVertical |
| 41 | * @param {boolean} secondIsSidebar |
| 42 | * @param {string=} settingName |
| 43 | * @param {number=} defaultSidebarWidth |
| 44 | * @param {number=} defaultSidebarHeight |
| 45 | * @param {boolean=} constraintsInDip |
| 46 | */ |
| 47 | constructor(isVertical, secondIsSidebar, settingName, defaultSidebarWidth, defaultSidebarHeight, constraintsInDip) { |
| 48 | super(true); |
| 49 | this.element.classList.add('split-widget'); |
| 50 | this.registerRequiredCSS('ui/splitWidget.css'); |
| 51 | |
| 52 | this.contentElement.classList.add('shadow-split-widget'); |
Joel Einbinder | 27131b2 | 2019-03-14 09:29:54 | [diff] [blame] | 53 | this._sidebarElement = |
| 54 | this.contentElement.createChild('div', 'shadow-split-widget-contents shadow-split-widget-sidebar vbox'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 55 | this._mainElement = |
| 56 | this.contentElement.createChild('div', 'shadow-split-widget-contents shadow-split-widget-main vbox'); |
Joel Einbinder | 7fbe24c | 2019-01-24 05:19:01 | [diff] [blame] | 57 | this._mainElement.createChild('slot').name = 'insertion-point-main'; |
Joel Einbinder | 7fbe24c | 2019-01-24 05:19:01 | [diff] [blame] | 58 | this._sidebarElement.createChild('slot').name = 'insertion-point-sidebar'; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 59 | this._resizerElement = this.contentElement.createChild('div', 'shadow-split-widget-resizer'); |
| 60 | this._resizerElementSize = null; |
| 61 | |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 62 | this._resizerWidget = new SimpleResizerWidget(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 63 | this._resizerWidget.setEnabled(true); |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 64 | this._resizerWidget.addEventListener(ResizerWidgetEvents.ResizeStart, this._onResizeStart, this); |
| 65 | this._resizerWidget.addEventListener(ResizerWidgetEvents.ResizeUpdate, this._onResizeUpdate, this); |
| 66 | this._resizerWidget.addEventListener(ResizerWidgetEvents.ResizeEnd, this._onResizeEnd, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 67 | |
| 68 | this._defaultSidebarWidth = defaultSidebarWidth || 200; |
| 69 | this._defaultSidebarHeight = defaultSidebarHeight || this._defaultSidebarWidth; |
| 70 | this._constraintsInDip = !!constraintsInDip; |
| 71 | this._resizeStartSizeDIP = 0; |
| 72 | this._setting = settingName ? Common.settings.createSetting(settingName, {}) : null; |
| 73 | |
| 74 | this._totalSizeCSS = 0; |
| 75 | this._totalSizeOtherDimensionCSS = 0; |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 76 | /** @type {?Widget} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 77 | this._mainWidget = null; |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 78 | /** @type {?Widget} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 79 | this._sidebarWidget = null; |
| 80 | this._animationFrameHandle = 0; |
| 81 | /** @type {?function()} */ |
| 82 | this._animationCallback = null; |
| 83 | this._showHideSidebarButtonTitle = ''; |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 84 | /** @type {?ToolbarButton} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 85 | this._showHideSidebarButton = null; |
| 86 | this._isVertical = false; |
| 87 | this._sidebarMinimized = false; |
| 88 | this._detaching = false; |
| 89 | this._sidebarSizeDIP = -1; |
| 90 | this._savedSidebarSizeDIP = this._sidebarSizeDIP; |
| 91 | this._secondIsSidebar = false; |
| 92 | this._shouldSaveShowMode = false; |
| 93 | /** @type {?number} */ |
| 94 | this._savedVerticalMainSize = null; |
| 95 | /** @type {?number} */ |
| 96 | this._savedHorizontalMainSize = null; |
| 97 | |
| 98 | this.setSecondIsSidebar(secondIsSidebar); |
| 99 | |
| 100 | this._innerSetVertical(isVertical); |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 101 | this._showMode = ShowMode.Both; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 102 | this._savedShowMode = this._showMode; |
| 103 | |
| 104 | // Should be called after isVertical has the right value. |
| 105 | this.installResizer(this._resizerElement); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @return {boolean} |
| 110 | */ |
| 111 | isVertical() { |
| 112 | return this._isVertical; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @param {boolean} isVertical |
| 117 | */ |
| 118 | setVertical(isVertical) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 119 | if (this._isVertical === isVertical) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 120 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 121 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 122 | |
| 123 | this._innerSetVertical(isVertical); |
| 124 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 125 | if (this.isShowing()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 126 | this._updateLayout(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 127 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @param {boolean} isVertical |
| 132 | */ |
| 133 | _innerSetVertical(isVertical) { |
| 134 | this.contentElement.classList.toggle('vbox', !isVertical); |
| 135 | this.contentElement.classList.toggle('hbox', isVertical); |
| 136 | this._isVertical = isVertical; |
| 137 | |
| 138 | this._resizerElementSize = null; |
| 139 | this._sidebarSizeDIP = -1; |
| 140 | this._restoreSidebarSizeFromSettings(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 141 | if (this._shouldSaveShowMode) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 142 | this._restoreAndApplyShowModeFromSettings(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 143 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 144 | this._updateShowHideSidebarButton(); |
| 145 | // FIXME: reverse SplitWidget.isVertical meaning. |
| 146 | this._resizerWidget.setVertical(!isVertical); |
| 147 | this.invalidateConstraints(); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @param {boolean=} animate |
| 152 | */ |
| 153 | _updateLayout(animate) { |
| 154 | this._totalSizeCSS = 0; // Lazy update. |
| 155 | this._totalSizeOtherDimensionCSS = 0; |
| 156 | |
| 157 | // Remove properties that might affect total size calculation. |
| 158 | this._mainElement.style.removeProperty('width'); |
| 159 | this._mainElement.style.removeProperty('height'); |
| 160 | this._sidebarElement.style.removeProperty('width'); |
| 161 | this._sidebarElement.style.removeProperty('height'); |
| 162 | |
| 163 | this._innerSetSidebarSizeDIP(this._preferredSidebarSizeDIP(), !!animate); |
| 164 | } |
| 165 | |
| 166 | /** |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 167 | * @param {!Widget} widget |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 168 | */ |
| 169 | setMainWidget(widget) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 170 | if (this._mainWidget === widget) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 171 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 172 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 173 | this.suspendInvalidations(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 174 | if (this._mainWidget) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 175 | this._mainWidget.detach(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 176 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 177 | this._mainWidget = widget; |
| 178 | if (widget) { |
Joel Einbinder | 7fbe24c | 2019-01-24 05:19:01 | [diff] [blame] | 179 | widget.element.slot = 'insertion-point-main'; |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 180 | if (this._showMode === ShowMode.OnlyMain || this._showMode === ShowMode.Both) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 181 | widget.show(this.element); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 182 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 183 | } |
| 184 | this.resumeInvalidations(); |
| 185 | } |
| 186 | |
| 187 | /** |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 188 | * @param {!Widget} widget |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 189 | */ |
| 190 | setSidebarWidget(widget) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 191 | if (this._sidebarWidget === widget) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 192 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 193 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 194 | this.suspendInvalidations(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 195 | if (this._sidebarWidget) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 196 | this._sidebarWidget.detach(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 197 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 198 | this._sidebarWidget = widget; |
| 199 | if (widget) { |
Joel Einbinder | 7fbe24c | 2019-01-24 05:19:01 | [diff] [blame] | 200 | widget.element.slot = 'insertion-point-sidebar'; |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 201 | if (this._showMode === ShowMode.OnlySidebar || this._showMode === ShowMode.Both) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 202 | widget.show(this.element); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 203 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 204 | } |
| 205 | this.resumeInvalidations(); |
| 206 | } |
| 207 | |
| 208 | /** |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 209 | * @return {?Widget} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 210 | */ |
| 211 | mainWidget() { |
| 212 | return this._mainWidget; |
| 213 | } |
| 214 | |
| 215 | /** |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 216 | * @return {?Widget} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 217 | */ |
| 218 | sidebarWidget() { |
| 219 | return this._sidebarWidget; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * @override |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 224 | * @param {!Widget} widget |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 225 | */ |
| 226 | childWasDetached(widget) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 227 | if (this._detaching) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 228 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 229 | } |
| 230 | if (this._mainWidget === widget) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 231 | this._mainWidget = null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 232 | } |
| 233 | if (this._sidebarWidget === widget) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 234 | this._sidebarWidget = null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 235 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 236 | this.invalidateConstraints(); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * @return {boolean} |
| 241 | */ |
| 242 | isSidebarSecond() { |
| 243 | return this._secondIsSidebar; |
| 244 | } |
| 245 | |
| 246 | enableShowModeSaving() { |
| 247 | this._shouldSaveShowMode = true; |
| 248 | this._restoreAndApplyShowModeFromSettings(); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * @return {string} |
| 253 | */ |
| 254 | showMode() { |
| 255 | return this._showMode; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * @param {boolean} secondIsSidebar |
| 260 | */ |
| 261 | setSecondIsSidebar(secondIsSidebar) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 262 | if (secondIsSidebar === this._secondIsSidebar) { |
Joel Einbinder | 27131b2 | 2019-03-14 09:29:54 | [diff] [blame] | 263 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 264 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 265 | this._secondIsSidebar = secondIsSidebar; |
Joel Einbinder | 27131b2 | 2019-03-14 09:29:54 | [diff] [blame] | 266 | if (!this._mainWidget || !this._mainWidget.shouldHideOnDetach()) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 267 | if (secondIsSidebar) { |
Joel Einbinder | 27131b2 | 2019-03-14 09:29:54 | [diff] [blame] | 268 | this.contentElement.insertBefore(this._mainElement, this._sidebarElement); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 269 | } else { |
Joel Einbinder | 27131b2 | 2019-03-14 09:29:54 | [diff] [blame] | 270 | this.contentElement.insertBefore(this._mainElement, this._resizerElement); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 271 | } |
Joel Einbinder | 27131b2 | 2019-03-14 09:29:54 | [diff] [blame] | 272 | } else if (!this._sidebarWidget || !this._sidebarWidget.shouldHideOnDetach()) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 273 | if (secondIsSidebar) { |
Joel Einbinder | 27131b2 | 2019-03-14 09:29:54 | [diff] [blame] | 274 | this.contentElement.insertBefore(this._sidebarElement, this._resizerElement); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 275 | } else { |
Joel Einbinder | 27131b2 | 2019-03-14 09:29:54 | [diff] [blame] | 276 | this.contentElement.insertBefore(this._sidebarElement, this._mainElement); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 277 | } |
Joel Einbinder | 27131b2 | 2019-03-14 09:29:54 | [diff] [blame] | 278 | } else { |
| 279 | console.error('Could not swap split widget side. Both children widgets contain iframes.'); |
| 280 | this._secondIsSidebar = !secondIsSidebar; |
| 281 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /** |
| 285 | * @return {?string} |
| 286 | */ |
| 287 | sidebarSide() { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 288 | if (this._showMode !== ShowMode.Both) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 289 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 290 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 291 | return this._isVertical ? (this._secondIsSidebar ? 'right' : 'left') : (this._secondIsSidebar ? 'bottom' : 'top'); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * @return {!Element} |
| 296 | */ |
| 297 | resizerElement() { |
| 298 | return this._resizerElement; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * @param {boolean=} animate |
| 303 | */ |
| 304 | hideMain(animate) { |
| 305 | this._showOnly(this._sidebarWidget, this._mainWidget, this._sidebarElement, this._mainElement, animate); |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 306 | this._updateShowMode(ShowMode.OnlySidebar); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /** |
| 310 | * @param {boolean=} animate |
| 311 | */ |
| 312 | hideSidebar(animate) { |
| 313 | this._showOnly(this._mainWidget, this._sidebarWidget, this._mainElement, this._sidebarElement, animate); |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 314 | this._updateShowMode(ShowMode.OnlyMain); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /** |
| 318 | * @param {boolean} minimized |
| 319 | */ |
| 320 | setSidebarMinimized(minimized) { |
| 321 | this._sidebarMinimized = minimized; |
| 322 | this.invalidateConstraints(); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * @return {boolean} |
| 327 | */ |
| 328 | isSidebarMinimized() { |
| 329 | return this._sidebarMinimized; |
| 330 | } |
| 331 | |
| 332 | /** |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 333 | * @param {?Widget} sideToShow |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 334 | * @param {?UI.Widget} sideToHide |
| 335 | * @param {!Element} shadowToShow |
| 336 | * @param {!Element} shadowToHide |
| 337 | * @param {boolean=} animate |
| 338 | */ |
| 339 | _showOnly(sideToShow, sideToHide, shadowToShow, shadowToHide, animate) { |
| 340 | this._cancelAnimation(); |
| 341 | |
| 342 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 343 | * @this {SplitWidget} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 344 | */ |
| 345 | function callback() { |
| 346 | if (sideToShow) { |
| 347 | // Make sure main is first in the children list. |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 348 | if (sideToShow === this._mainWidget) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 349 | this._mainWidget.show(this.element, this._sidebarWidget ? this._sidebarWidget.element : null); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 350 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 351 | this._sidebarWidget.show(this.element); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 352 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 353 | } |
| 354 | if (sideToHide) { |
| 355 | this._detaching = true; |
| 356 | sideToHide.detach(); |
| 357 | this._detaching = false; |
| 358 | } |
| 359 | |
| 360 | this._resizerElement.classList.add('hidden'); |
| 361 | shadowToShow.classList.remove('hidden'); |
| 362 | shadowToShow.classList.add('maximized'); |
| 363 | shadowToHide.classList.add('hidden'); |
| 364 | shadowToHide.classList.remove('maximized'); |
| 365 | this._removeAllLayoutProperties(); |
| 366 | this.doResize(); |
| 367 | this._showFinishedForTest(); |
| 368 | } |
| 369 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 370 | if (animate) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 371 | this._animate(true, callback.bind(this)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 372 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 373 | callback.call(this); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 374 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 375 | |
| 376 | this._sidebarSizeDIP = -1; |
| 377 | this.setResizable(false); |
| 378 | } |
| 379 | |
| 380 | _showFinishedForTest() { |
| 381 | // This method is sniffed in tests. |
| 382 | } |
| 383 | |
| 384 | _removeAllLayoutProperties() { |
| 385 | this._sidebarElement.style.removeProperty('flexBasis'); |
| 386 | |
| 387 | this._mainElement.style.removeProperty('width'); |
| 388 | this._mainElement.style.removeProperty('height'); |
| 389 | this._sidebarElement.style.removeProperty('width'); |
| 390 | this._sidebarElement.style.removeProperty('height'); |
| 391 | |
| 392 | this._resizerElement.style.removeProperty('left'); |
| 393 | this._resizerElement.style.removeProperty('right'); |
| 394 | this._resizerElement.style.removeProperty('top'); |
| 395 | this._resizerElement.style.removeProperty('bottom'); |
| 396 | |
| 397 | this._resizerElement.style.removeProperty('margin-left'); |
| 398 | this._resizerElement.style.removeProperty('margin-right'); |
| 399 | this._resizerElement.style.removeProperty('margin-top'); |
| 400 | this._resizerElement.style.removeProperty('margin-bottom'); |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * @param {boolean=} animate |
| 405 | */ |
| 406 | showBoth(animate) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 407 | if (this._showMode === ShowMode.Both) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 408 | animate = false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 409 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 410 | |
| 411 | this._cancelAnimation(); |
| 412 | this._mainElement.classList.remove('maximized', 'hidden'); |
| 413 | this._sidebarElement.classList.remove('maximized', 'hidden'); |
| 414 | this._resizerElement.classList.remove('hidden'); |
| 415 | this.setResizable(true); |
| 416 | |
| 417 | // Make sure main is the first in the children list. |
| 418 | this.suspendInvalidations(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 419 | if (this._sidebarWidget) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 420 | this._sidebarWidget.show(this.element); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 421 | } |
| 422 | if (this._mainWidget) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 423 | this._mainWidget.show(this.element, this._sidebarWidget ? this._sidebarWidget.element : null); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 424 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 425 | this.resumeInvalidations(); |
| 426 | // Order widgets in DOM properly. |
| 427 | this.setSecondIsSidebar(this._secondIsSidebar); |
| 428 | |
| 429 | this._sidebarSizeDIP = -1; |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 430 | this._updateShowMode(ShowMode.Both); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 431 | this._updateLayout(animate); |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * @param {boolean} resizable |
| 436 | */ |
| 437 | setResizable(resizable) { |
| 438 | this._resizerWidget.setEnabled(resizable); |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * @return {boolean} |
| 443 | */ |
| 444 | isResizable() { |
| 445 | return this._resizerWidget.isEnabled(); |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * @param {number} size |
| 450 | */ |
| 451 | setSidebarSize(size) { |
| 452 | const sizeDIP = UI.zoomManager.cssToDIP(size); |
| 453 | this._savedSidebarSizeDIP = sizeDIP; |
| 454 | this._saveSetting(); |
| 455 | this._innerSetSidebarSizeDIP(sizeDIP, false, true); |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * @return {number} |
| 460 | */ |
| 461 | sidebarSize() { |
| 462 | const sizeDIP = Math.max(0, this._sidebarSizeDIP); |
| 463 | return UI.zoomManager.dipToCSS(sizeDIP); |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Returns total size in DIP. |
| 468 | * @return {number} |
| 469 | */ |
| 470 | _totalSizeDIP() { |
| 471 | if (!this._totalSizeCSS) { |
| 472 | this._totalSizeCSS = this._isVertical ? this.contentElement.offsetWidth : this.contentElement.offsetHeight; |
| 473 | this._totalSizeOtherDimensionCSS = |
| 474 | this._isVertical ? this.contentElement.offsetHeight : this.contentElement.offsetWidth; |
| 475 | } |
| 476 | return UI.zoomManager.cssToDIP(this._totalSizeCSS); |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * @param {string} showMode |
| 481 | */ |
| 482 | _updateShowMode(showMode) { |
| 483 | this._showMode = showMode; |
| 484 | this._saveShowModeToSettings(); |
| 485 | this._updateShowHideSidebarButton(); |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 486 | this.dispatchEventToListeners(SplitWidget.Events.ShowModeChanged, showMode); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 487 | this.invalidateConstraints(); |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * @param {number} sizeDIP |
| 492 | * @param {boolean} animate |
| 493 | * @param {boolean=} userAction |
| 494 | */ |
| 495 | _innerSetSidebarSizeDIP(sizeDIP, animate, userAction) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 496 | if (this._showMode !== ShowMode.Both || !this.isShowing()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 497 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 498 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 499 | |
| 500 | sizeDIP = this._applyConstraints(sizeDIP, userAction); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 501 | if (this._sidebarSizeDIP === sizeDIP) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 502 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 503 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 504 | |
| 505 | if (!this._resizerElementSize) { |
| 506 | this._resizerElementSize = |
| 507 | this._isVertical ? this._resizerElement.offsetWidth : this._resizerElement.offsetHeight; |
| 508 | } |
| 509 | |
| 510 | // Invalidate layout below. |
| 511 | |
| 512 | this._removeAllLayoutProperties(); |
| 513 | |
| 514 | // this._totalSizeDIP is available below since we successfully applied constraints. |
| 515 | const roundSizeCSS = Math.round(UI.zoomManager.dipToCSS(sizeDIP)); |
| 516 | const sidebarSizeValue = roundSizeCSS + 'px'; |
| 517 | const mainSizeValue = (this._totalSizeCSS - roundSizeCSS) + 'px'; |
| 518 | this._sidebarElement.style.flexBasis = sidebarSizeValue; |
| 519 | |
| 520 | // Make both sides relayout boundaries. |
| 521 | if (this._isVertical) { |
| 522 | this._sidebarElement.style.width = sidebarSizeValue; |
| 523 | this._mainElement.style.width = mainSizeValue; |
| 524 | this._sidebarElement.style.height = this._totalSizeOtherDimensionCSS + 'px'; |
| 525 | this._mainElement.style.height = this._totalSizeOtherDimensionCSS + 'px'; |
| 526 | } else { |
| 527 | this._sidebarElement.style.height = sidebarSizeValue; |
| 528 | this._mainElement.style.height = mainSizeValue; |
| 529 | this._sidebarElement.style.width = this._totalSizeOtherDimensionCSS + 'px'; |
| 530 | this._mainElement.style.width = this._totalSizeOtherDimensionCSS + 'px'; |
| 531 | } |
| 532 | |
| 533 | // Position resizer. |
| 534 | if (this._isVertical) { |
| 535 | if (this._secondIsSidebar) { |
| 536 | this._resizerElement.style.right = sidebarSizeValue; |
| 537 | this._resizerElement.style.marginRight = -this._resizerElementSize / 2 + 'px'; |
| 538 | } else { |
| 539 | this._resizerElement.style.left = sidebarSizeValue; |
| 540 | this._resizerElement.style.marginLeft = -this._resizerElementSize / 2 + 'px'; |
| 541 | } |
| 542 | } else { |
| 543 | if (this._secondIsSidebar) { |
| 544 | this._resizerElement.style.bottom = sidebarSizeValue; |
| 545 | this._resizerElement.style.marginBottom = -this._resizerElementSize / 2 + 'px'; |
| 546 | } else { |
| 547 | this._resizerElement.style.top = sidebarSizeValue; |
| 548 | this._resizerElement.style.marginTop = -this._resizerElementSize / 2 + 'px'; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | this._sidebarSizeDIP = sizeDIP; |
| 553 | |
| 554 | // Force layout. |
| 555 | |
| 556 | if (animate) { |
| 557 | this._animate(false); |
| 558 | } else { |
| 559 | // No need to recalculate this._sidebarSizeDIP and this._totalSizeDIP again. |
| 560 | this.doResize(); |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 561 | this.dispatchEventToListeners(SplitWidget.Events.SidebarSizeChanged, this.sidebarSize()); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * @param {boolean} reverse |
| 567 | * @param {function()=} callback |
| 568 | */ |
| 569 | _animate(reverse, callback) { |
| 570 | const animationTime = 50; |
| 571 | this._animationCallback = callback || null; |
| 572 | |
| 573 | let animatedMarginPropertyName; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 574 | if (this._isVertical) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 575 | animatedMarginPropertyName = this._secondIsSidebar ? 'margin-right' : 'margin-left'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 576 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 577 | animatedMarginPropertyName = this._secondIsSidebar ? 'margin-bottom' : 'margin-top'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 578 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 579 | |
| 580 | const marginFrom = reverse ? '0' : '-' + UI.zoomManager.dipToCSS(this._sidebarSizeDIP) + 'px'; |
| 581 | const marginTo = reverse ? '-' + UI.zoomManager.dipToCSS(this._sidebarSizeDIP) + 'px' : '0'; |
| 582 | |
| 583 | // This order of things is important. |
| 584 | // 1. Resize main element early and force layout. |
| 585 | this.contentElement.style.setProperty(animatedMarginPropertyName, marginFrom); |
| 586 | if (!reverse) { |
| 587 | suppressUnused(this._mainElement.offsetWidth); |
| 588 | suppressUnused(this._sidebarElement.offsetWidth); |
| 589 | } |
| 590 | |
| 591 | // 2. Issue onresize to the sidebar element, its size won't change. |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 592 | if (!reverse) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 593 | this._sidebarWidget.doResize(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 594 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 595 | |
| 596 | // 3. Configure and run animation |
| 597 | this.contentElement.style.setProperty('transition', animatedMarginPropertyName + ' ' + animationTime + 'ms linear'); |
| 598 | |
| 599 | const boundAnimationFrame = animationFrame.bind(this); |
| 600 | let startTime; |
| 601 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 602 | * @this {SplitWidget} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 603 | */ |
| 604 | function animationFrame() { |
| 605 | this._animationFrameHandle = 0; |
| 606 | |
| 607 | if (!startTime) { |
| 608 | // Kick animation on first frame. |
| 609 | this.contentElement.style.setProperty(animatedMarginPropertyName, marginTo); |
| 610 | startTime = window.performance.now(); |
| 611 | } else if (window.performance.now() < startTime + animationTime) { |
| 612 | // Process regular animation frame. |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 613 | if (this._mainWidget) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 614 | this._mainWidget.doResize(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 615 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 616 | } else { |
| 617 | // Complete animation. |
| 618 | this._cancelAnimation(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 619 | if (this._mainWidget) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 620 | this._mainWidget.doResize(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 621 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 622 | this.dispatchEventToListeners(SplitWidget.Events.SidebarSizeChanged, this.sidebarSize()); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 623 | return; |
| 624 | } |
| 625 | this._animationFrameHandle = this.contentElement.window().requestAnimationFrame(boundAnimationFrame); |
| 626 | } |
| 627 | this._animationFrameHandle = this.contentElement.window().requestAnimationFrame(boundAnimationFrame); |
| 628 | } |
| 629 | |
| 630 | _cancelAnimation() { |
| 631 | this.contentElement.style.removeProperty('margin-top'); |
| 632 | this.contentElement.style.removeProperty('margin-right'); |
| 633 | this.contentElement.style.removeProperty('margin-bottom'); |
| 634 | this.contentElement.style.removeProperty('margin-left'); |
| 635 | this.contentElement.style.removeProperty('transition'); |
| 636 | |
| 637 | if (this._animationFrameHandle) { |
| 638 | this.contentElement.window().cancelAnimationFrame(this._animationFrameHandle); |
| 639 | this._animationFrameHandle = 0; |
| 640 | } |
| 641 | if (this._animationCallback) { |
| 642 | this._animationCallback(); |
| 643 | this._animationCallback = null; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * @param {number} sidebarSize |
| 649 | * @param {boolean=} userAction |
| 650 | * @return {number} |
| 651 | */ |
| 652 | _applyConstraints(sidebarSize, userAction) { |
| 653 | const totalSize = this._totalSizeDIP(); |
| 654 | const zoomFactor = this._constraintsInDip ? 1 : UI.zoomManager.zoomFactor(); |
| 655 | |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 656 | let constraints = this._sidebarWidget ? this._sidebarWidget.constraints() : new Constraints(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 657 | let minSidebarSize = this.isVertical() ? constraints.minimum.width : constraints.minimum.height; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 658 | if (!minSidebarSize) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 659 | minSidebarSize = MinPadding; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 660 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 661 | minSidebarSize *= zoomFactor; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 662 | if (this._sidebarMinimized) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 663 | sidebarSize = minSidebarSize; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 664 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 665 | |
| 666 | let preferredSidebarSize = this.isVertical() ? constraints.preferred.width : constraints.preferred.height; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 667 | if (!preferredSidebarSize) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 668 | preferredSidebarSize = MinPadding; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 669 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 670 | preferredSidebarSize *= zoomFactor; |
| 671 | // Allow sidebar to be less than preferred by explicit user action. |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 672 | if (sidebarSize < preferredSidebarSize) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 673 | preferredSidebarSize = Math.max(sidebarSize, minSidebarSize); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 674 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 675 | preferredSidebarSize += zoomFactor; // 1 css pixel for splitter border. |
| 676 | |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 677 | constraints = this._mainWidget ? this._mainWidget.constraints() : new Constraints(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 678 | let minMainSize = this.isVertical() ? constraints.minimum.width : constraints.minimum.height; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 679 | if (!minMainSize) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 680 | minMainSize = MinPadding; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 681 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 682 | minMainSize *= zoomFactor; |
| 683 | |
| 684 | let preferredMainSize = this.isVertical() ? constraints.preferred.width : constraints.preferred.height; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 685 | if (!preferredMainSize) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 686 | preferredMainSize = MinPadding; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 687 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 688 | preferredMainSize *= zoomFactor; |
| 689 | const savedMainSize = this.isVertical() ? this._savedVerticalMainSize : this._savedHorizontalMainSize; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 690 | if (savedMainSize !== null) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 691 | preferredMainSize = Math.min(preferredMainSize, savedMainSize * zoomFactor); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 692 | } |
| 693 | if (userAction) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 694 | preferredMainSize = minMainSize; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 695 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 696 | |
| 697 | // Enough space for preferred. |
| 698 | const totalPreferred = preferredMainSize + preferredSidebarSize; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 699 | if (totalPreferred <= totalSize) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 700 | return Number.constrain(sidebarSize, preferredSidebarSize, totalSize - preferredMainSize); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 701 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 702 | |
| 703 | // Enough space for minimum. |
| 704 | if (minMainSize + minSidebarSize <= totalSize) { |
| 705 | const delta = totalPreferred - totalSize; |
| 706 | const sidebarDelta = delta * preferredSidebarSize / totalPreferred; |
| 707 | sidebarSize = preferredSidebarSize - sidebarDelta; |
| 708 | return Number.constrain(sidebarSize, minSidebarSize, totalSize - minMainSize); |
| 709 | } |
| 710 | |
| 711 | // Not enough space even for minimum sizes. |
| 712 | return Math.max(0, totalSize - minMainSize); |
| 713 | } |
| 714 | |
| 715 | /** |
| 716 | * @override |
| 717 | */ |
| 718 | wasShown() { |
| 719 | this._forceUpdateLayout(); |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 720 | UI.zoomManager.addEventListener(ZoomManagerEvents.ZoomChanged, this._onZoomChanged, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | /** |
| 724 | * @override |
| 725 | */ |
| 726 | willHide() { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 727 | UI.zoomManager.removeEventListener(ZoomManagerEvents.ZoomChanged, this._onZoomChanged, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | /** |
| 731 | * @override |
| 732 | */ |
| 733 | onResize() { |
| 734 | this._updateLayout(); |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * @override |
| 739 | */ |
| 740 | onLayout() { |
| 741 | this._updateLayout(); |
| 742 | } |
| 743 | |
| 744 | /** |
| 745 | * @override |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 746 | * @return {!Constraints} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 747 | */ |
| 748 | calculateConstraints() { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 749 | if (this._showMode === ShowMode.OnlyMain) { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 750 | return this._mainWidget ? this._mainWidget.constraints() : new Constraints(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 751 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 752 | if (this._showMode === ShowMode.OnlySidebar) { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 753 | return this._sidebarWidget ? this._sidebarWidget.constraints() : new Constraints(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 754 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 755 | |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 756 | let mainConstraints = this._mainWidget ? this._mainWidget.constraints() : new Constraints(); |
| 757 | let sidebarConstraints = this._sidebarWidget ? this._sidebarWidget.constraints() : new Constraints(); |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 758 | const min = MinPadding; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 759 | if (this._isVertical) { |
| 760 | mainConstraints = mainConstraints.widthToMax(min).addWidth(1); // 1 for splitter |
| 761 | sidebarConstraints = sidebarConstraints.widthToMax(min); |
| 762 | return mainConstraints.addWidth(sidebarConstraints).heightToMax(sidebarConstraints); |
| 763 | } else { |
| 764 | mainConstraints = mainConstraints.heightToMax(min).addHeight(1); // 1 for splitter |
| 765 | sidebarConstraints = sidebarConstraints.heightToMax(min); |
| 766 | return mainConstraints.widthToMax(sidebarConstraints).addHeight(sidebarConstraints); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * @param {!Common.Event} event |
| 772 | */ |
| 773 | _onResizeStart(event) { |
| 774 | this._resizeStartSizeDIP = this._sidebarSizeDIP; |
| 775 | } |
| 776 | |
| 777 | /** |
| 778 | * @param {!Common.Event} event |
| 779 | */ |
| 780 | _onResizeUpdate(event) { |
| 781 | const offset = event.data.currentPosition - event.data.startPosition; |
| 782 | const offsetDIP = UI.zoomManager.cssToDIP(offset); |
| 783 | const newSizeDIP = |
| 784 | this._secondIsSidebar ? this._resizeStartSizeDIP - offsetDIP : this._resizeStartSizeDIP + offsetDIP; |
| 785 | const constrainedSizeDIP = this._applyConstraints(newSizeDIP, true); |
| 786 | this._savedSidebarSizeDIP = constrainedSizeDIP; |
| 787 | this._saveSetting(); |
| 788 | this._innerSetSidebarSizeDIP(constrainedSizeDIP, false, true); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 789 | if (this.isVertical()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 790 | this._savedVerticalMainSize = this._totalSizeDIP() - this._sidebarSizeDIP; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 791 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 792 | this._savedHorizontalMainSize = this._totalSizeDIP() - this._sidebarSizeDIP; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 793 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | /** |
| 797 | * @param {!Common.Event} event |
| 798 | */ |
| 799 | _onResizeEnd(event) { |
| 800 | this._resizeStartSizeDIP = 0; |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * @param {boolean=} noSplitter |
| 805 | */ |
| 806 | hideDefaultResizer(noSplitter) { |
| 807 | this.uninstallResizer(this._resizerElement); |
Pavel Feldman | c9060ea | 2018-04-30 04:42:18 | [diff] [blame] | 808 | this._sidebarElement.classList.toggle('no-default-splitter', !!noSplitter); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | /** |
| 812 | * @param {!Element} resizerElement |
| 813 | */ |
| 814 | installResizer(resizerElement) { |
| 815 | this._resizerWidget.addElement(resizerElement); |
| 816 | } |
| 817 | |
| 818 | /** |
| 819 | * @param {!Element} resizerElement |
| 820 | */ |
| 821 | uninstallResizer(resizerElement) { |
| 822 | this._resizerWidget.removeElement(resizerElement); |
| 823 | } |
| 824 | |
| 825 | /** |
| 826 | * @return {boolean} |
| 827 | */ |
| 828 | hasCustomResizer() { |
| 829 | const elements = this._resizerWidget.elements(); |
| 830 | return elements.length > 1 || (elements.length === 1 && elements[0] !== this._resizerElement); |
| 831 | } |
| 832 | |
| 833 | /** |
| 834 | * @param {!Element} resizer |
| 835 | * @param {boolean} on |
| 836 | */ |
| 837 | toggleResizer(resizer, on) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 838 | if (on) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 839 | this.installResizer(resizer); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 840 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 841 | this.uninstallResizer(resizer); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 842 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 846 | * @return {?SplitWidget.SettingForOrientation} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 847 | */ |
| 848 | _settingForOrientation() { |
| 849 | const state = this._setting ? this._setting.get() : {}; |
| 850 | return this._isVertical ? state.vertical : state.horizontal; |
| 851 | } |
| 852 | |
| 853 | /** |
| 854 | * @return {number} |
| 855 | */ |
| 856 | _preferredSidebarSizeDIP() { |
| 857 | let size = this._savedSidebarSizeDIP; |
| 858 | if (!size) { |
| 859 | size = this._isVertical ? this._defaultSidebarWidth : this._defaultSidebarHeight; |
| 860 | // If we have default value in percents, calculate it on first use. |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 861 | if (0 < size && size < 1) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 862 | size *= this._totalSizeDIP(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 863 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 864 | } |
| 865 | return size; |
| 866 | } |
| 867 | |
| 868 | _restoreSidebarSizeFromSettings() { |
| 869 | const settingForOrientation = this._settingForOrientation(); |
| 870 | this._savedSidebarSizeDIP = settingForOrientation ? settingForOrientation.size : 0; |
| 871 | } |
| 872 | |
| 873 | _restoreAndApplyShowModeFromSettings() { |
| 874 | const orientationState = this._settingForOrientation(); |
| 875 | this._savedShowMode = orientationState && orientationState.showMode ? orientationState.showMode : this._showMode; |
| 876 | this._showMode = this._savedShowMode; |
| 877 | |
| 878 | switch (this._savedShowMode) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 879 | case ShowMode.Both: |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 880 | this.showBoth(); |
| 881 | break; |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 882 | case ShowMode.OnlyMain: |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 883 | this.hideSidebar(); |
| 884 | break; |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 885 | case ShowMode.OnlySidebar: |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 886 | this.hideMain(); |
| 887 | break; |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | _saveShowModeToSettings() { |
| 892 | this._savedShowMode = this._showMode; |
| 893 | this._saveSetting(); |
| 894 | } |
| 895 | |
| 896 | _saveSetting() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 897 | if (!this._setting) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 898 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 899 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 900 | const state = this._setting.get(); |
| 901 | const orientationState = (this._isVertical ? state.vertical : state.horizontal) || {}; |
| 902 | |
| 903 | orientationState.size = this._savedSidebarSizeDIP; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 904 | if (this._shouldSaveShowMode) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 905 | orientationState.showMode = this._savedShowMode; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 906 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 907 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 908 | if (this._isVertical) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 909 | state.vertical = orientationState; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 910 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 911 | state.horizontal = orientationState; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 912 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 913 | this._setting.set(state); |
| 914 | } |
| 915 | |
| 916 | _forceUpdateLayout() { |
| 917 | // Force layout even if sidebar size does not change. |
| 918 | this._sidebarSizeDIP = -1; |
| 919 | this._updateLayout(); |
| 920 | } |
| 921 | |
| 922 | /** |
| 923 | * @param {!Common.Event} event |
| 924 | */ |
| 925 | _onZoomChanged(event) { |
| 926 | this._forceUpdateLayout(); |
| 927 | } |
| 928 | |
| 929 | /** |
| 930 | * @param {string} title |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 931 | * @return {!ToolbarButton} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 932 | */ |
| 933 | createShowHideSidebarButton(title) { |
Mandy Chen | 84d56b6 | 2019-09-03 18:21:18 | [diff] [blame] | 934 | this._showHideSidebarButtonTitle = title; |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 935 | this._showHideSidebarButton = new ToolbarButton('', ''); |
| 936 | this._showHideSidebarButton.addEventListener(ToolbarButton.Events.Click, buttonClicked, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 937 | this._updateShowHideSidebarButton(); |
| 938 | |
| 939 | /** |
| 940 | * @param {!Common.Event} event |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 941 | * @this {SplitWidget} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 942 | */ |
| 943 | function buttonClicked(event) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 944 | if (this._showMode !== ShowMode.Both) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 945 | this.showBoth(true); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 946 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 947 | this.hideSidebar(true); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 948 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | return this._showHideSidebarButton; |
| 952 | } |
| 953 | |
| 954 | _updateShowHideSidebarButton() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 955 | if (!this._showHideSidebarButton) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 956 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 957 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 958 | const sidebarHidden = this._showMode === ShowMode.OnlyMain; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 959 | let glyph = ''; |
| 960 | if (sidebarHidden) { |
| 961 | glyph = this.isVertical() ? |
| 962 | (this.isSidebarSecond() ? 'largeicon-show-right-sidebar' : 'largeicon-show-left-sidebar') : |
| 963 | (this.isSidebarSecond() ? 'largeicon-show-bottom-sidebar' : 'largeicon-show-top-sidebar'); |
| 964 | } else { |
| 965 | glyph = this.isVertical() ? |
| 966 | (this.isSidebarSecond() ? 'largeicon-hide-right-sidebar' : 'largeicon-hide-left-sidebar') : |
| 967 | (this.isSidebarSecond() ? 'largeicon-hide-bottom-sidebar' : 'largeicon-hide-top-sidebar'); |
| 968 | } |
| 969 | this._showHideSidebarButton.setGlyph(glyph); |
| 970 | this._showHideSidebarButton.setTitle( |
| 971 | sidebarHidden ? Common.UIString('Show %s', this._showHideSidebarButtonTitle) : |
| 972 | Common.UIString('Hide %s', this._showHideSidebarButtonTitle)); |
| 973 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 974 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 975 | |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 976 | export const ShowMode = { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 977 | Both: 'Both', |
| 978 | OnlyMain: 'OnlyMain', |
| 979 | OnlySidebar: 'OnlySidebar' |
| 980 | }; |
| 981 | |
| 982 | /** @enum {symbol} */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 983 | export const Events = { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 984 | SidebarSizeChanged: Symbol('SidebarSizeChanged'), |
| 985 | ShowModeChanged: Symbol('ShowModeChanged') |
| 986 | }; |
| 987 | |
Tim van der Lippe | c96ccd9 | 2019-11-29 16:23:54 | [diff] [blame] | 988 | const MinPadding = 20; |