blob: 229b3c469b706468843f6c7b48eadcd58ac260ce [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371/*
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 *
Tim van der Lippe83f02be2020-01-23 11:11:408 * * Redistributions of source code must retain the above copyright
Blink Reformat4c46d092018-04-07 15:32:379 * notice, this list of conditions and the following disclaimer.
Tim van der Lippe83f02be2020-01-23 11:11:4010 * * Redistributions in binary form must reproduce the above
Blink Reformat4c46d092018-04-07 15:32:3711 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
Tim van der Lippe83f02be2020-01-23 11:11:4014 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
Blink Reformat4c46d092018-04-07 15:32:3717 *
Tim van der Lippe83f02be2020-01-23 11:11:4018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Blink Reformat4c46d092018-04-07 15:32:3719 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Tim van der Lippe83f02be2020-01-23 11:11:4021 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Blink Reformat4c46d092018-04-07 15:32:3723 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
Tim van der Lippeee97fa32020-04-23 15:20:5631// @ts-nocheck
32// TODO(crbug.com/1011811): Enable TypeScript compiler checks
33
Paul Lewis17e384e2020-01-08 15:46:5134import * as Common from '../common/common.js';
Jack Franklin1be909c2020-03-04 08:57:4135import * as Platform from '../platform/platform.js';
Tim van der Lippeaa76aa22020-02-14 14:38:2436
Paul Lewis9950e182019-12-16 16:06:0737import {Constraints} from './Geometry.js';
38import {Events as ResizerWidgetEvents, SimpleResizerWidget} from './ResizerWidget.js';
39import {ToolbarButton} from './Toolbar.js';
40import {Widget} from './Widget.js';
Paul Lewis6c914a12020-03-19 11:23:2141import {Events as ZoomManagerEvents, ZoomManager} from './ZoomManager.js';
Paul Lewis9950e182019-12-16 16:06:0742
Sigurd Schneider46da7db2020-05-20 13:45:1143
Paul Lewis9950e182019-12-16 16:06:0744export class SplitWidget extends Widget {
Blink Reformat4c46d092018-04-07 15:32:3745 /**
46 * @param {boolean} isVertical
47 * @param {boolean} secondIsSidebar
48 * @param {string=} settingName
49 * @param {number=} defaultSidebarWidth
50 * @param {number=} defaultSidebarHeight
51 * @param {boolean=} constraintsInDip
52 */
53 constructor(isVertical, secondIsSidebar, settingName, defaultSidebarWidth, defaultSidebarHeight, constraintsInDip) {
54 super(true);
55 this.element.classList.add('split-widget');
56 this.registerRequiredCSS('ui/splitWidget.css');
57
58 this.contentElement.classList.add('shadow-split-widget');
Joel Einbinder27131b22019-03-14 09:29:5459 this._sidebarElement =
60 this.contentElement.createChild('div', 'shadow-split-widget-contents shadow-split-widget-sidebar vbox');
Blink Reformat4c46d092018-04-07 15:32:3761 this._mainElement =
62 this.contentElement.createChild('div', 'shadow-split-widget-contents shadow-split-widget-main vbox');
Joel Einbinder7fbe24c2019-01-24 05:19:0163 this._mainElement.createChild('slot').name = 'insertion-point-main';
Joel Einbinder7fbe24c2019-01-24 05:19:0164 this._sidebarElement.createChild('slot').name = 'insertion-point-sidebar';
Blink Reformat4c46d092018-04-07 15:32:3765 this._resizerElement = this.contentElement.createChild('div', 'shadow-split-widget-resizer');
66 this._resizerElementSize = null;
67
Paul Lewis9950e182019-12-16 16:06:0768 this._resizerWidget = new SimpleResizerWidget();
Blink Reformat4c46d092018-04-07 15:32:3769 this._resizerWidget.setEnabled(true);
Paul Lewis9950e182019-12-16 16:06:0770 this._resizerWidget.addEventListener(ResizerWidgetEvents.ResizeStart, this._onResizeStart, this);
71 this._resizerWidget.addEventListener(ResizerWidgetEvents.ResizeUpdate, this._onResizeUpdate, this);
72 this._resizerWidget.addEventListener(ResizerWidgetEvents.ResizeEnd, this._onResizeEnd, this);
Blink Reformat4c46d092018-04-07 15:32:3773
74 this._defaultSidebarWidth = defaultSidebarWidth || 200;
75 this._defaultSidebarHeight = defaultSidebarHeight || this._defaultSidebarWidth;
76 this._constraintsInDip = !!constraintsInDip;
77 this._resizeStartSizeDIP = 0;
Paul Lewis17e384e2020-01-08 15:46:5178 // Note: go via self.Common for globally-namespaced singletons.
Paul Lewis2d7d65c2020-03-16 17:26:3079 this._setting = settingName ? Common.Settings.Settings.instance().createSetting(settingName, {}) : null;
Blink Reformat4c46d092018-04-07 15:32:3780
81 this._totalSizeCSS = 0;
82 this._totalSizeOtherDimensionCSS = 0;
Paul Lewis9950e182019-12-16 16:06:0783 /** @type {?Widget} */
Blink Reformat4c46d092018-04-07 15:32:3784 this._mainWidget = null;
Paul Lewis9950e182019-12-16 16:06:0785 /** @type {?Widget} */
Blink Reformat4c46d092018-04-07 15:32:3786 this._sidebarWidget = null;
87 this._animationFrameHandle = 0;
Tim van der Lippeee97fa32020-04-23 15:20:5688 /** @type {?function():void} */
Blink Reformat4c46d092018-04-07 15:32:3789 this._animationCallback = null;
90 this._showHideSidebarButtonTitle = '';
Paul Lewis9950e182019-12-16 16:06:0791 /** @type {?ToolbarButton} */
Blink Reformat4c46d092018-04-07 15:32:3792 this._showHideSidebarButton = null;
93 this._isVertical = false;
94 this._sidebarMinimized = false;
95 this._detaching = false;
96 this._sidebarSizeDIP = -1;
97 this._savedSidebarSizeDIP = this._sidebarSizeDIP;
98 this._secondIsSidebar = false;
99 this._shouldSaveShowMode = false;
100 /** @type {?number} */
101 this._savedVerticalMainSize = null;
102 /** @type {?number} */
103 this._savedHorizontalMainSize = null;
104
105 this.setSecondIsSidebar(secondIsSidebar);
106
107 this._innerSetVertical(isVertical);
Tim van der Lippe0830b3d2019-10-03 13:20:07108 this._showMode = ShowMode.Both;
Blink Reformat4c46d092018-04-07 15:32:37109 this._savedShowMode = this._showMode;
110
111 // Should be called after isVertical has the right value.
112 this.installResizer(this._resizerElement);
113 }
114
115 /**
116 * @return {boolean}
117 */
118 isVertical() {
119 return this._isVertical;
120 }
121
122 /**
123 * @param {boolean} isVertical
124 */
125 setVertical(isVertical) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34126 if (this._isVertical === isVertical) {
Blink Reformat4c46d092018-04-07 15:32:37127 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34128 }
Blink Reformat4c46d092018-04-07 15:32:37129
130 this._innerSetVertical(isVertical);
131
Tim van der Lippe1d6e57a2019-09-30 11:55:34132 if (this.isShowing()) {
Blink Reformat4c46d092018-04-07 15:32:37133 this._updateLayout();
Tim van der Lippe1d6e57a2019-09-30 11:55:34134 }
Blink Reformat4c46d092018-04-07 15:32:37135 }
136
137 /**
138 * @param {boolean} isVertical
139 */
140 _innerSetVertical(isVertical) {
141 this.contentElement.classList.toggle('vbox', !isVertical);
142 this.contentElement.classList.toggle('hbox', isVertical);
143 this._isVertical = isVertical;
144
145 this._resizerElementSize = null;
146 this._sidebarSizeDIP = -1;
147 this._restoreSidebarSizeFromSettings();
Tim van der Lippe1d6e57a2019-09-30 11:55:34148 if (this._shouldSaveShowMode) {
Blink Reformat4c46d092018-04-07 15:32:37149 this._restoreAndApplyShowModeFromSettings();
Tim van der Lippe1d6e57a2019-09-30 11:55:34150 }
Blink Reformat4c46d092018-04-07 15:32:37151 this._updateShowHideSidebarButton();
152 // FIXME: reverse SplitWidget.isVertical meaning.
153 this._resizerWidget.setVertical(!isVertical);
154 this.invalidateConstraints();
155 }
156
157 /**
158 * @param {boolean=} animate
159 */
160 _updateLayout(animate) {
161 this._totalSizeCSS = 0; // Lazy update.
162 this._totalSizeOtherDimensionCSS = 0;
163
164 // Remove properties that might affect total size calculation.
165 this._mainElement.style.removeProperty('width');
166 this._mainElement.style.removeProperty('height');
167 this._sidebarElement.style.removeProperty('width');
168 this._sidebarElement.style.removeProperty('height');
169
170 this._innerSetSidebarSizeDIP(this._preferredSidebarSizeDIP(), !!animate);
171 }
172
173 /**
Paul Lewis9950e182019-12-16 16:06:07174 * @param {!Widget} widget
Blink Reformat4c46d092018-04-07 15:32:37175 */
176 setMainWidget(widget) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34177 if (this._mainWidget === widget) {
Blink Reformat4c46d092018-04-07 15:32:37178 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34179 }
Blink Reformat4c46d092018-04-07 15:32:37180 this.suspendInvalidations();
Tim van der Lippe1d6e57a2019-09-30 11:55:34181 if (this._mainWidget) {
Blink Reformat4c46d092018-04-07 15:32:37182 this._mainWidget.detach();
Tim van der Lippe1d6e57a2019-09-30 11:55:34183 }
Blink Reformat4c46d092018-04-07 15:32:37184 this._mainWidget = widget;
185 if (widget) {
Joel Einbinder7fbe24c2019-01-24 05:19:01186 widget.element.slot = 'insertion-point-main';
Tim van der Lippe0830b3d2019-10-03 13:20:07187 if (this._showMode === ShowMode.OnlyMain || this._showMode === ShowMode.Both) {
Blink Reformat4c46d092018-04-07 15:32:37188 widget.show(this.element);
Tim van der Lippe1d6e57a2019-09-30 11:55:34189 }
Blink Reformat4c46d092018-04-07 15:32:37190 }
191 this.resumeInvalidations();
192 }
193
194 /**
Paul Lewis9950e182019-12-16 16:06:07195 * @param {!Widget} widget
Blink Reformat4c46d092018-04-07 15:32:37196 */
197 setSidebarWidget(widget) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34198 if (this._sidebarWidget === widget) {
Blink Reformat4c46d092018-04-07 15:32:37199 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34200 }
Blink Reformat4c46d092018-04-07 15:32:37201 this.suspendInvalidations();
Tim van der Lippe1d6e57a2019-09-30 11:55:34202 if (this._sidebarWidget) {
Blink Reformat4c46d092018-04-07 15:32:37203 this._sidebarWidget.detach();
Tim van der Lippe1d6e57a2019-09-30 11:55:34204 }
Blink Reformat4c46d092018-04-07 15:32:37205 this._sidebarWidget = widget;
206 if (widget) {
Joel Einbinder7fbe24c2019-01-24 05:19:01207 widget.element.slot = 'insertion-point-sidebar';
Tim van der Lippe0830b3d2019-10-03 13:20:07208 if (this._showMode === ShowMode.OnlySidebar || this._showMode === ShowMode.Both) {
Blink Reformat4c46d092018-04-07 15:32:37209 widget.show(this.element);
Tim van der Lippe1d6e57a2019-09-30 11:55:34210 }
Blink Reformat4c46d092018-04-07 15:32:37211 }
212 this.resumeInvalidations();
213 }
214
215 /**
Paul Lewis9950e182019-12-16 16:06:07216 * @return {?Widget}
Blink Reformat4c46d092018-04-07 15:32:37217 */
218 mainWidget() {
219 return this._mainWidget;
220 }
221
222 /**
Paul Lewis9950e182019-12-16 16:06:07223 * @return {?Widget}
Blink Reformat4c46d092018-04-07 15:32:37224 */
225 sidebarWidget() {
226 return this._sidebarWidget;
227 }
228
229 /**
230 * @override
Paul Lewis9950e182019-12-16 16:06:07231 * @param {!Widget} widget
Blink Reformat4c46d092018-04-07 15:32:37232 */
233 childWasDetached(widget) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34234 if (this._detaching) {
Blink Reformat4c46d092018-04-07 15:32:37235 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34236 }
237 if (this._mainWidget === widget) {
Blink Reformat4c46d092018-04-07 15:32:37238 this._mainWidget = null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34239 }
240 if (this._sidebarWidget === widget) {
Blink Reformat4c46d092018-04-07 15:32:37241 this._sidebarWidget = null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34242 }
Blink Reformat4c46d092018-04-07 15:32:37243 this.invalidateConstraints();
244 }
245
246 /**
247 * @return {boolean}
248 */
249 isSidebarSecond() {
250 return this._secondIsSidebar;
251 }
252
253 enableShowModeSaving() {
254 this._shouldSaveShowMode = true;
255 this._restoreAndApplyShowModeFromSettings();
256 }
257
258 /**
259 * @return {string}
260 */
261 showMode() {
262 return this._showMode;
263 }
264
265 /**
266 * @param {boolean} secondIsSidebar
267 */
268 setSecondIsSidebar(secondIsSidebar) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34269 if (secondIsSidebar === this._secondIsSidebar) {
Joel Einbinder27131b22019-03-14 09:29:54270 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34271 }
Blink Reformat4c46d092018-04-07 15:32:37272 this._secondIsSidebar = secondIsSidebar;
Joel Einbinder27131b22019-03-14 09:29:54273 if (!this._mainWidget || !this._mainWidget.shouldHideOnDetach()) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34274 if (secondIsSidebar) {
Joel Einbinder27131b22019-03-14 09:29:54275 this.contentElement.insertBefore(this._mainElement, this._sidebarElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:34276 } else {
Joel Einbinder27131b22019-03-14 09:29:54277 this.contentElement.insertBefore(this._mainElement, this._resizerElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:34278 }
Joel Einbinder27131b22019-03-14 09:29:54279 } else if (!this._sidebarWidget || !this._sidebarWidget.shouldHideOnDetach()) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34280 if (secondIsSidebar) {
Joel Einbinder27131b22019-03-14 09:29:54281 this.contentElement.insertBefore(this._sidebarElement, this._resizerElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:34282 } else {
Joel Einbinder27131b22019-03-14 09:29:54283 this.contentElement.insertBefore(this._sidebarElement, this._mainElement);
Tim van der Lippe1d6e57a2019-09-30 11:55:34284 }
Joel Einbinder27131b22019-03-14 09:29:54285 } else {
286 console.error('Could not swap split widget side. Both children widgets contain iframes.');
287 this._secondIsSidebar = !secondIsSidebar;
288 }
Blink Reformat4c46d092018-04-07 15:32:37289 }
290
291 /**
292 * @return {?string}
293 */
294 sidebarSide() {
Tim van der Lippe0830b3d2019-10-03 13:20:07295 if (this._showMode !== ShowMode.Both) {
Blink Reformat4c46d092018-04-07 15:32:37296 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34297 }
Blink Reformat4c46d092018-04-07 15:32:37298 return this._isVertical ? (this._secondIsSidebar ? 'right' : 'left') : (this._secondIsSidebar ? 'bottom' : 'top');
299 }
300
301 /**
302 * @return {!Element}
303 */
304 resizerElement() {
305 return this._resizerElement;
306 }
307
308 /**
309 * @param {boolean=} animate
310 */
311 hideMain(animate) {
312 this._showOnly(this._sidebarWidget, this._mainWidget, this._sidebarElement, this._mainElement, animate);
Tim van der Lippe0830b3d2019-10-03 13:20:07313 this._updateShowMode(ShowMode.OnlySidebar);
Blink Reformat4c46d092018-04-07 15:32:37314 }
315
316 /**
317 * @param {boolean=} animate
318 */
319 hideSidebar(animate) {
320 this._showOnly(this._mainWidget, this._sidebarWidget, this._mainElement, this._sidebarElement, animate);
Tim van der Lippe0830b3d2019-10-03 13:20:07321 this._updateShowMode(ShowMode.OnlyMain);
Blink Reformat4c46d092018-04-07 15:32:37322 }
323
324 /**
325 * @param {boolean} minimized
326 */
327 setSidebarMinimized(minimized) {
328 this._sidebarMinimized = minimized;
329 this.invalidateConstraints();
330 }
331
332 /**
333 * @return {boolean}
334 */
335 isSidebarMinimized() {
336 return this._sidebarMinimized;
337 }
338
339 /**
Paul Lewis9950e182019-12-16 16:06:07340 * @param {?Widget} sideToShow
Tim van der Lippeaa76aa22020-02-14 14:38:24341 * @param {?Widget} sideToHide
Blink Reformat4c46d092018-04-07 15:32:37342 * @param {!Element} shadowToShow
343 * @param {!Element} shadowToHide
344 * @param {boolean=} animate
345 */
346 _showOnly(sideToShow, sideToHide, shadowToShow, shadowToHide, animate) {
347 this._cancelAnimation();
348
349 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07350 * @this {SplitWidget}
Blink Reformat4c46d092018-04-07 15:32:37351 */
352 function callback() {
353 if (sideToShow) {
354 // Make sure main is first in the children list.
Tim van der Lippe1d6e57a2019-09-30 11:55:34355 if (sideToShow === this._mainWidget) {
Blink Reformat4c46d092018-04-07 15:32:37356 this._mainWidget.show(this.element, this._sidebarWidget ? this._sidebarWidget.element : null);
Tim van der Lippe1d6e57a2019-09-30 11:55:34357 } else {
Blink Reformat4c46d092018-04-07 15:32:37358 this._sidebarWidget.show(this.element);
Tim van der Lippe1d6e57a2019-09-30 11:55:34359 }
Blink Reformat4c46d092018-04-07 15:32:37360 }
361 if (sideToHide) {
362 this._detaching = true;
363 sideToHide.detach();
364 this._detaching = false;
365 }
366
367 this._resizerElement.classList.add('hidden');
368 shadowToShow.classList.remove('hidden');
369 shadowToShow.classList.add('maximized');
370 shadowToHide.classList.add('hidden');
371 shadowToHide.classList.remove('maximized');
372 this._removeAllLayoutProperties();
373 this.doResize();
374 this._showFinishedForTest();
375 }
376
Tim van der Lippe1d6e57a2019-09-30 11:55:34377 if (animate) {
Blink Reformat4c46d092018-04-07 15:32:37378 this._animate(true, callback.bind(this));
Tim van der Lippe1d6e57a2019-09-30 11:55:34379 } else {
Blink Reformat4c46d092018-04-07 15:32:37380 callback.call(this);
Tim van der Lippe1d6e57a2019-09-30 11:55:34381 }
Blink Reformat4c46d092018-04-07 15:32:37382
383 this._sidebarSizeDIP = -1;
384 this.setResizable(false);
385 }
386
387 _showFinishedForTest() {
388 // This method is sniffed in tests.
389 }
390
391 _removeAllLayoutProperties() {
392 this._sidebarElement.style.removeProperty('flexBasis');
393
394 this._mainElement.style.removeProperty('width');
395 this._mainElement.style.removeProperty('height');
396 this._sidebarElement.style.removeProperty('width');
397 this._sidebarElement.style.removeProperty('height');
398
399 this._resizerElement.style.removeProperty('left');
400 this._resizerElement.style.removeProperty('right');
401 this._resizerElement.style.removeProperty('top');
402 this._resizerElement.style.removeProperty('bottom');
403
404 this._resizerElement.style.removeProperty('margin-left');
405 this._resizerElement.style.removeProperty('margin-right');
406 this._resizerElement.style.removeProperty('margin-top');
407 this._resizerElement.style.removeProperty('margin-bottom');
408 }
409
410 /**
411 * @param {boolean=} animate
412 */
413 showBoth(animate) {
Tim van der Lippe0830b3d2019-10-03 13:20:07414 if (this._showMode === ShowMode.Both) {
Blink Reformat4c46d092018-04-07 15:32:37415 animate = false;
Tim van der Lippe1d6e57a2019-09-30 11:55:34416 }
Blink Reformat4c46d092018-04-07 15:32:37417
418 this._cancelAnimation();
419 this._mainElement.classList.remove('maximized', 'hidden');
420 this._sidebarElement.classList.remove('maximized', 'hidden');
421 this._resizerElement.classList.remove('hidden');
422 this.setResizable(true);
423
424 // Make sure main is the first in the children list.
425 this.suspendInvalidations();
Tim van der Lippe1d6e57a2019-09-30 11:55:34426 if (this._sidebarWidget) {
Blink Reformat4c46d092018-04-07 15:32:37427 this._sidebarWidget.show(this.element);
Tim van der Lippe1d6e57a2019-09-30 11:55:34428 }
429 if (this._mainWidget) {
Blink Reformat4c46d092018-04-07 15:32:37430 this._mainWidget.show(this.element, this._sidebarWidget ? this._sidebarWidget.element : null);
Tim van der Lippe1d6e57a2019-09-30 11:55:34431 }
Blink Reformat4c46d092018-04-07 15:32:37432 this.resumeInvalidations();
433 // Order widgets in DOM properly.
434 this.setSecondIsSidebar(this._secondIsSidebar);
435
436 this._sidebarSizeDIP = -1;
Tim van der Lippe0830b3d2019-10-03 13:20:07437 this._updateShowMode(ShowMode.Both);
Blink Reformat4c46d092018-04-07 15:32:37438 this._updateLayout(animate);
439 }
440
441 /**
442 * @param {boolean} resizable
443 */
444 setResizable(resizable) {
445 this._resizerWidget.setEnabled(resizable);
446 }
447
448 /**
449 * @return {boolean}
450 */
451 isResizable() {
452 return this._resizerWidget.isEnabled();
453 }
454
455 /**
456 * @param {number} size
457 */
458 setSidebarSize(size) {
Paul Lewis6c914a12020-03-19 11:23:21459 const sizeDIP = ZoomManager.instance().cssToDIP(size);
Blink Reformat4c46d092018-04-07 15:32:37460 this._savedSidebarSizeDIP = sizeDIP;
461 this._saveSetting();
462 this._innerSetSidebarSizeDIP(sizeDIP, false, true);
463 }
464
465 /**
466 * @return {number}
467 */
468 sidebarSize() {
469 const sizeDIP = Math.max(0, this._sidebarSizeDIP);
Paul Lewis6c914a12020-03-19 11:23:21470 return ZoomManager.instance().dipToCSS(sizeDIP);
Blink Reformat4c46d092018-04-07 15:32:37471 }
472
473 /**
474 * Returns total size in DIP.
475 * @return {number}
476 */
477 _totalSizeDIP() {
478 if (!this._totalSizeCSS) {
479 this._totalSizeCSS = this._isVertical ? this.contentElement.offsetWidth : this.contentElement.offsetHeight;
480 this._totalSizeOtherDimensionCSS =
481 this._isVertical ? this.contentElement.offsetHeight : this.contentElement.offsetWidth;
482 }
Paul Lewis6c914a12020-03-19 11:23:21483 return ZoomManager.instance().cssToDIP(this._totalSizeCSS);
Blink Reformat4c46d092018-04-07 15:32:37484 }
485
486 /**
487 * @param {string} showMode
488 */
489 _updateShowMode(showMode) {
490 this._showMode = showMode;
491 this._saveShowModeToSettings();
492 this._updateShowHideSidebarButton();
Tim van der Lippe3fffd0d2020-03-27 15:47:51493 this.dispatchEventToListeners(Events.ShowModeChanged, showMode);
Blink Reformat4c46d092018-04-07 15:32:37494 this.invalidateConstraints();
495 }
496
497 /**
498 * @param {number} sizeDIP
499 * @param {boolean} animate
500 * @param {boolean=} userAction
501 */
502 _innerSetSidebarSizeDIP(sizeDIP, animate, userAction) {
Tim van der Lippe0830b3d2019-10-03 13:20:07503 if (this._showMode !== ShowMode.Both || !this.isShowing()) {
Blink Reformat4c46d092018-04-07 15:32:37504 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34505 }
Blink Reformat4c46d092018-04-07 15:32:37506
507 sizeDIP = this._applyConstraints(sizeDIP, userAction);
Tim van der Lippe1d6e57a2019-09-30 11:55:34508 if (this._sidebarSizeDIP === sizeDIP) {
Blink Reformat4c46d092018-04-07 15:32:37509 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34510 }
Blink Reformat4c46d092018-04-07 15:32:37511
512 if (!this._resizerElementSize) {
513 this._resizerElementSize =
514 this._isVertical ? this._resizerElement.offsetWidth : this._resizerElement.offsetHeight;
515 }
516
517 // Invalidate layout below.
518
519 this._removeAllLayoutProperties();
520
521 // this._totalSizeDIP is available below since we successfully applied constraints.
Paul Lewis6c914a12020-03-19 11:23:21522 const roundSizeCSS = Math.round(ZoomManager.instance().dipToCSS(sizeDIP));
Blink Reformat4c46d092018-04-07 15:32:37523 const sidebarSizeValue = roundSizeCSS + 'px';
524 const mainSizeValue = (this._totalSizeCSS - roundSizeCSS) + 'px';
525 this._sidebarElement.style.flexBasis = sidebarSizeValue;
526
527 // Make both sides relayout boundaries.
528 if (this._isVertical) {
529 this._sidebarElement.style.width = sidebarSizeValue;
530 this._mainElement.style.width = mainSizeValue;
531 this._sidebarElement.style.height = this._totalSizeOtherDimensionCSS + 'px';
532 this._mainElement.style.height = this._totalSizeOtherDimensionCSS + 'px';
533 } else {
534 this._sidebarElement.style.height = sidebarSizeValue;
535 this._mainElement.style.height = mainSizeValue;
536 this._sidebarElement.style.width = this._totalSizeOtherDimensionCSS + 'px';
537 this._mainElement.style.width = this._totalSizeOtherDimensionCSS + 'px';
538 }
539
540 // Position resizer.
541 if (this._isVertical) {
542 if (this._secondIsSidebar) {
543 this._resizerElement.style.right = sidebarSizeValue;
544 this._resizerElement.style.marginRight = -this._resizerElementSize / 2 + 'px';
545 } else {
546 this._resizerElement.style.left = sidebarSizeValue;
547 this._resizerElement.style.marginLeft = -this._resizerElementSize / 2 + 'px';
548 }
549 } else {
550 if (this._secondIsSidebar) {
551 this._resizerElement.style.bottom = sidebarSizeValue;
552 this._resizerElement.style.marginBottom = -this._resizerElementSize / 2 + 'px';
553 } else {
554 this._resizerElement.style.top = sidebarSizeValue;
555 this._resizerElement.style.marginTop = -this._resizerElementSize / 2 + 'px';
556 }
557 }
558
559 this._sidebarSizeDIP = sizeDIP;
560
561 // Force layout.
562
563 if (animate) {
564 this._animate(false);
565 } else {
566 // No need to recalculate this._sidebarSizeDIP and this._totalSizeDIP again.
567 this.doResize();
Tim van der Lippe3fffd0d2020-03-27 15:47:51568 this.dispatchEventToListeners(Events.SidebarSizeChanged, this.sidebarSize());
Blink Reformat4c46d092018-04-07 15:32:37569 }
570 }
571
572 /**
573 * @param {boolean} reverse
Tim van der Lippe403a88d2020-05-13 11:51:32574 * @param {function():void=} callback
Blink Reformat4c46d092018-04-07 15:32:37575 */
576 _animate(reverse, callback) {
577 const animationTime = 50;
578 this._animationCallback = callback || null;
579
580 let animatedMarginPropertyName;
Tim van der Lippe1d6e57a2019-09-30 11:55:34581 if (this._isVertical) {
Blink Reformat4c46d092018-04-07 15:32:37582 animatedMarginPropertyName = this._secondIsSidebar ? 'margin-right' : 'margin-left';
Tim van der Lippe1d6e57a2019-09-30 11:55:34583 } else {
Blink Reformat4c46d092018-04-07 15:32:37584 animatedMarginPropertyName = this._secondIsSidebar ? 'margin-bottom' : 'margin-top';
Tim van der Lippe1d6e57a2019-09-30 11:55:34585 }
Blink Reformat4c46d092018-04-07 15:32:37586
Paul Lewis6c914a12020-03-19 11:23:21587 const marginFrom = reverse ? '0' : '-' + ZoomManager.instance().dipToCSS(this._sidebarSizeDIP) + 'px';
588 const marginTo = reverse ? '-' + ZoomManager.instance().dipToCSS(this._sidebarSizeDIP) + 'px' : '0';
Blink Reformat4c46d092018-04-07 15:32:37589
590 // This order of things is important.
591 // 1. Resize main element early and force layout.
592 this.contentElement.style.setProperty(animatedMarginPropertyName, marginFrom);
593 if (!reverse) {
594 suppressUnused(this._mainElement.offsetWidth);
595 suppressUnused(this._sidebarElement.offsetWidth);
596 }
597
598 // 2. Issue onresize to the sidebar element, its size won't change.
Tim van der Lippe1d6e57a2019-09-30 11:55:34599 if (!reverse) {
Blink Reformat4c46d092018-04-07 15:32:37600 this._sidebarWidget.doResize();
Tim van der Lippe1d6e57a2019-09-30 11:55:34601 }
Blink Reformat4c46d092018-04-07 15:32:37602
603 // 3. Configure and run animation
604 this.contentElement.style.setProperty('transition', animatedMarginPropertyName + ' ' + animationTime + 'ms linear');
605
606 const boundAnimationFrame = animationFrame.bind(this);
607 let startTime;
608 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07609 * @this {SplitWidget}
Blink Reformat4c46d092018-04-07 15:32:37610 */
611 function animationFrame() {
612 this._animationFrameHandle = 0;
613
614 if (!startTime) {
615 // Kick animation on first frame.
616 this.contentElement.style.setProperty(animatedMarginPropertyName, marginTo);
617 startTime = window.performance.now();
618 } else if (window.performance.now() < startTime + animationTime) {
619 // Process regular animation frame.
Tim van der Lippe1d6e57a2019-09-30 11:55:34620 if (this._mainWidget) {
Blink Reformat4c46d092018-04-07 15:32:37621 this._mainWidget.doResize();
Tim van der Lippe1d6e57a2019-09-30 11:55:34622 }
Blink Reformat4c46d092018-04-07 15:32:37623 } else {
624 // Complete animation.
625 this._cancelAnimation();
Tim van der Lippe1d6e57a2019-09-30 11:55:34626 if (this._mainWidget) {
Blink Reformat4c46d092018-04-07 15:32:37627 this._mainWidget.doResize();
Tim van der Lippe1d6e57a2019-09-30 11:55:34628 }
Tim van der Lippe3fffd0d2020-03-27 15:47:51629 this.dispatchEventToListeners(Events.SidebarSizeChanged, this.sidebarSize());
Blink Reformat4c46d092018-04-07 15:32:37630 return;
631 }
632 this._animationFrameHandle = this.contentElement.window().requestAnimationFrame(boundAnimationFrame);
633 }
634 this._animationFrameHandle = this.contentElement.window().requestAnimationFrame(boundAnimationFrame);
635 }
636
637 _cancelAnimation() {
638 this.contentElement.style.removeProperty('margin-top');
639 this.contentElement.style.removeProperty('margin-right');
640 this.contentElement.style.removeProperty('margin-bottom');
641 this.contentElement.style.removeProperty('margin-left');
642 this.contentElement.style.removeProperty('transition');
643
644 if (this._animationFrameHandle) {
645 this.contentElement.window().cancelAnimationFrame(this._animationFrameHandle);
646 this._animationFrameHandle = 0;
647 }
648 if (this._animationCallback) {
649 this._animationCallback();
650 this._animationCallback = null;
651 }
652 }
653
654 /**
655 * @param {number} sidebarSize
656 * @param {boolean=} userAction
657 * @return {number}
658 */
659 _applyConstraints(sidebarSize, userAction) {
660 const totalSize = this._totalSizeDIP();
Paul Lewis6c914a12020-03-19 11:23:21661 const zoomFactor = this._constraintsInDip ? 1 : ZoomManager.instance().zoomFactor();
Blink Reformat4c46d092018-04-07 15:32:37662
Paul Lewis9950e182019-12-16 16:06:07663 let constraints = this._sidebarWidget ? this._sidebarWidget.constraints() : new Constraints();
Blink Reformat4c46d092018-04-07 15:32:37664 let minSidebarSize = this.isVertical() ? constraints.minimum.width : constraints.minimum.height;
Tim van der Lippe1d6e57a2019-09-30 11:55:34665 if (!minSidebarSize) {
Tim van der Lippe0830b3d2019-10-03 13:20:07666 minSidebarSize = MinPadding;
Tim van der Lippe1d6e57a2019-09-30 11:55:34667 }
Blink Reformat4c46d092018-04-07 15:32:37668 minSidebarSize *= zoomFactor;
Tim van der Lippe1d6e57a2019-09-30 11:55:34669 if (this._sidebarMinimized) {
Blink Reformat4c46d092018-04-07 15:32:37670 sidebarSize = minSidebarSize;
Tim van der Lippe1d6e57a2019-09-30 11:55:34671 }
Blink Reformat4c46d092018-04-07 15:32:37672
673 let preferredSidebarSize = this.isVertical() ? constraints.preferred.width : constraints.preferred.height;
Tim van der Lippe1d6e57a2019-09-30 11:55:34674 if (!preferredSidebarSize) {
Tim van der Lippe0830b3d2019-10-03 13:20:07675 preferredSidebarSize = MinPadding;
Tim van der Lippe1d6e57a2019-09-30 11:55:34676 }
Blink Reformat4c46d092018-04-07 15:32:37677 preferredSidebarSize *= zoomFactor;
678 // Allow sidebar to be less than preferred by explicit user action.
Tim van der Lippe1d6e57a2019-09-30 11:55:34679 if (sidebarSize < preferredSidebarSize) {
Blink Reformat4c46d092018-04-07 15:32:37680 preferredSidebarSize = Math.max(sidebarSize, minSidebarSize);
Tim van der Lippe1d6e57a2019-09-30 11:55:34681 }
Blink Reformat4c46d092018-04-07 15:32:37682 preferredSidebarSize += zoomFactor; // 1 css pixel for splitter border.
683
Paul Lewis9950e182019-12-16 16:06:07684 constraints = this._mainWidget ? this._mainWidget.constraints() : new Constraints();
Blink Reformat4c46d092018-04-07 15:32:37685 let minMainSize = this.isVertical() ? constraints.minimum.width : constraints.minimum.height;
Tim van der Lippe1d6e57a2019-09-30 11:55:34686 if (!minMainSize) {
Tim van der Lippe0830b3d2019-10-03 13:20:07687 minMainSize = MinPadding;
Tim van der Lippe1d6e57a2019-09-30 11:55:34688 }
Blink Reformat4c46d092018-04-07 15:32:37689 minMainSize *= zoomFactor;
690
691 let preferredMainSize = this.isVertical() ? constraints.preferred.width : constraints.preferred.height;
Tim van der Lippe1d6e57a2019-09-30 11:55:34692 if (!preferredMainSize) {
Tim van der Lippe0830b3d2019-10-03 13:20:07693 preferredMainSize = MinPadding;
Tim van der Lippe1d6e57a2019-09-30 11:55:34694 }
Blink Reformat4c46d092018-04-07 15:32:37695 preferredMainSize *= zoomFactor;
696 const savedMainSize = this.isVertical() ? this._savedVerticalMainSize : this._savedHorizontalMainSize;
Tim van der Lippe1d6e57a2019-09-30 11:55:34697 if (savedMainSize !== null) {
Blink Reformat4c46d092018-04-07 15:32:37698 preferredMainSize = Math.min(preferredMainSize, savedMainSize * zoomFactor);
Tim van der Lippe1d6e57a2019-09-30 11:55:34699 }
700 if (userAction) {
Blink Reformat4c46d092018-04-07 15:32:37701 preferredMainSize = minMainSize;
Tim van der Lippe1d6e57a2019-09-30 11:55:34702 }
Blink Reformat4c46d092018-04-07 15:32:37703
704 // Enough space for preferred.
705 const totalPreferred = preferredMainSize + preferredSidebarSize;
Tim van der Lippe1d6e57a2019-09-30 11:55:34706 if (totalPreferred <= totalSize) {
Jack Franklin1be909c2020-03-04 08:57:41707 return Platform.NumberUtilities.clamp(sidebarSize, preferredSidebarSize, totalSize - preferredMainSize);
Tim van der Lippe1d6e57a2019-09-30 11:55:34708 }
Blink Reformat4c46d092018-04-07 15:32:37709
710 // Enough space for minimum.
711 if (minMainSize + minSidebarSize <= totalSize) {
712 const delta = totalPreferred - totalSize;
713 const sidebarDelta = delta * preferredSidebarSize / totalPreferred;
714 sidebarSize = preferredSidebarSize - sidebarDelta;
Jack Franklin1be909c2020-03-04 08:57:41715 return Platform.NumberUtilities.clamp(sidebarSize, minSidebarSize, totalSize - minMainSize);
Blink Reformat4c46d092018-04-07 15:32:37716 }
717
718 // Not enough space even for minimum sizes.
719 return Math.max(0, totalSize - minMainSize);
720 }
721
722 /**
723 * @override
724 */
725 wasShown() {
726 this._forceUpdateLayout();
Paul Lewis6c914a12020-03-19 11:23:21727 ZoomManager.instance().addEventListener(ZoomManagerEvents.ZoomChanged, this._onZoomChanged, this);
Blink Reformat4c46d092018-04-07 15:32:37728 }
729
730 /**
731 * @override
732 */
733 willHide() {
Paul Lewis6c914a12020-03-19 11:23:21734 ZoomManager.instance().removeEventListener(ZoomManagerEvents.ZoomChanged, this._onZoomChanged, this);
Blink Reformat4c46d092018-04-07 15:32:37735 }
736
737 /**
738 * @override
739 */
740 onResize() {
741 this._updateLayout();
742 }
743
744 /**
745 * @override
746 */
747 onLayout() {
748 this._updateLayout();
749 }
750
751 /**
752 * @override
Paul Lewis9950e182019-12-16 16:06:07753 * @return {!Constraints}
Blink Reformat4c46d092018-04-07 15:32:37754 */
755 calculateConstraints() {
Tim van der Lippe0830b3d2019-10-03 13:20:07756 if (this._showMode === ShowMode.OnlyMain) {
Paul Lewis9950e182019-12-16 16:06:07757 return this._mainWidget ? this._mainWidget.constraints() : new Constraints();
Tim van der Lippe1d6e57a2019-09-30 11:55:34758 }
Tim van der Lippe0830b3d2019-10-03 13:20:07759 if (this._showMode === ShowMode.OnlySidebar) {
Paul Lewis9950e182019-12-16 16:06:07760 return this._sidebarWidget ? this._sidebarWidget.constraints() : new Constraints();
Tim van der Lippe1d6e57a2019-09-30 11:55:34761 }
Blink Reformat4c46d092018-04-07 15:32:37762
Paul Lewis9950e182019-12-16 16:06:07763 let mainConstraints = this._mainWidget ? this._mainWidget.constraints() : new Constraints();
764 let sidebarConstraints = this._sidebarWidget ? this._sidebarWidget.constraints() : new Constraints();
Tim van der Lippe0830b3d2019-10-03 13:20:07765 const min = MinPadding;
Blink Reformat4c46d092018-04-07 15:32:37766 if (this._isVertical) {
767 mainConstraints = mainConstraints.widthToMax(min).addWidth(1); // 1 for splitter
768 sidebarConstraints = sidebarConstraints.widthToMax(min);
769 return mainConstraints.addWidth(sidebarConstraints).heightToMax(sidebarConstraints);
Mathias Bynensf06e8c02020-02-28 13:58:28770 }
Blink Reformat4c46d092018-04-07 15:32:37771 mainConstraints = mainConstraints.heightToMax(min).addHeight(1); // 1 for splitter
772 sidebarConstraints = sidebarConstraints.heightToMax(min);
773 return mainConstraints.widthToMax(sidebarConstraints).addHeight(sidebarConstraints);
Blink Reformat4c46d092018-04-07 15:32:37774 }
775
776 /**
Tim van der Lippec02a97c2020-02-14 14:39:27777 * @param {!Common.EventTarget.EventTargetEvent} event
Blink Reformat4c46d092018-04-07 15:32:37778 */
779 _onResizeStart(event) {
780 this._resizeStartSizeDIP = this._sidebarSizeDIP;
781 }
782
783 /**
Tim van der Lippec02a97c2020-02-14 14:39:27784 * @param {!Common.EventTarget.EventTargetEvent} event
Blink Reformat4c46d092018-04-07 15:32:37785 */
786 _onResizeUpdate(event) {
787 const offset = event.data.currentPosition - event.data.startPosition;
Paul Lewis6c914a12020-03-19 11:23:21788 const offsetDIP = ZoomManager.instance().cssToDIP(offset);
Blink Reformat4c46d092018-04-07 15:32:37789 const newSizeDIP =
790 this._secondIsSidebar ? this._resizeStartSizeDIP - offsetDIP : this._resizeStartSizeDIP + offsetDIP;
791 const constrainedSizeDIP = this._applyConstraints(newSizeDIP, true);
792 this._savedSidebarSizeDIP = constrainedSizeDIP;
793 this._saveSetting();
794 this._innerSetSidebarSizeDIP(constrainedSizeDIP, false, true);
Tim van der Lippe1d6e57a2019-09-30 11:55:34795 if (this.isVertical()) {
Blink Reformat4c46d092018-04-07 15:32:37796 this._savedVerticalMainSize = this._totalSizeDIP() - this._sidebarSizeDIP;
Tim van der Lippe1d6e57a2019-09-30 11:55:34797 } else {
Blink Reformat4c46d092018-04-07 15:32:37798 this._savedHorizontalMainSize = this._totalSizeDIP() - this._sidebarSizeDIP;
Tim van der Lippe1d6e57a2019-09-30 11:55:34799 }
Blink Reformat4c46d092018-04-07 15:32:37800 }
801
802 /**
Tim van der Lippec02a97c2020-02-14 14:39:27803 * @param {!Common.EventTarget.EventTargetEvent} event
Blink Reformat4c46d092018-04-07 15:32:37804 */
805 _onResizeEnd(event) {
806 this._resizeStartSizeDIP = 0;
807 }
808
809 /**
810 * @param {boolean=} noSplitter
811 */
812 hideDefaultResizer(noSplitter) {
813 this.uninstallResizer(this._resizerElement);
Pavel Feldmanc9060ea2018-04-30 04:42:18814 this._sidebarElement.classList.toggle('no-default-splitter', !!noSplitter);
Blink Reformat4c46d092018-04-07 15:32:37815 }
816
817 /**
818 * @param {!Element} resizerElement
819 */
820 installResizer(resizerElement) {
Simon Zünd3141f412020-08-12 09:52:51821 this._resizerWidget.addElement(/** @type {!HTMLElement} */ (resizerElement));
Blink Reformat4c46d092018-04-07 15:32:37822 }
823
824 /**
825 * @param {!Element} resizerElement
826 */
827 uninstallResizer(resizerElement) {
Simon Zünd3141f412020-08-12 09:52:51828 this._resizerWidget.removeElement(/** @type {!HTMLElement} */ (resizerElement));
Blink Reformat4c46d092018-04-07 15:32:37829 }
830
831 /**
832 * @return {boolean}
833 */
834 hasCustomResizer() {
835 const elements = this._resizerWidget.elements();
836 return elements.length > 1 || (elements.length === 1 && elements[0] !== this._resizerElement);
837 }
838
839 /**
840 * @param {!Element} resizer
841 * @param {boolean} on
842 */
843 toggleResizer(resizer, on) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34844 if (on) {
Blink Reformat4c46d092018-04-07 15:32:37845 this.installResizer(resizer);
Tim van der Lippe1d6e57a2019-09-30 11:55:34846 } else {
Blink Reformat4c46d092018-04-07 15:32:37847 this.uninstallResizer(resizer);
Tim van der Lippe1d6e57a2019-09-30 11:55:34848 }
Blink Reformat4c46d092018-04-07 15:32:37849 }
850
851 /**
Tim van der Lippeaa76aa22020-02-14 14:38:24852 * @return {?SettingForOrientation}
Blink Reformat4c46d092018-04-07 15:32:37853 */
854 _settingForOrientation() {
855 const state = this._setting ? this._setting.get() : {};
856 return this._isVertical ? state.vertical : state.horizontal;
857 }
858
859 /**
860 * @return {number}
861 */
862 _preferredSidebarSizeDIP() {
863 let size = this._savedSidebarSizeDIP;
864 if (!size) {
865 size = this._isVertical ? this._defaultSidebarWidth : this._defaultSidebarHeight;
866 // If we have default value in percents, calculate it on first use.
Tim van der Lippe1d6e57a2019-09-30 11:55:34867 if (0 < size && size < 1) {
Blink Reformat4c46d092018-04-07 15:32:37868 size *= this._totalSizeDIP();
Tim van der Lippe1d6e57a2019-09-30 11:55:34869 }
Blink Reformat4c46d092018-04-07 15:32:37870 }
871 return size;
872 }
873
874 _restoreSidebarSizeFromSettings() {
875 const settingForOrientation = this._settingForOrientation();
876 this._savedSidebarSizeDIP = settingForOrientation ? settingForOrientation.size : 0;
877 }
878
879 _restoreAndApplyShowModeFromSettings() {
880 const orientationState = this._settingForOrientation();
881 this._savedShowMode = orientationState && orientationState.showMode ? orientationState.showMode : this._showMode;
882 this._showMode = this._savedShowMode;
883
884 switch (this._savedShowMode) {
Tim van der Lippe0830b3d2019-10-03 13:20:07885 case ShowMode.Both:
Blink Reformat4c46d092018-04-07 15:32:37886 this.showBoth();
887 break;
Tim van der Lippe0830b3d2019-10-03 13:20:07888 case ShowMode.OnlyMain:
Blink Reformat4c46d092018-04-07 15:32:37889 this.hideSidebar();
890 break;
Tim van der Lippe0830b3d2019-10-03 13:20:07891 case ShowMode.OnlySidebar:
Blink Reformat4c46d092018-04-07 15:32:37892 this.hideMain();
893 break;
894 }
895 }
896
897 _saveShowModeToSettings() {
898 this._savedShowMode = this._showMode;
899 this._saveSetting();
900 }
901
902 _saveSetting() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34903 if (!this._setting) {
Blink Reformat4c46d092018-04-07 15:32:37904 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34905 }
Blink Reformat4c46d092018-04-07 15:32:37906 const state = this._setting.get();
907 const orientationState = (this._isVertical ? state.vertical : state.horizontal) || {};
908
909 orientationState.size = this._savedSidebarSizeDIP;
Tim van der Lippe1d6e57a2019-09-30 11:55:34910 if (this._shouldSaveShowMode) {
Blink Reformat4c46d092018-04-07 15:32:37911 orientationState.showMode = this._savedShowMode;
Tim van der Lippe1d6e57a2019-09-30 11:55:34912 }
Blink Reformat4c46d092018-04-07 15:32:37913
Tim van der Lippe1d6e57a2019-09-30 11:55:34914 if (this._isVertical) {
Blink Reformat4c46d092018-04-07 15:32:37915 state.vertical = orientationState;
Tim van der Lippe1d6e57a2019-09-30 11:55:34916 } else {
Blink Reformat4c46d092018-04-07 15:32:37917 state.horizontal = orientationState;
Tim van der Lippe1d6e57a2019-09-30 11:55:34918 }
Blink Reformat4c46d092018-04-07 15:32:37919 this._setting.set(state);
920 }
921
922 _forceUpdateLayout() {
923 // Force layout even if sidebar size does not change.
924 this._sidebarSizeDIP = -1;
925 this._updateLayout();
926 }
927
928 /**
Tim van der Lippec02a97c2020-02-14 14:39:27929 * @param {!Common.EventTarget.EventTargetEvent} event
Blink Reformat4c46d092018-04-07 15:32:37930 */
931 _onZoomChanged(event) {
932 this._forceUpdateLayout();
933 }
934
935 /**
936 * @param {string} title
Paul Lewis9950e182019-12-16 16:06:07937 * @return {!ToolbarButton}
Blink Reformat4c46d092018-04-07 15:32:37938 */
939 createShowHideSidebarButton(title) {
Mandy Chen84d56b62019-09-03 18:21:18940 this._showHideSidebarButtonTitle = title;
Paul Lewis9950e182019-12-16 16:06:07941 this._showHideSidebarButton = new ToolbarButton('', '');
942 this._showHideSidebarButton.addEventListener(ToolbarButton.Events.Click, buttonClicked, this);
Blink Reformat4c46d092018-04-07 15:32:37943 this._updateShowHideSidebarButton();
944
945 /**
Tim van der Lippec02a97c2020-02-14 14:39:27946 * @param {!Common.EventTarget.EventTargetEvent} event
Tim van der Lippe0830b3d2019-10-03 13:20:07947 * @this {SplitWidget}
Blink Reformat4c46d092018-04-07 15:32:37948 */
949 function buttonClicked(event) {
Tim van der Lippe0830b3d2019-10-03 13:20:07950 if (this._showMode !== ShowMode.Both) {
Blink Reformat4c46d092018-04-07 15:32:37951 this.showBoth(true);
Tim van der Lippe1d6e57a2019-09-30 11:55:34952 } else {
Blink Reformat4c46d092018-04-07 15:32:37953 this.hideSidebar(true);
Tim van der Lippe1d6e57a2019-09-30 11:55:34954 }
Blink Reformat4c46d092018-04-07 15:32:37955 }
956
957 return this._showHideSidebarButton;
958 }
959
960 _updateShowHideSidebarButton() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34961 if (!this._showHideSidebarButton) {
Blink Reformat4c46d092018-04-07 15:32:37962 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34963 }
Tim van der Lippe0830b3d2019-10-03 13:20:07964 const sidebarHidden = this._showMode === ShowMode.OnlyMain;
Blink Reformat4c46d092018-04-07 15:32:37965 let glyph = '';
966 if (sidebarHidden) {
967 glyph = this.isVertical() ?
968 (this.isSidebarSecond() ? 'largeicon-show-right-sidebar' : 'largeicon-show-left-sidebar') :
969 (this.isSidebarSecond() ? 'largeicon-show-bottom-sidebar' : 'largeicon-show-top-sidebar');
970 } else {
971 glyph = this.isVertical() ?
972 (this.isSidebarSecond() ? 'largeicon-hide-right-sidebar' : 'largeicon-hide-left-sidebar') :
973 (this.isSidebarSecond() ? 'largeicon-hide-bottom-sidebar' : 'largeicon-hide-top-sidebar');
974 }
975 this._showHideSidebarButton.setGlyph(glyph);
976 this._showHideSidebarButton.setTitle(
Paul Lewis17e384e2020-01-08 15:46:51977 sidebarHidden ? Common.UIString.UIString('Show %s', this._showHideSidebarButtonTitle) :
978 Common.UIString.UIString('Hide %s', this._showHideSidebarButtonTitle));
Blink Reformat4c46d092018-04-07 15:32:37979 }
Tim van der Lippe0830b3d2019-10-03 13:20:07980}
Blink Reformat4c46d092018-04-07 15:32:37981
Tim van der Lippe0830b3d2019-10-03 13:20:07982export const ShowMode = {
Blink Reformat4c46d092018-04-07 15:32:37983 Both: 'Both',
984 OnlyMain: 'OnlyMain',
985 OnlySidebar: 'OnlySidebar'
986};
987
988/** @enum {symbol} */
Tim van der Lippe0830b3d2019-10-03 13:20:07989export const Events = {
Blink Reformat4c46d092018-04-07 15:32:37990 SidebarSizeChanged: Symbol('SidebarSizeChanged'),
991 ShowModeChanged: Symbol('ShowModeChanged')
992};
993
Tim van der Lippec96ccd92019-11-29 16:23:54994const MinPadding = 20;
Tim van der Lippeaa76aa22020-02-14 14:38:24995
996/** @typedef {{showMode: string, size: number}} */
997export let SettingForOrientation;