Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(122)

Side by Side Diff: Source/devtools/front_end/ui/Tooltip.js

Issue 1318903007: Devtools UI: Fix tooltip issues (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** 5 /**
6 * @constructor 6 * @constructor
7 * @param {!Document} doc 7 * @param {!Document} doc
8 */ 8 */
9 WebInspector.Tooltip = function(doc) 9 WebInspector.Tooltip = function(doc)
10 { 10 {
11 this.element = doc.body.createChild("div"); 11 this.element = doc.body.createChild("div");
12 this._shadowRoot = WebInspector.createShadowRootWithCoreStyles(this.element) ; 12 this._shadowRoot = WebInspector.createShadowRootWithCoreStyles(this.element) ;
13 this._shadowRoot.appendChild(WebInspector.Widget.createStyleElement("ui/tool tip.css")); 13 this._shadowRoot.appendChild(WebInspector.Widget.createStyleElement("ui/tool tip.css"));
14 14
15 this._tooltipElement = this._shadowRoot.createChild("div", "tooltip"); 15 this._tooltipElement = this._shadowRoot.createChild("div", "tooltip");
16 doc.addEventListener("mousemove", this._mouseMove.bind(this), true); 16 doc.addEventListener("mousemove", this._mouseMove.bind(this), true);
17 doc.addEventListener("mousedown", this._hide.bind(this, true), true); 17 doc.addEventListener("mousedown", this._hide.bind(this, true), true);
18 doc.addEventListener("mouseout", this._hide.bind(this, true), true);
19 doc.addEventListener("keydown", this._hide.bind(this, true), true);
18 } 20 }
19 21
20 WebInspector.Tooltip.Timing = { 22 WebInspector.Tooltip.Timing = {
21 // Max time between tooltips showing that no opening delay is required. 23 // Max time between tooltips showing that no opening delay is required.
22 "InstantThreshold": 300, 24 "InstantThreshold": 300,
23 // Wait time before opening a tooltip. 25 // Wait time before opening a tooltip.
24 "OpeningDelay": 600 26 "OpeningDelay": 600
25 } 27 }
26 28
27 WebInspector.Tooltip.AlignmentOverride = { 29 WebInspector.Tooltip.AlignmentOverride = {
(...skipping 10 matching lines...) Expand all
38 if (!path || event.buttons !== 0) 40 if (!path || event.buttons !== 0)
39 return; 41 return;
40 42
41 if (this._anchorElement && path.indexOf(this._anchorElement) === -1) 43 if (this._anchorElement && path.indexOf(this._anchorElement) === -1)
42 this._hide(); 44 this._hide();
43 45
44 for (var element of path) { 46 for (var element of path) {
45 if (element === this._anchorElement) { 47 if (element === this._anchorElement) {
46 return; 48 return;
47 } else if (element[WebInspector.Tooltip._symbol]) { 49 } else if (element[WebInspector.Tooltip._symbol]) {
48 this._show(element); 50 this._show(element, event);
49 return; 51 return;
50 } 52 }
51 } 53 }
52 }, 54 },
53 55
54 /** 56 /**
55 * @param {!Element} anchorElement 57 * @param {!Element} anchorElement
58 * @param {!Event} event
56 */ 59 */
57 _show: function(anchorElement) 60 _show: function(anchorElement, event)
58 { 61 {
59 var tooltip = anchorElement[WebInspector.Tooltip._symbol]; 62 var tooltip = anchorElement[WebInspector.Tooltip._symbol];
60 this._anchorElement = anchorElement; 63 this._anchorElement = anchorElement;
61 this._tooltipElement.removeChildren(); 64 this._tooltipElement.removeChildren();
62 if (typeof tooltip.content === "string") 65 if (typeof tooltip.content === "string")
63 this._tooltipElement.textContent = tooltip.content; 66 this._tooltipElement.textContent = tooltip.content;
64 else 67 else
65 this._tooltipElement.appendChild(tooltip.content); 68 this._tooltipElement.appendChild(tooltip.content);
66 69
67 if (tooltip.actionId) { 70 if (tooltip.actionId) {
(...skipping 22 matching lines...) Expand all
90 // Posititon tooltip based on the anchor element. 93 // Posititon tooltip based on the anchor element.
91 var containerOffset = container.offsetRelativeToWindow(this.element.wind ow()); 94 var containerOffset = container.offsetRelativeToWindow(this.element.wind ow());
92 var containerOffsetWidth = container.offsetWidth; 95 var containerOffsetWidth = container.offsetWidth;
93 var containerOffsetHeight = container.offsetHeight; 96 var containerOffsetHeight = container.offsetHeight;
94 var anchorBox = this._anchorElement.boxInWindow(this.element.window()); 97 var anchorBox = this._anchorElement.boxInWindow(this.element.window());
95 const anchorOffset = 2; 98 const anchorOffset = 2;
96 const pageMargin = 2; 99 const pageMargin = 2;
97 this._tooltipElement.style.maxWidth = (containerOffsetWidth - pageMargin * 2) + "px"; 100 this._tooltipElement.style.maxWidth = (containerOffsetWidth - pageMargin * 2) + "px";
98 var tooltipWidth = this._tooltipElement.offsetWidth; 101 var tooltipWidth = this._tooltipElement.offsetWidth;
99 var tooltipHeight = this._tooltipElement.offsetHeight; 102 var tooltipHeight = this._tooltipElement.offsetHeight;
100 var tooltipX = anchorBox.x; 103 var tooltipX = tooltip.options[WebInspector.Tooltip.Options.HorizontalAl ignUnderCursor]
dgozman 2015/08/28 00:16:32 Should we make this a default? It does no harm.
samli 2015/08/28 01:32:14 I don't think this looks nice and it is confusing
104 ? event.x : anchorBox.x;
101 tooltipX = Number.constrain(tooltipX, 105 tooltipX = Number.constrain(tooltipX,
102 containerOffset.x + pageMargin, 106 containerOffset.x + pageMargin,
103 containerOffset.x + containerOffsetWidth - tooltipWidth - pageMargin ); 107 containerOffset.x + containerOffsetWidth - tooltipWidth - pageMargin );
104 var onBottom = anchorBox.y + anchorOffset + anchorBox.height + tooltipHe ight < containerOffset.y + containerOffsetHeight; 108 var onBottom = tooltip.options[WebInspector.Tooltip.Options.VerticalAlig nmentTop] ? false
109 : anchorBox.y + anchorOffset + anchorBox.height + tooltipHeight < co ntainerOffset.y + containerOffsetHeight;
105 var tooltipY = onBottom ? anchorBox.y + anchorBox.height + anchorOffset : anchorBox.y - tooltipHeight - anchorOffset; 110 var tooltipY = onBottom ? anchorBox.y + anchorBox.height + anchorOffset : anchorBox.y - tooltipHeight - anchorOffset;
106 this._tooltipElement.positionAt(tooltipX, tooltipY); 111 this._tooltipElement.positionAt(tooltipX, tooltipY);
107 }, 112 },
108 113
109 /** 114 /**
110 * @param {boolean=} removeInstant 115 * @param {boolean=} removeInstant
111 */ 116 */
112 _hide: function(removeInstant) 117 _hide: function(removeInstant)
113 { 118 {
114 delete this._anchorElement; 119 delete this._anchorElement;
115 this._tooltipElement.classList.remove("shown"); 120 this._tooltipElement.classList.remove("shown");
116 if (Date.now() > this._tooltipLastOpened) 121 if (Date.now() > this._tooltipLastOpened)
117 this._tooltipLastClosed = Date.now(); 122 this._tooltipLastClosed = Date.now();
118 if (removeInstant) 123 if (removeInstant)
119 delete this._tooltipLastClosed; 124 delete this._tooltipLastClosed;
120 } 125 }
121 } 126 }
122 127
123 WebInspector.Tooltip._symbol = Symbol("Tooltip"); 128 WebInspector.Tooltip._symbol = Symbol("Tooltip");
124 129
125 /** 130 /**
126 * @param {!Document} doc 131 * @param {!Document} doc
127 */ 132 */
128 WebInspector.Tooltip.installHandler = function(doc) 133 WebInspector.Tooltip.installHandler = function(doc)
129 { 134 {
130 new WebInspector.Tooltip(doc); 135 new WebInspector.Tooltip(doc);
131 } 136 }
132 137
138 WebInspector.Tooltip.Options = {
139 VerticalAlignmentTop: "VerticalAlignmentTop",
140 HorizontalAlignUnderCursor: "HorizontalAlignUnderCursor"
141 }
142
133 /** 143 /**
134 * @param {!Element} element 144 * @param {!Element} element
135 * @param {!Element|string} tooltipContent 145 * @param {!Element|string} tooltipContent
136 * @param {string=} actionId 146 * @param {string=} actionId
147 * @param {!Object=} options
137 */ 148 */
138 WebInspector.Tooltip.install = function(element, tooltipContent, actionId) 149 WebInspector.Tooltip.install = function(element, tooltipContent, actionId, optio ns)
139 { 150 {
140 if (typeof tooltipContent === "string" && tooltipContent === "") { 151 if (typeof tooltipContent === "string" && tooltipContent === "") {
141 delete element[WebInspector.Tooltip._symbol]; 152 delete element[WebInspector.Tooltip._symbol];
142 return; 153 return;
143 } 154 }
144 element[WebInspector.Tooltip._symbol] = { content: tooltipContent, actionId : actionId }; 155 element[WebInspector.Tooltip._symbol] = { content: tooltipContent, actionId: actionId, options: options || {} };
145 } 156 }
146 157
147 Object.defineProperty(HTMLElement.prototype, "title", { 158 Object.defineProperty(HTMLElement.prototype, "title", {
148 /** 159 /**
149 * @return {!Element|string} 160 * @return {!Element|string}
150 * @this {!Element} 161 * @this {!Element}
151 */ 162 */
152 get: function() 163 get: function()
153 { 164 {
154 var tooltip = this[WebInspector.Tooltip._symbol]; 165 var tooltip = this[WebInspector.Tooltip._symbol];
155 return tooltip ? tooltip.content : ""; 166 return tooltip ? tooltip.content : "";
156 }, 167 },
157 168
158 /** 169 /**
159 * @param {!Element|string} x 170 * @param {!Element|string} x
160 * @this {!Element} 171 * @this {!Element}
161 */ 172 */
162 set: function(x) 173 set: function(x)
163 { 174 {
164 WebInspector.Tooltip.install(this, x); 175 WebInspector.Tooltip.install(this, x);
165 } 176 }
166 }); 177 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698