From b71fa308a3ac48ce0d171559c3f2e37cc49724ac Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 24 Oct 2012 12:34:45 -0600 Subject: [PATCH] Always run `window.startup` when window.coffee is required When we actually want to attach the root view in window-bootstrap.coffee, we call `window.attachRootView(path)` instead of calling `window.startup(path)`. Having `startup` called automatically means we can be sure any code we add there runs in every environment (including benchmark and specs). This is where we do things like setup the global keymap, parse text mate bundles and themes, and establish the window close handler. Any globals other than the root view that we want to be available in all environments should be established here. Right now that's just the keymap, but soon I want to add a global pasteboard. --- benchmark/benchmark-helper.coffee | 3 --- benchmark/benchmark-suite.coffee | 2 +- spec/app/window-spec.coffee | 2 +- spec/spec-helper.coffee | 2 -- src/app/window.coffee | 38 +++++++++++++++---------------- src/window-bootstrap.coffee | 3 +-- 6 files changed, 22 insertions(+), 28 deletions(-) diff --git a/benchmark/benchmark-helper.coffee b/benchmark/benchmark-helper.coffee index 18aedf44f..2fe689ecc 100644 --- a/benchmark/benchmark-helper.coffee +++ b/benchmark/benchmark-helper.coffee @@ -9,10 +9,7 @@ TextMateBundle = require 'text-mate-bundle' TextMateTheme = require 'text-mate-theme' require 'window' - requireStylesheet "jasmine.css" -TextMateBundle.loadAll() -TextMateTheme.loadAll() RootView.prototype.loadUserConfiguration = -> diff --git a/benchmark/benchmark-suite.coffee b/benchmark/benchmark-suite.coffee index c0b194a5a..4cca9d5e2 100644 --- a/benchmark/benchmark-suite.coffee +++ b/benchmark/benchmark-suite.coffee @@ -10,7 +10,7 @@ describe "editor.", -> beforeEach -> window.rootViewParentSelector = '#jasmine-content' - window.startup() + window.attachRootView() rootView.project.setPath(require.resolve('benchmark/fixtures')) editor = rootView.getActiveEditor() diff --git a/spec/app/window-spec.coffee b/spec/app/window-spec.coffee index d3703e857..6dcc5bf49 100644 --- a/spec/app/window-spec.coffee +++ b/spec/app/window-spec.coffee @@ -3,7 +3,7 @@ fs = require 'fs' describe "Window", -> beforeEach -> - window.startup(require.resolve('fixtures')) + window.attachRootView(require.resolve('fixtures')) afterEach -> window.shutdown() diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index dafe5de7e..9a89070d9 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -14,8 +14,6 @@ require 'window' atom.showDevTools() requireStylesheet "jasmine.css" -TextMateBundle.loadAll() -TextMateTheme.loadAll() defaultTitle = document.title pathsWithSubscriptions = null diff --git a/src/app/window.coffee b/src/app/window.coffee index 4d488234e..108e305e6 100644 --- a/src/app/window.coffee +++ b/src/app/window.coffee @@ -8,6 +8,9 @@ fs = require 'fs' _ = require 'underscore' $ = require 'jquery' {CoffeeScript} = require 'coffee-script' +RootView = require 'root-view' +require 'jquery-extensions' +require 'underscore-extensions' windowAdditions = rootViewParentSelector: 'body' @@ -15,16 +18,28 @@ windowAdditions = keymap: null platform: $native.getPlatform() - startup: (path) -> + # This method runs when the file is required. Any code here will run + # in all environments: spec, benchmark, and application + startup: -> TextMateBundle.loadAll() TextMateTheme.loadAll() + @setUpKeymap() + + # This method is intended only to be run when starting a normal application + # Note: RootView assigns itself on window on initialization so that + # window.rootView is available when loading user configuration + attachRootView: (pathToOpen) -> + if rootViewState = atom.getRootViewStateForPath(pathToOpen) + RootView.deserialize(rootViewState) + else + new RootView(pathToOpen) - @attachRootView(path) $(window).on 'close', => @close() + $(@rootViewParentSelector).append(@rootView) + $(window).focus() $(window).on 'beforeunload', => @shutdown() false - $(window).focus() shutdown: -> @rootView.deactivate() @@ -42,16 +57,6 @@ windowAdditions = @_handleKeyEvent = (e) => @keymap.handleKeyEvent(e) $(document).on 'keydown', @_handleKeyEvent - # Note: RootView assigns itself on window on initialization so that - # window.rootView is available when loading user configuration - attachRootView: (pathToOpen) -> - if rootViewState = atom.getRootViewStateForPath(pathToOpen) - RootView.deserialize(rootViewState) - else - new RootView(pathToOpen) - - $(@rootViewParentSelector).append @rootView - requireStylesheet: (path) -> unless fullPath = require.resolve(path) throw new Error("requireStylesheet could not find a file at path '#{path}'") @@ -91,12 +96,7 @@ windowAdditions = console.log description, result window[key] = value for key, value of windowAdditions -window.setUpKeymap() - -RootView = require 'root-view' - -require 'jquery-extensions' -require 'underscore-extensions' +window.startup() requireStylesheet 'reset.css' requireStylesheet 'atom.css' diff --git a/src/window-bootstrap.coffee b/src/window-bootstrap.coffee index 46d4750c9..86404bb98 100644 --- a/src/window-bootstrap.coffee +++ b/src/window-bootstrap.coffee @@ -1,5 +1,4 @@ # Like sands through the hourglass, so are the days of our lives. require 'atom' require 'window' -window.startup window.location.params.pathToOpen - +window.attachRootView(window.location.params.pathToOpen)