Serialize the state of the config window on refresh

This commit is contained in:
Nathan Sobo 2013-04-10 17:27:35 -06:00 committed by Corey Johnson & Kevin Sawicki
parent c32c894d23
commit f0cddf9f32
5 changed files with 71 additions and 18 deletions

View File

@ -7,6 +7,23 @@ describe "ConfigView", ->
beforeEach ->
configView = new ConfigView
describe "serialization", ->
it "remembers which panel was visible", ->
configView.showPanel('Editor')
newConfigView = deserialize(configView.serialize())
configView.remove()
newConfigView.attachToDom()
expect(newConfigView.activePanelName).toBe 'Editor'
it "shows the previously active panel if it is added after deserialization", ->
configView.addPanel('Panel 1', $$ -> @div id: 'panel-1')
configView.showPanel('Panel 1')
newConfigView = deserialize(configView.serialize())
configView.remove()
newConfigView.attachToDom()
newConfigView.addPanel('Panel 1', $$ -> @div id: 'panel-1')
expect(newConfigView.activePanelName).toBe 'Panel 1'
describe ".addPanel(name, view)", ->
it "adds a menu entry to the left and a panel that can be activated by clicking it", ->
configView.addPanel('Panel 1', $$ -> @div id: 'panel-1')

View File

@ -12,7 +12,7 @@ describe "Window", ->
projectPath = project.getPath()
afterEach ->
window.shutdown()
window.unloadEditorWindow()
$(window).off 'beforeunload'
describe "when the window is loaded", ->
@ -120,7 +120,7 @@ describe "Window", ->
removeStylesheet(cssPath)
expect($(document.body).css('font-weight')).not.toBe("bold")
describe ".shutdown()", ->
describe ".unloadEditorWindow()", ->
it "saves the serialized state of the window so it can be deserialized after reload", ->
projectPath = project.getPath()
expect(atom.getWindowState()).toEqual {}
@ -130,7 +130,7 @@ describe "Window", ->
projectState = JSON.parse(JSON.stringify(project.serialize()))
syntaxState = JSON.parse(JSON.stringify(syntax.serialize()))
window.shutdown()
window.unloadEditorWindow()
windowState = atom.getWindowState()
expect(windowState.rootView).toEqual rootViewState
@ -145,14 +145,14 @@ describe "Window", ->
rootView.getActivePane().splitRight()
expect(window.rootView.find('.editor').length).toBe 2
window.shutdown()
window.unloadEditorWindow()
expect(buffer.subscriptionCount()).toBe 0
it "only serializes window state the first time it is called", ->
window.shutdown()
window.shutdown()
window.unloadEditorWindow()
window.unloadEditorWindow()
expect(atom.saveWindowState.callCount).toBe 1
describe ".installAtomCommand(commandPath)", ->

View File

@ -251,12 +251,16 @@ _.extend atom,
null
getSavedWindowState: ->
if pathToOpen = window.location.params.pathToOpen
localStorage[pathToOpen]
storageKey = switch @windowMode
when 'editor' then window.location.params.pathToOpen
when 'config' then 'config'
localStorage[storageKey] if storageKey
saveWindowState: ->
if pathToOpen = @getPathToOpen()
localStorage[pathToOpen] = JSON.stringify(@getWindowState())
storageKey = switch @windowMode
when 'editor' then @getPathToOpen()
when 'config' then 'config'
localStorage[storageKey] = JSON.stringify(@getWindowState())
update: ->
@sendMessageToBrowserProcess('update')

View File

@ -5,6 +5,13 @@ EditorConfigPanel = require 'editor-config-panel'
module.exports =
class ConfigView extends View
registerDeserializer(this)
@deserialize: ({activePanelName}) ->
view = new ConfigView()
view.showPanel(activePanelName)
view
@content: ->
@div id: 'config-view', =>
@ol id: 'panel-menu', outlet: 'panelMenu'
@ -25,10 +32,22 @@ class ConfigView extends View
panel.hide()
@panelsByName[name] = panel
@panels.append(panel)
@showPanel(name) if _.values(@panelsByName).length == 1
@showPanel(name) if @getPanelCount() is 1 or @panelToShow is name
getPanelCount: ->
_.values(@panelsByName).length
showPanel: (name) ->
if @panelsByName[name]
@panels.children().hide()
@panelMenu.children('.active').removeClass('active')
@panelsByName[name].show()
@panelMenu.children("[name='#{name}']").addClass('active')
@activePanelName = name
@panelToShow = null
else
@panelToShow = name
serialize: ->
deserializer: @constructor.name
activePanelName: @activePanelName

View File

@ -43,6 +43,7 @@ window.startEditorWindow = ->
else
console.warn "Failed to install `atom` binary"
atom.windowMode = 'editor'
handleWindowEvents()
handleDragDrop()
config.load()
@ -53,10 +54,11 @@ window.startEditorWindow = ->
atom.activatePackages()
keymap.loadUserKeymaps()
atom.requireUserInitScript()
$(window).on 'beforeunload', -> shutdown(); false
$(window).on 'beforeunload', -> unloadEditorWindow(); false
$(window).focus()
window.startConfigWindow = ->
atom.windowMode = 'config'
handleWindowEvents()
config.load()
keymap.loadBundledKeymaps()
@ -65,8 +67,10 @@ window.startConfigWindow = ->
deserializeConfigWindow()
atom.activatePackageConfigs()
keymap.loadUserKeymaps()
$(window).on 'beforeunload', -> unloadConfigWindow(); false
$(window).focus()
window.shutdown = ->
window.unloadEditorWindow = ->
return if not project and not rootView
atom.setWindowState('pathToOpen', project.getPath())
atom.setWindowState('project', project.serialize())
@ -99,6 +103,14 @@ window.installAtomCommand = (commandPath, done) ->
else
fs.chmod(commandPath, 0o755, commandPath)
window.unloadConfigWindow = ->
return if not configView
atom.setWindowState('configView', configView.serialize())
atom.saveWindowState()
configView.remove()
window.configView = null
$(window).off('focus blur before')
window.handleWindowEvents = ->
$(window).command 'window:toggle-full-screen', => atom.toggleFullScreen()
$(window).on 'focus', -> $("body").removeClass('is-blurred')
@ -143,7 +155,8 @@ window.deserializeEditorWindow = ->
window.deserializeConfigWindow = ->
ConfigView = require 'config-view'
window.configView = new ConfigView()
windowState = atom.getWindowState()
window.configView = deserialize(windowState.configView) ? new ConfigView()
$(rootViewParentSelector).append(configView)
window.stylesheetElementForId = (id) ->