From 9ffc6427842ba08c4d0287f695175efda03d287d Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Mon, 18 Jan 2016 13:39:07 -0400 Subject: [PATCH] Autoselect a single drive by using DriveScanner `scan` event - The autoselection only happens once the user has selected an image to prevent weird behaviour. - The autoselection happens only if the current selected drive is different from the new drive in order to avoid selecting the same driver over and over again if it still is the only one. --- build/browser/app.js | 23 +++++++++++++++++++---- lib/browser/app.js | 23 +++++++++++++++++++---- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/build/browser/app.js b/build/browser/app.js index 31fa1ef9..70d07b0e 100644 --- a/build/browser/app.js +++ b/build/browser/app.js @@ -20,6 +20,7 @@ */ var angular = require('angular'); +var _ = require('lodash'); var remote = window.require('remote'); var shell = remote.require('shell'); var dialog = remote.require('./src/dialog'); @@ -52,12 +53,26 @@ app.controller('AppController', function($q, DriveScannerService, SelectionState console.debug('Restarting'); this.selection.clear(); this.writer.setProgress(0); - this.scanner.start(2000); + this.scanner.start(2000).on('scan', function(drives) { + + // Notice we only autoselect the drive if there is an image, + // which means that the first step was completed successfully, + // otherwise the drive is selected while the drive step is disabled + // which looks very weird. + if (drives.length === 1 && self.selection.hasImage()) { + var drive = _.first(drives); + + // Do not autoselect the same drive over and over again + // and fill the logs unnecessary. + // `angular.equals` is used instead of `_.isEqual` to + // cope with `$$hashKey`. + if (!angular.equals(self.selection.getDrive(), drive)) { + console.debug('Autoselecting drive: ' + drive.device); + self.selectDrive(drive); + } - this.scanner.scan().then(function (res) { - if(res.length === 1){ - self.selectDrive(res[0]); } + }); }; diff --git a/lib/browser/app.js b/lib/browser/app.js index 82c366ac..556949d4 100644 --- a/lib/browser/app.js +++ b/lib/browser/app.js @@ -19,6 +19,7 @@ */ var angular = require('angular'); +var _ = require('lodash'); var remote = window.require('remote'); var shell = remote.require('shell'); var dialog = remote.require('./src/dialog'); @@ -51,12 +52,26 @@ app.controller('AppController', function($q, DriveScannerService, SelectionState console.debug('Restarting'); this.selection.clear(); this.writer.setProgress(0); - this.scanner.start(2000); + this.scanner.start(2000).on('scan', function(drives) { + + // Notice we only autoselect the drive if there is an image, + // which means that the first step was completed successfully, + // otherwise the drive is selected while the drive step is disabled + // which looks very weird. + if (drives.length === 1 && self.selection.hasImage()) { + var drive = _.first(drives); + + // Do not autoselect the same drive over and over again + // and fill the logs unnecessary. + // `angular.equals` is used instead of `_.isEqual` to + // cope with `$$hashKey`. + if (!angular.equals(self.selection.getDrive(), drive)) { + console.debug('Autoselecting drive: ' + drive.device); + self.selectDrive(drive); + } - this.scanner.scan().then(function (res) { - if(res.length === 1){ - self.selectDrive(res[0]); } + }); };