Moved complete focus detection to a new util component

This commit is contained in:
Dennis Ploeger 2019-01-14 08:56:33 +01:00
parent 69e079c319
commit 265b6efc96
4 changed files with 25 additions and 23 deletions

View File

@ -1,11 +0,0 @@
const BrowserFocus = function () {
this.isFocused = false;
window.onblur = function () { this.isFocused = false; }.bind(this);
window.onfocus = function () { this.isFocused = true; }.bind(this);
};
BrowserFocus.prototype.hasFocus = function () {
return this.isFocused;
};
module.exports = BrowserFocus;

View File

@ -14,6 +14,7 @@ const FeatureDetector = {
isFrame: window.top !== window,
isSelfHosted: !isDesktop && !/^http(s?):\/\/((localhost:8085)|((app|beta)\.keeweb\.info))/.test(location.href),
needFixClicks: /Edge\/14/.test(navigator.appVersion),
isBrowser: !this.isDesktop && !this.isMobile,
actionShortcutSymbol: function(formatting) {
return this.isMac ? '⌘' : formatting ? '<span class="thin">ctrl + </span>' : 'ctrl-';

View File

@ -0,0 +1,20 @@
const FeatureDetector = require('./feature-detector');
const Launcher = require('../comp/launcher');
const FocusDetector = function () {
this.isFocused = false;
if (FeatureDetector.isBrowser) {
window.onblur = () => { this.isFocused = false; };
window.onfocus = () => { this.isFocused = true; };
}
};
FocusDetector.prototype.hasFocus = function () {
if (FeatureDetector.isBrowser) {
return this.isFocused;
} else {
return Launcher.isAppFocused();
}
};
module.exports = FocusDetector;

View File

@ -7,7 +7,6 @@ const SecureInput = require('../comp/secure-input');
const DropboxChooser = require('../comp/dropbox-chooser');
const KeyHandler = require('../comp/key-handler');
const StorageFileListView = require('../views/storage-file-list-view');
const FeatureDetector = require('../util/feature-detector');
const Logger = require('../util/logger');
const Locale = require('../util/locale');
const UrlUtil = require('../util/url-util');
@ -15,7 +14,7 @@ const InputFx = require('../util/input-fx');
const Comparators = require('../util/comparators');
const Storage = require('../storage');
const Launcher = require('../comp/launcher');
const BrowserFocus = require('../util/browser-focus');
const FocusDetector = require('../util/focus-detector');
const logger = new Logger('open-view');
@ -70,9 +69,7 @@ const OpenView = Backbone.View.extend({
KeyHandler.onKey(Keys.DOM_VK_RETURN, this.enterKeyPress, this);
KeyHandler.onKey(Keys.DOM_VK_DOWN, this.moveOpenFileSelectionDown, this);
KeyHandler.onKey(Keys.DOM_VK_UP, this.moveOpenFileSelectionUp, this);
if (!FeatureDetector.isDesktop) {
this.browserFocus = new BrowserFocus();
}
this.focusDetector = new FocusDetector();
},
render: function () {
@ -110,13 +107,8 @@ const OpenView = Backbone.View.extend({
},
focusInput: function() {
if (!FeatureDetector.isMobile) {
if (
(FeatureDetector.isDesktop && Launcher.isAppFocused()) ||
(this.browserFocus.hasFocus())
) {
this.inputEl.focus();
}
if (this.focusDetector.hasFocus()) {
this.inputEl.focus();
}
},