From 76e7161608231bb20dfbe96f1565b788e30cc781 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki & Nathan Sobo Date: Thu, 16 May 2013 14:13:25 -0700 Subject: [PATCH] Extract window event handling into separate class --- spec/app/window-spec.coffee | 7 ++--- src/app/window-event-handler.coffee | 34 +++++++++++++++++++++++ src/app/window.coffee | 42 +++++------------------------ 3 files changed, 45 insertions(+), 38 deletions(-) create mode 100644 src/app/window-event-handler.coffee diff --git a/spec/app/window-spec.coffee b/spec/app/window-spec.coffee index 1f3e69ca7..631a36db5 100644 --- a/spec/app/window-spec.coffee +++ b/spec/app/window-spec.coffee @@ -1,18 +1,19 @@ $ = require 'jquery' fsUtils = require 'fs-utils' {less} = require 'less' +WindowEventHandler = require 'window-event-handler' describe "Window", -> - projectPath = null + [projectPath, windowEventHandler] = [] beforeEach -> spyOn(atom, 'getPathToOpen').andReturn(project.getPath()) - window.handleEvents() + windowEventHandler = new WindowEventHandler() window.deserializeEditorWindow() projectPath = project.getPath() afterEach -> - window.unloadEditorWindow() + windowEventHandler.unsubscribe() $(window).off 'beforeunload' describe "when the window is loaded", -> diff --git a/src/app/window-event-handler.coffee b/src/app/window-event-handler.coffee new file mode 100644 index 000000000..c34a7235a --- /dev/null +++ b/src/app/window-event-handler.coffee @@ -0,0 +1,34 @@ +$ = require 'jquery' +_ = require 'underscore' +Subscriber = require 'subscriber' + +module.exports = +class WindowEventHandler + constructor: -> + @subscribe $(window), 'focus', -> $("body").removeClass('is-blurred') + @subscribe $(window), 'blur', -> $("body").addClass('is-blurred') + @subscribeToCommand $(window), 'window:toggle-full-screen', => atom.toggleFullScreen() + @subscribeToCommand $(window), 'window:close', => + if rootView? + rootView.confirmClose().done -> window.close() + else + window.close() + @subscribeToCommand $(window), 'window:reload', => reload() + + @subscribe $(document), 'keydown', keymap.handleKeyEvent + + @subscribe $(document), 'drop', onDrop + @subscribe $(document), 'dragover', (e) -> + e.preventDefault() + e.stopPropagation() + + @subscribe $(document), 'click', 'a', (e) -> + location = $(e.target).attr('href') + return unless location + return if location[0] is '#' + + if location.indexOf('https://') is 0 or location.indexOf('http://') is 0 + require('child_process').spawn('open', [location]) + false + +_.extend WindowEventHandler.prototype, Subscriber diff --git a/src/app/window.coffee b/src/app/window.coffee index e6a04b1bc..4a5c9270c 100644 --- a/src/app/window.coffee +++ b/src/app/window.coffee @@ -3,6 +3,7 @@ fsUtils = require 'fs-utils' $ = require 'jquery' _ = require 'underscore' less = require 'less' +WindowEventHandler = require 'window-event-handler' require 'jquery-extensions' require 'underscore-extensions' require 'space-pen-extensions' @@ -12,6 +13,8 @@ deferredDeserializers = {} ### Internal ### +windowEventHandler = null + # This method is called in any window needing a general environment, including specs window.setUpEnvironment = -> Config = require 'config' @@ -24,7 +27,6 @@ window.setUpEnvironment = -> window.syntax = deserialize(atom.getWindowState('syntax')) ? new Syntax window.pasteboard = new Pasteboard window.keymap = new Keymap() - $(document).on 'keydown', keymap.handleKeyEvent keymap.bindDefaultKeys() @@ -39,8 +41,7 @@ window.startEditorWindow = -> installApmCommand() atom.windowMode = 'editor' - handleEvents() - handleDragDrop() + windowEventHandler = new WindowEventHandler config.load() keymap.loadBundledKeymaps() atom.loadThemes() @@ -54,7 +55,7 @@ window.startEditorWindow = -> window.startConfigWindow = -> atom.windowMode = 'config' - handleEvents() + windowEventHandler = new WindowEventHandler config.load() keymap.loadBundledKeymaps() atom.loadThemes() @@ -77,7 +78,7 @@ window.unloadEditorWindow = -> atom.saveWindowState() project.destroy() git?.destroy() - $(window).off('focus blur before') + windowEventHandler?.unsubscribe() window.rootView = null window.project = null window.git = null @@ -96,29 +97,7 @@ window.unloadConfigWindow = -> atom.saveWindowState() configView.remove() window.configView = null - $(window).off('focus blur before') - -window.handleEvents = -> - $(window).command 'window:toggle-full-screen', => atom.toggleFullScreen() - $(window).on 'focus', -> $("body").removeClass('is-blurred') - $(window).on 'blur', -> $("body").addClass('is-blurred') - $(window).command 'window:close', => confirmClose() - $(window).command 'window:reload', => reload() - - $(document).on 'click', 'a', (e) -> - location = $(e.target).attr('href') - return unless location - return if location[0] is '#' - - if location.indexOf('https://') is 0 or location.indexOf('http://') is 0 - require('child_process').spawn('open', [location]) - false - -window.handleDragDrop = -> - $(document).on 'dragover', (e) -> - e.preventDefault() - e.stopPropagation() - $(document).on 'drop', onDrop + windowEventHandler.unsubscribe() window.onDrop = (e) -> e.preventDefault() @@ -257,10 +236,3 @@ window.profile = (description, fn) -> value = fn() console.profileEnd(description) value - -# Public: Shows a dialog asking if the window was _really_ meant to be closed. -confirmClose = -> - if rootView? - rootView.confirmClose().done -> window.close() - else - window.close()