 
   destroy: ->
     @subscriptions.dispose()
+
+  getPreviouslyFocusedElement: () ->
+    if @previouslyFocusedElement[0] and @previouslyFocusedElement[0] isnt document.body
+      @eventElement = @previouslyFocusedElement[0]
+    else
+      @eventElement = atom.views.getView(atom.workspace)
+
+  storeFocusedElement: ->
+    @previouslyFocusedElement = $(document.activeElement)

{CompositeDisposable} = require 'atom'
{View} = require 'space-pen'

module.exports = class ToolBarButtonView extends View
  @content: ->

    @on 'click', =>
      if not @hasClass 'disabled'
        if typeof options.callback is 'string'
          atom.commands.dispatch @getPreviouslyFocusedElement(), options.callback
        else
          options.callback(options.data)

    @on 'mouseover', =>
      @storeFocusedElement()

  setEnabled: (enabled) ->
    if enabled
