Commit 4981992a authored by dpapad's avatar dpapad Committed by Commit Bot

MD Settings: Migrate settings-toggle-button to use cr-toggle.

Bug: 768073
Cq-Include-Trybots: master.tryserver.chromium.linux:closure_compilation
Change-Id: If3efa3993d9ff75cce73eb48d583adb0ea5db543
Reviewed-on: https://chromium-review.googlesource.com/686041
Commit-Queue: Demetrios Papadopoulos <dpapad@chromium.org>
Reviewed-by: default avatarScott Chen <scottchen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#505269}
parent 05e61fdd
...@@ -101,6 +101,7 @@ ...@@ -101,6 +101,7 @@
{ {
'target_name': 'settings_toggle_button', 'target_name': 'settings_toggle_button',
'dependencies': [ 'dependencies': [
'<(DEPTH)/ui/webui/resources/cr_elements/cr_toggle/compiled_resources2.gyp:cr_toggle',
'settings_boolean_control_behavior', 'settings_boolean_control_behavior',
], ],
'includes': ['../../../../../third_party/closure_compiler/compile_js2.gypi'], 'includes': ['../../../../../third_party/closure_compiler/compile_js2.gypi'],
......
<link rel="import" href="chrome://resources/html/polymer.html"> <link rel="import" href="chrome://resources/html/polymer.html">
<link rel="import" href="chrome://resources/cr_elements/cr_toggle/cr_toggle.html">
<link rel="import" href="chrome://resources/cr_elements/policy/cr_policy_pref_indicator.html"> <link rel="import" href="chrome://resources/cr_elements/policy/cr_policy_pref_indicator.html">
<link rel="import" href="chrome://resources/polymer/v1_0/paper-toggle-button/paper-toggle-button.html">
<link rel="import" href="chrome://resources/polymer/v1_0/iron-flex-layout/iron-flex-layout-classes.html"> <link rel="import" href="chrome://resources/polymer/v1_0/iron-flex-layout/iron-flex-layout-classes.html">
<link rel="import" href="settings_boolean_control_behavior.html"> <link rel="import" href="settings_boolean_control_behavior.html">
<link rel="import" href="../settings_shared_css.html"> <link rel="import" href="../settings_shared_css.html">
...@@ -62,13 +62,12 @@ ...@@ -62,13 +62,12 @@
<cr-policy-pref-indicator pref="[[pref]]" icon-aria-label="[[label]]"> <cr-policy-pref-indicator pref="[[pref]]" icon-aria-label="[[label]]">
</cr-policy-pref-indicator> </cr-policy-pref-indicator>
</template> </template>
<paper-toggle-button id="control" checked="{{checked}}" <cr-toggle id="control" checked="{{checked}}"
on-change="notifyChangedByUserInteraction" on-change="onChange_"
aria-label$="[[getAriaLabel_(label, ariaLabel)]]" aria-label$="[[getAriaLabel_(label, ariaLabel)]]"
aria-describedby="subLabel" on-up="resetTrackLock_" aria-describedby="subLabel"
disabled="[[controlDisabled_(disabled, pref)]]" disabled="[[controlDisabled_(disabled, pref)]]">
on-tap="onToggleTap_"> </cr-toggle>
</paper-toggle-button>
</div> </div>
</template> </template>
<script src="settings_toggle_button.js"></script> <script src="settings_toggle_button.js"></script>
......
...@@ -58,17 +58,6 @@ Polymer({ ...@@ -58,17 +58,6 @@ Polymer({
return this.label || this.ariaLabel; return this.label || this.ariaLabel;
}, },
/**
* Handle taps directly on the toggle (see: onLabelWrapperTap_ for non-toggle
* taps).
* @param {!Event} e
* @private
*/
onToggleTap_: function(e) {
// Stop the event from propagating to avoid firing two 'changed' events.
e.stopPropagation();
},
/** @private */ /** @private */
onDisableOrPrefChange_: function() { onDisableOrPrefChange_: function() {
if (this.controlDisabled_()) { if (this.controlDisabled_()) {
...@@ -79,28 +68,34 @@ Polymer({ ...@@ -79,28 +68,34 @@ Polymer({
}, },
/** /**
* Handle non-toggle button taps (see: onToggleTap_ for toggle taps). * Handles non cr-toggle button taps (cr-toggle handles its own tap events
* which don't bubble).
* @param {!Event} e * @param {!Event} e
* @private * @private
*/ */
onHostTap_: function(e) { onHostTap_: function(e) {
// Stop the event from propagating to avoid firing two 'changed' events.
e.stopPropagation(); e.stopPropagation();
if (this.controlDisabled_()) if (this.controlDisabled_())
return; return;
// Ignore this |tap| event, if the interaction sequence
// (pointerdown+pointerup) began within the cr-toggle itself.
if (/** @type {!CrToggleElement} */ (this.$.control)
.shouldIgnoreHostTap(e)) {
return;
}
this.checked = !this.checked; this.checked = !this.checked;
this.notifyChangedByUserInteraction(); this.notifyChangedByUserInteraction();
this.fire('change'); this.fire('change');
}, },
/** /**
* TODO(scottchen): temporary fix until polymer gesture bug resolved. See: * @param {!CustomEvent} e
* https://github.com/PolymerElements/paper-slider/issues/186
* @private * @private
*/ */
resetTrackLock_: function() { onChange_: function(e) {
// Run tap.reset in next run-loop to avoid reversing the current tap event. this.checked = /** @type {boolean} */ (e.detail);
setTimeout(() => Polymer.Gestures.gestures.tap.reset()); this.notifyChangedByUserInteraction();
}, },
}); });
...@@ -55,6 +55,9 @@ Polymer({ ...@@ -55,6 +55,9 @@ Polymer({
/** @private {boolean} */ /** @private {boolean} */
pointerMoveFired_: false, pointerMoveFired_: false,
/** @private {number} */
lastPointerUpTime_: 0,
/** @override */ /** @override */
attached: function() { attached: function() {
// Note: Doing the same in ready instead of attached produces incorrect // Note: Doing the same in ready instead of attached produces incorrect
...@@ -101,7 +104,8 @@ Polymer({ ...@@ -101,7 +104,8 @@ Polymer({
}, },
/** @private */ /** @private */
onPointerUp_: function() { onPointerUp_: function(e) {
this.lastPointerUpTime_ = e.timeStamp;
this.removeEventListener('pointermove', this.boundPointerMove_); this.removeEventListener('pointermove', this.boundPointerMove_);
}, },
...@@ -114,7 +118,7 @@ Polymer({ ...@@ -114,7 +118,7 @@ Polymer({
if (e.button != 0) if (e.button != 0)
return; return;
// This is necessary to have follow up events fire on |this|, even // This is necessary to have follow up pointer events fire on |this|, even
// if they occur outside of its bounds. // if they occur outside of its bounds.
this.setPointerCapture(e.pointerId); this.setPointerCapture(e.pointerId);
this.pointerDownX_ = e.clientX; this.pointerDownX_ = e.clientX;
...@@ -123,7 +127,11 @@ Polymer({ ...@@ -123,7 +127,11 @@ Polymer({
}, },
/** @private */ /** @private */
onTap_: function() { onTap_: function(e) {
// Prevent |tap| event from bubbling. It can cause parents of this elements
// to erroneously re-toggle this control.
e.stopPropagation();
// If pointermove fired it means that user tried to drag the toggle button, // If pointermove fired it means that user tried to drag the toggle button,
// and therefore its state has already been handled within the pointermove // and therefore its state has already been handled within the pointermove
// handler. Do nothing here. // handler. Do nothing here.
...@@ -135,6 +143,21 @@ Polymer({ ...@@ -135,6 +143,21 @@ Polymer({
this.toggleState_(false); this.toggleState_(false);
}, },
/**
* Whether the host of this element should handle a 'tap' event it received,
* to be used when tapping on the parent is supposed to toggle the cr-toggle.
*
* This is necessary to avoid a corner case when pointerdown is initiated
* in cr-toggle, but pointerup happens outside the bounds of cr-toggle, which
* ends up firing a 'tap' event on the parent (see context at crbug.com/689158
* and crbug.com/768555).
* @param {!Event} e
* @return {boolean}
*/
shouldIgnoreHostTap: function(e) {
return e.detail.sourceEvent.timeStamp == this.lastPointerUpTime_;
},
/** /**
* @param {boolean} fromKeyboard * @param {boolean} fromKeyboard
* @private * @private
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment