From f4d339ccac8d0a9d87e01f711c1fd2feb803e636 Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Thu, 21 Mar 2013 18:23:37 -0600 Subject: [PATCH] Unify root view state with window state --- native/v8_extensions/native.mm | 2 +- spec/app/window-spec.coffee | 19 ++++++++++--------- spec/spec-helper.coffee | 2 ++ src/app/atom.coffee | 18 ++++++------------ src/app/window.coffee | 14 ++++++++------ 5 files changed, 27 insertions(+), 28 deletions(-) diff --git a/native/v8_extensions/native.mm b/native/v8_extensions/native.mm index 00c2817a8..853d27c5f 100644 --- a/native/v8_extensions/native.mm +++ b/native/v8_extensions/native.mm @@ -8,7 +8,7 @@ #import -static std::string windowState = "{}"; +static std::string windowState = ""; static NSLock *windowStateLock = [[NSLock alloc] init]; namespace v8_extensions { diff --git a/spec/app/window-spec.coffee b/spec/app/window-spec.coffee index 5ec40eade..2bb983b63 100644 --- a/spec/app/window-spec.coffee +++ b/spec/app/window-spec.coffee @@ -8,12 +8,11 @@ describe "Window", -> beforeEach -> spyOn(atom, 'getPathToOpen').andReturn(project.getPath()) window.handleWindowEvents() - window.buildProjectAndRootView() + window.deserializeWindowState() projectPath = project.getPath() afterEach -> window.shutdown() - atom.setRootViewStateForPath(projectPath, null) $(window).off 'beforeunload' describe "when the window is loaded", -> @@ -129,18 +128,20 @@ describe "Window", -> expect($(document.body).css('font-weight')).not.toBe("bold") describe ".shutdown()", -> - it "saves the serialized state of the project and root view to the atom object so it can be rehydrated after reload", -> + it "saves the serialized state of the window so it can be deserialized after reload", -> projectPath = project.getPath() - expect(atom.getRootViewStateForPath(projectPath)).toBeUndefined() + expect(atom.getWindowState()).toEqual {} + # JSON.stringify removes keys with undefined values rootViewState = JSON.parse(JSON.stringify(rootView.serialize())) projectState = JSON.parse(JSON.stringify(project.serialize())) window.shutdown() - expect(atom.getRootViewStateForPath(projectPath)).toEqual - project: projectState - rootView: rootViewState + expect(atom.getWindowState().rootView).toEqual rootViewState + expect(atom.getWindowState().project).toEqual projectState + + expect(atom.saveWindowState).toHaveBeenCalled() it "unsubscribes from all buffers", -> rootView.open('sample.js') @@ -153,10 +154,10 @@ describe "Window", -> expect(buffer.subscriptionCount()).toBe 0 it "only serializes window state the first time it is called", -> - deactivateSpy = spyOn(atom, "setRootViewStateForPath").andCallThrough() + window.shutdown() window.shutdown() - expect(atom.setRootViewStateForPath.callCount).toBe 1 + expect(atom.saveWindowState.callCount).toBe 1 describe ".installAtomCommand(commandPath)", -> commandPath = '/tmp/installed-atom-command/atom' diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index ad3c816fa..d37eeb7a8 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -38,6 +38,8 @@ beforeEach -> window.resetTimeouts() atom.atomPackageStates = {} atom.loadedPackages = [] + spyOn(atom, 'saveWindowState') + $native.setWindowState('') # used to reset keymap after each spec bindingSetsToRestore = _.clone(keymap.bindingSets) diff --git a/src/app/atom.coffee b/src/app/atom.coffee index d30762b55..7957db51d 100644 --- a/src/app/atom.coffee +++ b/src/app/atom.coffee @@ -179,17 +179,6 @@ _.extend atom, toggleFullScreen: -> @sendMessageToBrowserProcess('toggleFullScreen') - getRootViewStateForPath: (path) -> - if json = localStorage[path] - JSON.parse(json) - - setRootViewStateForPath: (path, state) -> - return unless path - if state? - localStorage[path] = JSON.stringify(state) - else - delete localStorage[path] - sendMessageToBrowserProcess: (name, data=[], callbacks) -> messageId = messageIdCounter++ data.unshift(messageId) @@ -209,12 +198,17 @@ _.extend atom, windowState getWindowState: (keyPath) -> - windowState = JSON.parse($native.getWindowState()) + inMemoryState = $native.getWindowState() + inMemoryState = null unless inMemoryState.length > 0 + windowState = JSON.parse(inMemoryState ? localStorage[window.location.params.pathToOpen] ? '{}') if keyPath _.valueForKeyPath(windowState, keyPath) else windowState + saveWindowState: -> + localStorage[@getPathToOpen()] = JSON.stringify(@getWindowState()) + update: -> @sendMessageToBrowserProcess('update') diff --git a/src/app/window.coffee b/src/app/window.coffee index a6dd98080..c85d530ca 100644 --- a/src/app/window.coffee +++ b/src/app/window.coffee @@ -56,7 +56,7 @@ window.startup = -> keymap.loadBundledKeymaps() atom.loadThemes() atom.loadPackages() - buildProjectAndRootView() + deserializeWindowState() atom.activatePackages() keymap.loadUserKeymaps() atom.requireUserInitScript() @@ -66,9 +66,9 @@ window.startup = -> window.shutdown = -> return if not project and not rootView atom.setWindowState('pathToOpen', project.getPath()) - atom.setRootViewStateForPath project.getPath(), - project: project.serialize() - rootView: rootView.serialize() + atom.setWindowState('project', project.serialize()) + atom.setWindowState('rootView', rootView.serialize()) + atom.saveWindowState() rootView.deactivate() project.destroy() git?.destroy() @@ -91,13 +91,15 @@ window.handleWindowEvents = -> $(window).on 'blur', -> $("body").addClass('is-blurred') $(window).command 'window:close', => confirmClose() -window.buildProjectAndRootView = -> +window.deserializeWindowState = -> RootView = require 'root-view' Project = require 'project' Git = require 'git' pathToOpen = atom.getPathToOpen() - windowState = atom.getRootViewStateForPath(pathToOpen) ? {} + + windowState = atom.getWindowState() + window.project = deserialize(windowState.project) ? new Project(pathToOpen) window.rootView = deserialize(windowState.rootView) ? new RootView