       @bottomPanel.destroy()
       @bottomPanel = null
 
-  dispatchKeyEvent: (event) ->
+  dispatchKeyEvent: (event) =>
     buffer = @activeTerminal.getBuffer()
     keystroke = atom.keymaps.keystrokeForKeyboardEvent event
     keystrokeParts = if keystroke == '-' then ['-'] else keystroke.split '-'


  updateOnDidExitWorkspace: ->
    @hide()
    @activeTerminalSubscrs?.dispose()
    @activeLanguageSubscrs?.dispose()
    @activeTerminal = null
    @activeLanguage = null
    @fontSizeSelect.removeEventListener 'change', @onDidChangeFontSize
    # TODO: @off()

  updateOnDidChangeActiveLanguageOfWorkspace: (@activeLanguage) ->
    @activeLanguageSubscrs?.dispose()
    @activeLanguageSubscrs = new CompositeDisposable()
    @activeLanguageSubscrs.add @activeLanguage.observe => @updateOnDidChangeActiveLanguage()

  updateOnDidChangeActiveTerminalOfWorkspace: (@activeTerminal) ->
    @activeTerminalSubscrs?.dispose()
    @activeTerminalSubscrs = new CompositeDisposable()
    @activeTerminalSubscrs.add @activeTerminal.observeIsVisible (isVisible) => @updateOnDidChangeIsVisibleOfActiveTerminal(isVisible)
    @activeTerminalSubscrs.add @activeTerminal.onDidChangeSize (size) => @updateOnDidChangeTerminalSize(size)
    @activeTerminalSubscrs.add @activeTerminal.observeFontSize (fontSize) => @updateOnDidChangeTerminalFontSize(fontSize)
    @activeTerminalSubscrs.add @activeTerminal.observeIsExecuting (isExecuting) => @updateOnDidChangeIsExecutingOfActiveTerminal(isExecuting)

    @terminalContainer.innerHTML = ''
    @terminalContainer.appendChild(atom.views.getView(@activeTerminal))

  updateOnDidChangeActiveLanguage: ->
    # update terminal panel for current execution mode
    unless @activeTerminal.isExecuting()
      if @activeLanguage.isExecutable()
        @startExecutionLink.style.display = ''

    fontSize = parseInt(@fontSizeSelect.value)
    @activeTerminal.setFontSize(fontSize)

  focusTerminal: =>
    @activeTerminal.didFocus()

  blurTerminal: =>
    @activeTerminal.didBlur()

  updateOnDidChangeIsVisibleOfActiveTerminal: (isVisible) ->
    if isVisible
      @resizeHandle.style.display = ''
      @showTerminalLink.style.display = 'none'
      @hideTerminalLink.style.display = ''
      @terminalControls.style.display = 'inline'

      @fontSizeSelect.removeEventListener 'change', @onDidChangeFontSize
      @fontSizeSelect.addEventListener 'change', @onDidChangeFontSize

      @off()
      @resizeHandle.addEventListener 'mousedown', @resizeStarted
      @resizeHandle.addEventListener 'dblclick', @resizeToMinSize
      @element.addEventListener 'keydown', @dispatchKeyEvent

      @element.addEventListener 'focusin', @focusTerminal
      @element.addEventListener 'focusout', @blurTerminal
    else
      @resizeHandle.style.display = 'none'
      @showTerminalLink.style.display = ''
      @hideTerminalLink.style.display = 'none'
      @terminalControls.style.display = 'none'
      @fontSizeSelect.removeEventListener 'change', @onDidChangeFontSize
      @off()

  updateOnDidChangeTerminalSize: (size) ->
    @terminalInfo.innerHTML = "Lines: #{size}"
