Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
Merge pull request #139 from darahak/auto-reload-on-focus
Browse files Browse the repository at this point in the history
Don't steal focus after detecting file changes and deletions
  • Loading branch information
brrd authored Jan 5, 2017
2 parents fd2d0e6 + 38d8d71 commit b808362
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions app/renderer/abr-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,31 +368,56 @@ AbrDocument.prototype = {

initWatcher: function () {
var that = this;
this.watcher = files.createWatcher(this.path, {
change: function (path) {
that.pauseWatcher();
// All dialogs should be displayed only if the window is focused.
var runOnFocus = function (fn, path) {
var win = remote.getCurrentWindow();
if (win.isFocused()) {
fn(path);
} else {
win.once('focus', function () {
fn(path);
});
}
};
var handleAsyncFileChange = function (path) {
// This can be called asynchronously, so other changes could
// happen before the window is focused.
if (files.fileExists(path)) {
dialogs.askFileReload(path, function (reloadRequired) {
if (reloadRequired) {
files.readFile(path, function (data, path) {
that.clear(data, path);
that.startWatcher();
});
} else {
// The previous document is dropped from the editor.
// The watcher will resume on save.
that.setDirty();
that.updateWindowTitle();
}
});
},
unlink: function (path) {
} else {
dialogs.warnFileDeleted(path, function (keepFile) {
if (keepFile) {
that.setDirty();
that.updateWindowTitle();
that.startWatcher();
} else {
that.pauseWatcher();
that.clear();
}
});
}
};
this.watcher = files.createWatcher(this.path, {
change: function (path) {
// Pause the watcher to avoid triggering multiple warning dialogs
// while the first one is being handled.
that.pauseWatcher();
runOnFocus(handleAsyncFileChange, path);
},
unlink: function (path) {
that.pauseWatcher();
runOnFocus(handleAsyncFileChange, path);
},
error: function (err) {
console.error('Watcher error', err);
Expand Down

0 comments on commit b808362

Please # to comment.