Commit be07b7eb authored by Gavin Williams's avatar Gavin Williams Committed by Commit Bot

scanning: Use observer for AppState changes

Using an observer + boolean attributes to reduce the number of
unnecessary function calls from the HTML whenever AppState changes.

Bug: 1059779
Change-Id: I20f92489adcc351dace224d0fc33f4c95478f472
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2544751
Commit-Queue: Gavin Williams <gavinwill@chromium.org>
Reviewed-by: default avatarJesse Schettler <jschettler@chromium.org>
Cr-Commit-Position: refs/heads/master@{#828436}
parent 89b82202
...@@ -84,26 +84,26 @@ ...@@ -84,26 +84,26 @@
progress-percent="[[progressPercent_]]"></scan-preview> progress-percent="[[progressPercent_]]"></scan-preview>
</div> </div>
<div class="right-panel"> <div class="right-panel">
<template is="dom-if" if="[[!shouldShowDoneSection_(appState_)]]"> <template is="dom-if" if="[[!showDoneSection_]]">
<h1 id="appTitle">[[i18n('appTitle')]]</h1> <h1 id="appTitle">[[i18n('appTitle')]]</h1>
<scanner-select id="scannerSelect" scanners="[[scanners_]]" <scanner-select id="scannerSelect" scanners="[[scanners_]]"
loaded="[[areScannersLoaded_(appState_)]]" loaded="[[scannersLoaded_]]"
settings-disabled="[[areSettingsDisabled_(appState_)]]" settings-disabled="[[settingsDisabled_]]"
selected-scanner-id="{{selectedScannerId}}"></scanner-select> selected-scanner-id="{{selectedScannerId}}"></scanner-select>
<source-select id="sourceSelect" sources="[[capabilities_.sources]]" <source-select id="sourceSelect" sources="[[capabilities_.sources]]"
settings-disabled="[[areSettingsDisabled_(appState_)]]" settings-disabled="[[settingsDisabled_]]"
selected-source="{{selectedSource}}"></source-select> selected-source="{{selectedSource}}"></source-select>
<scan-to-select id="scanToSelect" <scan-to-select id="scanToSelect"
settings-disabled="[[areSettingsDisabled_(appState_)]]" settings-disabled="[[settingsDisabled_]]"
selected-file-path="{{selectedFilePath}}"> selected-file-path="{{selectedFilePath}}">
</scan-to-select> </scan-to-select>
<file-type-select id="fileTypeSelect" <file-type-select id="fileTypeSelect"
settings-disabled="[[areSettingsDisabled_(appState_)]]" settings-disabled="[[settingsDisabled_]]"
selected-file-type="{{selectedFileType}}"></file-type-select> selected-file-type="{{selectedFileType}}"></file-type-select>
<div id="more-settings-line-separator"></div> <div id="more-settings-line-separator"></div>
<cr-button id="moreSettingsButton" on-click="toggleClicked_" <cr-button id="moreSettingsButton" on-click="toggleClicked_"
aria-expanded$="[[opened]]" aria-expanded$="[[opened]]"
disabled="[[areSettingsDisabled_(appState_)]]"> disabled="[[settingsDisabled_]]">
<span>[[i18n('moreSettings')]]</span> <span>[[i18n('moreSettings')]]</span>
<iron-icon icon="[[getArrowIcon_(opened)]]"> <iron-icon icon="[[getArrowIcon_(opened)]]">
</iron-icon> </iron-icon>
...@@ -111,34 +111,34 @@ ...@@ -111,34 +111,34 @@
<iron-collapse id="collapse" opened="{{opened}}"> <iron-collapse id="collapse" opened="{{opened}}">
<color-mode-select id="colorModeSelect" <color-mode-select id="colorModeSelect"
color-modes="[[capabilities_.colorModes]]" color-modes="[[capabilities_.colorModes]]"
settings-disabled="[[areSettingsDisabled_(appState_)]]" settings-disabled="[[settingsDisabled_]]"
selected-color-mode="{{selectedColorMode}}"> selected-color-mode="{{selectedColorMode}}">
</color-mode-select> </color-mode-select>
<page-size-select id="pageSizeSelect" <page-size-select id="pageSizeSelect"
page-sizes="[[selectedSourcePageSizes_]]" page-sizes="[[selectedSourcePageSizes_]]"
settings-disabled="[[areSettingsDisabled_(appState_)]]" settings-disabled="[[settingsDisabled_]]"
selected-page-size="{{selectedPageSize}}"> selected-page-size="{{selectedPageSize}}">
</page-size-select> </page-size-select>
<resolution-select id="resolutionSelect" <resolution-select id="resolutionSelect"
resolutions="[[capabilities_.resolutions]]" resolutions="[[capabilities_.resolutions]]"
settings-disabled="[[areSettingsDisabled_(appState_)]]" settings-disabled="[[settingsDisabled_]]"
selected-resolution="{{selectedResolution}}"> selected-resolution="{{selectedResolution}}">
</resolution-select> </resolution-select>
</iron-collapse> </iron-collapse>
<div class="scan-button-container"> <div class="scan-button-container">
<cr-button id="scanButton" class="action-button" on-click="onScanClick_" <cr-button id="scanButton" class="action-button" on-click="onScanClick_"
disabled$="[[areSettingsDisabled_(appState_)]]" disabled$="[[settingsDisabled_]]"
hidden$="[[shouldShowCancelButton_(appState_)]]"> hidden$="[[showCancelButton_]]">
[[i18n('scanButtonText')]] [[i18n('scanButtonText')]]
</cr-button> </cr-button>
<cr-button id="cancelButton" on-click="onCancelClick_" <cr-button id="cancelButton" on-click="onCancelClick_"
hidden$="[[!shouldShowCancelButton_(appState_)]]"> hidden$="[[!showCancelButton_]]">
[[i18n('cancelButtonText')]] [[i18n('cancelButtonText')]]
</cr-button> </cr-button>
</div> </div>
<p id="statusText">[[statusText_]]</p> <p id="statusText">[[statusText_]]</p>
</template> </template>
<template is="dom-if" if="[[shouldShowDoneSection_(appState_)]]"> <template is="dom-if" if="[[showDoneSection_]]">
<scan-done-section page-number="[[pageNumber_]]" <scan-done-section page-number="[[pageNumber_]]"
on-change-app-state="onChangeAppState_"></scan-done-section> on-change-app-state="onChangeAppState_"></scan-done-section>
</template> </template>
......
...@@ -101,6 +101,7 @@ Polymer({ ...@@ -101,6 +101,7 @@ Polymer({
appState_: { appState_: {
type: Number, type: Number,
value: AppState.GETTING_SCANNERS, value: AppState.GETTING_SCANNERS,
observer: 'onAppStateChange_',
}, },
/** @private {!Array<string>} */ /** @private {!Array<string>} */
...@@ -136,6 +137,37 @@ Polymer({ ...@@ -136,6 +137,37 @@ Polymer({
/** @private {string} */ /** @private {string} */
statusText_: String, statusText_: String,
/**
* Determines whether settings should be disabled based on the current app
* state. Settings should be disabled until after the selected scanner's
* capabilities are fetched since the capabilities determine what options
* are available in the settings. They should also be disabled while
* scanning since settings cannot be changed while a scan is in progress.
* @private {boolean}
*/
settingsDisabled_: {
type: Boolean,
value: true,
},
/** @private {boolean} */
scannersLoaded_: {
type: Boolean,
value: false,
},
/** @private {boolean} */
showDoneSection_: {
type: Boolean,
value: false,
},
/** @private {boolean} */
showCancelButton_: {
type: Boolean,
value: false,
},
}, },
/** @override */ /** @override */
...@@ -347,35 +379,6 @@ Polymer({ ...@@ -347,35 +379,6 @@ Polymer({
return opened ? 'cr:expand-less' : 'cr:expand-more'; return opened ? 'cr:expand-less' : 'cr:expand-more';
}, },
/**
* @return {boolean}
* @private
*/
areScannersLoaded_() {
return this.appState_ !== AppState.GETTING_SCANNERS;
},
/**
* Determines whether settings should be disabled based on the current app
* state. Settings should be disabled until after the selected scanner's
* capabilities are fetched since the capabilities determine what options are
* available in the settings. They should also be disabled while scanning
* since settings cannot be changed while a scan is in progress.
* @return {boolean}
* @private
*/
areSettingsDisabled_() {
return this.appState_ !== AppState.READY;
},
/**
* @return {boolean}
* @private
*/
shouldShowDoneSection_() {
return this.appState_ === AppState.DONE;
},
/** /**
* @return {string} * @return {string}
* @private * @private
...@@ -400,14 +403,6 @@ Polymer({ ...@@ -400,14 +403,6 @@ Polymer({
*/ */
onCancelComplete(success) {}, onCancelComplete(success) {},
/**
* @return {boolean}
* @private
*/
shouldShowCancelButton_() {
return this.appState_ === AppState.SCANNING;
},
/** /**
* Sets the app state if the state transition is allowed. * Sets the app state if the state transition is allowed.
* @param {!AppState} newState * @param {!AppState} newState
...@@ -443,6 +438,14 @@ Polymer({ ...@@ -443,6 +438,14 @@ Polymer({
this.appState_ = newState; this.appState_ = newState;
}, },
/** @private */
onAppStateChange_() {
this.scannersLoaded_ = this.appState_ !== AppState.GETTING_SCANNERS;
this.settingsDisabled_ = this.appState_ !== AppState.READY;
this.showCancelButton_ = this.appState_ === AppState.SCANNING;
this.showDoneSection_ = this.appState_ === AppState.DONE;
},
/** /**
* @param {!Event} event * @param {!Event} event
* @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