Skip to content

Commit

Permalink
Gallery: use fancy Files.app tooltip.
Browse files Browse the repository at this point in the history
To use fancy Files.app tooltip outside of Files.app, extracted
tooltip_controller.js as files-tooltip element.

BUG=525332
TEST=browser_tests:FileManagerJsTest.FilesTooltip
     manually tested; In Gallery, see new tooltips by hovering a mouse cursor on buttons.

Review URL: https://codereview.chromium.org/1382763003

Cr-Commit-Position: refs/heads/master@{#353721}
  • Loading branch information
yawano authored and Commit bot committed Oct 13, 2015
1 parent 249210b commit bc1fe15
Show file tree
Hide file tree
Showing 20 changed files with 326 additions and 256 deletions.
4 changes: 2 additions & 2 deletions chrome/browser/chromeos/file_manager/file_manager_jstest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ IN_PROC_BROWSER_TEST_F(FileManagerJsTest, ProvidersModel) {
FILE_PATH_LITERAL("foreground/js/providers_model_unittest.html")));
}

IN_PROC_BROWSER_TEST_F(FileManagerJsTest, TooltipController) {
IN_PROC_BROWSER_TEST_F(FileManagerJsTest, FilesTooltip) {
RunTest(base::FilePath(
FILE_PATH_LITERAL("foreground/js/tooltip_controller_unittest.html")));
FILE_PATH_LITERAL("foreground/elements/files_tooltip_unittest.html")));
}

IN_PROC_BROWSER_TEST_F(FileManagerJsTest, SpinnerController) {
Expand Down
1 change: 1 addition & 0 deletions chrome/browser_tests.isolate
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@
'../ui/file_manager/integration_tests/',
'../ui/webui/resources/js/',
'../third_party/analytics/',
'../third_party/polymer/v1_0/components-chromium/polymer/',
'../chrome/browser/resources/chromeos/wallpaper_manager/',
'<(PRODUCT_DIR)/chromevox_test_data/',
'<(PRODUCT_DIR)/content_shell.pak',
Expand Down
23 changes: 0 additions & 23 deletions ui/file_manager/file_manager/foreground/css/file_manager.css
Original file line number Diff line number Diff line change
Expand Up @@ -2305,26 +2305,3 @@ files-toggle-ripple {
right: 0;
top: 0;
}

#tooltip {
opacity: 0;
padding: 4px 6px 0;
pointer-events: none;
position: absolute;
transition: opacity 300ms;
z-index: 1000;
}

#tooltip[visible] {
opacity: .9;
}

#tooltip-label {
background-color: #323232;
border-radius: 2px;
color: white;
font-size: 12px;
line-height: 28px;
padding: 0 14px;
white-space: nowrap;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!-- Copyright 2015 The Chromium Authors. All rights reserved.
-- Use of this source code is governed by a BSD-style license that can be
-- found in the LICENSE file.
-->

<link rel="import" href="chrome://resources/polymer/v1_0/polymer/polymer.html">

<dom-module id="files-tooltip">
<style>
:host {
opacity: 0;
padding: 4px 6px 0;
pointer-events: none;
position: absolute;
transition: opacity 300ms;
z-index: 1000;
}

:host([visible]) {
opacity: .9;
}

:host > #label {
background-color: #323232;
border-radius: 2px;
color: white;
font-size: 12px;
line-height: 28px;
padding: 0 14px;
white-space: nowrap;
}
</style>
<template>
<div id="label"></div>
</template>
</dom-module>

