Web Inspector: Remove redundant tooltips
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / ConsoleDrawer.js
1 /*
2  * Copyright (C) 2017 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WI.ConsoleDrawer = class ConsoleDrawer extends WI.ContentBrowser
27 {
28     constructor(element)
29     {
30         const delegate = null;
31         const disableBackForward = true;
32         const disableFindBanner = false;
33         super(element, delegate, disableBackForward, disableFindBanner);
34
35         this.element.classList.add("console-drawer");
36
37         this._drawerHeightSetting = new WI.Setting("console-drawer-height", 150);
38
39         this.navigationBar.element.addEventListener("mousedown", this._consoleResizerMouseDown.bind(this));
40
41         this._toggleDrawerButton = new WI.ToggleButtonNavigationItem("toggle-drawer", WI.UIString("Hide Console"), WI.UIString("Show Console"), "Images/HideConsoleDrawer.svg", "Images/ShowConsoleDrawer.svg");
42
43         this._toggleDrawerButton.visibilityPriority = WI.NavigationItem.VisibilityPriority.High;
44         this._toggleDrawerButton.addEventListener(WI.ButtonNavigationItem.Event.Clicked, () => { WI.toggleSplitConsole(); });
45         this.navigationBar.insertNavigationItem(this._toggleDrawerButton, 0);
46
47         this.collapsed = true;
48
49         WI.TabBrowser.addEventListener(WI.TabBrowser.Event.SelectedTabContentViewDidChange, this._selectedTabContentViewDidChange, this);
50     }
51
52     // Public
53
54     toggleButtonShortcutTooltip(keyboardShortcut)
55     {
56         this._toggleDrawerButton.defaultToolTip = WI.UIString("Hide Console (%s)").format(keyboardShortcut.displayName);
57     }
58
59     get collapsed()
60     {
61         return this._collapsed;
62     }
63
64     set collapsed(flag)
65     {
66         if (flag === this._collapsed)
67             return;
68
69         this._collapsed = flag || false;
70         this._toggleDrawerButton.toggled = this._collapsed;
71
72         this.element.classList.toggle("hidden", this._collapsed);
73
74         if (this._collapsed)
75             this.hidden();
76         else
77             this.shown();
78
79         this.dispatchEventToListeners(WI.ConsoleDrawer.Event.CollapsedStateChanged);
80     }
81
82     get height()
83     {
84         return this.element.offsetHeight;
85     }
86
87     shown()
88     {
89         super.shown();
90
91         this._restoreDrawerHeight();
92     }
93
94     // Protected
95
96     layout()
97     {
98         if (this._collapsed)
99             return;
100
101         if (this.layoutReason !== WI.View.LayoutReason.Resize)
102             return;
103
104         super.layout();
105
106         let height = this.height;
107         this._restoreDrawerHeight();
108
109         if (height !== this.height)
110             this.dispatchEventToListeners(WI.ConsoleDrawer.Event.Resized);
111     }
112
113     // Private
114
115     _consoleResizerMouseDown(event)
116     {
117         if (event.button !== 0 || event.ctrlKey)
118             return;
119
120         // Only start dragging if the target is one of the elements that we expect.
121         if (event.target !== this.navigationBar.element && !event.target.classList.contains("flexible-space"))
122             return;
123
124         let resizerElement = event.target;
125         let mouseOffset = resizerElement.offsetHeight - (event.pageY - resizerElement.totalOffsetTop);
126
127         function dockedResizerDrag(event)
128         {
129             let height = window.innerHeight - event.pageY - mouseOffset;
130             this._drawerHeightSetting.value = height;
131             this._updateDrawerHeight(height);
132             this.collapsed = false;
133         }
134
135         function dockedResizerDragEnd(event)
136         {
137             if (event.button !== 0)
138                 return;
139
140             WI.elementDragEnd(event);
141         }
142
143         WI.elementDragStart(resizerElement, dockedResizerDrag.bind(this), dockedResizerDragEnd.bind(this), event, "row-resize");
144     }
145
146     _restoreDrawerHeight()
147     {
148         this._updateDrawerHeight(this._drawerHeightSetting.value);
149     }
150
151     _updateDrawerHeight(height)
152     {
153         const minimumHeight = 64;
154         const maximumHeight = this.element.parentNode.offsetHeight - 100;
155
156         let heightCSSValue = Number.constrain(height, minimumHeight, maximumHeight) + "px";
157         if (this.element.style.height === heightCSSValue)
158             return;
159
160         this.element.style.height = heightCSSValue;
161
162         this.dispatchEventToListeners(WI.ConsoleDrawer.Event.Resized);
163     }
164
165     _selectedTabContentViewDidChange()
166     {
167         if (WI.doesCurrentTabSupportSplitContentBrowser())
168             return;
169
170         this.element.classList.add("hidden");
171     }
172 };
173
174 WI.ConsoleDrawer.Event = {
175     CollapsedStateChanged: "console-drawer-collapsed-state-changed",
176     Resized: "console-drawer-resized",
177 };