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