<script src="files_tooltip.js"></script>
226 changes: 226 additions & 0 deletions ui/file_manager/file_manager/foreground/elements/files_tooltip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
* Files Tooltip.
*
* Adds target elements with addTarget or addTargets. Value of aria-label is
* used as a label of the tooltip.
*
* Usage:
* document.querySelector('files-tooltip').addTargets(
* document.querySelectorAll('[has-tooltip]'))
*/
var FilesTooltip = Polymer({
is: 'files-tooltip',

properties: {
/**
* Delay for showing the tooltip in milliseconds.
*/
'showTimeout': {
type: Number,
value: 500, // ms
readOnly: true
},

/**
* Delay for hiding the tooltip in milliseconds.
*/
'hideTimeout': {
type: Number,
value: 250, // ms
readOnly: true
}
},

/**
* Initializes member variables.
*/
created: function() {
/**
* @private {HTMLElement}
*/
this.visibleTooltipTarget_ = null;

/**
* @private {HTMLElement}
*/
this.upcomingTooltipTarget_ = null;

/**
* @private {number}
*/
this.showTooltipTimerId_ = 0;

/**
* @private {number}
*/
this.hideTooltipTimerId_ = 0;
},

/**
* Adds an event listener to the body.
*/
attached: function() {
document.body.addEventListener(
'mousedown', this.onDocumentMouseDown_.bind(this));
},

/**
* Adds targets to tooltip.
* @param {!NodeList} targets
*/
addTargets: function(targets) {
for (var i = 0; i < targets.length; i++) {
this.addTarget(targets[i]);
}
},

/**
* Adds a target to tooltip.
* @param {!HTMLElement} target
*/
addTarget: function(target) {
target.addEventListener('mouseover', this.onMouseOver_.bind(this, target));
target.addEventListener('mouseout', this.onMouseOut_.bind(this, target));
target.addEventListener('focus', this.onFocus_.bind(this, target));
target.addEventListener('blur', this.onBlur_.bind(this, target));
},

/**
* @param {!HTMLElement} target
* @private
*/
initShowingTooltip_: function(target) {
// Some tooltip is already visible.
if (this.visibleTooltipTarget_) {
if (this.hideTooltipTimerId_) {
clearTimeout(this.hideTooltipTimerId_);
this.hideTooltipTimerId_ = 0;
}
}

if (this.visibleTooltipTarget_ === target)
return;

this.upcomingTooltipTarget_ = target;
if (this.showTooltipTimerId_)
clearTimeout(this.showTooltipTimerId_);
this.showTooltipTimerId_ = setTimeout(
this.showTooltip_.bind(this, target),
this.visibleTooltipTarget_ ? 0 : this.showTimeout);
},

/**
* @param {!HTMLElement} target
* @private
*/
initHidingTooltip_: function(target) {
// The tooltip is not visible.
if (this.visibleTooltipTarget_ !== target) {
if (this.upcomingTooltipTarget_ === target) {
clearTimeout(this.showTooltipTimerId_);
this.showTooltipTimerId_ = 0;
}
return;
}

if (this.hideTooltipTimerId_)
clearTimeout(this.hideTooltipTimerId_);
this.hideTooltipTimerId_ = setTimeout(
this.hideTooltip_.bind(this), this.hideTimeout);
},

/**
* @param {!HTMLElement} target
* @private
*/
showTooltip_: function(target) {
if (this.showTooltipTimerId_) {
clearTimeout(this.showTooltipTimerId_);
this.showTooltipTimerId_ = 0;
}

this.visibleTooltipTarget_ = target;

var label = target.getAttribute('aria-label');
if (!label)
return;

this.$.label.textContent = label;
var rect = target.getBoundingClientRect();

var top = rect.top + rect.height;
if (top + this.offsetHeight > document.body.offsetHeight)
top = rect.top - this.offsetHeight;
this.style.top = `${Math.round(top)}px`;

var left = rect.left + rect.width / 2 - this.offsetWidth / 2;
if (left + this.offsetWidth > document.body.offsetWidth)
left = document.body.offsetWidth - this.offsetWidth;
this.style.left = `${Math.round(left)}px`;

this.setAttribute('visible', true);
},

/**
* @private
*/
hideTooltip_: function() {
if (this.hideTooltipTimerId_) {
clearTimeout(this.hideTooltipTimerId_);
this.hideTooltipTimerId_ = 0;
}

this.visibleTooltipTarget_ = null;
this.removeAttribute('visible');
},

/**
* @param {Event} event
* @private
*/
onMouseOver_: function(target, event) {
this.initShowingTooltip_(target);
},

/**
* @param {Event} event
* @private
*/
onMouseOut_: function(target, event) {
this.initHidingTooltip_(target);
},

/**
* @param {Event} event
* @private
*/
onFocus_: function(target, event) {
this.initShowingTooltip_(target);
},

/**
* @param {Event} event
* @private
*/
onBlur_: function(target, event) {
this.initHidingTooltip_(target);
},

/**
* @param {Event} event
* @private
*/
onDocumentMouseDown_: function(event) {
this.hideTooltip_();

// Additionally prevent any scheduled tooltips from showing up.
if (this.showTooltipTimerId_) {
clearTimeout(this.showTooltipTimerId_);
this.showTooltipTimerId_ = 0;
}
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
-- found in the LICENSE file.
-->

<!-- Since resources under chrome://resources are not accessible from unittest,
-- we need to load them with relative paths. -->
<link rel="import" href="../../../../../third_party/polymer/v1_0/components-chromium/polymer/polymer.html">
<link rel="import" href="files_tooltip.html">

<style type="text/css">
button {
display: flex;
Expand All @@ -15,10 +20,11 @@
#container {
display: flex;
justify-content: space-between;
}
}

#tooltip {
files-tooltip {
background: yellow;
box-sizing: border-box;
position: absolute;
text-align: center;
width: 100px;
Expand All @@ -33,12 +39,9 @@
<!-- Button without a tooltip. -->
<button id="other"></button>

<div id="tooltip">
<div id="tooltip-label"></div>
</div>
<files-tooltip></files-tooltip>

<script src="../../../../webui/resources/js/assert.js"></script>
<script src="../../../../webui/resources/js/util.js"></script>
<script src="../../common/js/unittest_util.js"></script>
<script src="tooltip_controller.js"></script>
<script src="tooltip_controller_unittest.js"></script>
<script src="files_tooltip_unittest.js"></script>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var chocolateButton;
var cherriesButton;
var otherButton;
var tooltip;
var controller;

function waitForMutation(target) {
return new Promise(function(fulfill, reject) {
Expand All @@ -22,9 +21,8 @@ function setUp() {
chocolateButton = document.querySelector('#chocolate');
cherriesButton = document.querySelector('#cherries');
otherButton = document.querySelector('#other');
tooltip = document.querySelector('#tooltip');
controller = new TooltipController(
tooltip, [chocolateButton, cherriesButton]);
tooltip = document.querySelector('files-tooltip');
tooltip.addTargets([chocolateButton, cherriesButton]);
}

function testFocus(callback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
'../../background/js/background_base.js',
'../../background/js/background.js',
'../../../image_loader/image_loader_client.js',
'../elements/files_tooltip.js',
'./metrics_start.js',
'./ui/combobutton.js',
'./ui/commandbutton.js',
Expand Down Expand Up @@ -111,7 +112,6 @@
'./share_client.js',
'./task_controller.js',
'./toolbar_controller.js',
'./tooltip_controller.js',
'./thumbnail_loader.js',
'./list_thumbnail_loader.js',
'./providers_model.js',
Expand Down
Loading

0 comments on commit bc1fe15

Please sign in to comment.