🐛 Initialize active editor state correctly after reload

Fixes the following bug:

1. Open Atom
2. Open a file
3. Observe the file's encoding in the status bar
4. Reload Atom
5. Close the file
6. Observe that the closed file's encoding is still present in the
status bar

This bug occured because the reload did not deserialize/serialze the
workspace's active text editor state. As a result, when closing the
text editor in step 5, we failed to notify observers that there is no
longer an active text editor.
This commit is contained in:
Jason Rudolph
2017-06-02 07:39:52 -04:00
parent 5dfbb65b0e
commit 5b61c0a949
2 changed files with 25 additions and 7 deletions

View File

@@ -120,6 +120,20 @@ describe('Workspace', () => {
expect(atom.workspace.getTextEditors().length).toBe(0)
})
})
it('remembers whether the workspace had an active text editor', async () => {
await atom.workspace.open('../sample.txt')
expect(atom.workspace.hasActiveTextEditor).toBe(true)
simulateReload()
expect(atom.workspace.hasActiveTextEditor).toBe(true)
atom.workspace.getActivePane().destroy()
expect(atom.workspace.hasActiveTextEditor).toBe(false)
simulateReload()
expect(atom.workspace.hasActiveTextEditor).toBe(false)
})
})
describe('::open(itemOrURI, options)', () => {

View File

@@ -216,7 +216,7 @@ module.exports = class Workspace extends Model {
bottom: this.createDock('bottom')
}
this.activePaneContainer = this.paneContainers.center
this.activeTextEditor = null
this.hasActiveTextEditor = false
this.panelContainers = {
top: new PanelContainer({viewRegistry: this.viewRegistry, location: 'top'}),
@@ -345,7 +345,8 @@ module.exports = class Workspace extends Model {
left: this.paneContainers.left.serialize(),
right: this.paneContainers.right.serialize(),
bottom: this.paneContainers.bottom.serialize()
}
},
hasActiveTextEditor: this.hasActiveTextEditor
}
}
@@ -372,6 +373,10 @@ module.exports = class Workspace extends Model {
this.paneContainers.center.deserialize(state.paneContainer, deserializerManager)
}
if (state.hasActiveTextEditor != null) {
this.hasActiveTextEditor = state.hasActiveTextEditor
}
this.updateWindowTitle()
}
@@ -425,12 +430,11 @@ module.exports = class Workspace extends Model {
}
if (paneContainer === this.getCenter()) {
const newActiveTextEditor = (item instanceof TextEditor) ? item : null
const oldActiveTextEditor = this.activeTextEditor
const hadActiveTextEditor = this.hasActiveTextEditor
this.hasActiveTextEditor = item instanceof TextEditor
if (newActiveTextEditor || oldActiveTextEditor) {
this.activeTextEditor = newActiveTextEditor
const itemValue = (this.activeTextEditor || undefined)
if (this.hasActiveTextEditor || hadActiveTextEditor) {
const itemValue = this.hasActiveTextEditor ? item : undefined
this.emitter.emit('did-change-active-text-editor', itemValue)
}
}